Search in sources :

Example 1 with NamedEntryPoint

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

the class InputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    int type = context.stream.readInt();
    int id = context.stream.readInt();
    long recency = context.stream.readLong();
    long startTimeStamp = 0;
    long duration = 0;
    boolean expired = false;
    long activationsCount = 0;
    if (type == 2) {
        startTimeStamp = context.stream.readLong();
        duration = context.stream.readLong();
        expired = context.stream.readBoolean();
        activationsCount = context.stream.readLong();
    }
    int strategyIndex = context.stream.readInt();
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    // This is the old way of de/serializing strategy objects
    if (strategyIndex >= 0) {
        strategy = context.resolverStrategyFactory.getStrategy(strategyIndex);
    } else // This is the new way
    if (strategyIndex == -2) {
        String strategyClassName = context.stream.readUTF();
        if (!StringUtils.isEmpty(strategyClassName)) {
            strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
            if (strategy == null) {
                throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
            }
        }
    }
    // If either way retrieves a strategy, use it
    if (strategy != null) {
        object = strategy.read(context.stream);
    }
    EntryPoint entryPoint = null;
    if (context.readBoolean()) {
        String entryPointId = context.readUTF();
        if (entryPointId != null && !entryPointId.equals("")) {
            entryPoint = ((RuleRuntime) context.wm).getEntryPoint(entryPointId);
        }
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(type) {
        case 0:
            {
                handle = new DefaultFactHandle(id, object, recency, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case 1:
            {
                handle = new QueryElementFactHandle(object, id, recency);
                break;
            }
        case 2:
            {
                handle = new EventFactHandle(id, object, recency, startTimeStamp, duration, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(expired);
                ((EventFactHandle) handle).setActivationsCount(activationsCount);
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + type);
            }
    }
    return handle;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 2 with NamedEntryPoint

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

use of org.drools.core.common.NamedEntryPoint 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 4 with NamedEntryPoint

use of org.drools.core.common.NamedEntryPoint 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 5 with NamedEntryPoint

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

the class TraitHelper method update.

public void update(final FactHandle handle, BitMask mask, Class<?> modifiedClass, Activation activation) {
    InternalFactHandle h = (InternalFactHandle) handle;
    ((NamedEntryPoint) h.getEntryPoint()).update(h, ((InternalFactHandle) handle).getObject(), mask, modifiedClass, activation);
    if (h.isTraitOrTraitable()) {
        workingMemory.updateTraits(h, mask, modifiedClass, activation);
    }
}
Also used : NamedEntryPoint(org.drools.core.common.NamedEntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Aggregations

NamedEntryPoint (org.drools.core.common.NamedEntryPoint)32 Test (org.junit.Test)21 KieSession (org.kie.api.runtime.KieSession)19 EqualityKey (org.drools.core.common.EqualityKey)17 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)17 ObjectHashMap (org.drools.core.util.ObjectHashMap)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 FactHandle (org.kie.api.runtime.rule.FactHandle)12 Iterator (org.drools.core.util.Iterator)10 FactType (org.kie.api.definition.type.FactType)10 ArrayList (java.util.ArrayList)9 Person (org.drools.compiler.Person)6 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)5 Ignore (org.junit.Ignore)5 List (java.util.List)4 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)4 KieBase (org.kie.api.KieBase)4 Collection (java.util.Collection)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3