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