Commit 3912cd7b authored by Tobias Liese's avatar Tobias Liese

WIP

parent c96fce74
Pipeline #88960 passed with stages
in 2 minutes and 59 seconds
......@@ -4,8 +4,8 @@ plugins {
}
repositories {
mavenLocal()
mavenCentral()
mavenLocal()
mavenCentral()
}
dependencies {
......
......@@ -9,6 +9,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.un1qu3.models.Automaton;
import org.un1qu3.models.User;
@Path("/automaton")
public class AutomatonResource {
......@@ -21,6 +22,6 @@ public class AutomatonResource {
@Path("{name}")
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get(@PathParam("name") String name) {
return automaton.data("automaton", new Automaton(name));
return automaton.data("automaton", new Automaton(name, new User("")));
}
}
package org.un1qu3.models;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** Class to model an Automaton */
public class Automaton {
......@@ -8,17 +10,29 @@ public class Automaton {
private final ArrayList<State> states;
private final User owner;
public Automaton(String name) {
public Automaton(String name, User owner) {
this.name = name;
states = new ArrayList<>();
owner = new User("Hello");
this.owner = owner;
}
public void addState(State state) {
states.add(state);
}
public void removeState(State state) {
states.remove(state);
}
public User getOwner() {
return owner;
}
public String getName() {
return name;
}
public ArrayList<State> getStates() {
return states;
public List<State> getStates() {
return Collections.unmodifiableList(states);
}
}
package org.un1qu3.models;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/** This Class models a State from an {@link Automaton} */
public class State {
/** is this State accepting? default: false */
private boolean isAccepting;
/** How do we want to name our state? */
private String name;
/** How do we want to identify our state? */
private int id;
/** Where are we? */
private int x, y;
/** Where can we go from here? */
private final Map<String, ArrayList<State>> paths = new HashMap<>();
/**
* Let's assume this State is not starting nor accepting
* Let's assume this State is not accepting
*
* @param name the name
* @param x x position
* @param y y position
*/
public State(String name, int x, int y) {
public State(String name, int id, int x, int y) {
this.name = name;
this.x = x;
this.y = y;
this.id = id;
isAccepting = false;
}
......@@ -30,11 +39,12 @@ public class State {
* @param x x position
* @param y y position
*/
public State(String name, int x, int y, boolean isAccepting) {
public State(String name, int id, int x, int y, boolean isAccepting) {
this.name = name;
this.x = x;
this.y = y;
this.isAccepting = isAccepting;
this.id = id;
}
// getter and setter love <3 god damn I really should use kotlin...
......@@ -70,11 +80,19 @@ public class State {
this.y = y;
}
/**
* Let's rape the toString method to return the State in a Latex representation
*
* @return this as latex node
*/
public void addPath(String key, State state) {
if (!paths.containsKey(key)) paths.put(key, new ArrayList<>());
paths.get(key).add(state);
}
public Map<String, ArrayList<State>> getPaths() {
return paths;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "State{"
......
package org.un1qu3.serializer;
import org.un1qu3.models.Automaton;
import org.un1qu3.models.State;
public class Latex {
public static String serializeAutomaton(Automaton obj) {
StringBuilder sb = new StringBuilder("\\begin{tikzpicture}");
for (State state : obj.getStates()) {
sb.append(serializeState(state));
}
for (State state : obj.getStates()) {
serializeStateTransitions(state, sb);
}
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() + "$};";
}
private static void serializeStateTransitions(State state, StringBuilder sb) {
for (var path : state.getPaths().entrySet()) {
for (var destination : path.getValue()) {
sb.append("\"\\n (\"")
.append(state.getId())
.append(") edge node [above] [align=center] {$")
.append(path.getKey())
.append("$} (")
.append(destination)
.append(")");
}
}
}
public static Object deSerialzeObject(String latex) {
return null;
}
}
package org.un1qu3;
import static io.restassured.RestAssured.given;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import org.un1qu3.models.Automaton;
import org.un1qu3.models.State;
import org.un1qu3.models.User;
import org.un1qu3.serializer.Latex;
@QuarkusTest
public class AutomatonResourceTest {
@Test
public void testAutomatonEndpoint() {
given().when().get("/automaton/qwe").then().statusCode(200);
}
@Test
public void testHelloEndpoint() {
given()
.when().get("/automaton/qwe")
.then()
.statusCode(200);
}
}
\ No newline at end of file
@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));
}
}
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