Search in sources :

Example 1 with NamedEntryPoint

use of org.drools.kiesession.entrypoints.NamedEntryPoint in project drools by kiegroup.

the class TruthMaintenanceSystemKnowledgeHelper method bolster.

@Override
public InternalFactHandle bolster(final Object object, final Object value) {
    if (object == null || !activation.isMatched()) {
        return null;
    }
    InternalFactHandle handle = getFactHandleFromWM(object);
    NamedEntryPoint ep = (NamedEntryPoint) reteEvaluator.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
    ObjectTypeConf otc = ep.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(ep.getEntryPoint(), object);
    BeliefSet beliefSet = null;
    if (handle == null) {
        handle = RuntimeComponentFactory.get().getFactHandleFactoryService().newFactHandle(object, otc, reteEvaluator, ep);
    }
    if (handle.getEqualityKey() == null) {
        handle.setEqualityKey(new TruthMaintenanceSystemEqualityKey(handle, EqualityKey.STATED));
    } else {
        beliefSet = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
    }
    BeliefSystem beliefSystem = value instanceof BeliefSystemMode ? ((BeliefSystemMode) value).getBeliefSystem() : ((TruthMaintenanceSystemImpl) TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(toStatefulKnowledgeSession())).getBeliefSystem();
    if (beliefSet == null) {
        beliefSet = beliefSystem.newBeliefSet(handle);
        ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).setBeliefSet(beliefSet);
    }
    return beliefSystem.insert(beliefSystem.asMode(value), activation.getRule(), (TruthMaintenanceSystemActivation) activation, object, beliefSet, activation.getPropagationContext(), otc).getFactHandle();
}
Also used : TruthMaintenanceSystemActivation(org.drools.tms.agenda.TruthMaintenanceSystemActivation) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) BeliefSystemMode(org.drools.tms.beliefsystem.BeliefSystemMode) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) BeliefSystem(org.drools.tms.beliefsystem.BeliefSystem) BeliefSet(org.drools.tms.beliefsystem.BeliefSet) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 2 with NamedEntryPoint

use of org.drools.kiesession.entrypoints.NamedEntryPoint 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 3 with NamedEntryPoint

use of org.drools.kiesession.entrypoints.NamedEntryPoint in project drools by kiegroup.

the class JTMSTest method testConflictStrict.

@Test(timeout = 10000)
public void testConflictStrict() {
    KieSession kSession = getSessionFromFile("posNegConflict.drl");
    ArrayList list = new ArrayList();
    kSession.setGlobal("list", list);
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    JTMSBeliefSystem bs = (JTMSBeliefSystem) ((TruthMaintenanceSystemImpl) TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep)).getBeliefSystem();
    bs.STRICT = true;
    try {
        kSession.fireAllRules();
        fail("A fact and its negation should have been asserted, but no exception was trhown in strict mode");
    } catch (Exception e) {
    } finally {
        bs.STRICT = false;
    }
}
Also used : JTMSBeliefSystem(org.drools.tms.beliefsystem.jtms.JTMSBeliefSystem) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with NamedEntryPoint

use of org.drools.kiesession.entrypoints.NamedEntryPoint 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 NamedEntryPoint

use of org.drools.kiesession.entrypoints.NamedEntryPoint in project drools by kiegroup.

the class TraitHelperImpl method updateCore.

private <T> void updateCore(TraitableBean inner, Object core, Class<T> trait, boolean logical, Activation activation) {
    FactHandle handle = lookupFactHandle(inner);
    InternalFactHandle h = (InternalFactHandle) handle;
    if (handle != null) {
        TraitFieldTMS fieldTMS = inner._getFieldTMS();
        BitMask mask = fieldTMS == null ? onlyTraitBitSetMask() : fieldTMS.getModificationMask();
        Object o = h.getObject();
        NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint(workingMemory);
        PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getReteEvaluator().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation.getRule(), activation.getTuple().getTupleSink(), h, nep.getEntryPoint(), mask, core.getClass(), null);
        nep.update(h, o, o, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(o), propagationContext);
    } else {
        handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
    }
}
Also used : TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) BitMask(org.drools.core.util.bitmask.BitMask) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Aggregations

NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)18 Test (org.junit.Test)9 KieSession (org.kie.api.runtime.KieSession)8 ArrayList (java.util.ArrayList)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)6 List (java.util.List)5 TruthMaintenanceSystemEqualityKey (org.drools.tms.TruthMaintenanceSystemEqualityKey)5 FactHandle (org.kie.api.runtime.rule.FactHandle)5 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 ObjectHashMap (org.drools.core.util.ObjectHashMap)4 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 Person (org.drools.mvel.compiler.Person)3 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)2