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