Search in sources :

Example 31 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class VariableUtil method resolveVariable.

public static String resolveVariable(String s, NodeInstance nodeInstance) {
    if (s == null) {
        return null;
    }
    Map<String, String> replacements = new HashMap<String, String>();
    Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
    while (matcher.find()) {
        String paramName = matcher.group(1);
        if (replacements.get(paramName) == null) {
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
            if (variableScopeInstance != null) {
                Object variableValue = variableScopeInstance.getVariable(paramName);
                String variableValueString = variableValue == null ? "" : variableValue.toString();
                replacements.put(paramName, variableValueString);
            }
        }
    }
    for (Map.Entry<String, String> replacement : replacements.entrySet()) {
        s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
    }
    return s;
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) HashMap(java.util.HashMap)

Example 32 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class JavaScriptAction method execute.

public void execute(ProcessContext context) throws Exception {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    engine.put("kcontext", context);
    // insert globals into context
    Globals globals = context.getKieRuntime().getGlobals();
    if (globals != null && globals.getGlobalKeys() != null) {
        for (String gKey : globals.getGlobalKeys()) {
            engine.put(gKey, globals.get(gKey));
        }
    }
    if (context.getProcessInstance() != null && context.getProcessInstance().getProcess() != null) {
        // insert process variables
        VariableScopeInstance variableScope = (VariableScopeInstance) ((WorkflowProcessInstance) context.getProcessInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
        Map<String, Object> variables = variableScope.getVariables();
        if (variables != null) {
            for (Entry<String, Object> variable : variables.entrySet()) {
                engine.put(variable.getKey(), variable.getValue());
            }
        }
    }
    engine.eval(expr);
}
Also used : Globals(org.kie.api.runtime.Globals) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 33 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class MarshalVariablesProcessEventListener method afterProcessCompleted.

public void afterProcessCompleted(ProcessCompletedEvent event) {
    ObjectMarshallingStrategy[] strategies = (ObjectMarshallingStrategy[]) event.getKieRuntime().getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES);
    VariableScopeInstance variableScope = (VariableScopeInstance) ((WorkflowProcessInstance) event.getProcessInstance()).getContextInstance(VariableScope.VARIABLE_SCOPE);
    Map<String, Object> variables = variableScope.getVariables();
    for (Map.Entry<String, Object> variable : variables.entrySet()) {
        logger.debug("Searching for applicable strategy to handle variable name '{}' value '{}'", variable.getKey(), variable.getValue());
        for (ObjectMarshallingStrategy strategy : strategies) {
            // are removed together with process instance
            if (strategy instanceof SerializablePlaceholderResolverStrategy) {
                continue;
            }
            if (strategy.accept(variable.getValue())) {
                logger.debug("Strategy of type {} found to handle variable '{}'", strategy, variable.getKey());
                try {
                    ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(new ByteArrayOutputStream(), null, null, null, null, event.getKieRuntime().getEnvironment());
                    context.setProcessInstanceId(event.getProcessInstance().getId());
                    context.setState(ProcessMarshallerWriteContext.STATE_COMPLETED);
                    strategy.marshal(null, context, variable.getValue());
                    logger.debug("Variable '{}' successfully persisted by strategy {}", variable.getKey(), strategy);
                    break;
                } catch (Exception e) {
                    logger.warn("Errer while storing process variable {} due to {}", variable.getKey(), e.getMessage());
                    logger.debug("Variable marshal error:", e);
                }
            }
        }
    }
}
Also used : SerializablePlaceholderResolverStrategy(org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) ProcessMarshallerWriteContext(org.drools.core.marshalling.impl.ProcessMarshallerWriteContext)

Example 34 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class AbstractProcessInstanceMarshaller method readNodeInstance.

public NodeInstance readNodeInstance(MarshallerReaderContext context, NodeInstanceContainer nodeInstanceContainer, WorkflowProcessInstance processInstance) throws IOException {
    ObjectInputStream stream = context.stream;
    long id = stream.readLong();
    long nodeId = stream.readLong();
    int nodeType = stream.readShort();
    NodeInstanceImpl nodeInstance = readNodeInstanceContent(nodeType, stream, context, processInstance);
    nodeInstance.setNodeId(nodeId);
    nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
    nodeInstance.setProcessInstance((org.jbpm.workflow.instance.WorkflowProcessInstance) processInstance);
    nodeInstance.setId(id);
    switch(nodeType) {
        case PersisterEnums.COMPOSITE_NODE_INSTANCE:
        case PersisterEnums.DYNAMIC_NODE_INSTANCE:
            int nbVariables = stream.readInt();
            if (nbVariables > 0) {
                Context variableScope = ((org.jbpm.process.core.Process) ((org.jbpm.process.instance.ProcessInstance) processInstance).getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) nodeInstance).getContextInstance(variableScope);
                for (int i = 0; i < nbVariables; i++) {
                    String name = stream.readUTF();
                    try {
                        Object value = stream.readObject();
                        variableScopeInstance.internalSetVariable(name, value);
                    } catch (ClassNotFoundException e) {
                        throw new IllegalArgumentException("Could not reload variable " + name);
                    }
                }
            }
            while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
                readNodeInstance(context, (CompositeContextNodeInstance) nodeInstance, processInstance);
            }
            int exclusiveGroupInstances = stream.readInt();
            for (int i = 0; i < exclusiveGroupInstances; i++) {
                ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
                ((org.jbpm.process.instance.ProcessInstance) processInstance).addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
                int nodeInstances = stream.readInt();
                for (int j = 0; j < nodeInstances; j++) {
                    long nodeInstanceId = stream.readLong();
                    NodeInstance groupNodeInstance = processInstance.getNodeInstance(nodeInstanceId);
                    if (groupNodeInstance == null) {
                        throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
                    }
                    exclusiveGroupInstance.addNodeInstance(groupNodeInstance);
                }
            }
            break;
        case PersisterEnums.FOR_EACH_NODE_INSTANCE:
            while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
                readNodeInstance(context, (ForEachNodeInstance) nodeInstance, processInstance);
            }
            break;
        default:
    }
    return nodeInstance;
}
Also used : NodeInstanceImpl(org.jbpm.workflow.instance.impl.NodeInstanceImpl) SwimlaneContext(org.jbpm.process.core.context.swimlane.SwimlaneContext) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) Context(org.jbpm.process.core.Context) ExclusiveGroupInstance(org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance) Process(org.kie.api.definition.process.Process) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) ObjectInputStream(java.io.ObjectInputStream)

