use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class DataObjectHandler method start.
@SuppressWarnings("unchecked")
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
final String id = attrs.getValue("id");
final String name = attrs.getValue("name");
final String itemSubjectRef = attrs.getValue("itemSubjectRef");
Object parent = parser.getParent();
if (parent instanceof ContextContainer) {
ContextContainer contextContainer = (ContextContainer) parent;
VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
if (variableScope == null) {
return null;
}
List variables = variableScope.getVariables();
Variable variable = new Variable();
variable.setMetaData("DataObject", "true");
variable.setId(id);
variable.setName(name);
variable.setMetaData(id, variable.getName());
if (localName.equals("dataInput")) {
variable.setMetaData("DataInput", true);
} else if (localName.equals("dataOutput")) {
variable.setMetaData("DataOutput", true);
}
// retrieve type from item definition
DataType dataType = new ObjectDataType();
Map<String, ItemDefinition> itemDefinitions = (Map<String, ItemDefinition>) ((ProcessBuildData) parser.getData()).getMetaData("ItemDefinitions");
if (itemDefinitions != null) {
ItemDefinition itemDefinition = itemDefinitions.get(itemSubjectRef);
if (itemDefinition != null) {
String structureRef = itemDefinition.getStructureRef();
if ("java.lang.Boolean".equals(structureRef) || "Boolean".equals(structureRef)) {
dataType = new BooleanDataType();
} else if ("java.lang.Integer".equals(structureRef) || "Integer".equals(structureRef)) {
dataType = new IntegerDataType();
} else if ("java.lang.Float".equals(structureRef) || "Float".equals(structureRef)) {
dataType = new FloatDataType();
} else if ("java.lang.String".equals(structureRef) || "String".equals(structureRef)) {
dataType = new StringDataType();
} else if ("java.lang.Object".equals(structureRef) || "Object".equals(structureRef)) {
// use FQCN of Object
dataType = new ObjectDataType(java.lang.Object.class, structureRef);
} else {
dataType = new ObjectDataType(constructClass(structureRef, parser.getClassLoader()), structureRef);
}
}
}
variable.setType(dataType);
variables.add(variable);
return variable;
}
return new Variable();
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class NodeInnerClassesTest method testNodeReading.
@Test
public void testNodeReading() {
ExecutableProcess process = new ExecutableProcess();
process.setId("org.company.core.process.event");
process.setName("Event Process");
List<Variable> variables = new ArrayList<Variable>();
Variable variable = new Variable();
variable.setName("event");
ObjectDataType personDataType = new ObjectDataType(Person.class);
variable.setType(personDataType);
variables.add(variable);
process.getVariableScope().setVariables(variables);
process.setDynamic(true);
CompositeNode compositeNode = new CompositeNode();
compositeNode.setName("CompositeNode");
compositeNode.setId(2);
ForEachNode forEachNode = new ForEachNode();
ForEachNode.ForEachSplitNode split = new ForEachNode.ForEachSplitNode();
split.setName("ForEachSplit");
split.setMetaData("hidden", true);
split.setMetaData("UniqueId", forEachNode.getMetaData("Uniqueid") + ":foreach:split");
forEachNode.internalAddNode(split);
forEachNode.linkIncomingConnections(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE, new CompositeNode.NodeAndType(split, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE));
process.addNode(forEachNode);
InternalProcessRuntime ksession = createProcessRuntime(process);
TestProcessEventListener procEventListener = new TestProcessEventListener();
ksession.addEventListener(procEventListener);
ProcessInstance processInstance = ksession.startProcess("org.company.core.process.event");
assertNotNull(processInstance);
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class LambdaSubProcessNodeVisitor method unbind.
private BlockStmt unbind(VariableScope variableScope, SubProcessNode subProcessNode) {
BlockStmt stmts = new BlockStmt();
for (Map.Entry<String, String> e : subProcessNode.getOutMappings().entrySet()) {
// check if given mapping is an expression
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(e.getValue());
if (matcher.find()) {
String expression = matcher.group(1);
String topLevelVariable = expression.split("\\.")[0];
Map<String, String> dataOutputs = (Map<String, String>) subProcessNode.getMetaData("BPMN.OutputTypes");
Variable variable = new Variable();
variable.setName(topLevelVariable);
variable.setType(new ObjectDataType(constructClass(dataOutputs.get(e.getKey())), dataOutputs.get(e.getKey())));
stmts.addStatement(makeAssignment(variableScope.findVariable(topLevelVariable)));
stmts.addStatement(makeAssignmentFromModel(variable, e.getKey()));
stmts.addStatement(dotNotationToSetExpression(expression, e.getKey()));
stmts.addStatement(new MethodCallExpr().setScope(new NameExpr(KCONTEXT_VAR)).setName("setVariable").addArgument(new StringLiteralExpr(topLevelVariable)).addArgument(topLevelVariable));
} else {
stmts.addStatement(makeAssignmentFromModel(variableScope.findVariable(e.getValue()), e.getKey()));
stmts.addStatement(new MethodCallExpr().setScope(new NameExpr(KCONTEXT_VAR)).setName("setVariable").addArgument(new StringLiteralExpr(e.getValue())).addArgument(e.getKey()));
}
}
return stmts;
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class LambdaSubProcessNodeVisitor method bind.
private BlockStmt bind(VariableScope variableScope, SubProcessNode subProcessNode, ModelMetaData subProcessModel) {
BlockStmt actionBody = new BlockStmt();
actionBody.addStatement(subProcessModel.newInstance("model"));
for (Map.Entry<String, String> e : subProcessNode.getInMappings().entrySet()) {
// check if given mapping is an expression
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(e.getValue());
if (matcher.find()) {
String expression = matcher.group(1);
String topLevelVariable = expression.split("\\.")[0];
Variable v = variableScope.findVariable(topLevelVariable);
if (actionBody.findFirst(VariableDeclarationExpr.class, vd -> vd.getVariable(0).getNameAsString().equals(v.getSanitizedName())).isEmpty()) {
actionBody.addStatement(makeAssignment(v));
}
actionBody.addStatement(subProcessModel.callSetter("model", e.getKey(), dotNotationToGetExpression(expression)));
} else {
Variable v = variableScope.findVariable(e.getValue());
if (v != null) {
if (actionBody.findFirst(VariableDeclarationExpr.class, vd -> vd.getVariable(0).getNameAsString().equals(v.getSanitizedName())).isEmpty()) {
actionBody.addStatement(makeAssignment(v));
}
actionBody.addStatement(subProcessModel.callSetter("model", e.getKey(), e.getValue()));
}
}
}
actionBody.addStatement(new ReturnStmt(new NameExpr("model")));
return actionBody;
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class ModelMetaData method compilationUnit.
private CompilationUnit compilationUnit(String... annotations) {
CompilationUnit compilationUnit = parse(this.getClass().getResourceAsStream(templateName));
compilationUnit.setPackageDeclaration(packageName);
Optional<ClassOrInterfaceDeclaration> processMethod = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class, sl1 -> true);
if (!processMethod.isPresent()) {
throw new NoSuchElementException("Cannot find class declaration in the template");
}
ClassOrInterfaceDeclaration modelClass = processMethod.get();
for (String annotation : annotations) {
modelClass.addAnnotation(annotation);
}
if (asEntity) {
modelClass.addExtendedType("io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity");
modelClass.addAnnotation(new NormalAnnotationExpr(new Name("javax.persistence.Entity"), NodeList.nodeList(new MemberValuePair("name", new StringLiteralExpr(camelToSnake(processId.toUpperCase() + version(version).toUpperCase()))))));
modelClass.findAll(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("metadata")).forEach(fd -> fd.addAnnotation("javax.persistence.Transient"));
}
if (supportsOpenApi) {
modelClass.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.media.Schema"), NodeList.nodeList(new MemberValuePair("name", new StringLiteralExpr(name.replaceAll("\\s", ""))), new MemberValuePair("description", new StringLiteralExpr(description)))));
}
if (!WorkflowProcess.PRIVATE_VISIBILITY.equals(visibility)) {
modelClass.addAnnotation(new NormalAnnotationExpr(new Name(Generated.class.getCanonicalName()), NodeList.nodeList(new MemberValuePair("value", new StringLiteralExpr("automatik-codegen")), new MemberValuePair("reference", new StringLiteralExpr(processId)), new MemberValuePair("name", new StringLiteralExpr(StringUtils.capitalize(ProcessToExecModelGenerator.extractProcessId(processId, version)))), new MemberValuePair("hidden", new BooleanLiteralExpr(hidden)))));
}
modelClass.setName(modelClassSimpleName);
modelClass.getConstructors().forEach(c -> c.setName(modelClass.getName()));
modelClass.findAll(MethodDeclaration.class, md -> md.getNameAsString().equals("build")).forEach(md -> {
md.getBody().get().findAll(SimpleName.class).stream().findFirst().ifPresent(o -> {
o.setIdentifier(modelClassSimpleName);
});
md.toString();
});
modelClass.findAll(AnnotationExpr.class, ae -> ae.getNameAsString().equals("JsonDeserialize")).forEach(ae -> {
ae.findAll(ClassOrInterfaceType.class, cit -> cit.getNameAsString().contains("$TYPE$")).stream().findFirst().ifPresent(cit -> cit.setName(cit.getNameAsString().replaceAll("\\$TYPE\\$", modelClassSimpleName)));
ae.toString();
});
// 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, new SimpleName(HashMap.class.getSimpleName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getSimpleName()), new ClassOrInterfaceType(null, Object.class.getSimpleName()))), NodeList.nodeList()), AssignExpr.Operator.ASSIGN));
// setup of static fromMap method body
BlockStmt staticFromMap = new BlockStmt();
if (modelClass.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("getId")).isPresent()) {
FieldAccessExpr idField = new FieldAccessExpr(new ThisExpr(), "id");
staticFromMap.addStatement(new AssignExpr(idField, new NameExpr("id"), AssignExpr.Operator.ASSIGN));
}
for (Map.Entry<String, Variable> variable : variableScope.getTypes().entrySet()) {
String varName = variable.getValue().getName();
String vtype = variable.getValue().getType().getStringType();
String sanitizedName = variable.getValue().getSanitizedName();
FieldDeclaration fd = declareField(sanitizedName, vtype);
modelClass.addMember(fd);
List<String> tags = variable.getValue().getTags();
fd.addAnnotation(new NormalAnnotationExpr(new Name(VariableInfo.class.getCanonicalName()), NodeList.nodeList(new MemberValuePair("tags", new StringLiteralExpr(tags.stream().collect(Collectors.joining(",")))))));
fd.addAnnotation(new NormalAnnotationExpr(new Name(JsonProperty.class.getCanonicalName()), NodeList.nodeList(new MemberValuePair("value", new StringLiteralExpr(varName)))));
if (asEntity && tags.contains(Variable.TRANSIENT_TAG)) {
fd.addAnnotation("javax.persistence.Transient");
}
if (supportsOpenApi) {
fd.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.media.Schema"), NodeList.nodeList(new MemberValuePair("name", new StringLiteralExpr(sanitizedName)), new MemberValuePair("description", new StringLiteralExpr(getOrDefault((String) variable.getValue().getMetaData("Documentation"), ""))))));
}
applyValidation(fd, tags);
applyPersistence(variable.getValue().getType(), fd, tags);
fd.createGetter();
fd.createSetter();
// toMap method body
MethodCallExpr putVariable = new MethodCallExpr(new NameExpr("params"), "put");
putVariable.addArgument(new StringLiteralExpr(varName));
putVariable.addArgument(new FieldAccessExpr(new ThisExpr(), sanitizedName));
toMapBody.addStatement(putVariable);
ClassOrInterfaceType type = parseClassOrInterfaceType(vtype);
// from map instance method body
FieldAccessExpr instanceField = new FieldAccessExpr(new ThisExpr(), sanitizedName);
staticFromMap.addStatement(new AssignExpr(instanceField, new CastExpr(type, new MethodCallExpr(new NameExpr("params"), "get").addArgument(new StringLiteralExpr(varName))), AssignExpr.Operator.ASSIGN));
}
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));
modelClass.findFirst(MethodDeclaration.class, // make sure
sl -> sl.getName().asString().equals("fromMap") && sl.getParameters().size() == 2).ifPresent(m -> m.setBody(staticFromMap));
for (Consumer<CompilationUnit> augmentor : augmentors) {
augmentor.accept(compilationUnit);
}
if (!workflowType.equals(Process.WORKFLOW_TYPE)) {
// remove metadata field and it's getters/setters
modelClass.findFirst(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("metadata")).ifPresent(fd -> fd.removeForced());
modelClass.findAll(MethodDeclaration.class, md -> md.getNameAsString().equals("getMetadata") || md.getNameAsString().equals("setMetadata")).forEach(md -> md.removeForced());
}
return compilationUnit;
}
Aggregations