Search in sources :

Example 16 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class ObjectTypeConfigurationRegistry method getObjectTypeConf.

/**
 * Returns the ObjectTypeConfiguration object for the given object or
 * creates a new one if none is found in the cache
 */
public ObjectTypeConf getObjectTypeConf(EntryPointId entrypoint, Object object) {
    // first see if it's a ClassObjectTypeConf
    Object key;
    if (object instanceof Activation) {
        key = ClassObjectType.Match_ObjectType.getClassType();
    } else if (object instanceof Fact) {
        key = ((Fact) object).getFactTemplate().getName();
    } else {
        key = object.getClass();
    }
    ObjectTypeConf objectTypeConf = this.typeConfMap.get(key);
    // it doesn't exist, so create it.
    if (objectTypeConf == null) {
        if (object instanceof Fact) {
            objectTypeConf = new FactTemplateTypeConf(entrypoint, ((Fact) object).getFactTemplate(), this.kBase);
        } else {
            objectTypeConf = new ClassObjectTypeConf(entrypoint, (Class<?>) key, this.kBase);
        }
        ObjectTypeConf existing = this.typeConfMap.putIfAbsent(key, objectTypeConf);
        if (existing != null) {
            // Raced, take the (now) existing.
            objectTypeConf = existing;
        }
    }
    return objectTypeConf;
}
Also used : FactTemplateTypeConf(org.drools.core.reteoo.FactTemplateTypeConf) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) Activation(org.drools.core.spi.Activation) Fact(org.drools.core.facttemplates.Fact)

Example 17 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class TruthMaintenanceSystem method insert.

public InternalFactHandle insert(Object object, Object tmsValue, RuleImpl rule, Activation activation) {
    ObjectTypeConf typeConf = typeConfReg.getObjectTypeConf(ep.getEntryPoint(), object);
    if (!typeConf.isTMSEnabled()) {
        enableTMS(object, typeConf);
    }
    // get the key for other "equal" objects, returns null if none exist
    EqualityKey key = get(object);
    InternalFactHandle fh = null;
    if (key == null) {
        // no EqualityKey exits, so we construct one. We know it can only be justified.
        fh = ep.getHandleFactory().newFactHandle(object, typeConf, ep.getInternalWorkingMemory(), ep);
        key = new EqualityKey(fh, EqualityKey.JUSTIFIED);
        fh.setEqualityKey(key);
        put(key);
    } else {
        fh = key.getLogicalFactHandle();
        if (fh == null) {
            // The EqualityKey exists, but this is the first logical object in the key.
            fh = ep.getHandleFactory().newFactHandle(object, typeConf, ep.getInternalWorkingMemory(), ep);
            key.setLogicalFactHandle(fh);
            fh.setEqualityKey(key);
        }
    }
    // Any logical propagations are handled via the TMS.addLogicalDependency
    fh = addLogicalDependency(fh, object, tmsValue, activation, activation.getPropagationContext(), rule, typeConf);
    return fh;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf)

Example 18 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeObjectTypeConfiguration.

private static void writeObjectTypeConfiguration(MarshallerWriteContext context, ObjectTypeConfigurationRegistry otcr, org.drools.core.marshalling.impl.ProtobufMessages.EntryPoint.Builder _epb) {
    Collection<ObjectTypeConf> values = otcr.values();
    ObjectTypeConf[] otcs = values.toArray(new ObjectTypeConf[values.size()]);
    Arrays.sort(otcs, new Comparator<ObjectTypeConf>() {

        @Override
        public int compare(ObjectTypeConf o1, ObjectTypeConf o2) {
            return o1.getTypeName().compareTo(o2.getTypeName());
        }
    });
    for (ObjectTypeConf otc : otcs) {
        ObjectTypeNode objectTypeNode = otc.getConcreteObjectTypeNode();
        if (objectTypeNode != null) {
            final ObjectTypeNodeMemory memory = context.wm.getNodeMemory(objectTypeNode);
            if (memory != null) {
                ObjectTypeConfiguration _otc = ObjectTypeConfiguration.newBuilder().setType(otc.getTypeName()).setTmsEnabled(otc.isTMSEnabled()).build();
                _epb.addOtc(_otc);
            }
        }
    }
}
Also used : ObjectTypeConfiguration(org.drools.core.marshalling.impl.ProtobufMessages.ObjectTypeConfiguration) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory)

Example 19 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class LazyTMSEnablingTest method shouldLazilyAdd.

@Test
public void shouldLazilyAdd() throws Exception {
    final String fact1 = "logical";
    ksession.insert(fact1);
    assertEquals("Shouldn't have anything, since no logical insert was performed.", 0, tms.getEqualityKeyMap().size());
    final String fact2 = "logical";
    ksession.getTruthMaintenanceSystem().insert(fact2, null, null, new MockActivation());
    assertEquals("Now that a logical insert was done, it should have an element.", 1, tms.getEqualityKeyMap().size());
    // Make sure the internals are fine.
    ObjectTypeConf typeConf = ksession.getObjectTypeConfigurationRegistry().getObjectTypeConf(ksession.getEntryPoint(), fact1);
    assertTrue("Should have enabled TMS", typeConf.isTMSEnabled());
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) MockActivation(org.drools.core.test.model.MockActivation) Test(org.junit.Test)

Example 20 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf 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

ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 PropagationContext (org.drools.core.spi.PropagationContext)6 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)4 ObjectTypeConfigurationRegistry (org.drools.core.common.ObjectTypeConfigurationRegistry)4 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)3 BeliefSet (org.drools.core.beliefsystem.BeliefSet)3 Test (org.junit.Test)3 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EqualityKey (org.drools.core.common.EqualityKey)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)2 ObjectTypeConfiguration (org.drools.core.marshalling.impl.ProtobufMessages.ObjectTypeConfiguration)2 EntryPointId (org.drools.core.rule.EntryPointId)2 Activation (org.drools.core.spi.Activation)2 MockActivation (org.drools.core.test.model.MockActivation)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashSet (java.util.HashSet)1