Skip to content
Snippets Groups Projects
Commit 434db67a 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 trees

parent a780b9f3
No related branches found
No related tags found
No related merge requests found
package FaultTree;
import Heuristics.Heuristic;
import java.util.*;
/**
* A wrapper class to store some handy info on a fault tree.
*/
public class TreeWrapper {
//always exists
private String name;
private Map<String, FaultTree> nameMap;
private FaultTree rootNode;
//maybe exists
private Heuristic heuristic = null;
//additional representations, created when requested
private FaultTree[] nodesList = null;
private Map<Integer, FaultTree> intMap = null;
//--------------------- CONSTRUCTORS ---------------------
/**
* Create a tree wrapper given a mapping
* @param name A name for this fault tree
* @param treeMap a map of type String -> FaultTree
*
* Performs a search in the tree for the root node. If already known, please add it to the constructor.
*/
public TreeWrapper(String name, Map<String, FaultTree> treeMap){
this.name = name;
this.nameMap = treeMap;
this.rootNode = findRoot(treeMap);
}
/**
* Create a tree wrapper given a mapping and a root node
* @param name A name for this fault tree
* @param treeMap a map of type String -> FaultTree
* @param root the root node of the tree
*/
public TreeWrapper(String name, Map<String, FaultTree> treeMap, FaultTree root){
this.name = name;
this.nameMap = treeMap;
this.rootNode = root;
}
/**
* Create a tree wrapper given a mapping
* @param name A name for this fault tree
* @param treeMap a map of type String -> FaultTree
* @param heuristic A heuristic for this fault tree
*
* Performs a search in the tree for the root node. If already known, please add it to the constructor.
*/
public TreeWrapper(String name, Map<String, FaultTree> treeMap, Heuristic heuristic){
this.name = name;
this.nameMap = treeMap;
this.rootNode = findRoot(treeMap);
this.heuristic = heuristic;
}
/**
* Create a tree wrapper given a mapping and a root node
* @param name A name for this fault tree
* @param treeMap a map of type String -> FaultTree
* @param root the root node of the tree
* @param heuristic A heuristic for this fault tree
*/
public TreeWrapper(String name, Map<String, FaultTree> treeMap, FaultTree root, Heuristic heuristic){
this.name = name;
this.nameMap = treeMap;
this.rootNode = root;
this.heuristic = heuristic;
}
//--------------------- GETTERS ---------------------
/**
* @return The name of this fault tree
*/
public String getName(){
return this.name;
}
/**
* @return The root node of the tree
*/
public FaultTree getRoot(){
return this.rootNode;
}
/**
* @return the heuristic associated with this fault tree, if any.
*/
public Heuristic getHeuristic(){
return this.heuristic;
}
/**
* @return an array with array[node.id] == node
*/
public FaultTree[] asArray(){
if (nodesList == null){
this.makeNodesList();
}
return Arrays.copyOf(nodesList, nodesList.length);
}
/**
* @return a map with map.get(node.id) == node
*/
public Map<Integer, FaultTree> asIDMap(){
if (intMap == null){
this.makeIntMap();
}
return new HashMap<Integer, FaultTree>(intMap);
}
/**
* @return a map with map.get(node.name) == node
*/
public Map<String, FaultTree> asStringMap(){
return new HashMap<String, FaultTree>(nameMap);
}
//--------------------- SETTERS---------------------
public void setName(String name){
this.name = name;
}
public void setHeuristic(Heuristic heuristic){
this.heuristic = heuristic;
}
//--------------------- PRIVATE ---------------------
/**
* finds (a) root node of the tree. only returns first one found.
* @param tree a map of type String -> FaultTree
* @return Root node of a tree, or null if the tree has no root (every node has a parent).
*
*/
private static FaultTree findRoot(Map<String, FaultTree> tree){
for (FaultTree ft: tree.values()){
//faster than looping through the list, climbing the tree
int count =0;
while (!ft.getParents().isEmpty()) {
ft = ft.getParents().get(0);
count++;
if (count > tree.values().size()){
//protection against mall-formed trees.
return null;
}
}
return ft;
}
return null;
}
/**
* Construct an array where array[node.id] -> node
*/
private void makeNodesList(){
this.nodesList = new FaultTree[this.nameMap.size()];
for (FaultTree ft : this.nameMap.values()){
this.nodesList[ft.getID()]=ft;
}
}
/**
* Constructs a map of node.id -> node
*/
private void makeIntMap(){
this.intMap = new HashMap<Integer, FaultTree>(this.nameMap.size());
for (FaultTree ft : this.nameMap.values()){
this.intMap.put(ft.getID(), ft);
}
}
}
package IO;
import FaultTree.*;
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.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
public class Parser {
public static final String DIRECTORY_NAME = "???";
/**
* Function that parses a JSON file into an fault tree.
* @param filename the name of the file. It should located at CWD / *DIRECTORY_NAME* / *filename*
* @return A treeWrapper object containing the parsed fault tree.
*/
public static TreeWrapper parseFile(String filename) {
int nextID = 0;
String rootName = null;
JSONParser parser = new JSONParser();
Map<String, FaultTree> tree = new HashMap<>();
try {
//read file
Path path = FileSystems.getDefault().getPath(DIRECTORY_NAME, filename);
JSONArray nodes = (JSONArray) parser.parse(new FileReader(path.toFile()));
for (Object obj: nodes) {
JSONObject node = (JSONObject) obj;
String name = (String) node.get("name");
if (node.containsKey("gate")) {
// gate
JSONObject gate = (JSONObject) node.get("gate");
String type = (String) gate.get("type");
Type gateType;
if (type.equals("or")) gateType = Type.Or;
else if (type.equals("and")) gateType = Type.And;
else throw new ParserException("Unknown gate '"+type+"' found.");
JSONArray causes = (JSONArray) node.get("causes");
if (causes.size() != 2){
throw new ParserException("All gates should have 2 children. Found '"+name+"' with "+causes.size());
}
String left = (String) causes.get(0);
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(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 rightNode = tree.get(right);
tree.put(name, new IntermediateEvent(nextID++, name, gateType, leftNode, rightNode));
} else {
//leaf
tree.put(name, new BasicEvent(nextID++, name));
}
//topological order, so last node seen must be root
rootName = name;
}
if (rootName == null){
throw new ParserException("Empty tree, no nodes found.");
}
} catch (IOException | ParseException e) {
//ParseException is from JSONParser
//ParserException is defined locally
e.printStackTrace();
throw new ParserException(e.getMessage());
}
return new TreeWrapper(filename, tree, tree.get(rootName));
}
}
package IO;
public class ParserException extends RuntimeException{
public ParserException(String message){
super(message);
}
}
\ No newline at end of file
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