Search in sources :

Example 6 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.

the class InputMarshaller method readWorkItem.

public static WorkItem readWorkItem(MarshallerReaderContext context) throws IOException {
    ObjectInputStream stream = context.stream;
    WorkItemImpl workItem = new WorkItemImpl();
    workItem.setId(stream.readLong());
    workItem.setProcessInstanceId(stream.readLong());
    workItem.setName(stream.readUTF());
    workItem.setState(stream.readInt());
    // WorkItem Paramaters
    int nbVariables = stream.readInt();
    if (nbVariables > 0) {
        for (int i = 0; i < nbVariables; i++) {
            String name = stream.readUTF();
            try {
                int index = stream.readInt();
                ObjectMarshallingStrategy strategy = null;
                // Old way of retrieving strategy objects
                if (index >= 0) {
                    strategy = context.resolverStrategyFactory.getStrategy(index);
                    if (strategy == null) {
                        throw new IllegalStateException("No strategy of with index " + index + " available.");
                    }
                } else // New way
                if (index == -2) {
                    String strategyClassName = stream.readUTF();
                    // fix for backwards compatibility (5.x -> 6.x)
                    if ("org.drools.marshalling.impl.SerializablePlaceholderResolverStrategy".equals(strategyClassName)) {
                        strategyClassName = "org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy";
                    }
                    strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
                    if (strategy == null) {
                        throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
                    }
                }
                Object value = strategy.read(stream);
                workItem.setParameter(name, value);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Could not reload variable " + name);
            }
        }
    }
    return workItem;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.

the class ProtobufInputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context, EntryPoint entryPoint, FactHandle _handle) throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    if (_handle.hasStrategyIndex()) {
        strategy = context.usedStrategies.get(_handle.getStrategyIndex());
        object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _handle.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
    }
    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(_handle.getType()) {
        case FACT:
            {
                handle = new DefaultFactHandle(_handle.getId(), object, _handle.getRecency(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case QUERY:
            {
                handle = new QueryElementFactHandle(object, _handle.getId(), _handle.getRecency());
                break;
            }
        case EVENT:
            {
                handle = new EventFactHandle(_handle.getId(), object, _handle.getRecency(), _handle.getTimestamp(), _handle.getDuration(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(_handle.getIsExpired());
                ((EventFactHandle) handle).setOtnCount(_handle.getOtnCount());
                // ((EventFactHandle) handle).setActivationsCount( _handle.getActivationsCount() );
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + _handle.getType());
            }
    }
    return handle;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 8 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, EqualityKey key, ProtobufMessages.EqualityKey _key) throws IOException, ClassNotFoundException {
    if (_key.hasBeliefSet()) {
        ProtobufMessages.BeliefSet _beliefSet = _key.getBeliefSet();
        InternalFactHandle handle = (InternalFactHandle) context.handles.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();
                Activation activation = (Activation) context.filter.getTuplesCache().get(PersisterHelper.createActivationKey(_activation.getPackageName(), _activation.getRuleName(), _activation.getTuple())).getContextObject();
                Object object = null;
                ObjectMarshallingStrategy strategy = null;
                if (_logicalDependency.hasObjectStrategyIndex()) {
                    strategy = context.usedStrategies.get(_logicalDependency.getObjectStrategyIndex());
                    object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _logicalDependency.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
                }
                Object value = null;
                if (_logicalDependency.hasValueStrategyIndex()) {
                    strategy = context.usedStrategies.get(_logicalDependency.getValueStrategyIndex());
                    value = strategy.unmarshal(context.strategyContexts.get(strategy), context, _logicalDependency.getValue().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
                }
                ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
                tms.readLogicalDependency(handle, object, value, activation, activation.getPropagationContext(), activation.getRule(), typeConf);
            }
        } else {
            handle.getEqualityKey().setBeliefSet(tms.getBeliefSystem().newBeliefSet(handle));
        }
    }
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Activation(org.drools.core.spi.Activation) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 9 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.

the class OldOutputMarshallerMethods method writeWorkItem_v1.

// Changed with JBRULES-3257
public static void writeWorkItem_v1(MarshallerWriteContext context, WorkItem workItem) throws IOException {
    ObjectOutputStream stream = context.stream;
    stream.writeLong(workItem.getId());
    stream.writeLong(workItem.getProcessInstanceId());
    stream.writeUTF(workItem.getName());
    stream.writeInt(workItem.getState());
    // Work Item Parameters
    Map<String, Object> parameters = workItem.getParameters();
    Collection<Object> notNullValues = new ArrayList<Object>();
    for (Object value : parameters.values()) {
        if (value != null) {
            notNullValues.add(value);
        }
    }
    stream.writeInt(notNullValues.size());
    for (String key : parameters.keySet()) {
        Object object = parameters.get(key);
        if (object != null) {
            stream.writeUTF(key);
            int index = context.objectMarshallingStrategyStore.getStrategy(object);
            stream.writeInt(index);
            ObjectMarshallingStrategy strategy = context.objectMarshallingStrategyStore.getStrategy(index);
            if (strategy.accept(object)) {
                strategy.write(stream, object);
            }
        }
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ArrayList(java.util.ArrayList) ObjectOutputStream(java.io.ObjectOutputStream)

Example 10 with ObjectMarshallingStrategy

use of org.kie.api.marshalling.ObjectMarshallingStrategy in project drools by kiegroup.

the class EventAccessorRestoreTest method createMarshaller.

private Marshaller createMarshaller(KieBase kbase) {
    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)35 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 IOException (java.io.IOException)6 SerializablePlaceholderResolverStrategy (org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 File (java.io.File)4 Map (java.util.Map)4 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 EventFactHandle (org.drools.core.common.EventFactHandle)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 Marshaller (org.kie.api.marshalling.Marshaller)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)3 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)3 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)3 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 KieMarshallers (org.kie.api.marshalling.KieMarshallers)3