Search in sources :

Example 11 with WorkflowProcessInstanceImpl

use of io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl in project automatiko-engine by automatiko-io.

the class AbstractProcessInstance method startFrom.

@Override
public void startFrom(String nodeId, String referenceId) {
    lock();
    ((WorkflowProcessInstanceImpl) processInstance).setStartDate(new Date());
    ((WorkflowProcessInstanceImpl) processInstance).setState(STATE_ACTIVE);
    ((InternalProcessRuntime) getProcessRuntime()).getProcessInstanceManager().addProcessInstance(this.processInstance, this.correlationKey);
    this.id = processInstance.getId();
    addCompletionEventListener();
    if (referenceId != null) {
        ((WorkflowProcessInstanceImpl) processInstance).setReferenceId(referenceId);
    }
    ((WorkflowProcessInstanceImpl) processInstance).setMetaData("AutomatikProcessInstance", this);
    triggerNode(nodeId);
    syncProcessInstance((WorkflowProcessInstance) processInstance);
    addToUnitOfWork(pi -> ((MutableProcessInstances<T>) process.instances()).update(pi.id(), pi));
    unlock(false);
    unbind(variables, processInstance.getVariables());
    if (processInstance != null) {
        this.status = processInstance.getState();
    }
}
Also used : WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) Date(java.util.Date)

Example 12 with WorkflowProcessInstanceImpl

use of io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl in project automatiko-engine by automatiko-io.

the class WorkflowFunction method callTemplate.

public void callTemplate($Type$ resource) {
    AtomicBoolean hasData = new AtomicBoolean(true);
    if (resource == null) {
        resource = new $Type$();
        hasData.set(false);
    }
    String typePrefix = "$TypePrefix$";
    final $Type$ value = resource;
    identitySupplier.buildIdentityProvider(null, Collections.emptyList());
    FunctionContext ctx = io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
        String startFromNode = "$StartFromNode$";
        ProcessInstance<$Type$> pi = value.getId() == null ? null : process.instances().findById(value.getId()).orElse(null);
        if (pi != null) {
            if (hasData.get()) {
                pi.updateVariables(value);
            }
            pi.triggerNode(startFromNode);
        } else {
            pi = process.createInstance(value.getId(), value);
            pi.startFrom(startFromNode);
        }
        String pid = (String) ((WorkflowProcessInstanceImpl) ((AbstractProcessInstance<$Type$>) pi).processInstance()).getMetaData().remove("ATK_FUNC_FLOW_ID");
        if (pid == null) {
            pid = id(pi);
        }
        ((WorkflowProcessInstanceImpl) ((AbstractProcessInstance<$Type$>) pi).processInstance()).getMetaData().remove("ATK_FUNC_FLOW_COUNTER");
        return new FunctionContext(pid, (List<String>) ((WorkflowProcessInstanceImpl) ((AbstractProcessInstance<$Type$>) pi).processInstance()).getMetaData().remove("ATK_FUNC_FLOW_NEXT"), getModel(pi));
    });
    if (ctx.nextNodes != null && eventSource != null) {
        for (String nextNode : ctx.nextNodes) {
            LOGGER.debug("Next function to trigger {}", sanitizeIdentifier(nextNode));
            eventSource.produce(sanitizeIdentifier(nextNode), typePrefix + sanitizeIdentifier("$ThisNode$".toLowerCase()) + ctx.id, ctx.model);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)

Example 13 with WorkflowProcessInstanceImpl

use of io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl in project automatiko-engine by automatiko-io.

the class Controller method reconcile.

@Override
public synchronized UpdateControl<$DataType$> reconcile($DataType$ resource, Context context) {
    if (!acceptedPayload(resource)) {
        LOGGER.debug("Event has been rejected by the filter expression");
        return UpdateControl.noUpdate();
    }
    String trigger = "$Trigger$";
    IdentityProvider.set(new TrustedIdentityProvider("System<messaging>"));
    final $Type$ model = new $Type$();
    return io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
        try {
            String correlation = resource.getMetadata().getName();
            if (correlation != null) {
                LOGGER.debug("Correlation ({}) is set, attempting to find if there is matching instance already active", correlation);
                Optional<? extends ProcessInstance> possiblyFound = (Optional<? extends ProcessInstance>) process.instances().findById(correlation);
                if (possiblyFound.isPresent()) {
                    ProcessInstance pInstance = (ProcessInstance) possiblyFound.get();
                    LOGGER.debug("Found process instance {} matching correlation {}, signaling instead of starting new instance", pInstance.id(), correlation);
                    pInstance.send(Sig.of("Message-updated", resource));
                    $DataType$ updated = ($DataType$) ((Model) pInstance.variables()).toMap().get("resource");
                    if (updated == null || Boolean.TRUE.equals(((WorkflowProcessInstanceImpl) ((AbstractProcessInstance<?>) pInstance).processInstance()).getVariable("skipResourceUpdate"))) {
                        LOGGER.debug("Signalled and returned updated {} no need to updated custom resource", updated);
                        return UpdateControl.noUpdate();
                    }
                    LOGGER.debug("Signalled and returned updated {} that requires update of the custom resource", updated);
                    return UpdateControl.updateResourceAndStatus(updated);
                }
            }
            if (canStartInstance()) {
                LOGGER.debug("Received message without reference id and no correlation is set/matched, staring new process instance with trigger '{}'", trigger);
                ProcessInstance<?> pi = process.createInstance(correlation, model);
                pi.start(trigger, null, resource);
                $DataType$ updated = ($DataType$) ((Model) pi.variables()).toMap().get("resource");
                if (updated == null || Boolean.TRUE.equals(((WorkflowProcessInstanceImpl) ((AbstractProcessInstance<?>) pi).processInstance()).getVariable("skipResourceUpdate"))) {
                    LOGGER.debug("New instance started and not need to update custom resource");
                    return UpdateControl.noUpdate();
                }
                LOGGER.debug("New instance started and with the need to update custom resource");
                return UpdateControl.updateResourceAndStatus(updated);
            } else {
                LOGGER.warn("Received message without reference id and no correlation is set/matched, for trigger not capable of starting new instance '{}'", trigger);
            }
        } catch (Throwable t) {
            LOGGER.error("Encountered problems while creating/updating instance", t);
        }
        return UpdateControl.noUpdate();
    });
}
Also used : Optional(java.util.Optional) TrustedIdentityProvider(io.automatiko.engine.api.auth.TrustedIdentityProvider) Model(io.automatiko.engine.api.Model) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ProcessInstance(io.automatiko.engine.api.workflow.ProcessInstance)

