Search in sources :

Example 36 with TruthMaintenanceSystem

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

the class TruthMaintenanceTest method testLogicalInsertions2.

@Test(timeout = 10000)
public void testLogicalInsertions2() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertions2.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final List events = new ArrayList();
        ksession.setGlobal("events", events);
        final Sensor sensor = new Sensor(80, 80);
        FactHandle handle = ksession.insert(sensor);
        // everything should be normal
        ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
        ksession.fireAllRules();
        Collection list = ksession.getObjects();
        assertEquals("Only sensor is there", 1, list.size());
        assertEquals("Only one event", 1, events.size());
        // problems should be detected
        sensor.setPressure(200);
        sensor.setTemperature(200);
        handle = getFactHandle(handle, ksession);
        ksession.update(handle, sensor);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        list = ksession.getObjects();
        assertEquals("Only sensor is there", 1, list.size());
        TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
        assertTrue(tms.getEqualityKeyMap().isEmpty());
    } finally {
        ksession.dispose();
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Sensor(org.drools.mvel.compiler.Sensor) Test(org.junit.Test)

Example 37 with TruthMaintenanceSystem

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

the class TruthMaintenanceTest method testLogicalInsertionsWithModify.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsWithUpdate.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final Person p = new Person("person");
        p.setAge(2);
        FactHandle h = ksession.insert(p);
        assertEquals(1, ksession.getObjects().size());
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
        assertEquals(2, ksession.getObjects().size());
        Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
        assertEquals(1, l.size());
        assertEquals(2, ((CheeseEqual) l.iterator().next()).getPrice());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
        assertEquals(0, ksession.getObjects().size());
        TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
        final java.lang.reflect.Field field = tms.getClass().getDeclaredField("equalityKeyMap");
        field.setAccessible(true);
        final ObjectHashMap m = (ObjectHashMap) field.get(tms);
        field.setAccessible(false);
        assertEquals("assertMap should be empty", 0, m.size());
    } finally {
        ksession.dispose();
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) CheeseEqual(org.drools.mvel.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 38 with TruthMaintenanceSystem

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

the class JTMSTest method testRetractHandleWhenOnlyNeg.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testRetractHandleWhenOnlyNeg() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1_1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_2\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_3\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "\n" + "rule \"Negative\"\n" + "when\n" + "    $n : String(  _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + "    final String s = '-' + $n; \n" + "    final List l = list; \n" + "    l.add( s ); \n" + "end\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    ((RuleEventManager) kSession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            String rule = match.getRule().getName();
            if (rule.equals("Negative")) {
                list.remove("-" + match.getDeclarationValue("$n"));
            }
        }
    });
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    assertTrue(list.contains("-neg"));
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep);
    ObjectHashMap equalityMap = tms.getEqualityKeyMap();
    // go1, neg are two different strings.
    assertEquals(2, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals("neg")) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    tms.delete(key.getLogicalFactHandle());
    assertEquals(0, key.getBeliefSet().size());
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(0, getNegativeObjects(kSession).size());
    assertEquals(0, key.getBeliefSet().size());
    assertEquals(1, tms.getEqualityKeyMap().size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) Match(org.kie.api.runtime.rule.Match) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with TruthMaintenanceSystem

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

the class DefeasibilityTest method testMultipleDefeats.

@Test(timeout = 10000)
public void testMultipleDefeats() {
    KieSession kSession = getSession("org/drools/mvel/compiler/beliefsystem/defeasible/multiDefeat.drl");
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) kSession);
    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, 2, DefeasibilityStatus.DEFEATEDLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    for (Object o : kSession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(2, kSession.getObjects().size());
    kSession.fireAllRules();
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) 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 40 with TruthMaintenanceSystem

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

the class DefeasibilityTest method testRemoveDefeasibleJustifier.

@Test(timeout = 10000)
public void testRemoveDefeasibleJustifier() {
    KieSession kSession = getSession("org/drools/mvel/compiler/beliefsystem/defeasible/defeaterRetract.drl");
    FactHandle h = kSession.insert("go");
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) kSession);
    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) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) 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)40 Test (org.junit.Test)36 KieSession (org.kie.api.runtime.KieSession)33 EqualityKey (org.drools.core.common.EqualityKey)28 ObjectHashMap (org.drools.core.util.ObjectHashMap)27 Iterator (org.drools.core.util.Iterator)20 FactType (org.kie.api.definition.type.FactType)20 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)17 ArrayList (java.util.ArrayList)16 FactHandle (org.kie.api.runtime.rule.FactHandle)15 TruthMaintenanceSystemEqualityKey (org.drools.tms.TruthMaintenanceSystemEqualityKey)13 KieBase (org.kie.api.KieBase)11 List (java.util.List)8 Collection (java.util.Collection)6 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)6 Ignore (org.junit.Ignore)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)5 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)5 ClassObjectFilter (org.drools.core.ClassObjectFilter)4