use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method unmarshallInstance.
@SuppressWarnings("unchecked")
protected ProcessInstance<ProcessInstanceEntity> unmarshallInstance(ProcessInstanceReadMode mode, ProcessInstanceEntity entity) {
ProcessInstance<ProcessInstanceEntity> pi;
if (mode == MUTABLE) {
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.content), process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
pi = ((AbstractProcess<ProcessInstanceEntity>) process).createInstance(wpi, entity, entity.version);
} else {
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.content), process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
pi = ((AbstractProcess<ProcessInstanceEntity>) process).createReadOnlyInstance(wpi, entity);
}
return pi;
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class LightProcessRuntime method createProcessInstance.
private io.automatiko.engine.workflow.base.instance.ProcessInstance createProcessInstance(Process process, CorrelationKey correlationKey, Map<String, Object> parameters) {
io.automatiko.engine.workflow.base.instance.ProcessInstance pi = runtimeContext.createProcessInstance(process, correlationKey);
pi.setProcessRuntime(this);
runtimeContext.setupParameters(pi, parameters, variableInitializer);
Map<String, Object> temp = new HashMap<>();
if (parameters != null) {
temp.putAll(parameters);
}
temp.putAll(pi.getVariables());
String uuid;
if (correlationKey != null) {
uuid = UUID.nameUUIDFromBytes(correlationKey.toExternalForm().getBytes(StandardCharsets.UTF_8)).toString();
((WorkflowProcessInstanceImpl) pi).setCorrelationKey(correlationKey.toExternalForm());
} else {
VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
Optional<Object> businessKeyVar = variableScope.getVariables().stream().filter(var -> var.hasTag(Variable.BUSINESS_KEY_TAG)).map(v -> temp.get(v.getName())).filter(var -> var != null).findAny();
if (businessKeyVar.isPresent()) {
Object businessKey = businessKeyVar.get();
uuid = UUID.nameUUIDFromBytes(businessKey.toString().getBytes(StandardCharsets.UTF_8)).toString();
((WorkflowProcessInstanceImpl) pi).setCorrelationKey(businessKey.toString());
} else {
uuid = UUID.randomUUID().toString();
}
}
pi.setId(uuid);
VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) pi.getContextInstance(VariableScope.VARIABLE_SCOPE);
// set input parameters
if (parameters != null) {
if (variableScope != null) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
variableScope.validateVariable(process.getName(), entry.getKey(), entry.getValue());
variableScopeInstance.setVariable(entry.getKey(), entry.getValue());
}
} else {
throw new IllegalArgumentException("This process does not support parameters!");
}
}
variableScopeInstance.enforceRequiredVariables();
return pi;
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class JavaScriptReturnValueEvaluator method evaluate.
public Object evaluate(ProcessContext context) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
// insert process kcontext
engine.put("kcontext", context);
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());
}
}
}
Object value = engine.eval(expr);
if (!(value instanceof Boolean)) {
throw new RuntimeException("Constraints must return boolean values: " + expr + " returns " + value + (value == null ? "" : " (type=" + value.getClass()));
}
return ((Boolean) value).booleanValue();
}
use of io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance in project automatiko-engine by automatiko-io.
the class WorkflowRuntimeException method initialize.
private void initialize(NodeInstance nodeInstance, ProcessInstance processInstance) {
this.processInstanceId = processInstance.getId();
this.processId = processInstance.getProcessId();
if (nodeInstance != null) {
this.nodeInstanceId = nodeInstance.getId();
this.nodeId = nodeInstance.getNodeId();
if (((ProcessInstanceImpl) processInstance).getProcessRuntime() != null) {
this.nodeName = nodeInstance.getNodeName();
}
}
VariableScopeInstance variableScope = (VariableScopeInstance) ((io.automatiko.engine.workflow.base.instance.ProcessInstance) processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
// set input parameters
if (variableScope != null) {
this.variables = variableScope.getVariables();
} else {
this.variables = new HashMap<String, Object>(0);
}
}
Aggregations