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.opentrafficsim.core.dsol.OTSSimulatorInterface;
9 import org.opentrafficsim.core.network.OTSNetwork;
10
11 import nl.tudelft.simulation.dsol.SimRuntimeException;
12 import nl.tudelft.simulation.language.d3.DirectedPoint;
13
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.getSimulatorTime();
35 String fileName = String.format("%s%08.2f.txt", fileNamePrefix, now.si);
36 PrintWriter pw = new PrintWriter(new File(fileName));
37 for (GTU gtu : this.network.getGTUs())
38 {
39 DirectedPoint 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.getRotZ()), gtu.getSpeed());
42 }
43 pw.close();
44 this.simulator.scheduleEventRel(this.interval, this, 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 OTSNetwork 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
74 public GTUDumper(final OTSSimulatorInterface simulator, final Time firstDumpTime, final Duration interval,
75 final OTSNetwork network, final String fileNamePrefix) throws SimRuntimeException
76 {
77 this.simulator = simulator;
78 this.interval = interval;
79 this.network = network;
80 this.fileNamePrefix = fileNamePrefix;
81 simulator.scheduleEventAbs(firstDumpTime, this, this, "dump", new Object[] {});
82 }
83
84 }