Skip to content
Snippets Groups Projects
Select Git revision
  • cfdc26505328ec37cb36efe26a2f94195f188965
  • master default protected
2 results

IPOS_Rawdata_sep.py

  • ParserTest.java 3.73 KiB
    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 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;
    
    public class ParserTest {
    
      public static final Path LOAD_AST_DIAGRAM_DIR = Path.of("src", "gen", "resources", "diagrams", "parsing", "load");
      public static final Path TIDY_AST_DIAGRAM_DIR = Path.of("src", "gen", "resources", "diagrams", "parsing", "tidy");
    
      @BeforeAll
      static void prepareOutputPath() throws IOException {
        try {
          Files.walk(LOAD_AST_DIAGRAM_DIR).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
        } catch (IOException e) {
          // do nothing
        }
        try {
          Files.walk(TIDY_AST_DIAGRAM_DIR).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
        } catch (IOException e) {
          // do nothing
        }
        Files.createDirectories(LOAD_AST_DIAGRAM_DIR);
        Files.createDirectories(TIDY_AST_DIAGRAM_DIR);
      }
    
      @Test
      void runLodUnloadParser() throws MotionGrammarParser.ParseException {
    
        // for some reason, the best random seed value here is 1 and not 0???
        Container containerWorld = Container.initialWorld(new Random(1));
    
        LoadWorldParser parser = new LoadWorldParser(containerWorld);
        parser.setDebugDiagramDir(LOAD_AST_DIAGRAM_DIR);
    
        var result = parser.parse();
    
        assertThat(result).isNotNull().isInstanceOf(T1.class);
      }
    
      @Test
      void runTidyParser() throws MotionGrammarParser.ParseException {
    
        // for some reason, the best random seed value here is 1 and not 0???
        RobotScene world = RobotScene.initialWorld(new Random(1));
    
        // create a parser using the world
        RobotParser parser = new RobotParser(world);
        parser.setDebugDiagramDir(TIDY_AST_DIAGRAM_DIR);
    
        // parse (synchronously, long-running)
        var result = parser.parse();
    
        assertThat(result).isNotNull().isInstanceOf(Tidy.class);
      }
    
    
      /**
       * 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
       */
      @Test
      @Disabled
      void runRealTidyParser() throws MotionGrammarParser.ParseException {
    
        MotionGrammarConfig.astDiagramDir = TIDY_AST_DIAGRAM_DIR;
    
        RobotScene world = RobotScene.initialWorld();
    
        org.fusesource.mqtt.client.MQTT handler = new org.fusesource.mqtt.client.MQTT();
        try {
          handler.setHost("tcp://localhost:1883");
        } catch (URISyntaxException e) {
          e.printStackTrace();
        }
        world.connection = handler.blockingConnection();
        try {
          world.connection.connect();
        } catch (Exception e) {
          e.printStackTrace();
        }
    
        System.err.println(world.connection.isConnected());
    
        try {
          world.connectTable("mqtt://localhost//ceti_cell_empty/scene/update");
        } catch (IOException e) {
          e.printStackTrace();
        }
    
        while (world.getTable().getNumPhysicalObject() == 0) {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
    
        // create a parser using the world
        RobotParser parser = new RobotParser(world);
    
        // parse (synchronously, long-running)
        var result = parser.parse();
    
        assertThat(result).isNotNull().isInstanceOf(Tidy.class);
        try {
          Thread.sleep(100000*1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    
    }