Search in sources :

Example 1 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class DefeasibilityTest method checkStatus.

private void checkStatus(EqualityKey key, int support, DefeasibilityStatus status) {
    assertEquals(EqualityKey.JUSTIFIED, key.getStatus());
    BeliefSet set = ((TruthMaintenanceSystemEqualityKey) key).getBeliefSet();
    assertTrue(set instanceof DefeasibleBeliefSet);
    DefeasibleBeliefSet dfs = (DefeasibleBeliefSet) set;
    assertEquals(support, dfs.size());
    assertEquals(status, dfs.getStatus());
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) DefeasibleBeliefSet(org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet) DefeasibleBeliefSet(org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet) BeliefSet(org.drools.tms.beliefsystem.BeliefSet)

Example 2 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class DefeasibilityTest method testWMStatusOnNegativeDefeat.

@Test(timeout = 10000)
public void testWMStatusOnNegativeDefeat() {
    String droolsSource = "package org.drools.tms.test; " + "global java.util.List posList;" + "global java.util.List negList;" + "declare Bar value : int @key end " + "rule Top " + "@Defeasible " + "@Defeats( 'Sub' ) " + "when " + "   $i : Integer( this < 10 ) " + "then " + "   insertLogical( new Bar( $i ) ); " + "end " + "rule Sub " + "@Defeasible " + "when " + "   $i : Integer() " + "then " + "   insertLogical( new Bar( $i ), $i > 10 ? 'pos' : 'neg' ); " + "end " + "rule Sup " + "@Defeasible " + "@Defeats( 'Sub' ) " + "when " + "   $i : Integer( this > 10 ) " + "then " + "   insertLogical( new Bar( $i ), 'neg' ); " + "end " + "rule React_Pos " + "when " + "   $b : Bar() " + "then " + "   posList.add( $b ); " + "   System.out.println( ' ++++ ' + $b ); " + "end " + "rule React_Neg " + "when " + "   $b : Bar( _.neg )" + "then " + "   negList.add( $b ); " + "   System.out.println( ' ---- ' + $b ); " + "end " + "";
    KieSession session = getSessionFromString(droolsSource);
    List posList = new ArrayList();
    List negList = new ArrayList();
    session.setGlobal("posList", posList);
    session.setGlobal("negList", negList);
    session.insert(20);
    session.insert(5);
    session.fireAllRules();
    assertEquals(1, posList.size());
    assertEquals(1, negList.size());
    Object posBar = posList.get(0);
    InternalFactHandle posHandle = (InternalFactHandle) session.getFactHandle(posBar);
    DefeasibleBeliefSet dbs = (DefeasibleBeliefSet) ((TruthMaintenanceSystemEqualityKey) posHandle.getEqualityKey()).getBeliefSet();
    assertEquals(1, dbs.size());
    assertFalse(dbs.isNegated());
    assertTrue(dbs.isDecided());
    assertTrue(dbs.isPositive());
    assertSame(posHandle, dbs.getFactHandle());
    assertFalse(posHandle.isNegated());
    assertTrue(dbs.isDefeasiblyPosProveable());
    assertTrue(session.getObjects().contains(posBar));
    Object negBar = negList.get(0);
    InternalFactHandle negHandle = (InternalFactHandle) getNegativeHandles(session).get(0);
    dbs = (DefeasibleBeliefSet) ((TruthMaintenanceSystemEqualityKey) negHandle.getEqualityKey()).getBeliefSet();
    assertEquals(1, dbs.size());
    assertFalse(dbs.isPositive());
    assertTrue(dbs.isDecided());
    assertTrue(dbs.isNegated());
    assertSame(negHandle, dbs.getFactHandle());
    assertTrue(negHandle.isNegated());
    assertTrue(dbs.isDefeasiblyNegProveable());
    assertTrue(session.getObjects().contains(negBar));
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) DefeasibleBeliefSet(org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 3 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey 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.mvel.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 = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((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.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class JTMSTest method testChangeInNegativePrime.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInNegativePrime() {
    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.mvel.compiler.Person;" + "global java.util.List list;\n" + "\n" + "declare entry-point 'neg' end \n" + "" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(1); \n" + "    insertLogical( p, 'neg' );\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, 'neg' );\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, 'neg' );\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
    assertEquals(3, ep.getObjects().size());
    // Person(darth)
    assertEquals(1, getNegativeObjects(kSession).size());
    int count = 0;
    for (Object object : getNegativeObjects(kSession)) {
        if (object instanceof Person) {
            assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
            count++;
        }
    }
    assertEquals(1, count);
    ObjectHashMap equalityMap = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((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.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class ProtobufInputMarshaller method readTruthMaintenanceSystem.

public static void readTruthMaintenanceSystem(ProtobufMarshallerReaderContext context, EntryPoint wmep, ProtobufMessages.EntryPoint _ep, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
    TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((NamedEntryPoint) wmep);
    // if 0, then the OTC was not serialized (older versions of drools)
    boolean wasOTCSerialized = _ep.getOtcCount() > 0;
    Set<String> tmsEnabled = new HashSet<String>();
    for (ObjectTypeConfiguration _otc : _ep.getOtcList()) {
        if (_otc.getTmsEnabled()) {
            tmsEnabled.add(_otc.getType());
        }
    }
    ProtobufMessages.TruthMaintenanceSystem _tms = _ep.getTms();
    for (ProtobufMessages.EqualityKey _key : _tms.getKeyList()) {
        InternalFactHandle handle = (InternalFactHandle) context.getHandles().get(_key.getHandleId());
        // ObjectTypeConf state is not marshalled, so it needs to be re-determined
        ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(handle.getEntryPointId(), handle.getObject());
        if (!typeConf.isTMSEnabled() && (!wasOTCSerialized || tmsEnabled.contains(typeConf.getTypeName()))) {
            typeConf.enableTMS();
        }
        EqualityKey key = new TruthMaintenanceSystemEqualityKey(handle, _key.getStatus());
        handle.setEqualityKey(key);
        if (key.getStatus() == EqualityKey.JUSTIFIED) {
            // not yet added to the object stores
            handle.getEntryPoint(((NamedEntryPoint) wmep).getReteEvaluator()).getObjectStore().addHandle(handle, handle.getObject());
            // add handle to object type node
            assertHandleIntoOTN(context, context.getWorkingMemory(), handle, pctxs);
        }
        for (Long factHandleId : _key.getOtherHandleList()) {
            handle = context.getHandles().get(factHandleId);
            key.addFactHandle(handle);
            handle.setEqualityKey(key);
        }
        tms.put(key);
        context.getFilter().fireRNEAs(context.getWorkingMemory());
        readBeliefSet(context, tms, _key);
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) ObjectTypeConfiguration(org.drools.serialization.protobuf.ProtobufMessages.ObjectTypeConfiguration) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashSet(java.util.HashSet)

Aggregations

TruthMaintenanceSystemEqualityKey (org.drools.tms.TruthMaintenanceSystemEqualityKey)11 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EqualityKey (org.drools.core.common.EqualityKey)4 ObjectHashMap (org.drools.core.util.ObjectHashMap)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)3 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)3 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 Ignore (org.junit.Ignore)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)2 Person (org.drools.mvel.compiler.Person)2 BeliefSet (org.drools.tms.beliefsystem.BeliefSet)2 DefeasibleBeliefSet (org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet)2 ObjectInputStream (java.io.ObjectInputStream)1