Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.graphwalker.core.machine.Context;
import org.graphwalker.core.machine.Machine;
import org.graphwalker.core.machine.SimpleMachine;
import org.graphwalker.core.model.Action;
import org.graphwalker.core.model.Element;
import org.graphwalker.io.factory.json.JsonContextFactory;
import org.graphwalker.io.factory.yed.YEdContextFactory;
Expand Down Expand Up @@ -238,6 +239,25 @@ public void onMessage(WebSocket socket, String message) {
}
break;
}
case "SETDATA": {
response.put("command", "setData");
response.put("success", false);
Machine machine = machines.get(socket);
if (machine != null) {
JSONObject obj = new JSONObject();
try {
machine.getCurrentContext().execute(new Action(root.getString("action")));
obj.put("result", "ok");
response.put("success", true);
} catch (Exception e) {
logger.error(e.getMessage());
sendIssue(socket, e.getMessage());
}
} else {
response.put("message", "The GraphWalker state machine is not initiated. Is a model loaded, and started?");
}
break;
}
case "GETMODEL": {
response.put("command", "getmodel");
response.put("success", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ private enum RX_STATE {
START,
GETNEXT,
GETDATA,
VISITEDELEMENT
SETDATA,
VISITEDELEMENT;
}

public boolean connected = false;
public RX_STATE rxState = RX_STATE.NONE;
public boolean cmd = false;
public String response = "";
public boolean hasNext = false;
private int port = 8887;
private String host = "localhost";
Expand Down Expand Up @@ -145,6 +147,17 @@ public void onMessage(String message) {
if (root.getBoolean("success")) {
cmd = true;
}
if (root.has("data")) {
response = root.getJSONObject("data").toString();
} else {
response = "";
}
break;
case "SETDATA":
rxState = RX_STATE.SETDATA;
if (root.getBoolean("success")) {
cmd = true;
}
break;
case "VISITEDELEMENT":
rxState = RX_STATE.VISITEDELEMENT;
Expand Down Expand Up @@ -267,9 +280,16 @@ public boolean hasNext() {
/**
* Asks the machine to return all data from the current model context.
*/
public void getData() {
public String getData() {
logger.debug("Get data");
client.wsc.send("{ command: \"getData\"}");
wait(client, RX_STATE.GETDATA);
return client.response;
}

public void setData(String action) {
logger.debug("Set data as an action: " + action);
client.wsc.send("{ command: \"setData\", action: \"" + action + "\"}");
wait(client, RX_STATE.SETDATA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@

import java.io.IOException;
import java.nio.file.Paths;

import netscape.javascript.JSObject;
import org.graphwalker.core.generator.SingletonRandomGenerator;
import org.graphwalker.core.machine.ExecutionContext;
import org.graphwalker.java.annotation.BeforeExecution;
import org.graphwalker.java.annotation.GraphWalker;
import org.graphwalker.java.test.TestExecutionException;
import org.graphwalker.java.test.TestExecutor;
import org.java_websocket.WebSocket;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
Expand All @@ -52,9 +56,11 @@ public class WebSocketServerTest extends ExecutionContext implements WebSocketFl
private WebSocketClient client = new WebSocketClient();
private WebSocketServer server;
private int numberOfConnections = 0;
private int expectedValue = 54686327;

@BeforeExecution
public void startServer() throws Exception {
SingletonRandomGenerator.setSeed(222930684376058L);
server = new WebSocketServer(8887);
server.start();
}
Expand Down Expand Up @@ -107,7 +113,15 @@ public void v_EmptyMachine() {

@Override
public void e_GetData() {
client.getData();
String response = client.getData();
JSONObject jsonObject = new JSONObject(response);
Assert.assertEquals(expectedValue, jsonObject.getInt("value"));
}

@Override
public void e_SetData() {
expectedValue = SingletonRandomGenerator.nextInt();
client.setData("value=" + expectedValue);
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions graphwalker-websocket/src/test/resources/json/SmallModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"name": "SmallModel",
"generator": "random(edge_coverage(100))",
"startElementId": "e0",
"actions": [
"value=54686327;"
],
"vertices": [
{
"name": "v_VerifySomeAction",
Expand Down
Loading