use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testRuleUnit.
@Test
public void testRuleUnit() {
List<String> result = new ArrayList<>();
Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
Rule rule = rule("org.drools.retebuilder", "Adult").unit(AdultUnit.class).build(expr("$expr$1$", adult, p -> p.getAge() > 18), on(adult).execute(p -> {
System.out.println(p.getName());
result.add(p.getName());
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kieBase);
executor.newDataSource("persons", new Person("Mario", 43), new Person("Marilena", 44), new Person("Sofia", 5));
executor.run(AdultUnit.class);
assertTrue(result.containsAll(asList("Mario", "Marilena")));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class UnitCompilationTest method testOr.
@Test
public void testOr() throws Exception {
CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit4");
RuleUnitExecutor executor = unit.createExecutor();
Constructor<?> personConstructor = unit.getConstructorFor("org.model.Person", String.class, int.class);
Constructor<?> childConstructor = unit.getConstructorFor("org.model.Child", String.class, int.class, int.class);
DataSource<?> persons = executor.newDataSource("persons", personConstructor.newInstance("Mario", 43), personConstructor.newInstance("Marilena", 44), childConstructor.newInstance("Sofia", 5, 10));
RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
assertEquals(1, executor.run(ruleUnit));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testWithOOPathAndNotNoMatch.
@Test
public void testWithOOPathAndNotNoMatch() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " not /persons[age >= 18]\n" + "then\n" + " System.out.println(\"No adults\");\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", 44), new Person("Marilena", 170), new Person("Sofia", 18));
RuleUnit adultUnit = new AdultUnit(persons);
assertEquals(0, executor.run(adultUnit));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testWithDataSource.
@Test
public void testWithDataSource() 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 = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
// explicitly create unit
assertEquals(2, executor.run(new AdultUnit(persons)));
// 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 testReactiveDataSourceWithRunUntilHalt.
@Test(timeout = 10000L)
public void testReactiveDataSourceWithRunUntilHalt() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + ReactiveAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( ReactiveAdultUnit.class ) 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);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
ReactiveAdultUnit adultUnit = new ReactiveAdultUnit(persons, list);
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());
persons.insert(new Person("Sofia", 4));
persons.insert(new Person("Marilena", 44));
ready.acquire();
assertEquals(1, list.size());
assertEquals("Marilena", list.get(0));
executor.halt();
}
Aggregations