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