diff --git a/src/main/java/de/tudresden/inf/st/mg/LoadWorldParser.java b/src/main/java/de/tudresden/inf/st/mg/LoadWorldParser.java index e7a17ec6ebabaa3b95f02399be6db0949314d8fa..c460a863fc85647121be220156179e6a4ceaaed6 100644 --- a/src/main/java/de/tudresden/inf/st/mg/LoadWorldParser.java +++ b/src/main/java/de/tudresden/inf/st/mg/LoadWorldParser.java @@ -4,15 +4,10 @@ import de.tudresden.inf.st.mg.common.MotionGrammarParser; import de.tudresden.inf.st.mg.jastadd.model.*; - public final class LoadWorldParser extends MotionGrammarParser<T> { private static final String WORLD = "World"; - private Load peekedLoad_ = null; - private Unload peekedUnload_ = null; - private Full peekedFull_ = null; - public LoadWorldParser(World world) { contexts_.put(WORLD, world); } @@ -23,49 +18,10 @@ public final class LoadWorldParser extends MotionGrammarParser<T> { @Override protected void parse(ASTNode<?> parent, int index) throws ParseException { - T result; - - // try to parse a T - if (peekFull()) { - result = new T2(); - result.parser = this; - parent.setChild(result, index); - parseT2(parent, index); - } else if (peekLoad()) { - result = new T1(); - result.parser = this; - parent.setChild(result, index); - parseT1(parent, index); - } else { - throw new ParseException("T", Load.type(), Full.type()); - } - - // semantic action for T - result.action(getWorld()); - printAST("parseT", result); + parseT(parent, index); } - private void parseT1(ASTNode<?> parent, int index) throws ParseException { - T1 result = (T1) parent.getChild(index); - - parseLoad(result, 0); - parse(result, 1); - parseUnload(result, 2); - - // semantic action for T1 - result.action(getWorld()); - printAST("parseT1", result); - } - - private void parseT2(ASTNode<?> parent, int index) throws ParseException { - T2 result = (T2) parent.getChild(index); - - parseFull(result, 0); - - // semantic action for T2 - result.action(getWorld()); - printAST("parseT2", result); - } + private Load peekedLoad_ = null; private boolean peekLoad() { peekedLoad_ = getWorld().parseLoad(); @@ -87,11 +43,13 @@ public final class LoadWorldParser extends MotionGrammarParser<T> { parent.setChild(result, index); - // semantic action for Unload + // semantic action for Load result.action(getWorld()); printAST("parseLoad", result); } + private Unload peekedUnload_ = null; + private boolean peekUnload() { peekedUnload_ = getWorld().parseUnload(); return peekedUnload_ != null; @@ -117,12 +75,14 @@ public final class LoadWorldParser extends MotionGrammarParser<T> { printAST("parseUnload", result); } + private Full peekedFull_ = null; + private boolean peekFull() { peekedFull_ = getWorld().parseFull(); return peekedFull_ != null; } - void parseFull(ASTNode<?> parent, int index) throws ParseException { + private void parseFull(ASTNode<?> parent, int index) throws ParseException { Full result; if (peekedFull_ != null) { @@ -137,8 +97,55 @@ public final class LoadWorldParser extends MotionGrammarParser<T> { parent.setChild(result, index); - // semantic action for Unload + // semantic action for Full result.action(getWorld()); printAST("parseFull", result); } + + private void parseT(ASTNode<?> parent, int index) throws ParseException { + T result; + + // try the different types of T + if (peekFull()) { + result = new T2(); + result.parser = this; + parent.setChild(result, index); + parseT2(parent, index); + } else if (peekLoad()) { + result = new T1(); + result.parser = this; + parent.setChild(result, index); + parseT1(parent, index); + } else { + throw new ParseException("T", Full.type(), Load.type()); + } + + + // semantic action for T + result.action(getWorld()); + printAST("parseT", result); + } + + private void parseT1(ASTNode<?> parent, int index) throws ParseException { + T1 result = (T1) parent.getChild(index); + + parseLoad(result, 0); + parseT(result, 1); + parseUnload(result, 2); + + // semantic action for T1 + result.action(getWorld()); + printAST("parseT1", result); + } + + private void parseT2(ASTNode<?> parent, int index) throws ParseException { + T2 result = (T2) parent.getChild(index); + + parseFull(result, 0); + + // semantic action for T2 + result.action(getWorld()); + printAST("parseT2", result); + } + } diff --git a/src/main/java/de/tudresden/inf/st/mg/Main.java b/src/main/java/de/tudresden/inf/st/mg/Main.java index 5faa0d5a082753a3bd0d2428e9ebeae69b3d96cd..872da41b3fdb95c189ec5f5b4010f7cc97681f0b 100644 --- a/src/main/java/de/tudresden/inf/st/mg/Main.java +++ b/src/main/java/de/tudresden/inf/st/mg/Main.java @@ -3,20 +3,15 @@ package de.tudresden.inf.st.mg; import de.tudresden.inf.st.mg.common.MotionGrammarConfig; import de.tudresden.inf.st.mg.common.MotionGrammarParser; import de.tudresden.inf.st.mg.jastadd.model.RobotWorld; -import de.tudresden.inf.st.mg.jastadd.model.Tidy; -import de.tudresden.inf.st.mg.jastadd.model.World; -import java.io.File; import java.io.IOException; import java.net.URISyntaxException; -import java.nio.file.Files; import java.nio.file.Path; -import java.util.Comparator; -import java.util.Random; public class Main { public static final Path TIDY_AST_DIAGRAM_DIR = Path.of("src", "gen", "resources", "diagrams", "parsing", "tidy"); + /** * Runs the parser on the actual robot. For this to work, a robot controller must be connected to an MQTT server running * on localhost:1883 @@ -67,7 +62,7 @@ public class Main { } try { - Thread.sleep(100000*1000); + Thread.sleep(100000 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/java/de/tudresden/inf/st/mg/RobotParser.java b/src/main/java/de/tudresden/inf/st/mg/RobotParser.java index 2d77f1190dfc6b1978fb81f8a38c35feb885b106..b59cf427cc87af07416f3ff9e2fdfffb02c6d508 100644 --- a/src/main/java/de/tudresden/inf/st/mg/RobotParser.java +++ b/src/main/java/de/tudresden/inf/st/mg/RobotParser.java @@ -16,184 +16,94 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { return (World) contexts_.get(WORLD); } - private EmptyTable peekedEmptyTable_ = null; - private NotEmptyTable peekedNotEmptyTable_ = null; - private ObjectAtWrongPlace peekedObjectAtWrongPlace_ = null; - private RobotIsReadyToPickToken peekedRobotIsReadyToPickToken_ = null; - private RobotIsNotReadyToPickToken peekedRobotIsNotReadyToPickToken_ = null; - private RobotIsReadyToDropToken peekedRobotIsReadyToDropToken_ = null; - private RobotIsNotReadyToDropToken peekedRobotIsNotReadyToDropToken_ = null; - private RobotIsIdle peekedRobotIsIdle_ = null; - private RobotIsNotIdle peekedRobotIsNotIdle_ = null; - private RobotHasItemAttached peekedRobotHasItemAttached_ = null; - private RobotHasNoItemAttached peekedRobotHasNoItemAttached_ = null; - private Wait peekedWait_ = null; - private RightPlace peekedRightPlace_ = null; - @Override protected void parse(ASTNode<?> parent, int index) throws ParseException { - Tidy result = new Tidy(); - result.parser = this; - parent.setChild(result, index); - - while (true) { - peekEmptyTable(); - if (peekedEmptyTable_ != null) { - int i = result.getNumWaitForFullTable(); - result.addWaitForFullTable(null); - parseWaitForFullTable(result.getWaitForFullTableList(), i); - } else { - break; - } - } - - while (true) { - peekObjectAtWrongPlace(result); - if (peekedObjectAtWrongPlace_ != null) { - int i = result.getNumMoveObjectToCorrectPlace(); - result.addMoveObjectToCorrectPlace(null); - parseMoveObjectToCorrectPlace(result.getMoveObjectToCorrectPlaceList(), i); - } else { - break; - } - } - - while (true) { - peekNotEmptyTable(); - if (peekedNotEmptyTable_ != null) { - int i = result.getNumWaitForEmptyTable(); - result.addWaitForEmptyTable(null); - parseWaitForEmptyTable(result.getWaitForEmptyTableList(), i); - } else { - break; - } - } - - parseEmptyTable(result, 3); - - // semantic action for Tidy - result.action(getWorld()); - printAST("parseTidy", result); + parseTidy(parent, index); } - private void parseMoveObjectToCorrectPlace(ASTNode<?> parent, int index) throws ParseException { - MoveObjectToCorrectPlace result = new MoveObjectToCorrectPlace(); - result.parser = this; - parent.setChild(result, index); - - parseObjectAtWrongPlace(result, 0, parent.containingTidy()); - parsePickUpObject(result, 1); - parseDropObjectAtRightPlace(result, 2); + private EmptyTable peekedEmptyTable_ = null; - // semantic action for MoveObjectToCorrectPlace - result.action(getWorld()); - printAST("parseMoveObjectToCorrectPlace", result); + private boolean peekEmptyTable() { + peekedEmptyTable_ = getWorld().parseEmptyTable(); + return peekedEmptyTable_ != null; } - private void parseWaitForFullTable(ASTNode<?> parent, int index) throws ParseException { - WaitForFullTable result = new WaitForFullTable(); - result.parser = this; - parent.setChild(result, index); - - parseEmptyTable(result, 0); - parseWait(result, 1); + private void parseEmptyTable(ASTNode<?> parent, int index) throws ParseException { + EmptyTable result; - // semantic action for WaitForFullTable - result.action(getWorld()); - printAST("parseWaitForFullTable", result); - } + if (peekedEmptyTable_ != null) { + result = peekedEmptyTable_; + peekedEmptyTable_ = null; // TODO check if all peeked values are actually parsed afterwards + } else { + result = getWorld().parseEmptyTable(); + if (result == null) { + throw new ParseException(EmptyTable.type()); + } + } - private void parseWaitForEmptyTable(ASTNode<?> parent, int index) throws ParseException { - WaitForEmptyTable result = new WaitForEmptyTable(); - result.parser = this; parent.setChild(result, index); - parseNotEmptyTable(result, 0); - parseWait(result, 1); - - // semantic action for WaitForEmptyTable + // semantic action for EmptyTable result.action(getWorld()); - printAST("parseWaitForEmptyTable", result); + printAST("parseEmptyTable", result); } - private void parseDropObjectAtRightPlace(ASTNode<?> parent, int index) throws ParseException { - DropObjectAtRightPlace result = new DropObjectAtRightPlace(); - result.parser = this; - parent.setChild(result, index); - - parseRobotIsReadyToDrop(result, 0); - parseRightPlace(result, 1); + private NotEmptyTable peekedNotEmptyTable_ = null; - // semantic action for DropObjectAtRightPlace - result.action(getWorld()); - printAST("parseDropObjectAtRightPlace", result); + private boolean peekNotEmptyTable() { + peekedNotEmptyTable_ = getWorld().parseNotEmptyTable(); + return peekedNotEmptyTable_ != null; } - private void parseRightPlace(ASTNode<?> parent, int index) throws ParseException { - RightPlace result; + private void parseNotEmptyTable(ASTNode<?> parent, int index) throws ParseException { + NotEmptyTable result; - if (peekedRightPlace_ != null) { - result = peekedRightPlace_; - peekedRightPlace_ = null; // TODO check if all peeked values are actually parsed afterwards + if (peekedNotEmptyTable_ != null) { + result = peekedNotEmptyTable_; + peekedNotEmptyTable_ = null; // TODO check if all peeked values are actually parsed afterwards } else { - result = getWorld().parseRightPlace(parent.object()); + result = getWorld().parseNotEmptyTable(); if (result == null) { - throw new ParseException(RightPlace.type()); + throw new ParseException(NotEmptyTable.type()); } } parent.setChild(result, index); - // semantic action for RightPlace + // semantic action for NotEmptyTable result.action(getWorld()); - printAST("parseRightPlace", result); + printAST("parseNotEmptyTable", result); } - private void parsePickUpObject(ASTNode<?> parent, int index) throws ParseException { - PickUpObject result = new PickUpObject(); - result.parser = this; - parent.setChild(result, index); - - parseRobotIsReadyToPick(result, 0); + private ObjectAtWrongPlace peekedObjectAtWrongPlace_ = null; - // semantic action for T1 - result.action(getWorld()); - printAST("parsePickUpObject", result); + private boolean peekObjectAtWrongPlace(Tidy context) { + peekedObjectAtWrongPlace_ = getWorld().parseObjectAtWrongPlace(context); + return peekedObjectAtWrongPlace_ != null; } - private void parseRobotIsReadyToPick(ASTNode<?> parent, int index) throws ParseException { - RobotIsReadyToPick result; + private void parseObjectAtWrongPlace(ASTNode<?> parent, int index, Tidy context) throws ParseException { + ObjectAtWrongPlace result; - // try to parse a RobotIsReadyToPickToken - if (peekRobotIsReadyToPickToken()) { - result = new RobotIsReallyReadyToPick(); - result.parser = this; - parent.setChild(result, index); - parseRobotIsReallyReadyToPick(parent, index); - } else if (peekRobotIsNotReadyToPickToken()) { - result = new RobotIsNotReadyToPick(); - result.parser = this; - parent.setChild(result, index); - parseRobotIsNotReadyToPick(parent, index); + if (peekedObjectAtWrongPlace_ != null) { + result = peekedObjectAtWrongPlace_; + peekedObjectAtWrongPlace_ = null; // TODO check if all peeked values are actually parsed afterwards } else { - throw new ParseException("RobotIsReadyToPick", RobotIsReadyToPickToken.type(), RobotIsNotIdle.type()); + result = getWorld().parseObjectAtWrongPlace(context); + if (result == null) { + throw new ParseException(ObjectAtWrongPlace.type()); + } } - // semantic action for T1 - result.action(getWorld()); - printAST("parseRobotIsReadyToPick", result); - } - - private void parseRobotIsReallyReadyToPick(ASTNode<?> parent, int index) throws ParseException { - RobotIsReallyReadyToPick result = (RobotIsReallyReadyToPick) parent.getChild(index); - - parseRobotIsReadyToPickToken(result, 0); + parent.setChild(result, index); - // semantic action for T1 + // semantic action for ObjectAtWrongPlace result.action(getWorld()); - printAST("parseRobotIsReallyReadyToPick", result); + printAST("parseObjectAtWrongPlace", result); } + private RobotIsReadyToPickToken peekedRobotIsReadyToPickToken_ = null; + private boolean peekRobotIsReadyToPickToken() { peekedRobotIsReadyToPickToken_ = getWorld().parseRobotIsReadyToPickToken(); return peekedRobotIsReadyToPickToken_ != null; @@ -219,6 +129,8 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { printAST("parseRobotIsReadyToPickToken", result); } + private RobotIsNotReadyToPickToken peekedRobotIsNotReadyToPickToken_ = null; + private boolean peekRobotIsNotReadyToPickToken() { peekedRobotIsNotReadyToPickToken_ = getWorld().parseRobotIsNotReadyToPickToken(); return peekedRobotIsNotReadyToPickToken_ != null; @@ -244,52 +156,7 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { printAST("parseRobotIsNotReadyToPickToken", result); } - private void parseRobotIsNotReadyToPick(ASTNode<?> parent, int index) throws ParseException { - RobotIsNotReadyToPick result = (RobotIsNotReadyToPick) parent.getChild(index); - - parseRobotIsNotReadyToPickToken(result, 0); - parseWait(result, 1); - parseRobotIsReadyToPick(result, 2); - - // semantic action for RobotIsNotReadyToPick - result.action(getWorld()); - printAST("parseRobotIsNotReadyToPick", result); - } - - // ---- - - private void parseRobotIsReadyToDrop(ASTNode<?> parent, int index) throws ParseException { - RobotIsReadyToDrop result; - - // try to parse a RobotIsReadyToDropToken - if (peekRobotIsReadyToDropToken()) { - result = new RobotIsReallyReadyToDrop(); - result.parser = this; - parent.setChild(result, index); - parseRobotIsReallyReadyToDrop(parent, index); - } else if (peekRobotIsNotReadyToDropToken()) { - result = new RobotIsNotReadyToDrop(); - result.parser = this; - parent.setChild(result, index); - parseRobotIsNotReadyToDrop(parent, index); - } else { - throw new ParseException("RobotIsReadyToDrop", RobotIsReadyToDropToken.type(), RobotIsNotIdle.type()); - } - - // semantic action for T1 - result.action(getWorld()); - printAST("parseRobotIsReadyToDrop", result); - } - - private void parseRobotIsReallyReadyToDrop(ASTNode<?> parent, int index) throws ParseException { - RobotIsReallyReadyToDrop result = (RobotIsReallyReadyToDrop) parent.getChild(index); - - parseRobotIsReadyToDropToken(result, 0); - - // semantic action for RobotIsReallyReadyToDrop - result.action(getWorld()); - printAST("parseRobotIsReallyReadyToDrop", result); - } + private RobotIsReadyToDropToken peekedRobotIsReadyToDropToken_ = null; private boolean peekRobotIsReadyToDropToken() { peekedRobotIsReadyToDropToken_ = getWorld().parseRobotIsReadyToDropToken(); @@ -316,6 +183,8 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { printAST("parseRobotIsReadyToDropToken", result); } + private RobotIsNotReadyToDropToken peekedRobotIsNotReadyToDropToken_ = null; + private boolean peekRobotIsNotReadyToDropToken() { peekedRobotIsNotReadyToDropToken_ = getWorld().parseRobotIsNotReadyToDropToken(); return peekedRobotIsNotReadyToDropToken_ != null; @@ -341,19 +210,34 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { printAST("parseRobotIsNotReadyToDropToken", result); } - private void parseRobotIsNotReadyToDrop(ASTNode<?> parent, int index) throws ParseException { - RobotIsNotReadyToDrop result = (RobotIsNotReadyToDrop) parent.getChild(index); + private RobotIsIdle peekedRobotIsIdle_ = null; - parseRobotIsNotReadyToDropToken(result, 0); - parseWait(result, 1); - parseRobotIsReadyToDrop(result, 2); + private boolean peekRobotIsIdle() { + peekedRobotIsIdle_ = getWorld().parseRobotIsIdle(); + return peekedRobotIsIdle_ != null; + } - // semantic action for RobotIsNotReadyToDrop + private void parseRobotIsIdle(ASTNode<?> parent, int index) throws ParseException { + RobotIsIdle result; + + if (peekedRobotIsIdle_ != null) { + result = peekedRobotIsIdle_; + peekedRobotIsIdle_ = null; // TODO check if all peeked values are actually parsed afterwards + } else { + result = getWorld().parseRobotIsIdle(); + if (result == null) { + throw new ParseException(RobotIsIdle.type()); + } + } + + parent.setChild(result, index); + + // semantic action for RobotIsIdle result.action(getWorld()); - printAST("parseRobotIsNotReadyToDrop", result); + printAST("parseRobotIsIdle", result); } - // ---- + private RobotIsNotIdle peekedRobotIsNotIdle_ = null; private boolean peekRobotIsNotIdle() { peekedRobotIsNotIdle_ = getWorld().parseRobotIsNotIdle(); @@ -380,98 +264,290 @@ public final class RobotParser extends MotionGrammarParser<Tidy> { printAST("parseRobotIsNotIdle", result); } - private void parseWait(ASTNode<?> parent, int index) throws ParseException { - Wait result; + private RobotHasItemAttached peekedRobotHasItemAttached_ = null; + + private boolean peekRobotHasItemAttached() { + peekedRobotHasItemAttached_ = getWorld().parseRobotHasItemAttached(); + return peekedRobotHasItemAttached_ != null; + } + + private void parseRobotHasItemAttached(ASTNode<?> parent, int index) throws ParseException { + RobotHasItemAttached result; - if (peekedWait_ != null) { - result = peekedWait_; - peekedWait_ = null; // TODO check if all peeked values are actually parsed afterwards + if (peekedRobotHasItemAttached_ != null) { + result = peekedRobotHasItemAttached_; + peekedRobotHasItemAttached_ = null; // TODO check if all peeked values are actually parsed afterwards } else { - result = getWorld().parseWait(parent.time()); + result = getWorld().parseRobotHasItemAttached(); if (result == null) { - throw new ParseException(Wait.type()); + throw new ParseException(RobotHasItemAttached.type()); } } parent.setChild(result, index); - // semantic action for Wait + // semantic action for RobotHasItemAttached result.action(getWorld()); - printAST("parseWait", result); + printAST("parseRobotHasItemAttached", result); } - private boolean peekObjectAtWrongPlace(Tidy context) { - peekedObjectAtWrongPlace_ = getWorld().parseObjectAtWrongPlace(context); - return peekedObjectAtWrongPlace_ != null; + private RobotHasNoItemAttached peekedRobotHasNoItemAttached_ = null; + + private boolean peekRobotHasNoItemAttached() { + peekedRobotHasNoItemAttached_ = getWorld().parseRobotHasNoItemAttached(); + return peekedRobotHasNoItemAttached_ != null; } - private void parseObjectAtWrongPlace(ASTNode<?> parent, int index, Tidy context) throws ParseException { - ObjectAtWrongPlace result; + private void parseRobotHasNoItemAttached(ASTNode<?> parent, int index) throws ParseException { + RobotHasNoItemAttached result; - if (peekedObjectAtWrongPlace_ != null) { - result = peekedObjectAtWrongPlace_; - peekedObjectAtWrongPlace_ = null; // TODO check if all peeked values are actually parsed afterwards + if (peekedRobotHasNoItemAttached_ != null) { + result = peekedRobotHasNoItemAttached_; + peekedRobotHasNoItemAttached_ = null; // TODO check if all peeked values are actually parsed afterwards } else { - result = getWorld().parseObjectAtWrongPlace(context); + result = getWorld().parseRobotHasNoItemAttached(); if (result == null) { - throw new ParseException(ObjectAtWrongPlace.type()); + throw new ParseException(RobotHasNoItemAttached.type()); } } parent.setChild(result, index); - // semantic action for ObjectAtWrongPlace + // semantic action for RobotHasNoItemAttached result.action(getWorld()); - printAST("parseObjectAtWrongPlace", result); + printAST("parseRobotHasNoItemAttached", result); } - private boolean peekEmptyTable() { - peekedEmptyTable_ = getWorld().parseEmptyTable(); - return peekedEmptyTable_ != null; + private void parseWait(ASTNode<?> parent, int index) throws ParseException { + Wait result; + + result = getWorld().parseWait(parent.time()); + if (result == null) { + throw new ParseException(Wait.type()); + } + + parent.setChild(result, index); + + // semantic action for Wait + result.action(getWorld()); + printAST("parseWait", result); } - private void parseEmptyTable(ASTNode<?> parent, int index) throws ParseException { - EmptyTable result; + private void parseRightPlace(ASTNode<?> parent, int index) throws ParseException { + RightPlace result; - if (peekedEmptyTable_ != null) { - result = peekedEmptyTable_; - peekedEmptyTable_ = null; // TODO check if all peeked values are actually parsed afterwards - } else { - result = getWorld().parseEmptyTable(); - if (result == null) { - throw new ParseException(EmptyTable.type()); + result = getWorld().parseRightPlace(parent.object()); + if (result == null) { + throw new ParseException(RightPlace.type()); + } + + parent.setChild(result, index); + + // semantic action for RightPlace + result.action(getWorld()); + printAST("parseRightPlace", result); + } + + private void parseTidy(ASTNode<?> parent, int index) throws ParseException { + Tidy result = new Tidy(); + result.parser = this; + parent.setChild(result, index); + + while (true) { + peekEmptyTable(); + if (peekedEmptyTable_ != null) { + int i = result.getNumWaitForFullTable(); + result.addWaitForFullTable(null); + parseWaitForFullTable(result.getWaitForFullTableList(), i); + } else { + break; + } + } + + while (true) { + peekObjectAtWrongPlace(result); + if (peekedObjectAtWrongPlace_ != null) { + int i = result.getNumMoveObjectToCorrectPlace(); + result.addMoveObjectToCorrectPlace(null); + parseMoveObjectToCorrectPlace(result.getMoveObjectToCorrectPlaceList(), i); + } else { + break; } } + while (true) { + peekNotEmptyTable(); + if (peekedNotEmptyTable_ != null) { + int i = result.getNumWaitForEmptyTable(); + result.addWaitForEmptyTable(null); + parseWaitForEmptyTable(result.getWaitForEmptyTableList(), i); + } else { + break; + } + } + + parseEmptyTable(result, 3); + + // semantic action for Tidy + result.action(getWorld()); + printAST("parseTidy", result); + } + + private void parseMoveObjectToCorrectPlace(ASTNode<?> parent, int index) throws ParseException { + MoveObjectToCorrectPlace result = new MoveObjectToCorrectPlace(); + result.parser = this; parent.setChild(result, index); - // semantic action for EmptyTable + parseObjectAtWrongPlace(result, 0, parent.containingTidy()); + parsePickUpObject(result, 1); + parseDropObjectAtRightPlace(result, 2); + + // semantic action for MoveObjectToCorrectPlace result.action(getWorld()); - printAST("parseEmptyTable", result); + printAST("parseMoveObjectToCorrectPlace", result); } - private boolean peekNotEmptyTable() { - peekedNotEmptyTable_ = getWorld().parseNotEmptyTable(); - return peekedNotEmptyTable_ != null; + private void parseWaitForFullTable(ASTNode<?> parent, int index) throws ParseException { + WaitForFullTable result = new WaitForFullTable(); + result.parser = this; + parent.setChild(result, index); + + parseEmptyTable(result, 0); + parseWait(result, 1); + + // semantic action for WaitForFullTable + result.action(getWorld()); + printAST("parseWaitForFullTable", result); } - private void parseNotEmptyTable(ASTNode<?> parent, int index) throws ParseException { - NotEmptyTable result; + private void parseWaitForEmptyTable(ASTNode<?> parent, int index) throws ParseException { + WaitForEmptyTable result = new WaitForEmptyTable(); + result.parser = this; + parent.setChild(result, index); - if (peekedNotEmptyTable_ != null) { - result = peekedNotEmptyTable_; - peekedNotEmptyTable_ = null; // TODO check if all peeked values are actually parsed afterwards + parseNotEmptyTable(result, 0); + parseWait(result, 1); + + // semantic action for WaitForEmptyTable + result.action(getWorld()); + printAST("parseWaitForEmptyTable", result); + } + + private void parseDropObjectAtRightPlace(ASTNode<?> parent, int index) throws ParseException { + DropObjectAtRightPlace result = new DropObjectAtRightPlace(); + result.parser = this; + parent.setChild(result, index); + + parseRobotIsReadyToDrop(result, 0); + parseRightPlace(result, 1); + + // semantic action for DropObjectAtRightPlace + result.action(getWorld()); + printAST("parseDropObjectAtRightPlace", result); + } + + private void parsePickUpObject(ASTNode<?> parent, int index) throws ParseException { + PickUpObject result = new PickUpObject(); + result.parser = this; + parent.setChild(result, index); + + parseRobotIsReadyToPick(result, 0); + + // semantic action for PickUpObject + result.action(getWorld()); + printAST("parsePickUpObject", result); + } + + private void parseRobotIsReadyToPick(ASTNode<?> parent, int index) throws ParseException { + RobotIsReadyToPick result; + + // try the different types of RobotIsReadyToPick + if (peekRobotIsReadyToPickToken()) { + result = new RobotIsReallyReadyToPick(); + result.parser = this; + parent.setChild(result, index); + parseRobotIsReallyReadyToPick(parent, index); + } else if (peekRobotIsNotReadyToPickToken()) { + result = new RobotIsNotReadyToPick(); + result.parser = this; + parent.setChild(result, index); + parseRobotIsNotReadyToPick(parent, index); } else { - result = getWorld().parseNotEmptyTable(); - if (result == null) { - throw new ParseException(NotEmptyTable.type()); - } + throw new ParseException("RobotIsReadyToPick", RobotIsReadyToPickToken.type(), RobotIsNotReadyToPickToken.type()); } - parent.setChild(result, index); - // semantic action for NotEmptyTable + // semantic action for RobotIsReadyToPick result.action(getWorld()); - printAST("parseNotEmptyTable", result); + printAST("parseRobotIsReadyToPick", result); + } + + private void parseRobotIsReallyReadyToPick(ASTNode<?> parent, int index) throws ParseException { + RobotIsReallyReadyToPick result = (RobotIsReallyReadyToPick) parent.getChild(index); + + parseRobotIsReadyToPickToken(result, 0); + + // semantic action for RobotIsReallyReadyToPick + result.action(getWorld()); + printAST("parseRobotIsReallyReadyToPick", result); } + + private void parseRobotIsNotReadyToPick(ASTNode<?> parent, int index) throws ParseException { + RobotIsNotReadyToPick result = (RobotIsNotReadyToPick) parent.getChild(index); + + parseRobotIsNotReadyToPickToken(result, 0); + parseWait(result, 1); + parseRobotIsReadyToPick(result, 2); + + // semantic action for RobotIsNotReadyToPick + result.action(getWorld()); + printAST("parseRobotIsNotReadyToPick", result); + } + + private void parseRobotIsReadyToDrop(ASTNode<?> parent, int index) throws ParseException { + RobotIsReadyToDrop result; + + // try the different types of RobotIsReadyToDrop + if (peekRobotIsReadyToDropToken()) { + result = new RobotIsReallyReadyToDrop(); + result.parser = this; + parent.setChild(result, index); + parseRobotIsReallyReadyToDrop(parent, index); + } else if (peekRobotIsNotReadyToDropToken()) { + result = new RobotIsNotReadyToDrop(); + result.parser = this; + parent.setChild(result, index); + parseRobotIsNotReadyToDrop(parent, index); + } else { + throw new ParseException("RobotIsReadyToDrop", RobotIsReadyToDropToken.type(), RobotIsNotReadyToDropToken.type()); + } + + + // semantic action for RobotIsReadyToDrop + result.action(getWorld()); + printAST("parseRobotIsReadyToDrop", result); + } + + private void parseRobotIsReallyReadyToDrop(ASTNode<?> parent, int index) throws ParseException { + RobotIsReallyReadyToDrop result = (RobotIsReallyReadyToDrop) parent.getChild(index); + + parseRobotIsReadyToDropToken(result, 0); + + // semantic action for RobotIsReallyReadyToDrop + result.action(getWorld()); + printAST("parseRobotIsReallyReadyToDrop", result); + } + + private void parseRobotIsNotReadyToDrop(ASTNode<?> parent, int index) throws ParseException { + RobotIsNotReadyToDrop result = (RobotIsNotReadyToDrop) parent.getChild(index); + + parseRobotIsNotReadyToDropToken(result, 0); + parseWait(result, 1); + parseRobotIsReadyToDrop(result, 2); + + // semantic action for RobotIsNotReadyToDrop + result.action(getWorld()); + printAST("parseRobotIsNotReadyToDrop", result); + } + } diff --git a/src/main/java/de/tudresden/inf/st/mg/common/DiagramProvider.java b/src/main/java/de/tudresden/inf/st/mg/common/DiagramProvider.java index 787cf9f119563d46bbd2ea1a036c218250b2f701..d7c7881b2d701ef911b517ade0d25e3ed8bef76b 100644 --- a/src/main/java/de/tudresden/inf/st/mg/common/DiagramProvider.java +++ b/src/main/java/de/tudresden/inf/st/mg/common/DiagramProvider.java @@ -1,13 +1,7 @@ package de.tudresden.inf.st.mg.common; -import com.fasterxml.jackson.databind.ObjectMapper; -import de.tudresden.inf.st.mg.common.DiagramProvider.AST; import io.javalin.Javalin; -import io.javalin.http.ContentType; import io.javalin.websocket.WsContext; -import org.eclipse.jetty.util.ConcurrentHashSet; -import org.json.JSONArray; -import org.json.JSONObject; import java.io.IOException; import java.nio.file.Files; @@ -22,7 +16,7 @@ public class DiagramProvider { private static DiagramProvider INSTANCE; - private static Map<WsContext, String> clients = new ConcurrentHashMap<>(); + private static final Map<WsContext, String> clients = new ConcurrentHashMap<>(); public static DiagramProvider getInstance() { if (INSTANCE == null) { @@ -31,7 +25,7 @@ public class DiagramProvider { return INSTANCE; } - private List<AST> asts; + private final List<AST> asts; private DiagramProvider() { asts = new ArrayList<>(); @@ -45,7 +39,7 @@ public class DiagramProvider { app.ws("ast-events", ws -> { ws.onConnect(ctx -> { - System.err.println("somebody connected " +ctx.host()); + System.err.println("somebody connected " + ctx.host()); clients.put(ctx, ctx.host()); }); ws.onClose(ctx -> { @@ -64,7 +58,7 @@ public class DiagramProvider { asts.add(new AST(timestamp, step, parseRule, Files.readString(diagramPath))); clients.keySet().forEach(session -> { System.err.println("sending"); - session.send(asts.get(asts.size()-1)); + session.send(asts.get(asts.size() - 1)); }); } catch (IOException e) { System.err.println("Unable to read AST diagram file " + diagramPath); diff --git a/src/main/resources/template-configs/LoadWorldParser.yaml b/src/main/resources/template-configs/LoadWorldParser.yaml new file mode 100644 index 0000000000000000000000000000000000000000..2d5d591a3470f3b64d431bf4dcaa3f1b9960e150 --- /dev/null +++ b/src/main/resources/template-configs/LoadWorldParser.yaml @@ -0,0 +1,50 @@ +name: "LoadWorldParser" +package: "de.tudresden.inf.st.mg" +genPackage: "de.tudresden.inf.st.mg.jastadd.model" +targetType: "T" + +tokenContext: + context: false # default value + peek: true # default value + worldParserArguments: [ ] # default value + tokens: + - name: "Load" + - name: "Unload" + - name: "Full" + +ruleContext: + constructObject: false # default value + peekForType: false # default value + token: false # default value + first: false # default value + last: false # default value + list: false # default value + additionalArgs: "" # default value + peekContext: "" # default value + rules: + - ruleName: "T" + peekForType: true + types: + - type: "T2" + token: "Full" + first: true + - type: "T1" + token: "Load" + last: true + components: [] + - ruleName: "T1" + components: + - componentName: "Load" + token: true + index: 0 + - componentName: "T" + token: true + index: 1 + - componentName: "Unload" + token: true + index: 2 + - ruleName: "T2" + components: + - componentName: "Full" + token: true + index: 0 diff --git a/src/main/resources/template-configs/RobotParser.yaml b/src/main/resources/template-configs/RobotParser.yaml new file mode 100644 index 0000000000000000000000000000000000000000..45e1c588ff7460492aed3b5ead8e7cc138b0e67d --- /dev/null +++ b/src/main/resources/template-configs/RobotParser.yaml @@ -0,0 +1,148 @@ +name: "RobotParser" +package: "de.tudresden.inf.st.mg" +genPackage: "de.tudresden.inf.st.mg.jastadd.model" +targetType: "Tidy" + +tokenContext: + context: false # default value + peek: true # default value + worldParserArguments: [ ] # default value + tokens: + - name: "EmptyTable" + - name: "NotEmptyTable" + - name: "ObjectAtWrongPlace" + context: "Tidy" + - name: "RobotIsReadyToPickToken" + - name: "RobotIsNotReadyToPickToken" + - name: "RobotIsReadyToDropToken" + - name: "RobotIsNotReadyToDropToken" + - name: "RobotIsIdle" + - name: "RobotIsNotIdle" + - name: "RobotHasItemAttached" + - name: "RobotHasNoItemAttached" + - name: "Wait" + worldParserArguments: + - argument: "time()" + peek: false + - name: "RightPlace" + worldParserArguments: + - argument: "object()" + peek: false + +ruleContext: + constructObject: false # default value + peekForType: false # default value + token: false # default value + first: false # default value + last: false # default value + list: false # default value + additionalArgs: "" # default value + peekContext: "" # default value + rules: + - ruleName: "Tidy" + constructObject: true + components: + - componentName: "WaitForFullTable" + list: true + peekToken: "EmptyTable" + - componentName: "MoveObjectToCorrectPlace" + list: true + peekToken: "ObjectAtWrongPlace" + peekContext: "result" + - componentName: "WaitForEmptyTable" + list: true + peekToken: "NotEmptyTable" + - componentName: "EmptyTable" + index: 3 + - ruleName: "MoveObjectToCorrectPlace" + constructObject: true + components: + - componentName: "ObjectAtWrongPlace" + token: true + index: 0 + additionalArgs: ", parent.containingTidy()" + - componentName: "PickUpObject" + index: 1 + - componentName: "DropObjectAtRightPlace" + index: 2 + - ruleName: "WaitForFullTable" + constructObject: true + components: + - componentName: "EmptyTable" + token: true + index: 0 + - componentName: "Wait" + token: true + index: 1 + - ruleName: "WaitForEmptyTable" + constructObject: true + components: + - componentName: "NotEmptyTable" + token: true + index: 0 + - componentName: "Wait" + token: true + index: 1 + - ruleName: "DropObjectAtRightPlace" + constructObject: true + components: + - componentName: "RobotIsReadyToDrop" + index: 0 + - componentName: "RightPlace" + token: true + index: 1 + - ruleName: "PickUpObject" + constructObject: true + components: + - componentName: "RobotIsReadyToPick" + index: 0 + - ruleName: "RobotIsReadyToPick" + peekForType: true + types: + - type: "RobotIsReallyReadyToPick" + token: "RobotIsReadyToPickToken" + first: true + - type: "RobotIsNotReadyToPick" + token: "RobotIsNotReadyToPickToken" + last: true + components: [] + - ruleName: "RobotIsReallyReadyToPick" + components: + - componentName: "RobotIsReadyToPickToken" + token: true + index: 0 + - ruleName: "RobotIsNotReadyToPick" + components: + - componentName: "RobotIsNotReadyToPickToken" + token: true + index: 0 + - componentName: "Wait" + token: true + index: 1 + - componentName: "RobotIsReadyToPick" + index: 2 + - ruleName: "RobotIsReadyToDrop" + peekForType: true + types: + - type: "RobotIsReallyReadyToDrop" + token: "RobotIsReadyToDropToken" + first: true + - type: "RobotIsNotReadyToDrop" + token: "RobotIsNotReadyToDropToken" + last: true + components: [] + - ruleName: "RobotIsReallyReadyToDrop" + components: + - componentName: "RobotIsReadyToDropToken" + token: true + index: 0 + - ruleName: "RobotIsNotReadyToDrop" + components: + - componentName: "RobotIsNotReadyToDropToken" + token: true + index: 0 + - componentName: "Wait" + token: true + index: 1 + - componentName: "RobotIsReadyToDrop" + index: 2 diff --git a/src/main/resources/templates/Parser.mustache b/src/main/resources/templates/Parser.mustache new file mode 100644 index 0000000000000000000000000000000000000000..bd3b6b615ba592f18f0d627f44162203e7e16db1 --- /dev/null +++ b/src/main/resources/templates/Parser.mustache @@ -0,0 +1,127 @@ +package {{{package}}}; + +import de.tudresden.inf.st.mg.common.MotionGrammarParser; +import {{{genPackage}}}.*; + + +public final class {{{name}}} extends MotionGrammarParser<{{{targetType}}}> { + + private static final String WORLD = "World"; + + public {{{name}}}(World world) { + contexts_.put(WORLD, world); + } + + private World getWorld() { + return (World) contexts_.get(WORLD); + } + + @Override + protected void parse(ASTNode<?> parent, int index) throws ParseException { + parse{{{targetType}}}(parent, index); + } + +{{#tokenContext}} +{{#tokens}} +{{#peek}} + private {{{name}}} peeked{{{name}}}_ = null; + + private boolean peek{{{name}}}({{#context}}{{{context}}} context{{/context}}) { + peeked{{{name}}}_ = getWorld().parse{{{name}}}({{#context}}context{{/context}}{{#worldParserArguments}}parent.{{{argument}}}{{/worldParserArguments}}); + return peeked{{{name}}}_ != null; + } + +{{/peek}} + private void parse{{{name}}}(ASTNode<?> parent, int index{{#context}}, {{{context}}} context{{/context}}) throws ParseException { + {{{name}}} result; + + {{#peek}} + if (peeked{{{name}}}_ != null) { + result = peeked{{{name}}}_; + peeked{{{name}}}_ = null; // TODO check if all peeked values are actually parsed afterwards + } else { + result = getWorld().parse{{{name}}}({{#context}}context{{/context}}{{#worldParserArguments}}parent.{{{argument}}}{{/worldParserArguments}}); + if (result == null) { + throw new ParseException({{{name}}}.type()); + } + } + {{/peek}} + {{^peek}} + result = getWorld().parse{{{name}}}({{#context}}context{{/context}}{{#worldParserArguments}}parent.{{{argument}}}{{/worldParserArguments}}); + if (result == null) { + throw new ParseException({{{name}}}.type()); + } + {{/peek}} + + parent.setChild(result, index); + + // semantic action for {{{name}}} + result.action(getWorld()); + printAST("parse{{{name}}}", result); + } + +{{/tokens}} +{{/tokenContext}} +{{#ruleContext}} +{{#rules}} + private void parse{{{ruleName}}}(ASTNode<?> parent, int index) throws ParseException { + {{#constructObject}} + {{{ruleName}}} result = new {{{ruleName}}}(); + result.parser = this; + parent.setChild(result, index); + {{/constructObject}} + {{^constructObject}} + {{#peekForType}} + {{{ruleName}}} result; + + // try the different types of {{{ruleName}}} + {{#types}} + {{#first}} + if (peek{{{token}}}()) { + {{/first}} + {{^first}} + } else if (peek{{{token}}}()) { + {{/first}} + result = new {{{type}}}(); + result.parser = this; + parent.setChild(result, index); + parse{{{type}}}(parent, index); + {{#last}} + } else { + throw new ParseException("{{{ruleName}}}"{{#types}}, {{{token}}}.type(){{/types}}); + } + {{/last}} + {{/types}} + {{/peekForType}} + {{^peekForType}} + {{{ruleName}}} result = ({{{ruleName}}}) parent.getChild(index); + {{/peekForType}} + {{/constructObject}} + + {{#components}} + {{#list}} + while (true) { + peek{{{peekToken}}}({{{peekContext}}}); + if (peeked{{{peekToken}}}_ != null) { + int i = result.getNum{{{componentName}}}(); + result.add{{{componentName}}}(null); + parse{{{componentName}}}(result.get{{{componentName}}}List(), i); + } else { + break; + } + } + + {{/list}} + {{^list}} + parse{{{componentName}}}(result, {{{index}}}{{{additionalArgs}}}); + {{/list}} + {{/components}} + + // semantic action for {{{ruleName}}} + result.action(getWorld()); + printAST("parse{{{ruleName}}}", result); + } + +{{/rules}} +{{/ruleContext}} +} diff --git a/src/test/java/de/tudresden/inf/st/mg/ParserTest.java b/src/test/java/de/tudresden/inf/st/mg/ParserTest.java index 820043c185c53c5168916dfa2b438b275f4c918d..7cfbe9573765e445a000108d19acda9a2b8feaa7 100644 --- a/src/test/java/de/tudresden/inf/st/mg/ParserTest.java +++ b/src/test/java/de/tudresden/inf/st/mg/ParserTest.java @@ -1,20 +1,19 @@ package de.tudresden.inf.st.mg; -import de.tudresden.inf.st.mg.common.MotionGrammarConfig; import de.tudresden.inf.st.mg.common.MotionGrammarParser; -import de.tudresden.inf.st.mg.jastadd.model.*; +import de.tudresden.inf.st.mg.jastadd.model.Container; +import de.tudresden.inf.st.mg.jastadd.model.RobotWorld; +import de.tudresden.inf.st.mg.jastadd.model.T1; +import de.tudresden.inf.st.mg.jastadd.model.Tidy; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; import java.util.Random; -import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; @@ -69,12 +68,4 @@ public class ParserTest { assertThat(result).isNotNull().isInstanceOf(Tidy.class); } - - - @Test - @Disabled - void runRealTidyParser() throws MotionGrammarParser.ParseException { - - } - }