Search in sources :

Example 6 with TruthMaintenanceSystem

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

the class TruthMaintenanceTest method testStatedWithShadowAndDeleteException.

@Test(timeout = 10000)
public void testStatedWithShadowAndDeleteException() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go1");
    session.fireAllRules();
    // TMS is now enabled
    assertNotNull(fh1.getEqualityKey());
    assertNotNull(fh2.getEqualityKey());
    // EqualtyKey shows both are stated
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertEquals(EqualityKey.STATED, fh2.getEqualityKey().getStatus());
    // Only fh1 has a logical
    assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
    assertNull(fh2.getEqualityKey().getBeliefSet());
    // Get the logical Handle too
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    EqualityKey key = jfh1.getEqualityKey();
    assertSame(fh1.getEqualityKey(), key);
    assertNotSame(fh1, jfh1);
    assertEquals(2, key.size());
    assertSame(jfh1, key.getLogicalFactHandle());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 7 with TruthMaintenanceSystem

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

the class DefeasibilityTest method testDefeasibleEntailmentWithStrictOverride.

@Test(timeout = 10000)
public void testDefeasibleEntailmentWithStrictOverride() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strictOverride.drl");
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
    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 == Ctype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
        } else if (factClass == Xtype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(5, kSession.getObjects().size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) 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 8 with TruthMaintenanceSystem

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

the class DefeasibilityTest method testRemoveDefiniteJustifier.

@Test(timeout = 10000)
public void testRemoveDefiniteJustifier() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strictRetract.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.DEFEASIBLY);
        } 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 9 with TruthMaintenanceSystem

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

the class DefeasibilityTest method defeasibleEntailmentMultiActivation.

@Test(timeout = 10000)
public void defeasibleEntailmentMultiActivation() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeat.drl");
    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, 3, DefeasibilityStatus.UNDECIDABLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(3, kSession.getObjects().size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) 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 10 with TruthMaintenanceSystem

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

the class DefeasibilityTest method testStrictEntailment.

@Test(timeout = 10000)
public void testStrictEntailment() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strict.drl");
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
    FactType Dtype = kSession.getKieBase().getFactType("org.drools.defeasible", "D");
    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, 2, DefeasibilityStatus.DEFINITELY);
        } else if (factClass == Dtype.getFactClass()) {
            checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(5, kSession.getObjects().size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) 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)

Aggregations

TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)22 Test (org.junit.Test)20 KieSession (org.kie.api.runtime.KieSession)19 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)17 EqualityKey (org.drools.core.common.EqualityKey)15 ObjectHashMap (org.drools.core.util.ObjectHashMap)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)12 ArrayList (java.util.ArrayList)10 Iterator (org.drools.core.util.Iterator)10 FactType (org.kie.api.definition.type.FactType)10 KieBase (org.kie.api.KieBase)8 FactHandle (org.kie.api.runtime.rule.FactHandle)7 List (java.util.List)6 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)6 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 Collection (java.util.Collection)3 Person (org.drools.compiler.Person)3 CheeseEqual (org.drools.compiler.CheeseEqual)2 ClassObjectFilter (org.drools.core.ClassObjectFilter)2