Search in sources :

Example 16 with EqualityKey

use of org.drools.core.common.EqualityKey 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");
    ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
    // go1, neg are two different strings.
    assertEquals(2, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals("neg")) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    ep.getTruthMaintenanceSystem().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, ep.getTruthMaintenanceSystem().getEqualityKeyMap().size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.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) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) EqualityKey(org.drools.core.common.EqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with EqualityKey

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

the class DefeasibilityTest method testDefeatOutcomePosNeg.

@Test(timeout = 10000)
public void testDefeatOutcomePosNeg() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/negDefeatPos.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.DEFEASIBLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(2, kSession.getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    assertEquals(1, list.size());
    assertTrue(list.contains("-1"));
}
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 18 with EqualityKey

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

the class DefeasibilityTest method testMultipleDefeats.

@Test(timeout = 10000)
public void testMultipleDefeats() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/multiDefeat.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, 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) 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 19 with EqualityKey

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

the class ProtobufOutputMarshaller method writeTruthMaintenanceSystem.

public static void writeTruthMaintenanceSystem(MarshallerWriteContext context, EntryPoint wmep, ProtobufMessages.EntryPoint.Builder _epb) throws IOException {
    TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
    ObjectHashMap justifiedMap = tms.getEqualityKeyMap();
    if (!justifiedMap.isEmpty()) {
        EqualityKey[] keys = new EqualityKey[justifiedMap.size()];
        org.drools.core.util.Iterator it = justifiedMap.iterator();
        int i = 0;
        for (org.drools.core.util.ObjectHashMap.ObjectEntry entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next(); entry != null; entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
            EqualityKey key = (EqualityKey) entry.getKey();
            keys[i++] = key;
        }
        Arrays.sort(keys, EqualityKeySorter.instance);
        ProtobufMessages.TruthMaintenanceSystem.Builder _tms = ProtobufMessages.TruthMaintenanceSystem.newBuilder();
        // write the assert map of Equality keys
        for (EqualityKey key : keys) {
            ProtobufMessages.EqualityKey.Builder _key = ProtobufMessages.EqualityKey.newBuilder();
            _key.setStatus(key.getStatus());
            _key.setHandleId(key.getFactHandle().getId());
            if (key.size() > 1) {
                // add all the other key's if they exist
                FastIterator keyIter = key.fastIterator();
                for (DefaultFactHandle handle = key.getFirst().getNext(); handle != null; handle = (DefaultFactHandle) keyIter.next(handle)) {
                    _key.addOtherHandle(handle.getId());
                }
            }
            if (key.getBeliefSet() != null) {
                writeBeliefSet(context, key.getBeliefSet(), _key);
            }
            _tms.addKey(_key.build());
        }
        _epb.setTms(_tms.build());
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) EqualityKey(org.drools.core.common.EqualityKey) FastIterator(org.drools.core.util.FastIterator)

Example 20 with EqualityKey

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

the class DefaultKnowledgeHelper method bolster.

public InternalFactHandle bolster(final Object object, final Object value) {
    if (object == null || !activation.isMatched()) {
        return null;
    }
    InternalFactHandle handle = getFactHandleFromWM(object);
    NamedEntryPoint ep = (NamedEntryPoint) workingMemory.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
    ObjectTypeConf otc = ep.getObjectTypeConfigurationRegistry().getObjectTypeConf(ep.getEntryPoint(), object);
    BeliefSystem beliefSystem;
    if (value == null) {
        beliefSystem = workingMemory.getTruthMaintenanceSystem().getBeliefSystem();
    } else {
        if (value instanceof Mode) {
            Mode m = (Mode) value;
            beliefSystem = (BeliefSystem) m.getBeliefSystem();
        } else {
            beliefSystem = workingMemory.getTruthMaintenanceSystem().getBeliefSystem();
        }
    }
    BeliefSet beliefSet = null;
    if (handle == null) {
        handle = workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getFactHandleFactoryService().newFactHandle(object, otc, workingMemory, ep);
    }
    if (handle.getEqualityKey() == null) {
        handle.setEqualityKey(new EqualityKey(handle, EqualityKey.STATED));
    } else {
        beliefSet = handle.getEqualityKey().getBeliefSet();
    }
    if (beliefSet == null) {
        beliefSet = beliefSystem.newBeliefSet(handle);
        handle.getEqualityKey().setBeliefSet(beliefSet);
    }
    return beliefSystem.insert(beliefSystem.asMode(value), activation.getRule(), activation, object, beliefSet, activation.getPropagationContext(), otc).getFactHandle();
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) SimpleMode(org.drools.core.beliefsystem.simple.SimpleMode) Mode(org.kie.api.internal.runtime.beliefs.Mode) BeliefSystem(org.drools.core.beliefsystem.BeliefSystem) BeliefSet(org.drools.core.beliefsystem.BeliefSet) InternalFactHandle(org.drools.core.common.InternalFactHandle)

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