Search in sources :

Example 1 with Constants

use of io.serverlessworkflow.api.workflow.Constants in project kogito-runtimes by kiegroup.

the class ServerlessWorkflowParser method parseProcess.

private GeneratedInfo<KogitoWorkflowProcess> parseProcess() {
    String workflowStartStateName = workflow.getStart().getStateName();
    if (workflowStartStateName == null || workflowStartStateName.trim().isEmpty()) {
        throw new IllegalArgumentException("workflow does not define a starting state");
    }
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess(workflow.getId()).name(workflow.getName() == null ? DEFAULT_NAME : workflow.getName()).version(workflow.getVersion() == null ? DEFAULT_VERSION : workflow.getVersion()).packageName(workflow.getMetadata() != null ? workflow.getMetadata().getOrDefault("package", DEFAULT_PACKAGE) : DEFAULT_PACKAGE).visibility("Public").variable(DEFAULT_WORKFLOW_VAR, new ObjectDataType(JsonNode.class), ObjectMapperFactory.get().createObjectNode());
    ParserContext parserContext = new ParserContext(idGenerator, factory, context);
    Constants constants = workflow.getConstants();
    if (constants != null) {
        factory.metaData(Metadata.CONSTANTS, constants.getConstantsDef());
    }
    Collection<StateHandler<?>> handlers = workflow.getStates().stream().map(state -> StateHandlerFactory.getStateHandler(state, workflow, parserContext)).filter(Optional::isPresent).map(Optional::get).filter(state -> !state.usedForCompensation()).collect(Collectors.toList());
    handlers.forEach(StateHandler::handleStart);
    handlers.forEach(StateHandler::handleEnd);
    handlers.forEach(StateHandler::handleState);
    handlers.forEach(StateHandler::handleTransitions);
    handlers.forEach(StateHandler::handleErrors);
    handlers.forEach(StateHandler::handleConnections);
    if (parserContext.isCompensation()) {
        factory.metaData(Metadata.COMPENSATION, true);
        factory.addCompensationContext(workflow.getId());
    }
    return new GeneratedInfo<>(factory.validate().getProcess(), parserContext.generatedFiles());
}
Also used : ObjectMapperFactory(org.kie.kogito.jackson.utils.ObjectMapperFactory) DataDefinition(org.jbpm.workflow.core.impl.DataDefinition) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) Constants(io.serverlessworkflow.api.workflow.Constants) Split(org.jbpm.workflow.core.node.Split) NodeFactory(org.jbpm.ruleflow.core.factory.NodeFactory) Workflow(io.serverlessworkflow.api.Workflow) JoinFactory(org.jbpm.ruleflow.core.factory.JoinFactory) StateHandlerFactory(org.kie.kogito.serverless.workflow.parser.handlers.StateHandlerFactory) StateHandler(org.kie.kogito.serverless.workflow.parser.handlers.StateHandler) DataAssociation(org.jbpm.workflow.core.impl.DataAssociation) Map(java.util.Map) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) SubProcessNodeFactory(org.jbpm.ruleflow.core.factory.SubProcessNodeFactory) UNIQUE_ID(org.jbpm.ruleflow.core.Metadata.UNIQUE_ID) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Join(org.jbpm.workflow.core.node.Join) Metadata(org.jbpm.ruleflow.core.Metadata) TimerNodeFactory(org.jbpm.ruleflow.core.factory.TimerNodeFactory) Collection(java.util.Collection) RuleFlowNodeContainerFactory(org.jbpm.ruleflow.core.RuleFlowNodeContainerFactory) TIME_DURATION(org.jbpm.process.core.timer.Timer.TIME_DURATION) IOException(java.io.IOException) Reader(java.io.Reader) EventDefinition(io.serverlessworkflow.api.events.EventDefinition) VariableScope(org.jbpm.process.core.context.variable.VariableScope) Collectors(java.util.stream.Collectors) GeneratedInfo(org.kie.kogito.codegen.api.GeneratedInfo) SWFConstants(org.kie.kogito.serverless.workflow.SWFConstants) Optional(java.util.Optional) KogitoWorkflowProcess(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess) Collections(java.util.Collections) SplitFactory(org.jbpm.ruleflow.core.factory.SplitFactory) ServerlessWorkflowUtils(org.kie.kogito.serverless.workflow.utils.ServerlessWorkflowUtils) StateHandler(org.kie.kogito.serverless.workflow.parser.handlers.StateHandler) Optional(java.util.Optional) Constants(io.serverlessworkflow.api.workflow.Constants) SWFConstants(org.kie.kogito.serverless.workflow.SWFConstants) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) JsonNode(com.fasterxml.jackson.databind.JsonNode) GeneratedInfo(org.kie.kogito.codegen.api.GeneratedInfo) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory)

Example 2 with Constants

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

the class ServerlessWorkflowFactory method createProcess.

