1 package org.opentrafficsim.core.gtu;
2
3 import java.io.File;
4 import java.io.PrintWriter;
5
6 import org.djunits.value.vdouble.scalar.Duration;
7 import org.djunits.value.vdouble.scalar.Time;
8 import org.djutils.exceptions.Throw;
9 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
10 import org.opentrafficsim.core.network.OTSNetwork;
11
12 import nl.tudelft.simulation.dsol.SimRuntimeException;
13 import nl.tudelft.simulation.language.d3.DirectedPoint;
14
15
16
17
18
19
20
21
22
23
24
25
26 public class GTUDumper
27 {
28
29
30
31 public void dump()
32 {
33 try
34 {
35 Time now = this.simulator.getSimulatorTime();
36 String fileName = String.format("%s%08.2f.txt", fileNamePrefix, now.si);
37 PrintWriter pw = new PrintWriter(new File(fileName));
38 for (GTU gtu : this.network.getGTUs())
39 {
40 DirectedPoint dp = gtu.getOperationalPlan().getLocation(now);
41 pw.format("%s position %.3f,%.3f dir=%5.1f speed %s\n", gtu.toString(), dp.x, dp.y,
42 Math.toDegrees(dp.getRotZ()), gtu.getSpeed());
43 }
44 pw.close();
45 this.simulator.scheduleEventRel(this.interval, this, this, "dump", new Object[] {});
46 }
47 catch (Exception e)
48 {
49 e.printStackTrace();
50 }
51 }
52
53
54 private final Duration interval;
55
56
57 private final OTSNetwork network;
58
59
60 private final String fileNamePrefix;
61
62
63 private final OTSSimulatorInterface simulator;
64
65
66
67
68
69
70
71
72
73
74 public GTUDumper(final Time firstDumpTime, final Duration interval, final OTSNetwork network,
75 final String fileNamePrefix) throws SimRuntimeException
76 {
77 Throw.whenNull(network, "Network may not be null");
78 this.simulator = network.getSimulator();
79 Throw.whenNull(firstDumpTime, "firstDumpTime may not be null");
80 Throw.when(firstDumpTime.lt(this.simulator.getSimulatorTime()), RuntimeException.class,
81 "firstDumptTime may not be before current simulator time");
82 Throw.whenNull(interval, "interval may not be null");
83 Throw.when(interval.le(Duration.ZERO), RuntimeException.class, "Duration must be positive");
84 Throw.whenNull(fileNamePrefix, "fileNamePrefix may not be null");
85 this.interval = interval;
86 this.network = network;
87 this.fileNamePrefix = fileNamePrefix;
88 simulator.scheduleEventAbs(firstDumpTime, this, this, "dump", new Object[] {});
89 }
90
91
92 @Override
93 public String toString()
94 {
95 return "GTUDumper [interval=" + interval + ", network=" + network + ", fileNamePrefix=" + fileNamePrefix + "]";
96 }
97
98 }