Search in sources :

Example 1 with VariableInitializer

use of io.automatiko.engine.api.workflow.VariableInitializer in project automatiko-engine by automatiko-io.

the class AbstractProtobufProcessInstanceMarshaller method readVariableScope.

protected void readVariableScope(MarshallerReaderContext context, Process process, WorkflowProcessInstanceImpl processInstance, AutomatikoMessages.ProcessInstance _instance) throws IOException {
    if (_instance.getVariableCount() > 0) {
        VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.base.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(variableScope);
        for (AutomatikoMessages.Variable _variable : _instance.getVariableList()) {
            try {
                Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
                if ((boolean) context.env.getOrDefault("_import_", false)) {
                    VariableInitializer initializer = ((ProcessRuntimeServiceProvider) context.env.get("_services_")).getVariableInitializer();
                    for (VariableAugmentor augmentor : initializer.augmentors()) {
                        Variable var = variableScope.findVariable(_variable.getName());
                        if (augmentor.accept(var, _value)) {
                            _value = augmentor.augmentOnCreate(process.getId(), process.getVersion(), _instance.getId(), var, _value);
                        }
                    }
                }
                variableScopeInstance.internalSetVariable(_variable.getName(), _value);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
            }
        }
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) VariableAugmentor(io.automatiko.engine.api.workflow.VariableAugmentor) ProcessRuntimeServiceProvider(io.automatiko.engine.workflow.base.instance.ProcessRuntimeServiceProvider) VariableInitializer(io.automatiko.engine.api.workflow.VariableInitializer) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 2 with VariableInitializer

use of io.automatiko.engine.api.workflow.VariableInitializer 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)

Aggregations

VariableInitializer (io.automatiko.engine.api.workflow.VariableInitializer)2 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)2 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)2 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)2 Node (io.automatiko.engine.api.definition.process.Node)1 Process (io.automatiko.engine.api.definition.process.Process)1 ProcessEventListener (io.automatiko.engine.api.event.process.ProcessEventListener)1 DurationExpirationTime (io.automatiko.engine.api.jobs.DurationExpirationTime)1 ExactExpirationTime (io.automatiko.engine.api.jobs.ExactExpirationTime)1 ExpirationTime (io.automatiko.engine.api.jobs.ExpirationTime)1 JobsService (io.automatiko.engine.api.jobs.JobsService)1 ProcessJobDescription (io.automatiko.engine.api.jobs.ProcessJobDescription)1 EventListener (io.automatiko.engine.api.runtime.process.EventListener)1 ProcessInstance (io.automatiko.engine.api.runtime.process.ProcessInstance)1 WorkItemManager (io.automatiko.engine.api.runtime.process.WorkItemManager)1 UnitOfWorkManager (io.automatiko.engine.api.uow.UnitOfWorkManager)1 VariableAugmentor (io.automatiko.engine.api.workflow.VariableAugmentor)1 SignalManager (io.automatiko.engine.api.workflow.signal.SignalManager)1 WorkItemExecutionError (io.automatiko.engine.api.workflow.workitem.WorkItemExecutionError)1 CorrelationKey (io.automatiko.engine.services.correlation.CorrelationKey)1