Commit df616657 authored by SR-Lut3t1um's avatar SR-Lut3t1um

Added logic to add state info into Latex

parent 50e3a08f
Pipeline #89170 passed with stages
in 2 minutes and 56 seconds
......@@ -8,29 +8,39 @@ import java.util.List;
/** Class to model an Automaton */
public class Automaton {
private String name;
private final ArrayList<State> states;
private final ArrayList<State> states = new ArrayList<>();
private final User owner;
private final ArrayList<String> alphabet;
private State start;
public Automaton(String name, User owner) {
this.name = name;
states = new ArrayList<>();
this.owner = owner;
alphabet = new ArrayList<>();
}
public Automaton(String name, User owner, Collection<? extends String> alphabet) {
this.name = name;
states = new ArrayList<>();
this.owner = owner;
this.alphabet = new ArrayList<>();
this.alphabet.addAll(alphabet);
}
public void addState(State state) {
if (states.size() == 0) {
start = state;
}
states.add(state);
}
public void setStart(int i) {
start = states.get(i);
}
public State getStart() {
return start;
}
public void removeState(State state) {
states.remove(state);
}
......
......@@ -7,7 +7,7 @@ public class Latex {
public static String serializeAutomaton(Automaton obj) {
StringBuilder sb = new StringBuilder("\\begin{tikzpicture}");
for (State state : obj.getStates()) {
sb.append(serializeState(state));
sb.append(serializeState(state, obj.getStart()));
}
sb.append("\n \\path[->] ");
for (State state : obj.getStates()) {
......@@ -17,17 +17,20 @@ public class Latex {
return sb.toString();
}
private static String serializeState(State state) {
return "\n \\node[state]"
+ " ("
+ state.getId()
+ ") at ("
+ state.getX()
+ ","
+ state.getY()
+ ") {$"
+ state.getName()
+ "$};";
private static String serializeState(State state, State initial) {
StringBuilder sb = new StringBuilder("\n \\node[state");
if (state == initial) sb.append(",initial");
if (state.isAccepting()) sb.append(",accepting");
sb.append("] (")
.append(state.getId())
.append(") at (")
.append(state.getX())
.append(",")
.append(state.getY())
.append(") {$")
.append(state.getName())
.append("$};");
return sb.toString();
}
private static void serializeStateTransitions(State state, StringBuilder sb) {
......
......@@ -26,11 +26,11 @@ public class AutomatonResourceTest {
a.addState(new State("q2", 1, 2, 0, true));
a.addState(new State("q3", 2, 4, 0, true));
a.addPath(a.getStates().get(0), 0, a.getStates().get(1));
System.out.println(Latex.serializeAutomaton(a));
assert ("\\begin{tikzpicture}\n"
+ " \\node[state] (0) at (0,0) {$q1$};\n"
+ " \\node[state] (1) at (2,0) {$q2$};\n"
+ " \\node[state] (2) at (4,0) {$q3$};\n"
+ " \\node[state,initial,accepting] (0) at (0,0) {$q1$};\n"
+ " \\node[state,accepting] (1) at (2,0) {$q2$};\n"
+ " \\node[state,accepting] (2) at (4,0) {$q3$};\n"
+ " \\path[->] \n"
+ " (0) edge node [above] [align=center] {$a$} (1); \n"
+ "\\end{tikzpicture}")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment