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;
}
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;
}
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);
}
}
}
}
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());
}
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();
}
Aggregations