View Javadoc
1   package org.opentrafficsim.demo.conflict;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4   
5   import java.net.URL;
6   import java.rmi.RemoteException;
7   import java.util.ArrayList;
8   
9   import javax.naming.NamingException;
10  import javax.swing.SwingUtilities;
11  
12  import org.djunits.unit.DurationUnit;
13  import org.djunits.unit.LengthUnit;
14  import org.djunits.value.vdouble.scalar.Duration;
15  import org.djunits.value.vdouble.scalar.Length;
16  import org.djunits.value.vdouble.scalar.Time;
17  import org.opentrafficsim.base.modelproperties.Property;
18  import org.opentrafficsim.base.modelproperties.PropertyException;
19  import org.opentrafficsim.core.dsol.OTSModelInterface;
20  import org.opentrafficsim.core.network.OTSNetwork;
21  import org.opentrafficsim.road.animation.AnimationToggles;
22  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
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.simulationengine.AbstractWrappableAnimation;
27  import org.opentrafficsim.simulationengine.OTSSimulationException;
28  
29  import nl.tudelft.simulation.dsol.SimRuntimeException;
30  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
31  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
32  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
33  import nl.tudelft.simulation.language.io.URLResource;
34  
35  /**
36   * <p>
37   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
38   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
39   * <p>
40   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 dec. 2016 <br>
41   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
42   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
43   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
44   */
45  public class TestNetworkDemo extends AbstractWrappableAnimation
46  {
47  
48      /** */
49      private static final long serialVersionUID = 20161211L;
50  
51      /** {@inheritDoc} */
52      @Override
53      protected final OTSModelInterface makeModel() throws OTSSimulationException
54      {
55          return new TestNetworkModel();
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      protected final void addAnimationToggles()
61      {
62          AnimationToggles.setTextAnimationTogglesStandard(this);
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final String shortName()
68      {
69          return "Test network demonstration";
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public final String description()
75      {
76          return "Test network demonstration";
77      }
78  
79      /**
80       * The simulation model.
81       */
82      class TestNetworkModel implements OTSModelInterface
83      {
84  
85          /** */
86          private static final long serialVersionUID = 20161211L;
87  
88          /** The network. */
89          private OTSNetwork network;
90  
91          /** Simulator. */
92          private DEVSSimulatorInterface.TimeDoubleUnit simulator;
93  
94          /** {@inheritDoc} */
95          @Override
96          public void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> arg0)
97                  throws SimRuntimeException
98          {
99              this.simulator = (DEVSSimulatorInterface.TimeDoubleUnit) arg0;
100             try
101             {
102                 URL url = URLResource.getResource("/conflict/Test-Network-14.xml");
103                 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
104                 this.network = nlp.build(url, true);
105 
106                 LaneCombinationList ignoreList = new LaneCombinationList();
107                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
108                 // (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
109                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
110                 // (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
111                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
112                 // (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
113                 // ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
114                 // (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
115                 LaneCombinationList permittedList = new LaneCombinationList();
116                 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_D3b-D3a"),
117                         (CrossSectionLink) this.network.getLink("L_B3a-A3b"));
118                 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_A3a-D3a"),
119                         (CrossSectionLink) this.network.getLink("L_C3b-B3b"));
120                 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_H3b-H3a"),
121                         (CrossSectionLink) this.network.getLink("L_F3a-E3b"));
122                 permittedList.addLinkCombination((CrossSectionLink) this.network.getLink("L_E3a-H3a"),
123                         (CrossSectionLink) this.network.getLink("L_G3b-F3b"));
124                 ConflictBuilder.buildConflicts(this.network, VEHICLE, this.simulator,
125                         new ConflictBuilder.FixedWidthGenerator(new Length(2.0, LengthUnit.SI)), ignoreList, permittedList);
126                 // new ConflictBuilder.FixedWidthGenerator(new Length(1.0, LengthUnit.SI))
127                 // ConflictBuilder.DEFAULT_WIDTH_GENERATOR
128 
129             }
130             catch (Exception exception)
131             {
132                 exception.printStackTrace();
133             }
134         }
135 
136         /** {@inheritDoc} */
137         @Override
138         public SimulatorInterface<Time, Duration, SimTimeDoubleUnit> getSimulator()
139         {
140             return this.simulator;
141         }
142 
143         /** {@inheritDoc} */
144         @Override
145         public OTSNetwork getNetwork()
146         {
147             return this.network;
148         }
149 
150     }
151 
152     /**
153      * Main program.
154      * @param args String[]; the command line arguments (not used)
155      * @throws SimRuntimeException should never happen
156      */
157     public static void main(final String[] args) throws SimRuntimeException
158     {
159         SwingUtilities.invokeLater(new Runnable()
160         {
161             @Override
162             public void run()
163             {
164                 try
165                 {
166                     TestNetworkDemo animation = new TestNetworkDemo();
167                     // 1 hour simulation run for testing
168                     animation.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
169                             new ArrayList<Property<?>>(), null, true);
170                 }
171                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
172                 {
173                     exception.printStackTrace();
174                 }
175             }
176         });
177     }
178 
179 }