Example 14 with WorkflowProcessInstanceImpl

use of io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl in project automatiko-engine by automatiko-io.

the class ProtobufProcessMarshaller method readProcessInstances.

public List<ProcessInstance> readProcessInstances(MarshallerReaderContext context) throws IOException {
    AutomatikoMessages.ProcessData _pdata = (AutomatikoMessages.ProcessData) context.parameterObject;
    List<ProcessInstance> processInstanceList = new ArrayList<ProcessInstance>();
    for (AutomatikoMessages.ProcessInstance _instance : _pdata.getExtension(AutomatikoMessages.processInstance)) {
        context.parameterObject = _instance;
        ProcessInstance processInstance = ProcessMarshallerRegistry.INSTANCE.getMarshaller(_instance.getProcessType()).readProcessInstance(context);
        ((WorkflowProcessInstanceImpl) processInstance).reconnect();
        processInstanceList.add(processInstance);
    }
    return processInstanceList;
}
Also used : ArrayList(java.util.ArrayList) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl) ProcessInstance(io.automatiko.engine.api.runtime.process.ProcessInstance)

Example 15 with WorkflowProcessInstanceImpl

use of io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl in project automatiko-engine by automatiko-io.

the class JbpmBpmn2TestCase method assertNumOfIncommingConnections.

public void assertNumOfIncommingConnections(ProcessInstance process, String nodeName, int num) {
    assertNodeExists(process, nodeName);
    WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process;
    for (Node node : instance.getNodeContainer().getNodes()) {
        if (node.getName().equals(nodeName)) {
            if (node.getIncomingConnections().size() != num) {
                fail("Expected incomming connections: " + num + " - found " + node.getIncomingConnections().size());
            } else {
                break;
            }
        }
    }
}
Also used : Node(io.automatiko.engine.api.definition.process.Node) WorkflowProcessInstanceImpl(io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)

Aggregations

WorkflowProcessInstanceImpl (io.automatiko.engine.workflow.process.instance.impl.WorkflowProcessInstanceImpl)36 ArrayList (java.util.ArrayList)10 EventSubProcessNodeInstance (io.automatiko.engine.workflow.process.instance.node.EventSubProcessNodeInstance)8 Node (io.automatiko.engine.api.definition.process.Node)7 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)7 HumanTaskNodeInstance (io.automatiko.engine.workflow.process.instance.node.HumanTaskNodeInstance)7 Process (io.automatiko.engine.api.definition.process.Process)6 NodeInstance (io.automatiko.engine.workflow.process.instance.NodeInstance)6 CompositeContextNodeInstance (io.automatiko.engine.workflow.process.instance.node.CompositeContextNodeInstance)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 ProcessInstance (io.automatiko.engine.api.workflow.ProcessInstance)5 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)5 NodeInstanceContainer (io.automatiko.engine.workflow.process.instance.NodeInstanceContainer)5 LambdaSubProcessNodeInstance (io.automatiko.engine.workflow.process.instance.node.LambdaSubProcessNodeInstance)5 WorkItemNodeInstance (io.automatiko.engine.workflow.process.instance.node.WorkItemNodeInstance)5 Date (java.util.Date)5 ExpirationTime (io.automatiko.engine.api.jobs.ExpirationTime)4 JobsService (io.automatiko.engine.api.jobs.JobsService)4