Search in sources :

Example 11 with Workflow

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

the class SvgServerlessProcessImageGenerator method generate.

@Override
public String generate() {
    if (workflow == null) {
        return null;
    }
    WorkflowDiagram workflowDiagram = new WorkflowDiagramImpl().setWorkflow(workflow).setTemplate("automatiko-workflow-template");
    List<String> stateNames = workflow.getStates().stream().map(state -> state.getName()).collect(Collectors.toList());
    String diagramSVG = null;
    try {
        diagramSVG = workflowDiagram.getSvgDiagram();
        InputSource is = new InputSource(new StringReader(diagramSVG));
        DocumentBuilder dBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
        Document doc = dBuilder.parse(is);
        Node svg = doc.getElementsByTagName("svg").item(0);
        ((Element) svg).removeAttribute("height");
        ((Element) svg).removeAttribute("width");
        ((Element) svg).removeAttribute("style");
        ((Element) svg).removeAttribute("preserveAspectRatio");
        ((Element) svg).setAttribute("text-rendering", "auto");
        ((Element) svg).setAttribute("shape-rendering", "auto");
        NodeList textNodes = doc.getElementsByTagName("text");
        for (int i = 0; i < textNodes.getLength(); i++) {
            Element text = (Element) textNodes.item(i);
            if (stateNames.contains(text.getTextContent())) {
                Node sibling = text.getPreviousSibling();
                while (!sibling.getNodeName().equals("rect")) {
                    sibling = sibling.getPreviousSibling();
                }
                ((Element) sibling).setAttribute("id", UUID.nameUUIDFromBytes(text.getTextContent().getBytes(StandardCharsets.UTF_8)).toString());
            }
        }
        ByteArrayOutputStream dmnDocumentStream = new ByteArrayOutputStream();
        Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(dmnDocumentStream, StandardCharsets.UTF_8)));
        diagramSVG = new String(dmnDocumentStream.toByteArray());
    } catch (Exception e) {
        LOGGER.warn("Unable to generate workflow image for {} due to {}", workflow.getName(), e.getMessage());
    }
    return diagramSVG;
}
Also used : Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamResult(javax.xml.transform.stream.StreamResult) LoggerFactory(org.slf4j.LoggerFactory) Workflow(io.serverlessworkflow.api.Workflow) WorkflowDiagram(io.serverlessworkflow.api.interfaces.WorkflowDiagram) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) OutputStreamWriter(java.io.OutputStreamWriter) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) UUID(java.util.UUID) OutputKeys(javax.xml.transform.OutputKeys) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) WorkflowDiagramImpl(io.serverlessworkflow.diagram.WorkflowDiagramImpl) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) WorkflowDiagramImpl(io.serverlessworkflow.diagram.WorkflowDiagramImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder) WorkflowDiagram(io.serverlessworkflow.api.interfaces.WorkflowDiagram) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter)

Example 12 with Workflow

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

the class SwitchValidatorTest method mockWorkflow.

private Workflow mockWorkflow() {
    Workflow workflow = mock(Workflow.class);
    doReturn(WORKFLOW_NAME).when(workflow).getName();
    return workflow;
}
Also used : Workflow(io.serverlessworkflow.api.Workflow)

Example 13 with Workflow

use of io.serverlessworkflow.api.Workflow 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

Workflow (io.serverlessworkflow.api.Workflow)13 Connection (io.automatiko.engine.api.definition.process.Connection)5 DateTimeUtils (io.automatiko.engine.workflow.base.core.timer.DateTimeUtils)5 OutputJqAssignmentAction (io.automatiko.engine.workflow.base.instance.impl.jq.OutputJqAssignmentAction)5 NodeContainer (io.automatiko.engine.workflow.process.core.NodeContainer)5 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)5 Assignment (io.automatiko.engine.workflow.process.core.node.Assignment)5 BoundaryEventNode (io.automatiko.engine.workflow.process.core.node.BoundaryEventNode)5 CompositeContextNode (io.automatiko.engine.workflow.process.core.node.CompositeContextNode)5 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)5 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)5 EventNode (io.automatiko.engine.workflow.process.core.node.EventNode)5 EventDefinition (io.serverlessworkflow.api.events.EventDefinition)5 FunctionDefinition (io.serverlessworkflow.api.functions.FunctionDefinition)5 Collections (java.util.Collections)5 List (java.util.List)5 Map (java.util.Map)5 Join (io.automatiko.engine.workflow.process.core.node.Join)4 Split (io.automatiko.engine.workflow.process.core.node.Split)4 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)4