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