use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testRuleUnitWithAccumulate.
@Test
public void testRuleUnitWithAccumulate() throws Exception {
// DROOLS-2209
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule AccumulateAdults @Unit( AdultUnit.class ) when\n" + " accumulate( $p: Person( $age: age >= 18 ) from persons, \n" + " $sum : sum( $age ) )\n" + "then\n" + " list.add($sum); \n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
List<Integer> list = new ArrayList<>();
executor.getKieSession().setGlobal("list", list);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
assertEquals(1, executor.run(AdultUnit.class));
assertEquals(1, list.size());
assertEquals(86, (int) list.get(0));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testRunUnexistingUnit.
@Test
public void testRunUnexistingUnit() 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";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
try {
executor.run(NotAdultUnit.class);
fail("It should throw IllegalStateException");
} catch (IllegalStateException e) {
// expected
}
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testWithOOPathOnList.
@Test
public void testWithOOPathOnList() throws Exception {
// DROOLS-1646
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithList.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + " $p : /persons[age >= adultAge]\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);
List<Person> persons = asList(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
RuleUnit adultUnit = new AdultUnitWithList(persons);
assertEquals(2, executor.run(adultUnit));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testReactiveDataSource.
@Test
public void testReactiveDataSource() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + ReactiveAdultUnit.class.getCanonicalName() + "\n" + "import " + ReactiveNotAdultUnit.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\");\n" + "end\n" + "rule NotAdult @Unit( ReactiveNotAdultUnit.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));
ReactiveAdultUnit adultUnit = new ReactiveAdultUnit(persons, null);
assertEquals(1, executor.run(adultUnit));
ReactiveNotAdultUnit notAdultUnit = new ReactiveNotAdultUnit(persons);
assertEquals(0, executor.run(notAdultUnit));
persons.insert(new Person("Sofia", 4));
assertEquals(0, executor.run(adultUnit));
assertEquals(1, executor.run(notAdultUnit));
persons.insert(new Person("Marilena", 44));
assertEquals(1, executor.run(adultUnit));
assertEquals(0, executor.run(notAdultUnit));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testModifyGlobalFactWithMvelDialect.
@Test
public void testModifyGlobalFactWithMvelDialect() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult dialect \"mvel\" when\n" + " $p : Person(age < 18, $name : name)\n" + "then\n" + " System.out.println($name + \" is NOT adult\");\n" + " modify($p) { setAge(18) }\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
executor.getKieSession().insert(new Person("Sofia", 4));
assertEquals(1, executor.run(AdultUnit.class));
}
Aggregations