Skip to content

Commit

Permalink
Merge pull request #264 from KristianKarl/master
Browse files Browse the repository at this point in the history
Fix issue with accessing global attributes
  • Loading branch information
KristianKarl committed Apr 7, 2021
2 parents 6df587c + b4423f0 commit 2b64350
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,13 @@ public void execute(Element element) {
}

public Value getAttribute(String name) {
return executionEnvironment.getBindings("js").getMember(name);
Pattern pattern = Pattern.compile(REGEXP_GLOBAL);
Matcher matcher = pattern.matcher(name);
if (matcher.find()) {
return globalExecutionEnvironment.getBindings("js").getMember(name.replaceAll(REGEXP_GLOBAL, ""));
} else {
return executionEnvironment.getBindings("js").getMember(name);
}
}

public void setAttribute(String name, Value value) {
Expand All @@ -283,11 +289,22 @@ public void setAttribute(String name, Value value) {
public String data() {
StringBuilder data = new StringBuilder();
for (String member : executionEnvironment.getBindings("js").getMemberKeys()) {
if (executionEnvironment.getBindings("js").getMember(member).toString().contains("org.graphwalker.core.machine.TestExecutionContext")) {
continue;
}
data.append(member)
.append(": ")
.append(executionEnvironment.getBindings("js").getMember(member))
.append(", ");
}
if (isNotNull(globalExecutionEnvironment)) {
for (String member : globalExecutionEnvironment.getBindings("js").getMemberKeys()) {
data.append("global." + member)
.append(": ")
.append(globalExecutionEnvironment.getBindings("js").getMember(member))
.append(", ");
}
}
return data.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@
import org.slf4j.MDC;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.graphwalker.core.common.Objects.isNotNull;
import static org.graphwalker.core.common.Objects.isNull;
import static org.graphwalker.core.common.Objects.*;
import static org.graphwalker.core.model.Edge.RuntimeEdge;
import static org.graphwalker.core.model.Vertex.RuntimeVertex;

Expand Down Expand Up @@ -292,15 +289,7 @@ private void execute(List<Action> actions) {
}

private void execute(Action action) {
Pattern pattern = Pattern.compile(REGEXP_GLOBAL);
Matcher matcher = pattern.matcher(action.getScript());

if (matcher.find()) {
LOG.debug("Execute action: '{}' in model: '{}'", action.getScript(), getCurrentContext().getModel().getName());
globalExecutionEnvironment.eval("js", action.getScript().replaceAll(REGEXP_GLOBAL, ""));
} else {
getCurrentContext().execute(action);
}
getCurrentContext().execute(action);
}

private static class SharedStateTuple {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ public void accessGlobalAttribute() {
.setSharedState("SHARED_STATE_VERTEX")
)
)
.addAction(new Action("global.myVariable = false; global.myVariable_2 = false;"));
.addAction(new Action("myVariable = true;"))
.addAction(new Action("global.myVariable = false"))
.addAction(new Action("global.myVariable_2 = false;"));

// Model 2 has 2 vertices and 2 edges. One edge, e_C performs an action which has to execute in order
// to fulfill the condition for edge e_D
Expand Down Expand Up @@ -186,16 +188,21 @@ public void accessGlobalAttribute() {
List<String> actualPath = new ArrayList<String>();
while (machine.hasNextStep()) {
machine.getNextStep();
actualPath.add(machine.getCurrentContext().getCurrentElement().getName());
actualPath.add(machine.getCurrentContext().getCurrentElement().getName()
+ ": " + machine.getCurrentContext().getAttribute("myVariable")
+ ", " + machine.getCurrentContext().getAttribute("global.myVariable")
+ ", " + machine.getCurrentContext().getAttribute("global.myVariable_2")
+ ", data: " + ((ExecutionContext)machine.getCurrentContext()).data()
);
}
Assert.assertArrayEquals(new ArrayList<>(Arrays.asList(
"v_A",
"e_B",
"v_B",
"v_C",
"e_C",
"v_C",
"e_D",
"v_D"
"v_A: true, false, false, data: myVariable: true, global.myVariable: false, global.myVariable_2: false, ",
"e_B: true, false, false, data: myVariable: true, global.myVariable: false, global.myVariable_2: false, ",
"v_B: true, false, false, data: myVariable: true, global.myVariable: false, global.myVariable_2: false, ",
"v_C: null, false, false, data: global.myVariable: false, global.myVariable_2: false, ",
"e_C: null, true, false, data: global.myVariable: true, global.myVariable_2: false, ",
"v_C: null, true, false, data: global.myVariable: true, global.myVariable_2: false, ",
"e_D: null, true, false, data: global.myVariable: true, global.myVariable_2: false, ",
"v_D: null, true, false, data: global.myVariable: true, global.myVariable_2: false, "
)).toArray(), actualPath.toArray()); }
}

0 comments on commit 2b64350

Please sign in to comment.