use of org.kie.api.KieBase in project drools by kiegroup.
the class RuleUnitTest method testModifyGlobalFact.
@Test
public void testModifyGlobalFact() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult 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));
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class RuleUnitTest method testWithOOPathAndNot.
@Test
public void testWithOOPathAndNot() 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", 4), new Person("Marilena", 17), new Person("Sofia", 4));
RuleUnit adultUnit = new AdultUnit(persons);
assertEquals(1, executor.run(adultUnit));
}
use of org.kie.api.KieBase 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.api.KieBase 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.api.KieBase 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));
}
Aggregations