use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleUnitTest method testRuleWithoutUnitsIsNotExecutor.
@Test
public void testRuleWithoutUnitsIsNotExecutor() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult when\n" + " Person(age >= 18, $name : name)\n" + "then\n" + " System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult when\n" + " Person(age < 18, $name : name)\n" + "then\n" + " System.out.println($name + \" is NOT adult\");\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
try {
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
fail("It should throw IllegalStateException");
} catch (IllegalStateException e) {
// expected
}
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleUnitTest method testMultiLevelGuards.
@Test
public void testMultiLevelGuards() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(Unit0.class) + "\n" + "import " + UnitA.class.getCanonicalName() + "\n" + "rule X when\n" + " $b: /ds#Boolean\n" + "then\n" + " Boolean b = $b;\n" + " drools.guard( UnitA.class );\n" + "end";
String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(UnitA.class) + "\n" + "import " + UnitB.class.getCanonicalName() + "\n" + "rule A when\n" + " $s: /ds#String\n" + "then\n" + " drools.guard( UnitB.class );" + "end";
String drl3 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(UnitB.class) + "\n" + "import " + UnitB.class.getCanonicalName() + "\n" + "rule B when\n" + " $i: /ds#Integer\n" + "then\n" + " list.add($i);" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).addContent(drl3, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Object> ds = executor.newDataSource("ds");
List<Integer> list = new ArrayList<>();
executor.bindVariable("list", list);
ds.insert(1);
executor.run(Unit0.class);
// all units are inactive
assertEquals(0, list.size());
FactHandle guardA = ds.insert(true);
executor.run(Unit0.class);
// UnitB still inactive
assertEquals(0, list.size());
FactHandle guardB = ds.insert("test");
executor.run(Unit0.class);
// all units are active
assertEquals(1, list.size());
// all units are active
assertEquals(1, (int) list.get(0));
list.clear();
ds.insert(2);
executor.run(Unit0.class);
// all units are inactive
assertEquals(1, list.size());
// all units are active
assertEquals(2, (int) list.get(0));
list.clear();
// retracting guard A deactivate unitA and in cascade unit B
ds.delete(guardA);
ds.insert(3);
executor.run(Unit0.class);
// all units are inactive
assertEquals(0, list.size());
// activating guard A reactivate unitA and in cascade unit B
guardA = ds.insert(true);
executor.run(Unit0.class);
// all units are active
assertEquals(1, list.size());
list.clear();
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleUnitTest method testTwoPartsOOPath.
@Test
public void testTwoPartsOOPath() throws Exception {
// DROOLS-1539
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + LongAddress.class.getCanonicalName() + "\n" + "rule Adult when\n" + " $a: /persons[ age > 18 ]/addresses#LongAddress[ country == \"it\" ]\n" + "then\n" + " System.out.println($a.getCountry());\n" + "end";
KieBase kbase = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
Person mario = new Person("Mario", 43);
mario.setAddresses(asList(new LongAddress("street", "suburb", "zipCode", "it")));
Person mark = new Person("Mark", 40);
mark.setAddresses(asList(new LongAddress("street", "suburb", "zipCode", "uk")));
DataSource<Person> persons = executor.newDataSource("persons", mario, mark);
assertEquals(1, executor.run(AdultUnit.class));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleUnitTest method testDeleteFromDataSource.
@Test
public void testDeleteFromDataSource() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + FlowUnit.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + FilterUnit.class.getCanonicalName() + "\n" + "rule Flow @Unit( FlowUnit.class ) when\n" + "then\n" + " drools.run( FilterUnit.class );\n" + " drools.run( AdultUnit.class );\n" + "end\n" + "rule filter @Unit( FilterUnit.class ) when\n" + " $p:Person(name str[startsWith] \"D\") from persons\n" + "then\n" + " System.out.println(\"Deleting person: \" + $p.getName() + \". Sorry man, your name starts with a 'D' ....\");\n" + " persons.delete( $p );\n" + "end\n" + "rule AdultUnit @Unit( AdultUnit.class ) when\n" + " Person($age : age > 18, $name : name) from persons\n" + "then\n" + " results.add($name);\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", 43), new Person("Duncan", 34), new Person("Sofia", 6));
List<String> results = new ArrayList<>();
executor.bindVariable("results", results);
assertEquals(3, executor.run(FlowUnit.class));
assertEquals(1, results.size());
assertEquals("Mario", results.get(0));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class RuleUnitTest method testUnitDeclaration.
@Test
public void testUnitDeclaration() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + " Person(age >= adultAge, $name : name) from persons\n" + "then\n" + " System.out.println($name + \" is adult\");\n" + "end";
String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(NotAdultUnit.class) + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule NotAdult 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 );\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, 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).bindVariable("adultAge", 18);
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);
}
Aggregations