1 package org.opentrafficsim.demo.conflict;
2
3 import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4
5 import java.net.URL;
6 import java.rmi.RemoteException;
7 import java.util.ArrayList;
8
9 import javax.naming.NamingException;
10 import javax.swing.SwingUtilities;
11
12 import org.djunits.unit.DurationUnit;
13 import org.djunits.unit.LengthUnit;
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Time;
17 import org.opentrafficsim.base.modelproperties.Property;
18 import org.opentrafficsim.base.modelproperties.PropertyException;
19 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20 import org.opentrafficsim.core.dsol.OTSModelInterface;
21 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22 import org.opentrafficsim.core.gtu.animation.GTUColorer;
23 import org.opentrafficsim.core.network.OTSNetwork;
24 import org.opentrafficsim.road.animation.AnimationToggles;
25 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
26 import org.opentrafficsim.road.network.lane.CrossSectionLink;
27 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
28 import org.opentrafficsim.road.network.lane.conflict.LaneCombinationList;
29 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
30 import org.opentrafficsim.simulationengine.OTSSimulationException;
31
32 import nl.tudelft.simulation.dsol.SimRuntimeException;
33 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
34 import nl.tudelft.simulation.language.io.URLResource;
35
36
37
38
39
40
41
42
43
44
45
46 public class TestNetworkDemo extends AbstractWrappableAnimation
47 {
48
49
50 private static final long serialVersionUID = 20161211L;
51
52
53 @Override
54 protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
55 {
56 return new TestNetworkModel();
57 }
58
59
60 @Override
61 protected final void addAnimationToggles()
62 {
63 AnimationToggles.setTextAnimationTogglesStandard(this);
64 }
65
66
67 @Override
68 public final String shortName()
69 {
70 return "Test network demonstration";
71 }
72
73
74 @Override
75 public final String description()
76 {
77 return "Test network demonstration";
78 }
79
80
81
82
83 class TestNetworkModel implements OTSModelInterface
84 {
85
86
87 private static final long serialVersionUID = 20161211L;
88
89
90 private OTSNetwork network;
91
92
93 private OTSDEVSSimulatorInterface simulator;
94
95
96 @Override
97 public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> arg0)
98 throws SimRuntimeException, RemoteException
99 {
100 this.simulator = (OTSDEVSSimulatorInterface) arg0;
101 try
102 {
103 URL url = URLResource.getResource("/conflict/Test-Network-14.xml");
104 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
105 this.network = nlp.build(url);
106
107 LaneCombinationList ignoreList = new LaneCombinationList();
108
109
110
111
112
113
114
115
116 LaneCombinationList permittedList = new LaneCombinationList();
117 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
118 (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
119 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
120 (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
121 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
122 (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
123 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
124 (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
125 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
126 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)), ignoreList, permittedList);
127
128
129
130 }
131 catch (Exception exception)
132 {
133 exception.printStackTrace();
134 }
135 }
136
137
138 @Override
139 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
140 {
141 return this.simulator;
142 }
143
144
145 @Override
146 public OTSNetwork getNetwork()
147 {
148 return this.network;
149 }
150
151 }
152
153
154
155
156
157
158 public static void main(final String[] args) throws SimRuntimeException
159 {
160 SwingUtilities.invokeLater(new Runnable()
161 {
162 @Override
163 public void run()
164 {
165 try
166 {
167 TestNetworkDemo animation = new TestNetworkDemo();
168
169 animation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
170 new ArrayList<Property<?>>(), null, true);
171 }
172 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
173 {
174 exception.printStackTrace();
175 }
176 }
177 });
178 }
179
180 }