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