Search in sources :

Example 41 with VariableScopeInstance

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;
}
Also used : ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 42 with VariableScopeInstance

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;
}
Also used : SignalManager(io.automatiko.engine.api.workflow.signal.SignalManager) Trigger(io.automatiko.engine.workflow.process.core.node.Trigger) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) TimeUtils(io.automatiko.engine.workflow.base.core.timer.TimeUtils) ExactExpirationTime(io.automatiko.engine.api.jobs.ExactExpirationTime) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) CorrelationKey(io.automatiko.engine.services.correlation.CorrelationKey) HashMap(java.util.HashMap) DefaultProcessInstanceManager(io.automatiko.engine.workflow.base.instance.impl.DefaultProcessInstanceManager) Node(io.automatiko.engine.api.definition.process.Node) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) DurationExpirationTime(io.automatiko.engine.api.jobs.DurationExpirationTime) ProcessJobDescription(io.automatiko.engine.api.jobs.ProcessJobDescription) JobsService(io.automatiko.engine.api.jobs.JobsService) ContextContainer(io.automatiko.engine.workflow.base.core.ContextContainer) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance) ProcessEventSupport(io.automatiko.engine.workflow.base.core.event.ProcessEventSupport) Map(java.util.Map) EventListener(io.automatiko.engine.api.runtime.process.EventListener) Process(io.automatiko.engine.api.definition.process.Process) EventFilter(io.automatiko.engine.workflow.base.core.event.EventFilter) ProcessEventListener(io.automatiko.engine.api.event.process.ProcessEventListener) UnitOfWorkManager(io.automatiko.engine.api.uow.UnitOfWorkManager) Collection(java.util.Collection) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) EventTransformer(io.automatiko.engine.workflow.base.core.event.EventTransformer) DateTimeUtils(io.automatiko.engine.workflow.base.core.timer.DateTimeUtils) UUID(java.util.UUID) InMemoryJobService(io.automatiko.engine.services.jobs.impl.InMemoryJobService) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) StandardCharsets(java.nio.charset.StandardCharsets) WorkItemExecutionError(io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError) VariableInitializer(io.automatiko.engine.api.workflow.VariableInitializer) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) CronExpirationTime(io.automatiko.engine.workflow.base.core.timer.CronExpirationTime) Optional(java.util.Optional) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) WorkItemManager(io.automatiko.engine.api.runtime.process.WorkItemManager) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EventTrigger(io.automatiko.engine.workflow.process.core.node.EventTrigger) Collections(java.util.Collections) HashMap(java.util.HashMap) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 43 with VariableScopeInstance

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();
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 44 with VariableScopeInstance

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);
    }
}
Also used : VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)

Aggregations

VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)44 HashMap (java.util.HashMap)26 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)20 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)18 Map (java.util.Map)16 Matcher (java.util.regex.Matcher)12 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)11 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)11 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)10 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)9 Transformation (io.automatiko.engine.workflow.process.core.node.Transformation)9 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)7 ArrayList (java.util.ArrayList)7 NodeInstance (io.automatiko.engine.api.runtime.process.NodeInstance)6 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)6 Serializable (java.io.Serializable)6 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)5 Process (io.automatiko.engine.api.definition.process.Process)4 DataType (io.automatiko.engine.api.workflow.datatype.DataType)4 Context (io.automatiko.engine.workflow.base.core.Context)4