View Javadoc
1   package org.opentrafficsim.core.perception;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Time;
7   import org.djutils.exceptions.Try;
8   
9   import nl.tudelft.simulation.dsol.SimRuntimeException;
10  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
11  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12  import nl.tudelft.simulation.event.EventInterface;
13  import nl.tudelft.simulation.event.EventListenerInterface;
14  
15  /**
16   * History manager that uses an {@code DEVSSimulatorInterface.TimeDoubleUnit}.
17   * <p>
18   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
20   * <p>
21   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 5 jan. 2018 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
25   */
26  public class HistoryManagerDEVS extends HistoryManager implements EventListenerInterface
27  {
28  
29      /** Simulator. */
30      private final DEVSSimulatorInterface.TimeDoubleUnit simulator;
31  
32      /** Time over which history is guaranteed. */
33      private final Duration history;
34  
35      /** Clean-up interval. */
36      private final Duration cleanUpInterval;
37  
38      /** Event input, can be the same as it's nothing. */
39      private final Object[] none = new Object[0];
40  
41      /**
42       * Constructor.
43       * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; simulator
44       * @param history Duration; time over which history is guaranteed
45       * @param cleanUpInterval Duration; clean-up interval
46       */
47      public HistoryManagerDEVS(final DEVSSimulatorInterface.TimeDoubleUnit simulator, final Duration history,
48              final Duration cleanUpInterval)
49      {
50          this.simulator = simulator;
51          this.history = history;
52          this.cleanUpInterval = cleanUpInterval;
53          cleanUpHistory(); // start clean-up event chain
54          Try.execute(() -> this.simulator.addListener(this, SimulatorInterface.END_REPLICATION_EVENT),
55                  "Unable to add listener.");
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public Time now()
61      {
62          return this.simulator.getSimulatorTime();
63      }
64  
65      /**
66       * Cleans up the history of all registered {@code Historicals}.
67       */
68      protected final void cleanUpHistory()
69      {
70          for (HistoricalElement historical : getHistoricals())
71          {
72              historical.cleanUpHistory(this.history);
73          }
74          try
75          {
76              this.simulator.scheduleEventRel(this.cleanUpInterval, this, this, "cleanUpHistory", this.none);
77          }
78          catch (SimRuntimeException exception)
79          {
80              throw new RuntimeException(exception);
81          }
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public void notify(final EventInterface event) throws RemoteException
87      {
88          if (event.getType().equals(SimulatorInterface.END_REPLICATION_EVENT))
89          {
90              endOfSimulation();
91          }
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public String toString()
97      {
98          return "HistoryManagerDEVS [history=" + this.history + ", cleanUpInterval=" + this.cleanUpInterval + "]";
99      }
100 
101 }