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              animationPanel.enableSimulationControlButtons();
87          }
88          catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
89          {
90              exception.printStackTrace();
91          }
92      }
93  
94      /**
95       * The simulation model.
96       */
97      static class TestNetworkModel extends AbstractOTSModel
98      {
99  
100         /** */
101         private static final long serialVersionUID = 20161211L;
102 
103         /** The network. */
104         private OTSRoadNetwork network;
105 
106         /**
107          * @param simulator OTSSimulatorInterface; the simulator for this model
108          */
109         TestNetworkModel(final OTSSimulatorInterface simulator)
110         {
111             super(simulator);
112         }
113 
114         /** {@inheritDoc} */
115         @Override
116         public void constructModel() throws SimRuntimeException
117         {
118             try
119             {
120                 URL xmlURL = URLResource.getResource("/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                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
126                 // (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
127                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
128                 // (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
129                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
130                 // (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
131                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
132                 // (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
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                 // new ConflictBuilder.FixedWidthGenerator(new Length(1.0, LengthUnit.SI))
145                 // ConflictBuilder.DEFAULT_WIDTH_GENERATOR
146 
147             }
148             catch (Exception exception)
149             {
150                 exception.printStackTrace();
151             }
152         }
153 
154         /** {@inheritDoc} */
155         @Override
156         public OTSRoadNetwork getNetwork()
157         {
158             return this.network;
159         }
160 
161         /** {@inheritDoc} */
162         @Override
163         public Serializable getSourceId()
164         {
165             return "TestNetworkModel";
166         }
167 
168     }
169 }