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 TestNetworkDemoestNetworkDemo.html#TestNetworkDemo">TestNetworkDemo app = new TestNetworkDemo("Network test demo", animationPanel, networkModel);
85 app.setExitOnClose(exitOnClose);
86 }
87 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
88 {
89 exception.printStackTrace();
90 }
91 }
92
93
94
95
96 static class TestNetworkModel extends AbstractOTSModel
97 {
98
99
100 private static final long serialVersionUID = 20161211L;
101
102
103 private OTSRoadNetwork network;
104
105
106
107
108 TestNetworkModel(final OTSSimulatorInterface simulator)
109 {
110 super(simulator);
111 }
112
113
114 @Override
115 public void constructModel() throws SimRuntimeException
116 {
117 try
118 {
119 URL xmlURL = URLResource.getResource("/conflict/Test-Network-14.xml");
120 this.network = new OTSRoadNetwork("Test-Network-14", true, getSimulator());
121 XmlNetworkLaneParser.build(xmlURL, this.network, false);
122
123 LaneCombinationList ignoreList = new LaneCombinationList();
124
125
126
127
128
129
130
131
132 LaneCombinationList permittedList = new LaneCombinationList();
133 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
134 (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
135 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
136 (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
137 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
138 (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
139 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
140 (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
141 ConflictBuilder.buildConflicts(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE), this.simulator,
142 new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)), ignoreList, permittedList);
143
144
145
146 }
147 catch (Exception exception)
148 {
149 exception.printStackTrace();
150 }
151 }
152
153
154 @Override
155 public OTSRoadNetwork getNetwork()
156 {
157 return this.network;
158 }
159
160
161 @Override
162 public Serializable getSourceId()
163 {
164 return "TestNetworkModel";
165 }
166
167 }
168 }