use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testPrimitives.
@Test
public void testPrimitives() {
// DROOLS-1266
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Adult( $x: /children.age )\n" + "then\n" + " list.add( $x );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Man bob = new Man("Bob", 40);
bob.addChild(new Child("Charles", 12));
bob.addChild(new Child("Debbie", 8));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(2);
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testBackReferenceConstraint.
@Test
public void testBackReferenceConstraint() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children/toys[ name.length == ../name.length ] )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Carl", 12);
final Child debbie = new Child("Debbie", 8);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
debbie.addToy(new Toy("guitar"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("ball", "guitar");
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testWith2Peers.
@Test
public void testWith2Peers() {
// DROOLS-1589
String header = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n\n";
String drl1 = "rule R1 when\n" + " Man( $m: /wife[age == 25] )\n" + "then\n" + " list.add($m.getName());\n" + "end\n\n";
String drl2 = "rule R2 when\n" + " Man( $m: /wife[age == 26] )\n" + "then\n" + " list.add($m.getName());\n" + "end\n\n";
String drl3 = "rule R3 when\n" + " Man( $m: /wife[age == 27] )\n" + "then\n" + " list.add($m.getName());\n" + "end\n\n";
final KieSession ksession = new KieHelper().addContent(header + drl1 + drl2 + drl3, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Man bob = new Man("John", 25);
bob.setWife(new Woman("Jane", 25));
ksession.insert(bob);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("Jane", list.get(0));
list.clear();
bob.getWife().setAge(26);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("Jane", list.get(0));
list.clear();
bob.getWife().setAge(27);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("Jane", list.get(0));
list.clear();
bob.getWife().setAge(28);
ksession.fireAllRules();
assertEquals(0, list.size());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testDoubleRemove.
@Test
public void testDoubleRemove() {
// DROOLS-1376
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + " Group( $id: name, $p: /members[age >= 20] )\n" + "then\n" + " System.out.println( $id + \".\" + $p.getName() );\n" + " insertLogical( $id + \".\" + $p.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final Adult ada = new Adult("Ada", 20);
final Adult bea = new Adult("Bea", 20);
final Group x = new Group("X");
final Group y = new Group("Y");
x.addPerson(ada);
x.addPerson(bea);
y.addPerson(ada);
y.addPerson(bea);
ksession.insert(x);
ksession.insert(y);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.Ada"));
assertTrue(factsCollection(ksession).contains("X.Bea"));
assertTrue(factsCollection(ksession).contains("Y.Ada"));
assertTrue(factsCollection(ksession).contains("Y.Bea"));
x.removePerson(ada);
x.removePerson(bea);
y.removePerson(ada);
y.removePerson(bea);
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.Ada"));
assertFalse(factsCollection(ksession).contains("X.Bea"));
assertFalse(factsCollection(ksession).contains("Y.Ada"));
assertFalse(factsCollection(ksession).contains("Y.Bea"));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testOOPathWithLocalInnerDeclaration.
@Test
public void testOOPathWithLocalInnerDeclaration() {
// DROOLS-1411
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "import " + TMFileSetQuater.class.getCanonicalName() + ";\n" + "global java.util.Set duplicateNames; \n" + "\n" + "rule DIFF_FILES_BUT_WITH_SAME_FILENAME when\n" + " $ic1 : TMFileWithParentObj( $curName : name, $curId : id, \n" + " $ic2: /parent#TMFileSetQuater/files[name == $curName, id != $curId ] )\n" + "then\n" + " System.out.println( $ic1 + \" \" + $ic2 );\n" + " duplicateNames.add( $ic1.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final Set duplicateNames = new HashSet();
ksession.setGlobal("duplicateNames", duplicateNames);
final TMFileSetQuater x = new TMFileSetQuater("X");
final TMFileWithParentObj file0 = new TMFileWithParentObj(0, "File0", 47, x);
final TMFileWithParentObj file1 = new TMFileWithParentObj(1, "File1", 47, x);
final TMFileWithParentObj file2 = new TMFileWithParentObj(2, "File0", 47, x);
x.getFiles().addAll(Arrays.asList(file0, file1, file2));
ksession.insert(x);
ksession.insert(file0);
ksession.insert(file1);
ksession.insert(file2);
ksession.fireAllRules();
assertTrue(duplicateNames.contains("File0"));
assertFalse(duplicateNames.contains("File1"));
}
Aggregations