Skip to content
Snippets Groups Projects
Select Git revision
  • 91480e8801e7771e53c84ecbbf2f40c721e42582
  • master default protected
  • feature/ros-example
3 results

MinimalMain.java

Blame
  • MinimalMain.java 4.66 KiB
    package de.tudresden.inf.st.mrc;
    
    import de.tudresden.inf.st.jastadd.dumpAst.ast.Dumper;
    import de.tudresden.inf.st.mrc.ast.*;
    import de.tudresden.inf.st.mrc.ast.ASTState.Trace.Receiver;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    import java.io.IOException;
    import java.nio.file.Paths;
    import java.time.Instant;
    import java.util.ArrayList;
    import java.util.List;
    
    import static java.util.concurrent.TimeUnit.SECONDS;
    
    /**
     * Minimal main.
     *
     * @author rschoene - Initial contribution
     */
    public class MinimalMain {
      private static final Logger logger = LogManager.getLogger(MinimalMain.class);
      private static final String TOPIC_FOR_INPUT = "topic/for/input";
      private static final String TOPIC_URI_FOR_INPUT = "mqtt://localhost/" + TOPIC_FOR_INPUT;
      private static MqttHandler publisher;
    
      static class MinimalReceiver implements Receiver {
        List<String> latestAttributes;
        Receiver oldReceiver;
    
        MinimalReceiver(Receiver oldReceiver) {
          this.oldReceiver = oldReceiver;
          reset();
        }
    
        void reset() {
          latestAttributes = new ArrayList<>();
        }
    
        @SuppressWarnings("rawtypes")
        @Override
        public void accept(ASTState.Trace.Event event, ASTNode node, String attribute, Object params, Object value) {
          oldReceiver.accept(event, node, attribute, params, value);
          logger.info("event: {}, node: {}, attribute: {}, params: {}, value: {},", event, node, attribute, params, value);
          latestAttributes.add(attribute);
        }
      }
      static MinimalReceiver receiver = null;
    
      public static void main(String[] args) throws Exception {
        publisher = new MqttHandler("publisher").setHost("localhost");
        publisher.waitUntilReady(2, SECONDS);
        publisher.publish("---", ("Start at " + Instant.now()).getBytes());
    
        A normalA = addNewA(true);
    //    enableTracing(normalA);
        B b1 = normalA.getB(0);
        B b2 = normalA.getB(1);
    
        // a.OutputOnA -> a.Input
        normalA.addDependencyA(normalA);
        // b1.OutputOnB -> a.Input
        b1.addDependencyB(normalA);
        // b2.OutputOnB -> a.Input
        b2.addDependencyB(normalA);
    
        normalA.connectInput(TOPIC_URI_FOR_INPUT);
        normalA.connectOutputOnA("mqtt://localhost/normalA/out", true);
        b1.connectOutputOnB("mqtt://localhost/normalA/b1/out", true);
        b2.connectOutputOnB("mqtt://localhost/normalA/b2/out", false);
    
        A aWithNoDependency = addNewA(false);
    //    enableTracing(aWithNoDependency);
    
        aWithNoDependency.connectInput(TOPIC_URI_FOR_INPUT);
        aWithNoDependency.connectOutputOnA("mqtt://localhost/aNoDep/out", true);
        aWithNoDependency.getC(0).connectOutputOnC("mqtt://localhost/aNoDep/c1/out", true);
        aWithNoDependency.getC(1).connectOutputOnC("mqtt://localhost/aNoDep/c2/out", false);
    
        // stimulated incremental evaluation
        aWithNoDependency.getC(0).getOutputOnC();
    
        Dumper.read(normalA).dumpAsPNG(Paths.get("image.png"));
    
        describedWait(1, "Publish 2");
        publisher.publish(TOPIC_FOR_INPUT, "2".getBytes());
    
    //    aWithNoDependency.getC(0).getOutputOnC();
    
        describedWait(2, "Disconnect normalA");
        try {
          boolean success = normalA.disconnectInput(TOPIC_URI_FOR_INPUT);
          logger.info("disconnect success: " + success);
        } catch (IOException e) {
          e.printStackTrace();
        }
    
    //    aWithNoDependency.getC(0).getOutputOnC();
    
        describedWait(2, "Publish 4");
    //    receiver.reset();
        publisher.publish(TOPIC_FOR_INPUT, "4".getBytes());
    
    //    aWithNoDependency.getC(0).getOutputOnC();
    
    //    describedWait(1, "Print latest attributes");
    //    logger.info("latest attributes = {}", receiver.latestAttributes);
    
    //    while (true) {}
    //    System.out.println("[Enter] to exit");
    //    Scanner scanner = new Scanner(System.in);
    //    scanner.nextLine();
    //    SECONDS.sleep(15);
      }
    
      private static void describedWait(long seconds, String description) throws InterruptedException {
        SECONDS.sleep(seconds);
        logger.info("--- {} ---", description);
        publisher.publish("---", description.getBytes());
      }
    
      private static A addNewA(boolean useB) {
        A result = new A();
        result.setInput("1");
        if (useB) {
          B b1 = new B();
          B b2 = new B();
          result.addB(b1);
          result.addB(b2);
        } else {
          C c1 = new C();
          C c2 = new C();
          result.addC(c1);
          result.addC(c2);
        }
        return result;
      }
    
      private static void enableTracing(ASTNode<?> node) {
        if (node.trace().getReceiver() != null && node.trace().getReceiver() instanceof MinimalReceiver) {
          System.out.println("*** receiver already set up ***");
        } else {
          System.out.println("*** activate receiver ***");
          receiver = new MinimalReceiver(node.trace().getReceiver());
          node.trace().setReceiver(receiver);
        }
      }
    }