Search in sources :

Example 16 with AbstractProcessInstance

use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.

the class EventSubProcessTest method testEventSignalSubProcessWithVersionedData.

@SuppressWarnings("unchecked")
@Test
public void testEventSignalSubProcessWithVersionedData() throws Exception {
    Application app = generateCodeProcessesOnly("event-subprocess/EventSubprocessSignalWithVersionedData.bpmn2");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.processes().processById("EventSubprocessSignal_1");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    processInstance.send(Sig.of("MySignal", new Person("john", 20)));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKeys("person");
    Person person = (Person) result.toMap().get("person");
    assertThat(person).isNotNull();
    assertThat(person.getName()).isEqualTo("john");
    assertThat(person.getAge()).isEqualTo(20);
    processInstance.send(Sig.of("MySignal", new Person("john", 21)));
    result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKeys("person");
    person = (Person) result.toMap().get("person");
    assertThat(person).isNotNull();
    assertThat(person.getName()).isEqualTo("john");
    assertThat(person.getAge()).isEqualTo(21);
    processInstance.send(Sig.of("MySignal", new Person("john", 25)));
    result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKeys("person");
    person = (Person) result.toMap().get("person");
    assertThat(person).isNotNull();
    assertThat(person.getName()).isEqualTo("john");
    assertThat(person.getAge()).isEqualTo(25);
    Map<String, List<Object>> versions = (Map<String, List<Object>>) ((AbstractProcessInstance<?>) processInstance).processInstance().getVariable(VariableScope.VERSIONED_VARIABLES);
    List<Object> personVersions = (List<Object>) versions.get("person");
    assertThat(personVersions).hasSize(2);
    processInstance.send(Sig.of("MySignal", new Person("john", 25)));
    processInstance.abort();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
}
Also used : AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) HashMap(java.util.HashMap) Model(io.automatiko.engine.api.Model) List(java.util.List) Application(io.automatiko.engine.api.Application) Person(io.automatiko.engine.codegen.data.Person) HashMap(java.util.HashMap) Map(java.util.Map) AbstractCodegenTest(io.automatiko.engine.codegen.AbstractCodegenTest) Test(org.junit.jupiter.api.Test)

Example 17 with AbstractProcessInstance

use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.

the class ProcessInstanceMarshaller method exportProcessInstance.

public ExportedProcessInstance<?> exportProcessInstance(ProcessInstance<?> processInstance) {
    io.automatiko.engine.api.runtime.process.ProcessInstance pi = ((AbstractProcessInstance<?>) processInstance).internalGetProcessInstance();
    if (pi == null) {
        return null;
    }
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        Map<String, Object> localEnv = new HashMap<String, Object>(env);
        localEnv.put("_export_", true);
        ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(baos, ((io.automatiko.engine.workflow.base.instance.ProcessInstance) pi).getProcessRuntime(), null, localEnv);
        context.setProcessInstanceId(pi.getId());
        context.setState(pi.getState());
        String processType = pi.getProcess().getType();
        context.stream.writeUTF(processType);
        io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
        Object result = marshaller.writeProcessInstance(context, pi);
        AutomatikoMessages.Header.Builder _header = AutomatikoMessages.Header.newBuilder();
        _header.setVersion(AutomatikoMessages.Version.newBuilder().setVersionMajor(1).setVersionMinor(0).setVersionRevision(0).build());
        PersisterHelper.writeStrategiesIndex(context, _header);
        String header = JsonFormat.printer().print(_header);
        context.close();
        // collect all information about timers
        Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) pi).getNodeInstances(true);
        StringBuilder timers = new StringBuilder("[");
        for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
            String timerId = null;
            if (ni instanceof TimerNodeInstance) {
                timerId = ((TimerNodeInstance) ni).getTimerId();
            } else if (ni instanceof StateBasedNodeInstance) {
                if (((StateBasedNodeInstance) ni).getTimerInstances() != null) {
                    ((StateBasedNodeInstance) ni).getTimerInstances().forEach(timer -> {
                        ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(timer);
                        timers.append("{\"timerId\":\"").append(timer).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
                    });
                }
            }
            if (timerId != null) {
                ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(timerId);
                timers.append("{\"timerId\":\"").append(timerId).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
            }
            if (((NodeInstanceImpl) ni).getRetryJobId() != null && !((NodeInstanceImpl) ni).getRetryJobId().isEmpty()) {
                ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(((NodeInstanceImpl) ni).getRetryJobId());
                timers.append("{\"timerId\":\"").append(((NodeInstanceImpl) ni).getRetryJobId()).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
            }
        }
        timers.append("]");
        return StringExportedProcessInstance.of(header, JsonFormat.printer().print((MessageOrBuilder) result), timers.toString(), null);
    } catch (Exception e) {
        throw new RuntimeException("Error while marshalling process instance", e);
    }
}
Also used : StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProtobufRuleFlowProcessInstanceMarshaller(io.automatiko.engine.workflow.marshalling.impl.ProtobufRuleFlowProcessInstanceMarshaller) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ZonedDateTime(java.time.ZonedDateTime) ObjectInputStream(java.io.ObjectInputStream) ExpirationTime(io.automatiko.engine.api.jobs.ExpirationTime) NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) HashMap(java.util.HashMap) MessageOrBuilder(com.google.protobuf.MessageOrBuilder) SerializablePlaceholderResolverStrategy(io.automatiko.engine.workflow.marshalling.impl.strategies.SerializablePlaceholderResolverStrategy) LinkedHashMap(java.util.LinkedHashMap) JobsService(io.automatiko.engine.api.jobs.JobsService) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Process(io.automatiko.engine.api.workflow.Process) PersisterHelper(io.automatiko.engine.workflow.marshalling.impl.PersisterHelper) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) StreamCorruptedException(java.io.StreamCorruptedException) Iterator(java.util.Iterator) Model(io.automatiko.engine.api.Model) AutomatikoMessages(io.automatiko.engine.workflow.marshalling.impl.AutomatikoMessages) Collection(java.util.Collection) ClassObjectMarshallingStrategyAcceptor(io.automatiko.engine.workflow.marshalling.impl.ClassObjectMarshallingStrategyAcceptor) IOException(java.io.IOException) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) MarshallerReaderContext(io.automatiko.engine.workflow.marshalling.impl.MarshallerReaderContext) ProcessMarshallerRegistry(io.automatiko.engine.workflow.marshalling.impl.ProcessMarshallerRegistry) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance) StringExportedProcessInstance(io.automatiko.engine.workflow.StringExportedProcessInstance) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) ObjectMarshallingStrategy(io.automatiko.engine.api.marshalling.ObjectMarshallingStrategy) ExportedProcessInstance(io.automatiko.engine.api.workflow.ExportedProcessInstance) EnvironmentName(io.automatiko.engine.api.runtime.EnvironmentName) ProcessInstanceJobDescription(io.automatiko.engine.api.jobs.ProcessInstanceJobDescription) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) MessageOrBuilder(com.google.protobuf.MessageOrBuilder) ZonedDateTime(java.time.ZonedDateTime) NodeInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) StateBasedNodeInstance(io.automatiko.engine.workflow.process.instance.node.StateBasedNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance) TimerNodeInstance(io.automatiko.engine.workflow.process.instance.node.TimerNodeInstance)

