use of org.kie.internal.utils.KieHelper 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.internal.utils.KieHelper 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.internal.utils.KieHelper 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.internal.utils.KieHelper 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());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class TemporalOperatorTest method checkConstantTemporalConstraint.
public void checkConstantTemporalConstraint(String constraint) {
String str = "import " + TimestampedObject.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $t1 : TimestampedObject( " + constraint + " )\n" + "then\n" + " list.add($t1.getName());\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
TimestampedObject t1 = new TimestampedObject("t1", LocalDateTime.now());
ksession.insert(t1);
ksession.fireAllRules();
assertEquals(t1.getName(), list.get(0));
}
Aggregations