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/MermaidNode.java

138 lines
3.9 KiB
Java

package dk.camelot64.kickc.passes.reports;
import dk.camelot64.kickc.model.Program;
import dk.camelot64.kickc.model.statements.Statement;
import dk.camelot64.kickc.model.statements.StatementCall;
import dk.camelot64.kickc.model.statements.StatementConditionalJump;
import javax.swing.text.AbstractDocument;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
/**
* Create nodes for mermaid report generation.
* Nodes are the final element in sub graphs or root.
* They are expressed with an Id and a Title. The Id represents the
* key to be interconnected through flows, while the Title is displayed
* inside the graphics box. The title is expressed in markdown.
* Each node can have statements, which is a list of Statements and are
* shown in the graphics box expressed in markdown.
*/
public class MermaidNode extends Mermaid {
public MermaidNode(String title) {
super(toID(title), title);
this.type = Type.SEQUENCE;
this.statements = new LinkedList<Statement>();
this.flows = new HashSet<MermaidFlow>();
}
public MermaidNode(String id, String title) {
super(id, title);
this.type = Type.SEQUENCE;
this.statements = new LinkedList<Statement>();
this.flows = new HashSet<MermaidFlow>();
}
public enum Type { SEQUENCE, CALL, BRANCH }
private Type type;
protected HashSet<MermaidFlow> flows;
private List<Statement> statements;
public List<Statement> getStatements() {
return statements;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public void setStatements(List<Statement> statements) {
this.statements = statements;
if(!statements.isEmpty()) {
Statement lastStatement = this.statements.get(this.statements.size() - 1);
if (lastStatement instanceof StatementCall) {
type = Type.CALL;
} else if (lastStatement instanceof StatementConditionalJump) {
type = Type.BRANCH;
} else {
type = Type.SEQUENCE;
}
} else {
type = Type.SEQUENCE;
}
}
public String toStringStatements() {
StringBuilder stmtString = new StringBuilder();
for(Statement statement : getStatements()) {
stmtString.append(statement).append("\n");
}
return stmtString.toString();
}
public String getText(Program program) {
StringBuilder text = new StringBuilder();
text.append(getId());
switch(getType()) {
case SEQUENCE -> {
text.append("[\"`");
}
case CALL -> {
text.append("[[\"`");
}
case BRANCH -> {
text.append("{{\"`");
}
}
text.append("**").append(getTitle()).append("**").append("\n\n");
for(Statement statement: getStatements()) {
text.append(statement.toString(program, program.getLog().isVerboseLiveRanges())).append("\n");
}
switch(getType()) {
case SEQUENCE -> {
text.append("`\"]\n");
}
case CALL -> {
text.append("`\"]]\n");
}
case BRANCH -> {
text.append("`\"}}\n");
}
}
return text.toString();
}
public HashSet<MermaidFlow> getFlows() {
return flows;
}
public void setFlows(HashSet<MermaidFlow> flows) {
this.flows = flows;
}
public void addFlow(MermaidFlow flow) {
this.flows.add(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.add(flow);
}
}