Search in sources :

Example 11 with EqualityKey

use of org.drools.core.common.EqualityKey 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 12 with EqualityKey

use of org.drools.core.common.EqualityKey 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 13 with EqualityKey

use of org.drools.core.common.EqualityKey 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 14 with EqualityKey

use of org.drools.core.common.EqualityKey in project drools by kiegroup.

the class ReteooWorkingMemoryTest method testBasicWorkingMemoryActions.

/*
     * @see JBRULES-356
     */
@Test
@Ignore
public void testBasicWorkingMemoryActions() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getWorkingMemoryEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final String string = "test";
    FactHandle fd = ksession.insert(string);
    FactHandle fz = ksession.getTruthMaintenanceSystem().insert(string, null, null, new MockActivation());
    assertEquals(1, tms.getEqualityKeyMap().size());
    EqualityKey key = tms.get(string);
    assertSame(fz, key.getFactHandle());
    assertEquals(2, key.size());
    ksession.update(fd, string);
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    assertSame(fz, key.getFactHandle());
    assertEquals(2, key.size());
    ksession.retract(fd);
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    fd = ksession.insert(string);
    assertEquals(1, tms.getEqualityKeyMap().size());
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    assertSame(fd, key.getFactHandle());
    assertEquals(1, key.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) EqualityKey(org.drools.core.common.EqualityKey) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) MockActivation(org.drools.core.test.model.MockActivation) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with EqualityKey

use of org.drools.core.common.EqualityKey in project drools by kiegroup.

the class JTMSTest method testChangeInPositivePrime.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInPositivePrime() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.compiler.Person;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(1); \n" + "    insertLogical( p );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(2); \n" + "    insertLogical( p );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + "    String( this == 'go3' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(3); \n" + "    insertLogical( p );\n" + "end\n" + "\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    // We want to make sure go1 is prime, and then that it switches to go2
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    FactHandle fhGo2 = kSession.insert("go2");
    kSession.fireAllRules();
    FactHandle fhGo3 = kSession.insert("go3");
    kSession.fireAllRules();
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    // just go1, go2, go3, Person(darth)
    assertEquals(4, ep.getObjects().size());
    int count = 0;
    for (Object object : ep.getObjects()) {
        if (object instanceof Person) {
            assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
            count++;
        }
    }
    assertEquals(1, count);
    ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo1);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(2, key.getBeliefSet().size());
    assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo3);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(1, key.getBeliefSet().size());
    assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EqualityKey(org.drools.core.common.EqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

EqualityKey (org.drools.core.common.EqualityKey)21 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)17 Test (org.junit.Test)16 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)15 KieSession (org.kie.api.runtime.KieSession)15 ObjectHashMap (org.drools.core.util.ObjectHashMap)14 Iterator (org.drools.core.util.Iterator)10 FactType (org.kie.api.definition.type.FactType)10 ArrayList (java.util.ArrayList)9 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 FactHandle (org.kie.api.runtime.rule.FactHandle)7 List (java.util.List)5 Ignore (org.junit.Ignore)4 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 Person (org.drools.compiler.Person)2 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)2 KieBase (org.kie.api.KieBase)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 HashSet (java.util.HashSet)1