use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testConstraintExternalToOopath.
@Test
public void testConstraintExternalToOopath() {
// DROOLS-2135
final String drl = "import " + Parent.class.getCanonicalName() + ";\n" + "import " + Son.class.getCanonicalName() + ";\n" + "global java.util.List list\n\n" + "rule R when\n" + " Parent( $child : /children, $child.name == \"joe\" )\n" + "then\n" + " list.add( $child.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
Son joe = new Son("joe");
Son jack = new Son("jack");
Parent parent = new Parent(Arrays.asList(joe, jack));
ksession.insert(parent);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("joe", list.get(0));
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testAddAllRemoveIdx.
@Test
public void testAddAllRemoveIdx() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + " Group( $id: name, $p: /members[age >= 30] )\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 Group x = new Group("X");
final Group y = new Group("Y");
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"));
final Adult ada = new Adult("Ada", 29);
final Adult bea = new Adult("Bea", 29);
final List<Person> bothList = Arrays.asList(new Person[] { ada, bea });
x.getMembers().addAll(bothList);
y.getMembers().addAll(bothList);
ksession.insert(x);
ksession.insert(y);
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"));
ada.setAge(30);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.Ada"));
assertFalse(factsCollection(ksession).contains("X.Bea"));
assertTrue(factsCollection(ksession).contains("Y.Ada"));
assertFalse(factsCollection(ksession).contains("Y.Bea"));
// removing Bea from Y
y.getMembers().remove(1);
bea.setAge(30);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.Ada"));
assertTrue(factsCollection(ksession).contains("X.Bea"));
assertTrue(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 testIndexedAccess.
@Test
public void testIndexedAccess() {
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[0]/toys[1] )\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("Charles", 12);
final Child debbie = new Child("Debbie", 11);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("ball");
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathTest method testDoubleAdd.
@Test
public void testDoubleAdd() {
// 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 Group x = new Group("X");
final Group y = new Group("Y");
ksession.insert(x);
ksession.insert(y);
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"));
final Adult ada = new Adult("Ada", 20);
final Adult bea = new Adult("Bea", 20);
x.addPerson(ada);
x.addPerson(bea);
y.addPerson(ada);
y.addPerson(bea);
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 testListIteratorRemove.
@Test
public void testListIteratorRemove() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + " TMDirectory( $id: name, $p: /files[size >= 100] )\n" + "then\n" + " insertLogical( $id + \".\" + $p.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.addEventListener(new DefaultRuleRuntimeEventListener() {
@Override
public void objectDeleted(ObjectDeletedEvent event) {
System.out.println(event.getOldObject() + " -> " + "_");
}
@Override
public void objectInserted(ObjectInsertedEvent event) {
System.out.println("_" + " -> " + event.getObject());
}
@Override
public void objectUpdated(ObjectUpdatedEvent event) {
System.out.println(event.getOldObject() + " -> " + event.getObject());
}
});
final TMDirectory x = new TMDirectory("X");
final TMDirectory y = new TMDirectory("Y");
final TMFile file0 = new TMFile("File0", 999);
final TMFile file1 = new TMFile("File1", 999);
final TMFile file2 = new TMFile("File2", 999);
x.getFiles().addAll(Arrays.asList(file0, file1, file2));
y.getFiles().addAll(Arrays.asList(file0, file1, file2));
ksession.insert(x);
ksession.insert(y);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
final ListIterator<TMFile> xIterator = x.getFiles().listIterator(1);
final ListIterator<TMFile> yIterator = y.getFiles().listIterator();
xIterator.next();
xIterator.remove();
yIterator.next();
yIterator.next();
yIterator.next();
yIterator.remove();
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertTrue(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
assertTrue(xIterator.hasNext());
assertTrue(xIterator.hasPrevious());
assertFalse(yIterator.hasNext());
assertTrue(yIterator.hasPrevious());
xIterator.next();
xIterator.remove();
yIterator.previous();
yIterator.remove();
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertTrue(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
assertFalse(xIterator.hasNext());
assertTrue(xIterator.hasPrevious());
assertFalse(yIterator.hasNext());
assertTrue(yIterator.hasPrevious());
xIterator.previous();
xIterator.remove();
yIterator.previous();
yIterator.remove();
ksession.fireAllRules();
assertFalse(factsCollection(ksession).contains("X.File0"));
assertFalse(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertFalse(factsCollection(ksession).contains("Y.File0"));
assertFalse(factsCollection(ksession).contains("Y.File1"));
assertFalse(factsCollection(ksession).contains("Y.File2"));
assertFalse(xIterator.hasNext());
assertFalse(xIterator.hasPrevious());
assertFalse(yIterator.hasNext());
assertFalse(yIterator.hasPrevious());
}
Aggregations