use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class FirstOrderLogicTest method testRemoveIdentitiesSubNetwork.
@Test
public void testRemoveIdentitiesSubNetwork() throws Exception {
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(RemoveIdentitiesOption.YES);
KieBase kbase = loadKnowledgeBase(conf, "test_removeIdentitiesSubNetwork.drl");
KieSession workingMemory = createKnowledgeSession(kbase);
final List list = new ArrayList();
workingMemory.setGlobal("results", list);
final Person bob = new Person("bob", "stilton");
workingMemory.insert(bob);
final Person mark = new Person("mark", "stilton");
workingMemory.insert(mark);
final Cheese stilton1 = new Cheese("stilton", 6);
final FactHandle stilton1Handle = (FactHandle) workingMemory.insert(stilton1);
final Cheese stilton2 = new Cheese("stilton", 7);
final FactHandle stilton2Handle = (FactHandle) workingMemory.insert(stilton2);
workingMemory.fireAllRules();
assertEquals(0, list.size());
workingMemory.retract(stilton1Handle);
workingMemory.fireAllRules();
assertEquals(1, list.size());
assertEquals(mark, list.get(0));
workingMemory.retract(stilton2Handle);
workingMemory.fireAllRules();
assertEquals(2, list.size());
assertEquals(bob, list.get(1));
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class Misc2Test method testBetaMemoryLeakOnSegmentUnlinking.
@Test
@Ignore
public void testBetaMemoryLeakOnSegmentUnlinking() {
// DROOLS-915
String drl = "rule R1 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + " $c : Integer(this == 2)\n" + " $d : Integer(this == 3)\n" + "then \n" + "end\n" + "rule R2 when\n" + " $a : Integer(this == 1)\n" + " $b : String()\n" + "then \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
FactHandle fh1 = ksession.insert(1);
FactHandle fh2 = ksession.insert(2);
FactHandle fh3 = ksession.insert(3);
FactHandle fhtest = ksession.insert("test");
ksession.fireAllRules();
ksession.delete(fh3);
ksession.fireAllRules();
ksession.delete(fh1);
ksession.delete(fh2);
ksession.delete(fhtest);
ksession.fireAllRules();
NodeMemories nodeMemories = ((InternalWorkingMemory) ksession).getNodeMemories();
for (int i = 0; i < nodeMemories.length(); i++) {
Memory memory = nodeMemories.peekNodeMemory(i);
if (memory != null && memory.getSegmentMemory() != null) {
SegmentMemory segmentMemory = memory.getSegmentMemory();
System.out.println(memory);
LeftTuple deleteFirst = memory.getSegmentMemory().getStagedLeftTuples().getDeleteFirst();
System.out.println(deleteFirst);
assertNull(deleteFirst);
}
}
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class Misc2Test method testPhreakAccumulate.
@Test
public void testPhreakAccumulate() {
// DROOLS-7
String str = "import org.drools.compiler.integrationtests.Misc2Test.Lecture\n" + "global java.util.List list;\n" + "rule \"R1\"\n" + " when\n" + " $lecture : Lecture(\n" + " $day : day, $index : index\n" + " )\n" + " not Lecture(\n" + " day == $day, index == ($index + 1)\n" + " )\n" + " then\n" + " list.add($lecture.getId());\n" + "end\n" + "rule \"R2\"\n" + " when\n" + " $availableLectures : Number(intValue > 0) from accumulate(\n" + " $lecture : Lecture(\n" + " available == true\n" + " ),\n" + " count($lecture)\n" + " )\n\n" + " then\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
Lecture lA = new Lecture("A", 0, 4, true);
Lecture lB = new Lecture("B", 2, 2, true);
Lecture lC = new Lecture("C", 2, 1, true);
FactHandle fhA = ksession.insert(lA);
FactHandle fhB = ksession.insert(lB);
FactHandle fhC = ksession.insert(lC);
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("A", "B")));
list.clear();
ksession.update(fhB, lB.setAvailable(false));
ksession.fireAllRules();
ksession.update(fhB, lB.setDay(0).setIndex(3));
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("B", "C")));
list.clear();
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class Misc2Test method testReorderRightMemoryOnIndexedField.
@Test
public void testReorderRightMemoryOnIndexedField() {
// DROOLS-1174
String rule = "import " + Misc2Test.Seat.class.getCanonicalName() + ";\n" + "\n" + "rule twoSameJobTypePerTable when\n" + " $job: String()\n" + " $table : Long()\n" + " not (\n" + " Seat( guestJob == $job, table == $table, $leftId : id )\n" + " and Seat( guestJob == $job, table == $table, id > $leftId )\n" + " )\n" + "then\n" + "end";
KieSession kieSession = new KieHelper().addContent(rule, ResourceType.DRL).build().newKieSession();
String doctor = "D";
String politician = "P";
Long table1 = 1L;
Long table2 = 2L;
Seat seat0 = new Seat(0, politician, table2);
Seat seat1 = new Seat(1, politician, null);
Seat seat2 = new Seat(2, politician, table2);
Seat seat3 = new Seat(3, doctor, table1);
Seat seat4 = new Seat(4, doctor, table1);
kieSession.insert(seat0);
FactHandle fh1 = kieSession.insert(seat1);
FactHandle fh2 = kieSession.insert(seat2);
FactHandle fh3 = kieSession.insert(seat3);
kieSession.insert(seat4);
kieSession.insert(politician);
kieSession.insert(doctor);
kieSession.insert(table1);
kieSession.insert(table2);
assertEquals(2, kieSession.fireAllRules());
// no change but the update is necessary to reproduce the bug
kieSession.update(fh3, seat3);
kieSession.update(fh2, seat2.setTable(null));
kieSession.update(fh1, seat1.setTable(table2));
assertEquals(0, kieSession.fireAllRules());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class Misc2Test method testDynamicNegativeSalienceWithSpace.
@Test
public void testDynamicNegativeSalienceWithSpace() {
// DROOLS-302
String str = "import org.drools.compiler.Person\n" + "rule R\n" + "salience - $age\n" + "when\n" + " Person( $age : age )\n" + "then\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
Person p1 = new Person("A", 31);
FactHandle fh1 = ksession.insert(p1);
ksession.fireAllRules();
}
Aggregations