Search in sources :

Example 1 with TruthMaintenanceSystem

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

the class ProtobufInputMarshaller method readTruthMaintenanceSystem.

public static void readTruthMaintenanceSystem(MarshallerReaderContext context, EntryPoint wmep, ProtobufMessages.EntryPoint _ep, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
    TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
    // 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.handles.get(_key.getHandleId());
        // ObjectTypeConf state is not marshalled, so it needs to be re-determined
        ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
        if (!typeConf.isTMSEnabled() && (!wasOTCSerialized || tmsEnabled.contains(typeConf.getTypeName()))) {
            typeConf.enableTMS();
        }
        EqualityKey key = new EqualityKey(handle, _key.getStatus());
        handle.setEqualityKey(key);
        if (key.getStatus() == EqualityKey.JUSTIFIED) {
            // not yet added to the object stores
            ((NamedEntryPoint) handle.getEntryPoint()).getObjectStore().addHandle(handle, handle.getObject());
            // add handle to object type node
            assertHandleIntoOTN(context, context.wm, handle, pctxs);
        }
        for (Integer factHandleId : _key.getOtherHandleList()) {
            handle = (InternalFactHandle) context.handles.get(factHandleId.intValue());
            key.addFactHandle(handle);
            handle.setEqualityKey(key);
        }
        tms.put(key);
        context.filter.fireRNEAs(context.wm);
        readBeliefSet(context, tms, key, _key);
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectTypeConfiguration(org.drools.core.marshalling.impl.ProtobufMessages.ObjectTypeConfiguration) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashSet(java.util.HashSet)

Example 2 with TruthMaintenanceSystem

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

the class TruthMaintenanceTest method testLogicalInsertionsUpdateEqual.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsUpdateEqual() throws Exception {
    // calling update on a justified FH, states it
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsUpdateEqual.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(2, ksession.getObjects().size());
    Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
    assertEquals(1, l.size());
    assertEquals(3, ((CheeseEqual) l.iterator().next()).getPrice());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    Collection list = ksession.getObjects();
    // CheeseEqual was updated, making it stated, so it wouldn't have been logically deleted
    assertEquals(1, list.size());
    assertEquals(new CheeseEqual("person", 3), list.iterator().next());
    FactHandle fh = ksession.getFactHandle(list.iterator().next());
    ksession.retract(fh);
    list = ksession.getObjects();
    assertEquals(0, list.size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    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());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 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();
    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 = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    list = ksession.getObjects();
    assertEquals("Only sensor is there", 1, list.size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    assertTrue(tms.getEqualityKeyMap().isEmpty());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 4 with TruthMaintenanceSystem

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

the class TruthMaintenanceTest method testLogicalThenStatedShadowSingleOccurance.

@Test(timeout = 10000)
public void testLogicalThenStatedShadowSingleOccurance() {
    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" + "rule StillHere \n" + "when \n" + "    String( this == 'go2' ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \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);
    session.insert("go1");
    session.fireAllRules();
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go2");
    session.fireAllRules();
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
    assertNotSame(fh1, jfh1);
    EqualityKey key = jfh1.getEqualityKey();
    assertSame(fh1.getEqualityKey(), key);
    assertNotSame(fh1, jfh1);
    assertEquals(2, key.size());
    assertSame(jfh1, key.getLogicalFactHandle());
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
}
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 5 with TruthMaintenanceSystem

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

the class TruthMaintenanceTest method testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated.

@Test(timeout = 10000)
public void testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated() {
    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" + "rule StillHere \n" + "when \n" + "    String( this in ('go2', 'go3', 'go4') ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \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);
    session.insert("go1");
    session.fireAllRules();
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    session.insert("go2");
    session.fireAllRules();
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
    assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
    assertNotSame(fh1, jfh1);
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
    list.clear();
    tms.delete(jfh1);
    session.insert("go3");
    session.fireAllRules();
    assertNull(fh1.getEqualityKey().getBeliefSet());
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
    list.clear();
    session.delete(fh1);
    session.insert("go4");
    session.fireAllRules();
    assertEquals(0, list.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) 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)

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