View Javadoc
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   * <p>
35   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
37   * <p>
38   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 dec. 2016 <br>
39   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
40   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
41   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
42   */
43  public class TestNetworkDemo extends OTSSimulationApplication<TestNetworkModel>
44  {
45      /** */
46      private static final long serialVersionUID = 20161211L;
47  
48      /**
49       * Create a network test demo.
50       * @param title String; the title of the Frame
51       * @param panel OTSAnimationPanel; the tabbed panel to display
52       * @param model TestNetworkModel; the model
53       * @throws OTSDrawingException on animation error
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       * Main program.
63       * @param args String[]; the command line arguments (not used)
64       */
65      public static void main(final String[] args)
66      {
67          demo(true);
68      }
69  
70      /**
71       * Start the demo.
72       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
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       * The simulation model.
95       */
96      static class TestNetworkModel extends AbstractOTSModel
97      {
98  
99          /** */
100         private static final long serialVersionUID = 20161211L;
101 
102         /** The network. */
103         private OTSRoadNetwork network;
104 
105         /**
106          * @param simulator OTSSimulatorInterface; the simulator for this model
107          */
108         TestNetworkModel(final OTSSimulatorInterface simulator)
109         {
110             super(simulator);
111         }
112 
113         /** {@inheritDoc} */
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                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
125                 // (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
126                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
127                 // (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
128                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
129                 // (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
130                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
131                 // (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
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                 // new ConflictBuilder.FixedWidthGenerator(new Length(1.0, LengthUnit.SI))
144                 // ConflictBuilder.DEFAULT_WIDTH_GENERATOR
145 
146             }
147             catch (Exception exception)
148             {
149                 exception.printStackTrace();
150             }
151         }
152 
153         /** {@inheritDoc} */
154         @Override
155         public OTSRoadNetwork getNetwork()
156         {
157             return this.network;
158         }
159 
160         /** {@inheritDoc} */
161         @Override
162         public Serializable getSourceId()
163         {
164             return "TestNetworkModel";
165         }
166 
167     }
168 }