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
17
18
19
20
21
22
23
24
25
26 public class HistoryManagerDEVS extends HistoryManager implements EventListenerInterface
27 {
28
29
30 private final DEVSSimulatorInterface.TimeDoubleUnit simulator;
31
32
33 private final Duration history;
34
35
36 private final Duration cleanUpInterval;
37
38
39 private final Object[] none = new Object[0];
40
41
42
43
44
45
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();
54 Try.execute(() -> this.simulator.addListener(this, SimulatorInterface.END_REPLICATION_EVENT),
55 "Unable to add listener.");
56 }
57
58
59 @Override
60 public Time now()
61 {
62 return this.simulator.getSimulatorTime();
63 }
64
65
66
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
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
95 @Override
96 public String toString()
97 {
98 return "HistoryManagerDEVS [history=" + this.history + ", cleanUpInterval=" + this.cleanUpInterval + "]";
99 }
100
101 }