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