Skip to content
Snippets Groups Projects
Commit aa7813b9 authored by Henning's avatar Henning
Browse files

added setVariable/getVariable (Expression)

extends the existing logic to delcare all read/write Variables
parent c7b698a1
No related branches found
No related tags found
No related merge requests found
......@@ -55,7 +55,11 @@
<artifactId>jackson-core</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.model</groupId>
<artifactId>camunda-bpmn-model</artifactId>
......
......@@ -6,6 +6,10 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import de.fhmuenster.masterthesis.serialization.TestgeneratorDSLObjectCreator;
import de.fhmuenster.masterthesis.testgeneratorDSL.TestgeneratorDSLFactory;
import de.fhmuenster.masterthesis.testgeneratorDSL.VariableReference;
public class BPMNIOParameters {
private HashMap<String, List<String>> inputVariables = new HashMap<String, List<String>>();
......@@ -13,6 +17,7 @@ public class BPMNIOParameters {
private HashMap<String, List<String>> innerOutputVariables = new HashMap<String, List<String>>();
private HashMap<String, List<String>> removeVariables = new HashMap<String, List<String>>();
private HashMap<String, HashMap<String, String>> hardcodedInputParametersString = new HashMap<String, HashMap<String, String>>();
private HashMap<String, HashMap<String, Long>> hardcodedInputParametersLong = new HashMap<String, HashMap<String, Long>>();
private HashMap<String, HashMap<String, Boolean>> hardcodedInputParametersBoolean = new HashMap<String, HashMap<String, Boolean>>();
......@@ -68,6 +73,96 @@ public class BPMNIOParameters {
variables.add(removeVariable);
this.removeVariables.put(flowElementId, variables);
}
/**
* 20.12.2021
* addSetVariable add setVariable to HashMap setVariables to use it on BPMNIOParameters
* @param taskId
*/
public void addSetVariable(String flowElementId, String setVarName, Object setVarValue) {
// setVartiable == writeVariables/outputVariables
List<String> existingVariables = this.outputVariables.get(flowElementId);
List<String> variables = existingVariables != null ? existingVariables : new ArrayList<>();
if(!variables.contains(setVarName))
{
// Need to create a new Variable
if(setVarValue instanceof Integer)
{
List<Integer> iList = new ArrayList<>();
iList.add((Integer) setVarValue);
TestgeneratorDSLObjectCreator.createIntVariable(setVarName, iList);
variables.add(setVarName);
}
else if(setVarValue instanceof Boolean)
{
List<Boolean> bList = new ArrayList<>();
bList.add((Boolean) setVarValue);
TestgeneratorDSLObjectCreator.createBooleanVariable(setVarName, bList);
variables.add(setVarName);
}
else if(setVarValue instanceof String)
{
List<String> sList = new ArrayList<>();
sList.add((String) setVarValue);
TestgeneratorDSLObjectCreator.createStringVariable(setVarName, sList);
variables.add(setVarName);
}
}
else
{
// Variable already exists
variables.add(setVarName);
}
this.outputVariables.put(flowElementId, variables);
}
/**
* 20.12.2021
* addGetVariable add getVariable to HashMap getVariables to use it on BPMNIOParameters
* @param taskId
*/
public void addGetVariable(String flowElementId, String getVarName, Object getVarValue) {
// setVartiable == readVariables/inputVariables
List<String> existingVariables = this.inputVariables.get(flowElementId);
List<String> variables = existingVariables != null ? existingVariables : new ArrayList<>();
if(!variables.contains(getVarName))
{
// Need to create a new Variable
if(getVarValue instanceof Integer)
{
List<Integer> iList = new ArrayList<>();
iList.add((Integer) getVarValue);
TestgeneratorDSLObjectCreator.createIntVariable(getVarName, iList);
variables.add(getVarName);
}
else if(getVarValue instanceof Boolean)
{
List<Boolean> bList = new ArrayList<>();
bList.add((Boolean) getVarValue);
TestgeneratorDSLObjectCreator.createBooleanVariable(getVarName, bList);
variables.add(getVarName);
}
else if(getVarValue instanceof String)
{
List<String> sList = new ArrayList<>();
sList.add((String) getVarValue);
TestgeneratorDSLObjectCreator.createStringVariable(getVarName, sList);
variables.add(getVarName);
}
else
{
// Variable already exists
variables.add(getVarName);
}
}
this.inputVariables.put(flowElementId, variables);
}
public void addInputVariable(String flowElementId, String inputVariableName) {
List<String> existingVariables = this.inputVariables.get(flowElementId);
......
......@@ -9,6 +9,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.model.bpmn.instance.BaseElement;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaInputParameter;
......@@ -28,6 +29,7 @@ public class BPMNVariableIOScanner {
private static final String NAMESPACE_URI_BPMN = "http://camunda.org/schema/1.0/bpmn";
private static final String INPUT_OUTPUT_NAME = "inputOutput";
private static final String EXECUTION_LISTENER = "executionListener";
private static final String LISTENER_TYPE_EXPRESSION = "expression";
private BPMNFlowSet flowSet;
private BPMNIOParameters bpmnIOParameters;
......@@ -53,7 +55,7 @@ public class BPMNVariableIOScanner {
* added routine to scan in Camunda the removeExpressions
*/
extensionElements
.forEach(baseElement -> scanRemoveExpressions(baseElement)); //
.forEach(baseElement -> scanExpressions(baseElement)); //
}
......@@ -148,12 +150,32 @@ public class BPMNVariableIOScanner {
bpmnIOParameters.addRemoveVariable(baseElement.getId(), removeVar);
}
/**
* 20.12.2021
* insert getVariables to BPMNIOParameters
* @param baseElement
*/
private void insertGetVariable(FlowElement baseElement, String getVarName, Object getVarValue)
{
bpmnIOParameters.addGetVariable(baseElement.getId(), getVarName, getVarValue);
}
/**
* 20.12.2021
* insert setVariables to BPMNIOParameters
* @param baseElement
*/
private void insertSetVariable(FlowElement baseElement, String setVarName, Object setVarValue)
{
bpmnIOParameters.addSetVariable(baseElement.getId(), setVarName, setVarValue);
}
/**
* 15.12.2021
* Scan for removeExpressions
* @param baseElement
*/
private void scanRemoveExpressions(FlowElement baseElement) {
private void scanExpressions(FlowElement baseElement) {
Collection<ModelElementInstance> elements = baseElement.getExtensionElements().getElements();
......@@ -168,19 +190,31 @@ public class BPMNVariableIOScanner {
try
{
List<String> removeVars = new ArrayList<String>();
HashMap<String, Object> getVars = new HashMap<String, Object>();
HashMap<String, Object> setVars = new HashMap<String, Object>();
switch(expression.getAttributeName())
{
case "delegateExpression":
case LISTENER_TYPE_EXPRESSION:
String strExpression = expression.getValue(currentElement).toString();
if(strExpression.contains("execution.removeVariable")) // check the String for a removeVariable*s Expression
if(strExpression.contains("execution.removeVariable"))
{
Pattern pattern = Pattern.compile("\"(.*?)\"", Pattern.DOTALL);
Matcher matcher = pattern.matcher(strExpression);
while (matcher.find()) {
removeVars.add(matcher.group(1)); // Contains String with all variables from removeVar Expression
removeVars.add(matcher.group(1));
}
}
if(strExpression.contains("execution.getVariable"))
{
getVars = this.buildVariableHashMap(strExpression);
}
if(strExpression.contains("execution.setVariable"))
{
setVars = this.buildVariableHashMap(strExpression);
}
break;
default:
break;
......@@ -191,17 +225,88 @@ public class BPMNVariableIOScanner {
// add the removeVariables to BPMNIOParameters via. insertRemoveVariables
removeVars.forEach(var -> this.insertRemoveVariable(baseElement, var));
}
if(!getVars.isEmpty() && getVars!=null)
{
getVars.forEach((k, v ) -> {
this.insertGetVariable(baseElement, k, v);
});
}
if(!setVars.isEmpty() && setVars!=null)
{
setVars.forEach((k, v ) -> {
this.insertSetVariable(baseElement, k, v);
});
}
}
catch(Exception e)
{
System.out.println("[ERROR] " + e.getMessage());
}
}
}
}
}
/**
* Split the variables in the expression string to passable variables
* @param strExpression
* @return
*/
private HashMap<String, Object> buildVariableHashMap(String strExpression) {
int start, end;
start = strExpression.indexOf("(");
end = strExpression.indexOf(")");
strExpression = strExpression.substring(start+1, end);
String result [] = strExpression.split(",");
HashMap<String, Object> hmap = new HashMap<String, Object>();
try
{
// Every second Element is the key
for(int i = 0; i < result.length; i+=2)
{
String key = result[i];
String val = result[i+1];
Object _val;
if(StringUtils.isNumericSpace(val))
{
val = val.replace(" ", "");
_val = Integer.parseInt(val);
}
else if(val.contains("true") || val.contains("false"))
{
_val = Boolean.valueOf(Boolean.parseBoolean(val));
}
else
{
val = val.replace("\"", "");
_val = String.valueOf(val.toString());
}
key = key.replace("\"", "");
key = key.replace(" ", "");
hmap.put(key, _val);
}
}
catch(Exception e)
{
System.out.println("[ERROR] Expression syntax error (Index out of bounds)");
}
hmap.forEach((k, v) -> {
if( v instanceof Integer)
System.out.println(k + " " + v + " type integer");
else if( v instanceof Boolean)
System.out.println(k + " " + v + " type boolean");
else if( v instanceof String)
System.out.println(k + " " + v + " type String");
});
return hmap;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment