use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testRuleUnitInvocationFromConsequence.
@Test
public void testRuleUnitInvocationFromConsequence() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " Person(age >= 18, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + " $p : Person(age < 18, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is NOT adult\");\n" + " modify($p) { setAge(18); }\n" + " drools.run( AdultUnit.class );" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
List<String> log = new ArrayList<>();
executor.bindVariable("log", log);
assertEquals(4, executor.run(NotAdultUnit.class));
List<String> expectedLogs = asList("org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit yielded to org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit ended.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit ended.");
assertEquals(expectedLogs, log);
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testBindDataSource.
@Test
public void testBindDataSource() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " Person(age >= 18, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + " Person(age < 18, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is NOT adult\");\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Person> persons = DataSource.create(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
executor.bindVariable("persons", persons);
// explicitly create unit
assertEquals(2, executor.run(AdultUnit.class));
// let RuleUnitExecutor internally create and wire the unit instance
assertEquals(1, executor.run(NotAdultUnit.class));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testReactiveOnUnitCreatingDataSource.
@Test(timeout = 10000L)
public void testReactiveOnUnitCreatingDataSource() throws Exception {
// DROOLS-1647
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitCreatingDataSource.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + " Person(age >= 18, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is adult\");" + " list.add($name);\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DebugList<String> list = new DebugList<>();
executor.bindVariable("list", list);
AdultUnitCreatingDataSource adultUnit = new AdultUnitCreatingDataSource(list);
adultUnit.insertPerson(new Person("Mario", 42));
Semaphore ready = new Semaphore(0, true);
list.onItemAdded = (l -> ready.release());
new Thread(() -> executor.runUntilHalt(adultUnit)).start();
ready.acquire();
assertEquals(1, list.size());
assertEquals("Mario", list.get(0));
list.clear();
list.onItemAdded = (l -> ready.release());
adultUnit.insertPerson(new Person("Sofia", 4));
adultUnit.insertPerson(new Person("Marilena", 44));
ready.acquire();
assertEquals(1, list.size());
assertEquals("Marilena", list.get(0));
executor.halt();
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testWithOOPath.
@Test
public void testWithOOPath() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " $p : /persons[age >= 18]\n" + "then\n" + " System.out.println($p.getName() + \" is adult\");\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
RuleUnit adultUnit = new AdultUnit(persons);
assertEquals(2, executor.run(adultUnit));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testRuleUnitIdentity.
@Test
public void testRuleUnitIdentity() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(Unit0.class) + "\n" + "import " + AgeCheckUnit.class.getCanonicalName() + "\n" + "\n" + "rule R1 when\n" + " $i: /ds#Integer\n" + "then\n" + " drools.guard( new AgeCheckUnit($i) );" + "end\n" + "rule RegisterAdultTicket when\n" + " $s: /ds#String\n" + "then\n" + " drools.guard( new AgeCheckUnit($s.length()) );" + "end";
String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AgeCheckUnit.class) + ";\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule CheckAge when\n" + " $p : /persons[ age > minAge ]\n" + "then\n" + " list.add($p.getName() + \">\" + minAge);\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Object> ds = executor.newDataSource("ds");
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Sofia", 4));
List<String> list = new ArrayList<>();
executor.bindVariable("list", list);
ds.insert("test");
ds.insert(3);
ds.insert(4);
executor.run(Unit0.class);
System.out.println(list);
assertEquals(3, list.size());
assertTrue(list.containsAll(asList("Mario>4", "Mario>3", "Sofia>3")));
list.clear();
ds.insert("xxx");
ds.insert("yyyy");
executor.run(Unit0.class);
assertEquals(0, list.size());
}
Aggregations