-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraphParserExample.java
70 lines (64 loc) · 2.87 KB
/
GraphParserExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import com.alexmerz.graphviz.*;
import com.alexmerz.graphviz.objects.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
public class GraphParserExample {
public static void main(String[] args) {
try {
Parser parser = new Parser();
FileReader reader = new FileReader(args[0]);
parser.parse(reader);
ArrayList<Graph> graphs = parser.getGraphs();
ArrayList<Graph> subGraphs = graphs.get(0).getSubgraphs();
for(Graph g : subGraphs){
System.out.printf("id = %s\n",g.getId().getId());
ArrayList<Graph> subGraphs1 = g.getSubgraphs();
for (Graph g1 : subGraphs1){
ArrayList<Node> nodesLoc = g1.getNodes(false);
Node nLoc = nodesLoc.get(0);
System.out.printf("\tid = %s, name = %s\n",g1.getId().getId(), nLoc.getId().getId());
ArrayList<Graph> subGraphs2 = g1.getSubgraphs();
for (Graph g2 : subGraphs2) {
System.out.printf("\t\tid = %s\n", g2.getId().getId());
ArrayList<Node> nodesEnt = g2.getNodes(false);
for (Node nEnt : nodesEnt) {
System.out.printf("\t\t\tid = %s, description = %s\n", nEnt.getId().getId(), nEnt.getAttribute("description"));
}
}
}
ArrayList<Edge> edges = g.getEdges();
for (Edge e : edges){
System.out.printf("Path from %s to %s\n", e.getSource().getNode().getId().getId(), e.getTarget().getNode().getId().getId());
}
}
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe);
} catch (com.alexmerz.graphviz.ParseException pe) {
System.out.println(pe);
}
}
public static void readJSON(){
String filepath = "data/basic-actions.json";
JSONObject jObj = null;
JSONParser parser = new JSONParser();
try{
jObj = (JSONObject) parser.parse(new FileReader(filepath));
} catch (IOException | ParseException e) {
e.printStackTrace();
}
JSONArray array = (JSONArray) jObj.get("actions");
for(var v : array){
JSONArray consumed = (JSONArray) ((JSONObject)v).get("consumed");
JSONArray subjects = (JSONArray) ((JSONObject)v).get("subjects");
JSONArray triggers = (JSONArray) ((JSONObject)v).get("triggers");
String narration = (String)((JSONObject)v).get("narration");
System.out.println(v);
}
}
}