Search in sources :

Example 86 with FactHandle

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));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 87 with FactHandle

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);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Memory(org.drools.core.common.Memory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) NodeMemories(org.drools.core.common.NodeMemories) LeftTuple(org.drools.core.reteoo.LeftTuple) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 88 with FactHandle

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();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 89 with FactHandle

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());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 90 with FactHandle

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();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

FactHandle (org.kie.api.runtime.rule.FactHandle)650 Test (org.junit.Test)547 KieSession (org.kie.api.runtime.KieSession)506 KieBase (org.kie.api.KieBase)304 ArrayList (java.util.ArrayList)298 InternalFactHandle (org.drools.core.common.InternalFactHandle)192 List (java.util.List)165 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)119 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)66 KieHelper (org.kie.internal.utils.KieHelper)50 Person (org.drools.modelcompiler.domain.Person)46 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 HashMap (java.util.HashMap)41 Person (org.drools.compiler.Person)41 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)39 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 Person (org.drools.mvel.compiler.Person)38 Ignore (org.junit.Ignore)36 Match (org.kie.api.runtime.rule.Match)36 Collection (java.util.Collection)34