View Javadoc
1   package nl.tudelft.simulation.examples.dsol.terminal;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import org.djutils.event.Event;
8   import org.djutils.event.EventListener;
9   
10  import nl.tudelft.simulation.dsol.SimRuntimeException;
11  import nl.tudelft.simulation.dsol.experiment.Replication;
12  import nl.tudelft.simulation.dsol.experiment.SingleReplication;
13  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
14  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterInteger;
15  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
16  import nl.tudelft.simulation.dsol.simulators.DevsSimulator;
17  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
18  
19  /**
20   * <p>
21   * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
23   * project is distributed under a three-clause BSD-style license, which can be found at
24   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
25   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
26   * </p>
27   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs</a>
28   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   */
30  public final class ConsoleRunnerTerminal implements EventListener
31  {
32      /** */
33      private static final long serialVersionUID = 20220110L;
34  
35      /**
36       * Construct the terminal experiment.
37       * @throws SimRuntimeException on error
38       * @throws RemoteException on error
39       * @throws NamingException on error
40       * @throws InputParameterException on error
41       */
42      private ConsoleRunnerTerminal() throws SimRuntimeException, RemoteException, NamingException, InputParameterException
43      {
44          long seed = 127;
45          int rep = 1;
46          int numQC = 5;
47          int numAGV = 42;
48          double runtime = 40 * 60;
49          DevsSimulator<Double> simulator = new DevsSimulator<Double>("ConsoleRunnerTerminal");
50          Terminal model = new Terminal(simulator, rep);
51          Replication<Double> replication = new SingleReplication<Double>("rep1", 0.0, 0.0, runtime);
52          model.getStreams().put("default", new MersenneTwister(seed++));
53          InputParameterMap parameters = model.getInputParameterMap();
54          ((InputParameterInteger) parameters.get("numQC")).setIntValue(numQC);
55          ((InputParameterInteger) parameters.get("numAGV")).setIntValue(numAGV);
56          simulator.initialize(model, replication);
57          simulator.scheduleEventAbs(runtime - 0.00001, this, "terminate", new Object[] {simulator, numQC, numAGV, rep});
58          model.addListener(this, Terminal.READY_EVENT);
59          simulator.start();
60      }
61  
62      /**
63       * @param simulator DevsSimulator&lt;Double&gt;; the simulator
64       * @param numQC int; num QC
65       * @param numAGV int; num AGV
66       * @param rep int; replication number
67       * @throws SimRuntimeException on error
68       * @throws RemoteException on error
69       */
70      public synchronized void terminate(final DevsSimulator<Double> simulator, final int numQC, final int numAGV,
71              final int rep) throws SimRuntimeException, RemoteException
72      {
73          simulator.stop();
74          System.out.println(numQC + "\t" + numAGV + "\t" + rep + "\tNaN\tNaN");
75          System.exit(0);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public synchronized void notify(final Event event) throws RemoteException
81      {
82          if (event.getType().equals(Terminal.READY_EVENT))
83          {
84              Terminal.Output output = (Terminal.Output) event.getContent();
85              System.out.println(output.getNumQC() + "\t" + output.getNumAGV() + "\t" + output.getRep() + "\t"
86                      + output.getDelayHours() + "\t" + output.getCosts());
87              System.exit(0);
88          }
89      }
90  
91      /**
92       * @param args String[]; args
93       * @throws SimRuntimeException on error
94       * @throws RemoteException on error
95       * @throws NamingException on error
96       * @throws InputParameterException on error
97       */
98      public static void main(final String[] args)
99              throws SimRuntimeException, RemoteException, NamingException, InputParameterException
100     {
101         new ConsoleRunnerTerminal();
102     }
103 }