Skip to content

Commit

Permalink
Implement loading of graphs from .json
Browse files Browse the repository at this point in the history
Pass the filename of a .json file as first argument to Runtime
  • Loading branch information
jonnor committed Aug 25, 2014
1 parent c393770 commit 4fb10ac
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/main/java/com/jpmorrsn/fbp/engine/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,36 @@ public Definition() {
iips = new ArrayList<IIP>();
}

public void loadFromJson(String json) throws JSONException {
JSONTokener tokener = new JSONTokener(json);
JSONObject root = new JSONObject(tokener);

// Nodes
JSONObject processes = root.getJSONObject("processes");
for (String name : JSONObject.getNames(processes)) {
JSONObject node = processes.getJSONObject(name);
addNode(name, node.getString("component"));
}

// Connections
JSONArray connections = root.getJSONArray("connections");
for (int i=0; i<connections.length(); i++) {
JSONObject conn = connections.getJSONObject(i);
JSONObject src = conn.optJSONObject("src");
JSONObject tgt = conn.getJSONObject("tgt");
if (src == null) {
addInitial(tgt.getString("process"), tgt.getString("port"),
conn.getString("data")
);
} else {
addEdge(src.getString("process"), src.getString("port"),
tgt.getString("process"), tgt.getString("port")
);
}
}

}

public void addNode(String id, String component) {
this.nodes.put(id, component);
}
Expand Down Expand Up @@ -303,14 +333,14 @@ protected void define() {

// Connect
for (Definition.Connection conn : mDefinition.connections) {
connect(component(conn.srcNode), port(conn.srcPort),
component(conn.tgtNode), port(conn.tgtPort));
connect(component(conn.srcNode), port(conn.srcPort.toUpperCase()),
component(conn.tgtNode), port(conn.tgtPort.toUpperCase()));
}

// Add IIPs
for (Definition.IIP iip : mDefinition.iips) {
System.out.println("addInitial: " + iip.tgtNode + " " + iip.tgtPort + " " + iip.data);
initialize(iip.data, component(iip.tgtNode), port(iip.tgtPort));
initialize(iip.data, component(iip.tgtNode), port(iip.tgtPort.toUpperCase()));
}

}
Expand Down Expand Up @@ -566,12 +596,13 @@ public static void main(final String[] argv) throws Exception {

String p = "fbp.json"; // XXX: relative to cwd
Runtime.ComponentLibrary lib = new Runtime.ComponentLibrary(p);
Definition def = new Definition();
def.addNode("generate", "GenerateTestData");
def.addNode("write", "WriteToConsole");
def.addEdge("generate", "OUT", "write", "IN");
def.addInitial("generate", "COUNT", "10");
RuntimeNetwork.startNetwork(lib, def);

if (argv.length == 1) {
String graphPath = argv[0];
Definition def = new Definition();
def.loadFromJson(Runtime.Util.stringFromStream(new FileInputStream(graphPath)));
RuntimeNetwork.startNetwork(lib, def);
}

FlowhubApi api = FlowhubApi.create();

Expand Down

0 comments on commit 4fb10ac

Please sign in to comment.