Example 35 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class AbstractProcessInstanceMarshaller method readProcessInstance.

// Input methods
public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
    ObjectInputStream stream = context.stream;
    InternalKnowledgeBase kBase = context.kBase;
    InternalWorkingMemory wm = context.wm;
    WorkflowProcessInstanceImpl processInstance = createProcessInstance();
    processInstance.setId(stream.readLong());
    String processId = stream.readUTF();
    processInstance.setProcessId(processId);
    Process process = kBase.getProcess(processId);
    if (kBase != null) {
        processInstance.setProcess(process);
    }
    processInstance.setState(stream.readInt());
    long nodeInstanceCounter = stream.readLong();
    processInstance.setKnowledgeRuntime(wm.getKnowledgeRuntime());
    int nbSwimlanes = stream.readInt();
    if (nbSwimlanes > 0) {
        Context swimlaneContext = ((org.jbpm.process.core.Process) process).getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
        SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
        for (int i = 0; i < nbSwimlanes; i++) {
            String name = stream.readUTF();
            String value = stream.readUTF();
            swimlaneContextInstance.setActorId(name, value);
        }
    }
    while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
        readNodeInstance(context, processInstance, processInstance);
    }
    int exclusiveGroupInstances = stream.readInt();
    for (int i = 0; i < exclusiveGroupInstances; i++) {
        ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
        processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
        int nodeInstances = stream.readInt();
        for (int j = 0; j < nodeInstances; j++) {
            long nodeInstanceId = stream.readLong();
            NodeInstance nodeInstance = processInstance.getNodeInstance(nodeInstanceId);
            if (nodeInstance == null) {
                throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
            }
            exclusiveGroupInstance.addNodeInstance(nodeInstance);
        }
    }
    // Process Variables
    // - Number of Variables = keys.size()
    // For Each Variable
    // - Variable Key
    // - Marshalling Strategy Index
    // - Marshalled Object
    int nbVariables = stream.readInt();
    if (nbVariables > 0) {
        Context variableScope = ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(variableScope);
        for (int i = 0; i < nbVariables; i++) {
            String name = stream.readUTF();
            try {
                ObjectMarshallingStrategy strategy = null;
                int index = stream.readInt();
                // This is the old way of de/serializing strategy objects
                if (index >= 0) {
                    strategy = context.resolverStrategyFactory.getStrategy(index);
                } else // This is the new way
                if (index == -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
                Object value = null;
                if (strategy != null) {
                    value = strategy.read(stream);
                }
                variableScopeInstance.internalSetVariable(name, value);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Could not reload variable " + name);
            }
        }
    }
    processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
    if (wm != null) {
        processInstance.reconnect();
    }
    return processInstance;
}
Also used : SwimlaneContext(org.jbpm.process.core.context.swimlane.SwimlaneContext) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) Context(org.jbpm.process.core.Context) ExclusiveGroupInstance(org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance) SwimlaneContextInstance(org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Process(org.kie.api.definition.process.Process) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)38 HashMap (java.util.HashMap)17 Map (java.util.Map)12 NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)9 ExclusiveGroupInstance (org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance)8 NodeInstance (org.kie.api.runtime.process.NodeInstance)8 ArrayList (java.util.ArrayList)6 Matcher (java.util.regex.Matcher)6 SwimlaneContextInstance (org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance)6 DataAssociation (org.jbpm.workflow.core.node.DataAssociation)6 Transformation (org.jbpm.workflow.core.node.Transformation)6 DataTransformer (org.kie.api.runtime.process.DataTransformer)6 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)6 ContextInstance (org.jbpm.process.instance.ContextInstance)5 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)5 Process (org.kie.api.definition.process.Process)5 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)4 MarshallerWriteContext (org.drools.core.marshalling.impl.MarshallerWriteContext)4 Context (org.jbpm.process.core.Context)4 SwimlaneContext (org.jbpm.process.core.context.swimlane.SwimlaneContext)4