Search in sources :

Example 6 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class DefeasibilityTest method testRemoveDefeasibleJustifier.

@Test(timeout = 10000)
public void testRemoveDefeasibleJustifier() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeaterRetract.drl");
    FactHandle h = kSession.insert("go");
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
    FactType Atype = kSession.getKieBase().getFactType("org.drools.defeasible", "A");
    ObjectHashMap keys = tms.getEqualityKeyMap();
    Iterator iter = keys.iterator();
    ObjectHashMap.ObjectEntry entry;
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Ctype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
        } else if (factClass == Atype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    kSession.retract(h);
    kSession.fireAllRules();
    keys = tms.getEqualityKeyMap();
    iter = keys.iterator();
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Ctype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 7 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class DefeasibilityTest method testDefeaterNeutrality.

@Test(timeout = 10000)
public void testDefeaterNeutrality() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeaterOnly.drl");
    ArrayList list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
    ObjectHashMap keys = tms.getEqualityKeyMap();
    Iterator iter = keys.iterator();
    ObjectHashMap.ObjectEntry entry;
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Xtype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    for (Object o : kSession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(0, list.size());
    assertEquals(1, kSession.getFactCount());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 8 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class DefeasibilityTest method testRemoveDefeasibleEntailmentMultiActivationWithDefeat.

@Test(timeout = 10000)
public void testRemoveDefeasibleEntailmentMultiActivationWithDefeat() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatDefeaterRetract.drl");
    ArrayList list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
    ObjectHashMap keys = tms.getEqualityKeyMap();
    Iterator iter = keys.iterator();
    ObjectHashMap.ObjectEntry entry;
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Xtype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(1, list.size());
    assertTrue(list.contains("Stage1"));
    assertEquals(3, kSession.getObjects().size());
    for (Object o : kSession.getObjects()) {
        System.out.println(o);
    }
    FactHandle h = kSession.insert("go");
    kSession.fireAllRules();
    keys = tms.getEqualityKeyMap();
    iter = keys.iterator();
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Xtype.getFactClass()) {
            checkStatus(key, 3, DefeasibilityStatus.DEFEASIBLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    for (Object o : kSession.getObjects()) {
        System.out.println(o);
    }
    // A, A, B, X, GO
    assertEquals(5, kSession.getObjects().size());
    assertEquals(3, list.size());
    assertTrue(list.contains("Stage1"));
    assertTrue(list.contains("Stage2"));
    kSession.retract(h);
    kSession.fireAllRules();
    for (Object o : kSession.getObjects()) {
        System.out.println(o);
    }
    // A, A, B, X, GO
    assertEquals(3, kSession.getObjects().size());
    keys = tms.getEqualityKeyMap();
    iter = keys.iterator();
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Xtype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 9 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class DefeasibilityTest method testDefeaterPositiveVsNegative.

@Test(timeout = 10000)
public void testDefeaterPositiveVsNegative() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatersPosNeg.drl");
    ArrayList list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
    ObjectHashMap keys = tms.getEqualityKeyMap();
    Iterator iter = keys.iterator();
    ObjectHashMap.ObjectEntry entry;
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Object fact = key.getFactHandle().getObject();
        Class factClass = fact.getClass();
        if (factClass == Xtype.getFactClass()) {
            Integer val = (Integer) Xtype.get(fact, "id");
            switch(val) {
                case -1:
                    checkStatus(key, 2, DefeasibilityStatus.UNDECIDABLY);
                    break;
                case 3:
                    checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
                    break;
                case -35:
                    checkStatus(key, 3, DefeasibilityStatus.UNDECIDABLY);
                    break;
                case 44:
                    checkStatus(key, 2, DefeasibilityStatus.DEFEASIBLY);
                    break;
                default:
                    fail("Unrecognized fact");
            }
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(1, list.size());
    assertTrue(list.contains(-44));
    assertTrue(!list.contains(-35));
    assertEquals(2, kSession.getFactCount());
    assertEquals(1, getNegativeObjects(kSession).size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 10 with Iterator

use of org.drools.core.util.Iterator in project drools by kiegroup.

the class ActivationIteratorTest method testSingleJoinNode.

@Test
public void testSingleJoinNode() {
    String str = "package org.kie.test \n" + "\n" + "rule rule1  @Propagation(EAGER)  when\n" + "    $s1 : String( )\n" + "    $s2 : String( )\n" + "then\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 2; i++) {
        ksession.insert(new String("" + i));
    }
    evaluateEagerList(ksession);
    Iterator it = ActivationIterator.iterator(ksession);
    List list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.getDeclarationValue("$s2") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule1:0:1:true", "rule1:1:0:true", "rule1:1:1:true", "rule1:0:0:true" }, list);
    ksession.fireAllRules();
    it = ActivationIterator.iterator(ksession);
    list = new ArrayList();
    for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
        list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s1") + ":" + act.getDeclarationValue("$s2") + ":" + act.isQueued());
    }
    assertContains(new String[] { "rule1:0:1:false", "rule1:1:0:false", "rule1:1:1:false", "rule1:0:0:false" }, list);
}
Also used : ActivationIterator(org.drools.core.common.ActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Aggregations

Iterator (org.drools.core.util.Iterator)33 Test (org.junit.Test)24 KieSession (org.kie.api.runtime.KieSession)23 ArrayList (java.util.ArrayList)19 List (java.util.List)13 AgendaItem (org.drools.core.common.AgendaItem)13 ObjectHashMap (org.drools.core.util.ObjectHashMap)13 KieHelper (org.kie.internal.utils.KieHelper)13 ActivationIterator (org.drools.core.common.ActivationIterator)12 EqualityKey (org.drools.core.common.EqualityKey)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)10 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)10 FactType (org.kie.api.definition.type.FactType)10 Tuple (org.drools.core.spi.Tuple)4 FastIterator (org.drools.core.util.FastIterator)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)2