use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.
the class ProcessToExecModelGeneratorTest method testScriptAndWorkItemGeneration.
@Test
public void testScriptAndWorkItemGeneration() {
ExecutableProcessFactory factory = ExecutableProcessFactory.createProcess("demo.orders");
factory.variable("order", new ObjectDataType(Integer.class)).variable("approver", new ObjectDataType(String.class)).name("orders").packageName("com.myspace.demo").dynamic(false).version("1.0").workItemNode(1).name("Log").workName("Log").done().actionNode(2).name("Dump order").action("java", "System.out.println(\"Order has been created \" + order);").done().endNode(3).name("end").terminate(false).done().startNode(4).name("start").done().connection(2, 1).connection(4, 2).connection(1, 3);
WorkflowProcess process = factory.validate().getProcess();
ProcessMetaData processMetadata = ProcessToExecModelGenerator.INSTANCE.generate(process);
assertNotNull(processMetadata, "Dumper should return non null class for process");
logger.debug(processMetadata.getGeneratedClassModel().toString());
assertEquals("orders_1_0", processMetadata.getExtractedProcessId());
assertEquals("demo.orders", processMetadata.getProcessId());
assertEquals("orders", processMetadata.getProcessName());
assertEquals("_1_0", processMetadata.getProcessVersion());
assertEquals("com.myspace.demo.Orders_1_0Process", processMetadata.getProcessClassName());
assertNotNull(processMetadata.getGeneratedClassModel());
assertEquals(1, processMetadata.getWorkItems().size());
}
use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.
the class UserTaskModelMetaData method compilationUnitInput.
private CompilationUnit compilationUnitInput() {
// task input handling
CompilationUnit compilationUnit = parse(this.getClass().getResourceAsStream("/class-templates/TaskInputTemplate.java"));
compilationUnit.setPackageDeclaration(packageName);
Optional<ClassOrInterfaceDeclaration> processMethod = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class, sl1 -> true);
if (!processMethod.isPresent()) {
throw new RuntimeException("Cannot find class declaration in the template");
}
ClassOrInterfaceDeclaration modelClass = processMethod.get();
compilationUnit.addOrphanComment(new LineComment("Task input model for user task '" + humanTaskNode.getName() + "' in process '" + processId + "'"));
addUserTaskAnnotation(modelClass);
modelClass.setName(inputModelClassSimpleName);
if (templateSupported) {
modelClass.addAnnotation("io.quarkus.qute.TemplateData");
}
modelClass.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.media.Schema"), NodeList.nodeList(new MemberValuePair("name", new StringLiteralExpr(("Input model for " + humanTaskNode.getName()).replaceAll("\\s", ""))), new MemberValuePair("description", new StringLiteralExpr("Task input model for user task '" + humanTaskNode.getName() + "' in '" + processId + "'")))));
// setup of static fromMap method body
ClassOrInterfaceType modelType = new ClassOrInterfaceType(null, modelClass.getNameAsString());
BlockStmt staticFromMap = new BlockStmt();
VariableDeclarationExpr itemField = new VariableDeclarationExpr(modelType, "item");
staticFromMap.addStatement(new AssignExpr(itemField, new ObjectCreationExpr(null, modelType, NodeList.nodeList()), AssignExpr.Operator.ASSIGN));
NameExpr item = new NameExpr("item");
for (Entry<String, String> entry : humanTaskNode.getInMappings().entrySet()) {
Variable variable = Optional.ofNullable(variableScope.findVariable(entry.getValue())).orElse(processVariableScope.findVariable(entry.getValue()));
if (variable == null) {
throw new IllegalStateException("Task " + humanTaskNode.getName() + " (input) " + entry.getKey() + " reference not existing variable " + entry.getValue());
}
FieldDeclaration fd = new FieldDeclaration().addVariable(new VariableDeclarator().setType(variable.getType().getStringType()).setName(entry.getKey())).addModifier(Modifier.Keyword.PRIVATE);
modelClass.addMember(fd);
addUserTaskParamAnnotation(fd, UserTaskParam.ParamType.INPUT);
fd.createGetter();
fd.createSetter();
// fromMap static method body
FieldAccessExpr field = new FieldAccessExpr(item, entry.getKey());
ClassOrInterfaceType type = parseClassOrInterfaceType(variable.getType().getStringType());
staticFromMap.addStatement(new AssignExpr(field, new CastExpr(type, new MethodCallExpr(new NameExpr("params"), "get").addArgument(new StringLiteralExpr(entry.getKey()))), AssignExpr.Operator.ASSIGN));
}
for (Entry<String, Object> entry : humanTaskNode.getWork().getParameters().entrySet()) {
if (entry.getValue() == null || INTERNAL_FIELDS.contains(entry.getKey())) {
continue;
}
Map<String, String> dataInputs = (Map<String, String>) humanTaskNode.getMetaData(DATA_INPUTS);
FieldDeclaration fd = new FieldDeclaration().addVariable(new VariableDeclarator().setType(dataInputs.get(entry.getKey())).setName(entry.getKey())).addModifier(Modifier.Keyword.PUBLIC);
modelClass.addMember(fd);
addUserTaskParamAnnotation(fd, UserTaskParam.ParamType.INPUT);
fd.createGetter();
fd.createSetter();
// fromMap static method body
FieldAccessExpr field = new FieldAccessExpr(item, entry.getKey());
ClassOrInterfaceType type = parseClassOrInterfaceType(fd.getVariable(0).getType().asString());
staticFromMap.addStatement(new AssignExpr(field, new CastExpr(type, new MethodCallExpr(new NameExpr("params"), "get").addArgument(new StringLiteralExpr(entry.getKey()))), AssignExpr.Operator.ASSIGN));
}
Optional<MethodDeclaration> staticFromMapMethod = modelClass.findFirst(MethodDeclaration.class, sl -> sl.getName().asString().equals("fromMap") && sl.isStatic());
if (staticFromMapMethod.isPresent()) {
MethodDeclaration fromMap = staticFromMapMethod.get();
fromMap.setType(modelClass.getNameAsString());
staticFromMap.addStatement(new ReturnStmt(new NameExpr("item")));
fromMap.setBody(staticFromMap);
}
// setup of the toMap method body
BlockStmt toMapBody = new BlockStmt();
ClassOrInterfaceType toMap = new ClassOrInterfaceType(null, new SimpleName(Map.class.getSimpleName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getSimpleName()), new ClassOrInterfaceType(null, Object.class.getSimpleName())));
VariableDeclarationExpr paramsField = new VariableDeclarationExpr(toMap, "params");
toMapBody.addStatement(new AssignExpr(paramsField, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, HashMap.class.getSimpleName()), NodeList.nodeList()), AssignExpr.Operator.ASSIGN));
for (Entry<String, String> entry : humanTaskNode.getInMappings().entrySet()) {
if (entry.getValue() == null || INTERNAL_FIELDS.contains(entry.getKey())) {
continue;
}
Variable variable = Optional.ofNullable(variableScope.findVariable(entry.getValue())).orElse(processVariableScope.findVariable(entry.getValue()));
if (variable == null) {
// check if given mapping is an expression
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(entry.getValue());
if (matcher.find()) {
Map<String, String> dataOutputs = (Map<String, String>) humanTaskNode.getMetaData(DATA_INPUTS);
variable = new Variable();
variable.setName(entry.getKey());
variable.setType(new ObjectDataType(constructClass(dataOutputs.get(entry.getKey())), dataOutputs.get(entry.getKey())));
} else {
throw new IllegalStateException("Task " + humanTaskNode.getName() + " (output) " + entry.getKey() + " reference not existing variable " + entry.getValue());
}
}
// toMap method body
MethodCallExpr putVariable = new MethodCallExpr(new NameExpr("params"), "put");
putVariable.addArgument(new StringLiteralExpr(entry.getKey()));
putVariable.addArgument(new FieldAccessExpr(new ThisExpr(), entry.getKey()));
toMapBody.addStatement(putVariable);
}
Optional<MethodDeclaration> toMapMethod = modelClass.findFirst(MethodDeclaration.class, sl -> sl.getName().asString().equals("toMap"));
toMapBody.addStatement(new ReturnStmt(new NameExpr("params")));
toMapMethod.ifPresent(methodDeclaration -> methodDeclaration.setBody(toMapBody));
return compilationUnit;
}
use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.
the class VariableDeclarations method ofRawInfo.
public static VariableDeclarations ofRawInfo(Map<String, String> vscope) {
HashMap<String, Variable> vs = new HashMap<>();
if (vscope != null) {
for (Entry<String, String> entry : vscope.entrySet()) {
Variable variable = new Variable();
variable.setName(entry.getKey());
variable.setType(new ObjectDataType(constructClass(entry.getValue()), entry.getValue()));
vs.put(entry.getKey(), variable);
}
}
return new VariableDeclarations(vs);
}
use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.
the class TaskHandler method handleNode.
protected void handleNode(final Node node, final Element element, final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
super.handleNode(node, element, uri, localName, parser);
itemDefinitions = (Map<String, ItemDefinition>) ((ProcessBuildData) parser.getData()).getMetaData("ItemDefinitions");
dataTypeInputs.clear();
dataTypeOutputs.clear();
WorkItemNode workItemNode = (WorkItemNode) node;
String name = getTaskName(element);
Work work = new WorkImpl();
work.setName(name);
workItemNode.setWork(work);
org.w3c.dom.Node xmlNode = element.getFirstChild();
while (xmlNode != null) {
String nodeName = xmlNode.getNodeName();
if ("ioSpecification".equals(nodeName)) {
readIoSpecification(xmlNode, dataInputs, dataOutputs, dataInputTypes);
} else if ("dataInputAssociation".equals(nodeName)) {
readDataInputAssociation(xmlNode, workItemNode, dataInputs);
} else if ("dataOutputAssociation".equals(nodeName)) {
readDataOutputAssociation(xmlNode, workItemNode, dataOutputs);
}
xmlNode = xmlNode.getNextSibling();
}
workItemNode.setMetaData("DataInputs", new LinkedHashMap<String, String>(dataTypeInputs));
workItemNode.setMetaData("DataOutputs", new LinkedHashMap<String, String>(dataTypeOutputs));
handleScript(workItemNode, element, "onEntry");
handleScript(workItemNode, element, "onExit");
String compensation = element.getAttribute("isForCompensation");
if (compensation != null) {
boolean isForCompensation = Boolean.parseBoolean(compensation);
if (isForCompensation) {
workItemNode.setMetaData("isForCompensation", isForCompensation);
}
}
for (Entry<String, String> entryInputTypes : dataTypeInputs.entrySet()) {
ParameterDefinition parameterDefinition = new ParameterDefinitionImpl();
parameterDefinition.setName(entryInputTypes.getKey());
parameterDefinition.setType(new ObjectDataType(constructClass(entryInputTypes.getValue()), entryInputTypes.getValue()));
work.addParameterDefinition(parameterDefinition);
}
}
Aggregations