1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-06-03 07:29:37 +00:00
kickc/src/main/java/dk/camelot64/kickc/passes/reports/MermaidGraph.java

124 lines
3.5 KiB
Java

package dk.camelot64.kickc.passes.reports;
import dk.camelot64.kickc.model.Program;
import java.util.HashMap;
/**
* Create a flow chart for mermaid report generation.
* Flowcharts contain nodes or sub graphs and flows, and can either be located as a single entity in root, or embedded in other sub graphs.
*/
public class MermaidGraph extends Mermaid {
public enum Direction {
TOP_DOWN ("TD"),
BOTTOM_UP ("BU"),
LEFT_RIGHT("LR"),
RIGHT_LEFT("RL");
private final String direction;
Direction(String direction) {
this.direction = direction;
}
public String toString() {
return this.direction;
}
}
protected Direction direction;
protected HashMap<String, MermaidNode> nodes;
protected HashMap<String, MermaidFlow> flows;
protected HashMap<String, MermaidSubGraph> subGraphs;
public MermaidGraph(Direction direction) {
super(toID(""), "");
this.direction = direction;
this.nodes = new HashMap<String, MermaidNode>();
this.flows = new HashMap<String, MermaidFlow>();
this.subGraphs = new HashMap<String, MermaidSubGraph>();
}
public String getText(Program program) {
StringBuilder text = new StringBuilder();
// graph with direction
text.append("graph ").append(this.direction.toString()).append("\n");
// sub graphs
for(MermaidSubGraph subGraph : getSubGraphs().values()) {
text.append(subGraph.getText(program));
}
// nodes of the graph
for(MermaidNode node : nodes.values()) {
text.append(node.getText(program));
}
// flows to and from nodes
for(MermaidFlow flow : flows.values()) {
text.append(flow.getText(program));
}
return text.toString();
}
public HashMap<String, MermaidNode> getNodes() {
return nodes;
}
public void setNodes(HashMap<String, MermaidNode> nodes) {
this.nodes = nodes;
}
public MermaidNode addNode(MermaidNode node) {
this.nodes.putIfAbsent(node.id, node);
return node;
}
public MermaidNode addNode(String nodeName) {
MermaidNode node = new MermaidNode(nodeName);
this.nodes.putIfAbsent(node.id, node);
return node;
}
public HashMap<String, MermaidFlow> getFlows() {
return flows;
}
public void setFlows(HashMap<String, MermaidFlow> flows) {
this.flows = flows;
}
public void addFlow(MermaidFlow flow) {
this.flows.put(flow.getFrom().getId() + "::" + flow.getTo().getId(), flow);
}
public void addFlow(MermaidNode from, MermaidNode to, String title, MermaidFlow.Type type, MermaidFlow.Direction direction) {
MermaidFlow flow = new MermaidFlow(from, to, title, type, direction);
this.flows.put(from.getId() + "::" + to.getId(), flow);
}
public MermaidFlow getFlow(MermaidNode from, MermaidNode to) {
return this.flows.get(from.getId() + "::" + to.getId());
}
public void removeFlow(MermaidFlow flow) {
this.flows.remove(flow.getFrom().getId() + "::" + flow.getTo().getId());
}
public HashMap<String, MermaidSubGraph> getSubGraphs() {
return subGraphs;
}
public void setSubGraphs(HashMap<String, MermaidSubGraph> subGraphs) {
this.subGraphs = subGraphs;
}
public MermaidSubGraph addProcedureSubGraph(MermaidSubGraph subGraph) {
this.subGraphs.putIfAbsent(subGraph.id, subGraph);
return subGraph;
}
}