Commit 50e3a08f authored by SR-Lut3t1um's avatar SR-Lut3t1um

Added more features

parent 3912cd7b
Pipeline #89166 passed with stages
in 2 minutes and 57 seconds
package org.un1qu3.models;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
......@@ -9,11 +10,21 @@ public class Automaton {
private String name;
private final ArrayList<State> states;
private final User owner;
private final ArrayList<String> alphabet;
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) {
......@@ -32,6 +43,10 @@ public class Automaton {
return name;
}
public void addPath(State from, int index, State to) {
from.addPath(alphabet.get(index), to);
}
public List<State> getStates() {
return Collections.unmodifiableList(states);
}
......
......@@ -80,7 +80,7 @@ public class State {
this.y = y;
}
public void addPath(String key, State state) {
protected void addPath(String key, State state) {
if (!paths.containsKey(key)) paths.put(key, new ArrayList<>());
paths.get(key).add(state);
}
......
......@@ -9,27 +9,36 @@ public class Latex {
for (State state : obj.getStates()) {
sb.append(serializeState(state));
}
sb.append("\n \\path[->] ");
for (State state : obj.getStates()) {
serializeStateTransitions(state, sb);
}
sb.append("; \n \\end{tikzpicture}");
sb.append("; \n\\end{tikzpicture}");
return sb.toString();
}
private static String serializeState(State state) {
// @todo fix pos
return "\n \\node[state]" + "" + " (" + state.getId() + ") " + " {$" + state.getName() + "$};";
return "\n \\node[state]"
+ " ("
+ state.getId()
+ ") at ("
+ state.getX()
+ ","
+ state.getY()
+ ") {$"
+ state.getName()
+ "$};";
}
private static void serializeStateTransitions(State state, StringBuilder sb) {
for (var path : state.getPaths().entrySet()) {
for (var destination : path.getValue()) {
sb.append("\"\\n (\"")
sb.append("\n (")
.append(state.getId())
.append(") edge node [above] [align=center] {$")
.append(path.getKey())
.append("$} (")
.append(destination)
.append(destination.getId())
.append(")");
}
}
......
......@@ -3,6 +3,7 @@ package org.un1qu3;
import static io.restassured.RestAssured.given;
import io.quarkus.test.junit.QuarkusTest;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
import org.un1qu3.models.Automaton;
import org.un1qu3.models.State;
......@@ -18,10 +19,21 @@ public class AutomatonResourceTest {
@Test
public void testAutomaton() {
Automaton a = new Automaton("test", new User("Test"));
a.addState(new State("q1", 0, 10, 10, true));
a.addState(new State("q2", 1, 10, 10, true));
a.addState(new State("q3", 2, 10, 10, true));
System.out.println(Latex.serializeAutomaton(a));
ArrayList<String> alphabet = new ArrayList<>();
alphabet.add("a");
Automaton a = new Automaton("test", new User("Test"), alphabet);
a.addState(new State("q1", 0, 0, 0, true));
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));
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"
+ " \\path[->] \n"
+ " (0) edge node [above] [align=center] {$a$} (1); \n"
+ "\\end{tikzpicture}")
.equals(Latex.serializeAutomaton(a));
}
}
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