Example 18 with AbstractProcessInstance

use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.

the class LambdaSubProcessNodeInstance method internalTrigger.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("A SubProcess node only accepts default incoming connections!");
    }
    ProcessContext context = new ProcessContext(getProcessInstance().getProcessRuntime());
    context.setNodeInstance(this);
    context.setProcessInstance(getProcessInstance());
    SubProcessFactory subProcessFactory = getSubProcessNode().getSubProcessFactory();
    Object o = subProcessFactory.bind(context);
    io.automatiko.engine.api.workflow.ProcessInstance<?> processInstance = subProcessFactory.createInstance(o);
    io.automatiko.engine.api.runtime.process.ProcessInstance pi = ((AbstractProcessInstance<?>) processInstance).internalGetProcessInstance();
    String parentInstanceId = getProcessInstance().getId();
    if (getProcessInstance().getParentProcessInstanceId() != null && !getProcessInstance().getParentProcessInstanceId().isEmpty()) {
        parentInstanceId = getProcessInstance().getParentProcessInstanceId() + ":" + parentInstanceId;
    }
    ((ProcessInstanceImpl) pi).setMetaData("ParentProcessInstanceId", parentInstanceId);
    ((ProcessInstanceImpl) pi).setMetaData("ParentNodeInstanceId", getUniqueId());
    ((ProcessInstanceImpl) pi).setMetaData("ParentNodeId", getSubProcessNode().getUniqueId());
    ((ProcessInstanceImpl) pi).setParentProcessInstanceId(parentInstanceId);
    ((ProcessInstanceImpl) pi).setRootProcessInstanceId(StringUtils.isEmpty(getProcessInstance().getRootProcessInstanceId()) ? getProcessInstance().getId() : getProcessInstance().getRootProcessInstanceId());
    ((ProcessInstanceImpl) pi).setRootProcessId(StringUtils.isEmpty(getProcessInstance().getRootProcessId()) ? getProcessInstance().getProcessId() : getProcessInstance().getRootProcessId());
    ((ProcessInstanceImpl) pi).setSignalCompletion(getSubProcessNode().isWaitForCompletion());
    ((ProcessInstanceImpl) pi).setReferenceFromRoot(getProcessInstance().getReferenceFromRoot());
    processInstance.start();
    this.processInstanceId = processInstance.id();
    this.processInstanceName = processInstance.description();
    subProcessFactory.unbind(context, processInstance.variables());
    if (!getSubProcessNode().isWaitForCompletion()) {
        triggerCompleted();
    } else if (processInstance.status() == ProcessInstance.STATE_COMPLETED || processInstance.status() == ProcessInstance.STATE_ABORTED) {
        triggerCompleted();
    } else {
        String subprocessInstanceId = parentInstanceId + ":" + processInstance.id();
        ((ProcessInstanceImpl) getProcessInstance()).addChild(processInstance.process().id(), subprocessInstanceId);
        addProcessListener();
    }
}
Also used : AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) SubProcessFactory(io.automatiko.engine.workflow.process.core.node.SubProcessFactory) ProcessInstanceImpl(io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl) ProcessContext(io.automatiko.engine.workflow.base.core.context.ProcessContext)

Aggregations

AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)18 Model (io.automatiko.engine.api.Model)7 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)6 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)4 ProcessInstanceDuplicatedException (io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException)4 WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)4 Application (io.automatiko.engine.api.Application)3 Process (io.automatiko.engine.api.workflow.Process)3 UncheckedIOException (java.io.UncheckedIOException)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Document (org.bson.Document)3 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)3 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 IdentityProvider (io.automatiko.engine.api.auth.IdentityProvider)2 SecurityPolicy (io.automatiko.engine.api.auth.SecurityPolicy)2