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