Created
July 31, 2020 10:43
-
-
Save lucamolteni/60f14615c6528f9f4bc59c1cee175832 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class Measurement { | |
| private String id; | |
| private String val; | |
| public Measurement(String id, String val) { | |
| this.id = id; | |
| this.val = val; | |
| } | |
| public String getId() { | |
| return id; | |
| } | |
| public void setId(String id) { | |
| this.id = id; | |
| } | |
| public String getVal() { | |
| return val; | |
| } | |
| public void setVal(String val) { | |
| this.val = val; | |
| } | |
| } | |
| @Test | |
| public void testVariableAccessors() { | |
| // DROOLS-5548 | |
| String str = | |
| "package com.sample;" + | |
| "global java.util.Set controlSet;\n" + | |
| "import " + Measurement.class.getCanonicalName() + ";\n" + | |
| "" + | |
| "declare A\n" + | |
| " x: String\n" + | |
| "end\n" + | |
| "" + | |
| "declare B\n" + | |
| " a: A\n" + | |
| "end\n" + | |
| "" + | |
| "function String dummyFunction(A b) {\n" + | |
| " return \"test\";\n" + | |
| "}\n" + | |
| "\n" + | |
| "rule \"insertB\"\n" + | |
| "when\n" + | |
| "then\n" + | |
| "drools.insert(new B(new A()));" + | |
| "end;" + | |
| "rule \"will execute per each Measurement having ID color\"\n" + | |
| "no-loop\n" + | |
| "when\n" + | |
| " Measurement( id == \"color\", $colorVal : val )\n" + | |
| " $b: B()\n" + | |
| " $val: String() from dummyFunction($b.a)\n" + | |
| "then\n" + | |
| " controlSet.add($colorVal);\n" + | |
| "end"; | |
| KieSession ksession = getKieSession( str ); | |
| HashSet<Object> hashSet = new HashSet<>(); | |
| ksession.setGlobal("controlSet", hashSet); | |
| ksession.insert(new Measurement("color", "red")); | |
| int ruleFired = ksession.fireAllRules(); | |
| assertEquals( 2, ruleFired ); | |
| assertEquals( "red", hashSet.iterator().next() ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment