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