Search in sources :

Example 41 with ObjectMarshallingStrategy

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));
        }
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Activation(org.drools.core.spi.Activation) ActivationKey(org.drools.serialization.protobuf.marshalling.ActivationKey) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) InternalFactHandle(org.drools.core.common.InternalFactHandle) TruthMaintenanceSystemImpl(org.drools.tms.TruthMaintenanceSystemImpl) ObjectInputStream(java.io.ObjectInputStream)

Example 42 with ObjectMarshallingStrategy

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();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.drools.serialization.protobuf.ProtobufMessages.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 43 with ObjectMarshallingStrategy

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;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.kiesession.entrypoints.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) ObjectInputStream(java.io.ObjectInputStream)

Example 44 with ObjectMarshallingStrategy

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();
}
Also used : BaseTuple(org.drools.core.reteoo.BaseTuple) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) InternalFactHandle(org.drools.core.common.InternalFactHandle) Tuple(org.drools.serialization.protobuf.ProtobufMessages.Tuple) BaseTuple(org.drools.core.reteoo.BaseTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple)

Example 45 with ObjectMarshallingStrategy

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 });
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ObjectMarshallingStrategyAcceptor(org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor)

Aggregations

ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)52 ArrayList (java.util.ArrayList)11 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 EventFactHandle (org.drools.core.common.EventFactHandle)8 IOException (java.io.IOException)7 ObjectInputStream (java.io.ObjectInputStream)7 HashMap (java.util.HashMap)7 ObjectMarshallingStrategyAcceptor (org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)6 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)6 Marshaller (org.kie.api.marshalling.Marshaller)6 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)5 Test (org.junit.Test)5 Environment (org.kie.api.runtime.Environment)5 KieSession (org.kie.api.runtime.KieSession)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 Collection (java.util.Collection)4