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