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;
}
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;
}
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;
}
Aggregations