1 package org.opentrafficsim.demo.conflict;
2
3 import java.awt.Dimension;
4 import java.io.Serializable;
5 import java.net.URL;
6 import java.rmi.RemoteException;
7
8 import javax.naming.NamingException;
9
10 import org.djunits.unit.LengthUnit;
11 import org.djunits.value.vdouble.scalar.Duration;
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.djunits.value.vdouble.scalar.Time;
14 import org.djutils.io.URLResource;
15 import org.opentrafficsim.core.animation.gtu.colorer.DefaultSwitchableGTUColorer;
16 import org.opentrafficsim.core.dsol.AbstractOTSModel;
17 import org.opentrafficsim.core.dsol.OTSAnimator;
18 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
19 import org.opentrafficsim.core.gtu.GTUType;
20 import org.opentrafficsim.demo.conflict.TestNetworkDemo.TestNetworkModel;
21 import org.opentrafficsim.draw.core.OTSDrawingException;
22 import org.opentrafficsim.road.network.OTSRoadNetwork;
23 import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
24 import org.opentrafficsim.road.network.lane.CrossSectionLink;
25 import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
26 import org.opentrafficsim.road.network.lane.conflict.LaneCombinationList;
27 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
28 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
29
30 import nl.tudelft.simulation.dsol.SimRuntimeException;
31 import nl.tudelft.simulation.language.DSOLException;
32
33
34
35
36
37
38
39
40
41
42
43 public class TestNetworkDemo extends OTSSimulationApplication<TestNetworkModel>
44 {
45
46 private static final long serialVersionUID = 20161211L;
47
48
49
50
51
52
53
54
55 public TestNetworkDemo(final String title, final OTSAnimationPanel panel, final TestNetworkModel model)
56 throws OTSDrawingException
57 {
58 super(model, panel);
59 }
60
61
62
63
64
65 public static void main(final String[] args)
66 {
67 demo(true);
68 }
69
70
71
72
73
74 public static void demo(final boolean exitOnClose)
75 {
76 try
77 {
78 OTSAnimator simulator = new OTSAnimator("TestNetworkDemo");
79 final TestNetworkModel networkModel = new TestNetworkModel(simulator);
80 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), networkModel);
81 OTSAnimationPanel animationPanel =
82 new OTSAnimationPanel(networkModel.getNetwork().getExtent(), new Dimension(800, 600), simulator,
83 networkModel, new DefaultSwitchableGTUColorer(), networkModel.getNetwork());
84 TestNetworkDemo app = new TestNetworkDemo("Network test demo", animationPanel, networkModel);
85 app.setExitOnClose(exitOnClose);
86 animationPanel.enableSimulationControlButtons();
87 }
88 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
89 {
90 exception.printStackTrace();
91 }
92 }
93
94
95
96
97 static class TestNetworkModel extends AbstractOTSModel
98 {
99
100
101 private static final long serialVersionUID = 20161211L;
102
103
104 private OTSRoadNetwork network;
105
106
107
108
109 TestNetworkModel(final OTSSimulatorInterface simulator)
110 {
111 super(simulator);
112 }
113
114
115 @Override
116 public void constructModel() throws SimRuntimeException
117 {
118 try
119 {
120 URL xmlURL = URLResource.getResource("/resources/conflict/Test-Network-14.xml");
121 this.network = new OTSRoadNetwork("Test-Network-14", true, getSimulator());
122 XmlNetworkLaneParser.build(xmlURL, this.network, false);
123
124 LaneCombinationList ignoreList = new LaneCombinationList();
125
126
127
128
129
130
131
132
133 LaneCombinationList permittedList = new LaneCombinationList();
134 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
135 (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
136 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
137 (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
138 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
139 (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
140 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
141 (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
142 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
143 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)), ignoreList, permittedList);
144
145
146
147 }
148 catch (Exception exception)
149 {
150 exception.printStackTrace();
151 }
152 }
153
154
155 @Override
156 public OTSRoadNetwork getNetwork()
157 {
158 return this.network;
159 }
160
161
162 @Override
163 public Serializable getSourceId()
164 {
165 return "TestNetworkModel";
166 }
167
168 }
169 }