use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitExample method go.
public void go() {
KieServices ks = KieServices.Factory.get();
KieContainer kcontainer = ks.getKieClasspathContainer();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kcontainer.getKieBase());
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
executor.run(PersonRuleUnit.class);
persons.insert(new Person("Sofia", 4));
executor.run(PersonRuleUnit.class);
persons.insert(new Person("Marilena", 44));
executor.run(PersonRuleUnit.class);
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitExampleTest method testGo.
@Test
public void testGo() {
KieServices ks = KieServices.Factory.get();
KieContainer kcontainer = ks.getKieClasspathContainer();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kcontainer.getKieBase());
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
assertEquals(1, executor.run(PersonRuleUnit.class));
persons.insert(new Person("Sofia", 4));
assertEquals(0, executor.run(PersonRuleUnit.class));
persons.insert(new Person("Marilena", 44));
assertEquals(1, executor.run(PersonRuleUnit.class));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitCompilerTest method testRuleUnit.
@Test
public void testRuleUnit() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " Person(age >= 18, $name : name) from persons\n" + "then\n" + " results.add($name);\n" + "end\n";
KieContainer kieContainer = getKieContainer(null, str);
RuleUnitExecutor executor = kieContainer.newRuleUnitExecutor();
DataSource<Person> persons = DataSource.create(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
AdultUnit unit = new AdultUnit(persons);
assertEquals(2, executor.run(unit));
assertTrue(unit.getResults().containsAll(asList("Mario", "Marilena")));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitCompilerTest method testWith2Rules.
@Test
public void testWith2Rules() {
String str = "package " + this.getClass().getPackage().getName() + ";\n" + "unit " + getCanonicalSimpleName(PositiveNegativeDTUnit.class) + ";\n" + "import " + BigDecimal.class.getCanonicalName() + ";\n" + "rule R1 \n" + "when\n" + " BigDecimal( intValue() >= 0 ) from input1\n" + "then\n" + " output1.insert(\"positive\");\n" + "end\n" + "rule R2 \n" + "when\n" + " BigDecimal( intValue() < 0 ) from input1\n" + "then\n" + " output1.insert(\"negative\");\n" + "end\n";
KieContainer kieContainer = getKieContainer(null, str);
RuleUnitExecutor executor = kieContainer.newRuleUnitExecutor();
PositiveNegativeDTUnit ruleUnit = new PositiveNegativeDTUnit(47);
executor.run(ruleUnit);
assertEquals("positive", ruleUnit.getPositive_or_negative());
ruleUnit = new PositiveNegativeDTUnit(-999);
executor.run(ruleUnit);
assertEquals("negative", ruleUnit.getPositive_or_negative());
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testRuleUnitWithVarBinding.
@Test
public void testRuleUnitWithVarBinding() {
Variable<AdultUnit> unit = declarationOf(AdultUnit.class);
Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
Rule rule = rule("org.drools.retebuilder", "Adult").unit(RuleUnitTest.AdultUnit.class).build(expr("$expr$1$", adult, unit, (p, u) -> p.getAge() > u.getAdultAge()), on(adult, unitData(List.class, "results")).execute((p, r) -> {
System.out.println(p.getName());
r.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));
AdultUnit ruleUnit = new AdultUnit();
executor.run(ruleUnit);
assertTrue(ruleUnit.getResults().containsAll(asList("Mario", "Marilena")));
}
Aggregations