Skip to content
Snippets Groups Projects
Commit 6dcc1f45 authored by Leendertz, R.R. (Rens, Student M-CS)'s avatar Leendertz, R.R. (Rens, Student M-CS)
Browse files

Added parsing of simplified json trees2

parent 434db67a
No related branches found
No related tags found
No related merge requests found
...@@ -61,4 +61,9 @@ public class BasicEvent implements FaultTree { ...@@ -61,4 +61,9 @@ public class BasicEvent implements FaultTree {
public FaultTree copy() { public FaultTree copy() {
return new BasicEvent(ID, name); return new BasicEvent(ID, name);
} }
@Override
public String toString() {
return this.name+"[id="+this.ID+", type=basic]";
}
} }
...@@ -72,4 +72,9 @@ public class IntermediateEvent implements FaultTree { ...@@ -72,4 +72,9 @@ public class IntermediateEvent implements FaultTree {
public FaultTree copy() { public FaultTree copy() {
return new IntermediateEvent(ID, name, type, left.copy(), right.copy()); return new IntermediateEvent(ID, name, type, left.copy(), right.copy());
} }
@Override
public String toString(){
return this.name+"[ID="+this.ID+", type="+this.type+"]("+left.getName()+", "+right.getName()+")";
}
} }
...@@ -88,12 +88,23 @@ public class TreeWrapper { ...@@ -88,12 +88,23 @@ public class TreeWrapper {
return this.rootNode; return this.rootNode;
} }
public int getSize() {
return this.nameMap.size();
}
/** /**
* @return the heuristic associated with this fault tree, if any. * @return the heuristic associated with this fault tree, if any.
*/ */
public Heuristic getHeuristic(){ public Heuristic getHeuristic(){
return this.heuristic; return this.heuristic;
} }
public String toString(){
List<String> lines= new ArrayList<>();
lines.add("root="+this.rootNode.getName());
for (FaultTree ft : nameMap.values()){
lines.add(ft.toString());
}
return String.join("\n", lines);
}
/** /**
* @return an array with array[node.id] == node * @return an array with array[node.id] == node
...@@ -121,6 +132,7 @@ public class TreeWrapper { ...@@ -121,6 +132,7 @@ public class TreeWrapper {
public Map<String, FaultTree> asStringMap(){ public Map<String, FaultTree> asStringMap(){
return new HashMap<String, FaultTree>(nameMap); return new HashMap<String, FaultTree>(nameMap);
} }
//--------------------- SETTERS--------------------- //--------------------- SETTERS---------------------
public void setName(String name){ public void setName(String name){
this.name = name; this.name = name;
......
...@@ -15,7 +15,7 @@ import java.util.Map; ...@@ -15,7 +15,7 @@ import java.util.Map;
public class Parser { public class Parser {
public static final String DIRECTORY_NAME = "???"; public static final String DIRECTORY_NAME = "simpleTrees";
/** /**
* Function that parses a JSON file into an fault tree. * Function that parses a JSON file into an fault tree.
...@@ -37,7 +37,6 @@ public class Parser { ...@@ -37,7 +37,6 @@ public class Parser {
JSONObject node = (JSONObject) obj; JSONObject node = (JSONObject) obj;
String name = (String) node.get("name"); String name = (String) node.get("name");
if (node.containsKey("gate")) { if (node.containsKey("gate")) {
// gate // gate
JSONObject gate = (JSONObject) node.get("gate"); JSONObject gate = (JSONObject) node.get("gate");
...@@ -54,8 +53,8 @@ public class Parser { ...@@ -54,8 +53,8 @@ public class Parser {
} }
String left = (String) causes.get(0); String left = (String) causes.get(0);
String right = (String) causes.get(1); String right = (String) causes.get(1);
if (tree.containsKey(left)) throw new ParserException("Tree should be delivered in bottom-up fashion, found gate '"+name+"' with cause '"+left+"' which is not yet seen."); if (!tree.containsKey(left)) throw new ParserException("Tree should be delivered in bottom-up fashion, found gate '"+name+"' with cause '"+left+"' which is not yet seen.");
if (tree.containsKey(right)) throw new ParserException("Tree should be delivered in bottom-up fashion, found gate '"+name+"' with cause '"+right+"' which is not yet seen."); if (!tree.containsKey(right)) throw new ParserException("Tree should be delivered in bottom-up fashion, found gate '"+name+"' with cause '"+right+"' which is not yet seen.");
FaultTree leftNode = tree.get(left); FaultTree leftNode = tree.get(left);
FaultTree rightNode = tree.get(right); FaultTree rightNode = tree.get(right);
...@@ -81,4 +80,18 @@ public class Parser { ...@@ -81,4 +80,18 @@ public class Parser {
} }
public static void main(String[] args){
String[] filenames = new String[]{"safeRoadTrip.json", "ftaOverviewPaper_figure4.json"};
for (String filename : filenames) {
System.out.println("------------------------"+filename);
TreeWrapper tw = parseFile(filename);
if (tw.getSize() < 50) {
System.out.println(tw.toString());
} else {
System.out.println("<large tree> ("+tw.getSize()+" nodes)");
}
}
}
} }
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