public ExecutableProcess createProcess(Workflow workflow) {
    ExecutableProcess process = new ServerlessExecutableProcess();
    if (workflow.getId() != null && !workflow.getId().isEmpty()) {
        process.setId(workflow.getId());
    } else {
        LOGGER.info("setting default id {}", DEFAULT_WORKFLOW_ID);
        process.setId(DEFAULT_WORKFLOW_ID);
    }
    if (workflow.getName() != null && !workflow.getName().isEmpty()) {
        process.setName(workflow.getName());
    } else {
        LOGGER.info("setting default name {}", DEFAULT_WORKFLOW_NAME);
        process.setName(DEFAULT_WORKFLOW_NAME);
    }
    if (workflow.getVersion() != null && !workflow.getVersion().isEmpty()) {
        process.setVersion(workflow.getVersion());
    } else {
        LOGGER.info("no workflow version found.");
    }
    if (workflow.getMetadata() != null && workflow.getMetadata().get("package") != null) {
        process.setPackageName(workflow.getMetadata().get("package"));
    } else {
        process.setPackageName(DEFAULT_PACKAGE_NAME);
    }
    if (workflow.isKeepActive()) {
        process.setAutoComplete(false);
        process.setDynamic(true);
    } else {
        process.setAutoComplete(true);
    }
    process.setVisibility(DEFAULT_VISIBILITY);
    if (workflow.getMetadata() != null) {
        process.getMetaData().putAll(workflow.getMetadata());
    }
    if (workflow.getDescription() != null) {
        process.setMetaData("Documentation", workflow.getDescription());
    }
    if (workflow.getConstants() != null) {
        Constants constants = workflow.getConstants();
        String value = constants.getConstantsDef().toString();
        Variable constantsVariable = new Variable("contantsVariable", "$CONST", new JsonNodeDataType());
        constantsVariable.setMetaData("value", value.replaceAll("\"", "\\\""));
        process.getVariableScope().addVariable(constantsVariable);
    }
    if (workflow.getAnnotations() != null) {
        List<TagDefinition> tagDefinitions = new ArrayList<TagDefinition>();
        int counter = 0;
        for (String tag : workflow.getAnnotations()) {
            if (tag.startsWith("${")) {
                tagDefinitions.add(new FunctionTagDefinition(String.valueOf(++counter), unwrapExpression(tag), (exp, vars) -> {
                    Object result = ServerlessFunctions.expression(vars, exp);
                    if (result instanceof TextNode) {
                        return ((TextNode) result).asText();
                    }
                    return result.toString();
                }));
            } else {
                tagDefinitions.add(new StaticTagDefinition(String.valueOf(++counter), tag));
            }
        }
        ((Process) process).setTagDefinitions(tagDefinitions);
    }
    return process;
}
Also used : Arrays(java.util.Arrays) TimerNode(io.automatiko.engine.workflow.process.core.node.TimerNode) Metadata(io.automatiko.engine.workflow.process.executable.core.Metadata) ExecutableProcessValidator(io.automatiko.engine.workflow.process.executable.core.validation.ExecutableProcessValidator) Constants(io.serverlessworkflow.api.workflow.Constants) LoggerFactory(org.slf4j.LoggerFactory) Workflow(io.serverlessworkflow.api.Workflow) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) FunctionTagDefinition(io.automatiko.engine.workflow.base.core.FunctionTagDefinition) NodeImpl(io.automatiko.engine.workflow.process.core.impl.NodeImpl) ConstraintImpl(io.automatiko.engine.workflow.process.core.impl.ConstraintImpl) CompositeContextNode(io.automatiko.engine.workflow.process.core.node.CompositeContextNode) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) OnEvents(io.serverlessworkflow.api.events.OnEvents) TagDefinition(io.automatiko.engine.workflow.base.core.TagDefinition) ProcessAction(io.automatiko.engine.workflow.process.core.ProcessAction) ActionNode(io.automatiko.engine.workflow.process.core.node.ActionNode) Set(java.util.Set) JsonVariableScope(io.automatiko.engine.workflow.base.core.context.variable.JsonVariableScope) ErrorDefinition(io.serverlessworkflow.api.error.ErrorDefinition) WorkflowExecTimeout(io.serverlessworkflow.api.timeouts.WorkflowExecTimeout) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) Collectors(java.util.stream.Collectors) TextNode(com.fasterxml.jackson.databind.node.TextNode) List(java.util.List) EventTypeFilter(io.automatiko.engine.workflow.base.core.event.EventTypeFilter) ProcessInstanceCompensationAction(io.automatiko.engine.workflow.base.instance.impl.actions.ProcessInstanceCompensationAction) WorkItemNode(io.automatiko.engine.workflow.process.core.node.WorkItemNode) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) WorkImpl(io.automatiko.engine.workflow.base.core.impl.WorkImpl) Join(io.automatiko.engine.workflow.process.core.node.Join) StaticTagDefinition(io.automatiko.engine.workflow.base.core.StaticTagDefinition) ConsequenceAction(io.automatiko.engine.workflow.process.core.impl.ConsequenceAction) WorkflowUtils(io.serverlessworkflow.utils.WorkflowUtils) Work(io.automatiko.engine.workflow.base.core.Work) ProcessValidationError(io.automatiko.engine.workflow.base.core.validation.ProcessValidationError) SubProcessNode(io.automatiko.engine.workflow.process.core.node.SubProcessNode) Timer(io.automatiko.engine.workflow.base.core.timer.Timer) Assignment(io.automatiko.engine.workflow.process.core.node.Assignment) Action(io.serverlessworkflow.api.actions.Action) ServerlessExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ServerlessExecutableProcess) NodeContainer(io.automatiko.engine.workflow.process.core.NodeContainer) Split(io.automatiko.engine.workflow.process.core.node.Split) HashMap(java.util.HashMap) Node(io.automatiko.engine.api.definition.process.Node) ArrayList(java.util.ArrayList) TaskInputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.TaskInputJqAssignmentAction) ProduceEvent(io.serverlessworkflow.api.produce.ProduceEvent) HumanTaskNode(io.automatiko.engine.workflow.process.core.node.HumanTaskNode) OutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.OutputJqAssignmentAction) TaskOutputJqAssignmentAction(io.automatiko.engine.workflow.base.instance.impl.jq.TaskOutputJqAssignmentAction) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) FunctionDefinition(io.serverlessworkflow.api.functions.FunctionDefinition) BoundaryEventNode(io.automatiko.engine.workflow.process.core.node.BoundaryEventNode) ACTION(io.automatiko.engine.workflow.process.executable.core.Metadata.ACTION) Connection(io.automatiko.engine.api.definition.process.Connection) LinkedHashSet(java.util.LinkedHashSet) ActionDataFilter(io.serverlessworkflow.api.filters.ActionDataFilter) Logger(org.slf4j.Logger) ConnectionImpl(io.automatiko.engine.workflow.process.core.impl.ConnectionImpl) Iterator(java.util.Iterator) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) DateTimeUtils(io.automatiko.engine.workflow.base.core.timer.DateTimeUtils) EventDefinition(io.serverlessworkflow.api.events.EventDefinition) RetryDefinition(io.serverlessworkflow.api.retry.RetryDefinition) End(io.serverlessworkflow.api.end.End) EventDataFilter(io.serverlessworkflow.api.filters.EventDataFilter) StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) ServerlessFunctions(io.automatiko.engine.workflow.sw.ServerlessFunctions) Process(io.automatiko.engine.workflow.base.core.Process) ParameterDefinitionImpl(io.automatiko.engine.workflow.base.core.impl.ParameterDefinitionImpl) EventTrigger(io.automatiko.engine.workflow.process.core.node.EventTrigger) Collections(java.util.Collections) JsonNodeDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.JsonNodeDataType) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) EventNode(io.automatiko.engine.workflow.process.core.node.EventNode) FunctionTagDefinition(io.automatiko.engine.workflow.base.core.FunctionTagDefinition) TagDefinition(io.automatiko.engine.workflow.base.core.TagDefinition) StaticTagDefinition(io.automatiko.engine.workflow.base.core.StaticTagDefinition) StaticTagDefinition(io.automatiko.engine.workflow.base.core.StaticTagDefinition) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) FunctionTagDefinition(io.automatiko.engine.workflow.base.core.FunctionTagDefinition) JsonNodeDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.JsonNodeDataType) ArrayList(java.util.ArrayList) Constants(io.serverlessworkflow.api.workflow.Constants) TextNode(com.fasterxml.jackson.databind.node.TextNode) ServerlessExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ServerlessExecutableProcess) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) Process(io.automatiko.engine.workflow.base.core.Process) ServerlessExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ServerlessExecutableProcess) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) ServerlessExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ServerlessExecutableProcess)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 Connection (io.automatiko.engine.api.definition.process.Connection)1 Node (io.automatiko.engine.api.definition.process.Node)1 FunctionTagDefinition (io.automatiko.engine.workflow.base.core.FunctionTagDefinition)1 Process (io.automatiko.engine.workflow.base.core.Process)1 StaticTagDefinition (io.automatiko.engine.workflow.base.core.StaticTagDefinition)1 TagDefinition (io.automatiko.engine.workflow.base.core.TagDefinition)1 Work (io.automatiko.engine.workflow.base.core.Work)1 JsonVariableScope (io.automatiko.engine.workflow.base.core.context.variable.JsonVariableScope)1 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)1 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)1 JsonNodeDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.JsonNodeDataType)1 ObjectDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)1 EventTypeFilter (io.automatiko.engine.workflow.base.core.event.EventTypeFilter)1 ParameterDefinitionImpl (io.automatiko.engine.workflow.base.core.impl.ParameterDefinitionImpl)1 WorkImpl (io.automatiko.engine.workflow.base.core.impl.WorkImpl)1 DateTimeUtils (io.automatiko.engine.workflow.base.core.timer.DateTimeUtils)1 Timer (io.automatiko.engine.workflow.base.core.timer.Timer)1 ProcessValidationError (io.automatiko.engine.workflow.base.core.validation.ProcessValidationError)1