use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class ProtobufInputMarshaller method readBeliefSet.
private static void readBeliefSet(MarshallerReaderContext context, TruthMaintenanceSystem tms, ProtobufMessages.EqualityKey _key) throws IOException, ClassNotFoundException {
if (_key.hasBeliefSet()) {
ProtobufMessages.BeliefSet _beliefSet = _key.getBeliefSet();
InternalFactHandle handle = (InternalFactHandle) context.getHandles().get(_key.getHandleId());
// phreak might serialize empty belief sets, so he have to handle it during deserialization
if (_beliefSet.getLogicalDependencyCount() > 0) {
for (ProtobufMessages.LogicalDependency _logicalDependency : _beliefSet.getLogicalDependencyList()) {
ProtobufMessages.Activation _activation = _logicalDependency.getActivation();
ActivationKey activationKey = getActivationKey(context, _activation);
Activation activation = (Activation) ((PBActivationsFilter) context.getFilter()).getTuplesCache().get(activationKey);
Object object = null;
ObjectMarshallingStrategy strategy = null;
if (_logicalDependency.hasObjectStrategyIndex()) {
strategy = context.getUsedStrategies().get(_logicalDependency.getObjectStrategyIndex());
object = strategy.unmarshal(context.getStrategyContexts().get(strategy), (ObjectInputStream) context, _logicalDependency.getObject().toByteArray(), (context.getKnowledgeBase() == null) ? null : context.getKnowledgeBase().getRootClassLoader());
}
Object value = null;
if (_logicalDependency.hasValueStrategyIndex()) {
strategy = context.getUsedStrategies().get(_logicalDependency.getValueStrategyIndex());
value = strategy.unmarshal(context.getStrategyContexts().get(strategy), (ObjectInputStream) context, _logicalDependency.getValue().toByteArray(), (context.getKnowledgeBase() == null) ? null : context.getKnowledgeBase().getRootClassLoader());
}
ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(handle.getEntryPointId(), handle.getObject());
tms.readLogicalDependency(handle, object, value, activation, typeConf);
}
} else {
((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).setBeliefSet(((TruthMaintenanceSystemImpl) tms).getBeliefSystem().newBeliefSet(handle));
}
}
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeFactHandle.
private static ProtobufMessages.FactHandle writeFactHandle(MarshallerWriteContext context, ObjectMarshallingStrategyStore objectMarshallingStrategyStore, InternalFactHandle handle) throws IOException {
ProtobufMessages.FactHandle.Builder _handle = ProtobufMessages.FactHandle.newBuilder();
_handle.setType(getHandleType(handle));
_handle.setId(handle.getId());
_handle.setRecency(handle.getRecency());
if (_handle.getType() == ProtobufMessages.FactHandle.HandleType.EVENT) {
// is event
EventFactHandle efh = (EventFactHandle) handle;
_handle.setTimestamp(efh.getStartTimestamp());
_handle.setDuration(efh.getDuration());
_handle.setIsExpired(efh.isExpired());
_handle.setActivationsCount(efh.getActivationsCount());
_handle.setOtnCount(efh.getOtnCount());
}
if (handle.getEqualityKey() != null && handle.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
_handle.setIsJustified(true);
} else {
_handle.setIsJustified(false);
}
Object object = handle.getObject();
if (object != null) {
ObjectMarshallingStrategy strategy = objectMarshallingStrategyStore.getStrategyObject(object);
Integer index = context.getStrategyIndex(strategy);
_handle.setStrategyIndex(index);
_handle.setObject(ByteString.copyFrom(strategy.marshal(context.getStrategyContext().get(strategy), (ObjectOutputStream) context, object)));
}
return _handle.build();
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class InputMarshaller method readFactHandle.
public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
int type = context.readInt();
long id = context.readLong();
long recency = context.readLong();
long startTimeStamp = 0;
long duration = 0;
boolean expired = false;
long activationsCount = 0;
if (type == 2) {
startTimeStamp = context.readLong();
duration = context.readLong();
expired = context.readBoolean();
activationsCount = context.readLong();
}
int strategyIndex = context.readInt();
ObjectMarshallingStrategy strategy = null;
// This is the old way of de/serializing strategy objects
if (strategyIndex >= 0) {
strategy = context.getResolverStrategyFactory().getStrategy(strategyIndex);
} else // This is the new way
if (strategyIndex == -2) {
String strategyClassName = context.readUTF();
if (!StringUtils.isEmpty(strategyClassName)) {
strategy = context.getResolverStrategyFactory().getStrategyObject(strategyClassName);
if (strategy == null) {
throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
}
}
}
// If either way retrieves a strategy, use it
Object object = null;
if (strategy != null) {
object = strategy.read((ObjectInputStream) context);
}
EntryPoint entryPoint = null;
if (context.readBoolean()) {
String entryPointId = context.readUTF();
if (entryPointId != null && !entryPointId.equals("")) {
entryPoint = ((RuleRuntime) context.getWorkingMemory()).getEntryPoint(entryPointId);
}
}
EntryPointId confEP;
if (entryPoint != null) {
confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
} else {
confEP = context.getWorkingMemory().getEntryPoint();
}
ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(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.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeTuple.
public static Tuple writeTuple(MarshallerWriteContext context, Activation activation, boolean isDormient) {
org.drools.core.spi.Tuple tuple = activation.getTuple();
ProtobufMessages.Tuple.Builder _tb = ProtobufMessages.Tuple.newBuilder();
boolean serializeObjects = isDormient && hasNodeMemory((BaseTuple) activation);
if (tuple != null) {
// tuple can be null if this is a rule network evaluation activation, instead of terminal node left tuple.
for (org.drools.core.spi.Tuple entry = tuple.skipEmptyHandles(); entry != null; entry = entry.getParent()) {
InternalFactHandle handle = entry.getFactHandle();
_tb.addHandleId(handle.getId());
if (serializeObjects) {
ObjectMarshallingStrategy marshallingStrategy = context.getObjectMarshallingStrategyStore().getStrategyObject(handle.getObject());
Integer strategyIndex = context.getStrategyIndex(marshallingStrategy);
ProtobufMessages.SerializedObject.Builder _so = ProtobufMessages.SerializedObject.newBuilder();
_so.setObject(serializeObject(context, marshallingStrategy, handle.getObject()));
_so.setStrategyIndex(strategyIndex);
_tb.addObject(_so.build());
}
}
}
return _tb.build();
}
use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.
the class EventAccessorRestoreTest method createMarshaller.
private Marshaller createMarshaller() {
ObjectMarshallingStrategyAcceptor acceptor = MarshallerFactory.newClassFilterAcceptor(new String[] { "*.*" });
ObjectMarshallingStrategy strategy = MarshallerFactory.newSerializeMarshallingStrategy(acceptor);
return MarshallerFactory.newMarshaller(kbase, new ObjectMarshallingStrategy[] { strategy });
}
Aggregations