View Javadoc
1   package org.opentrafficsim.road.network.lane.conflict;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
8   import org.opentrafficsim.core.geometry.OTSGeometryException;
9   import org.opentrafficsim.road.network.lane.CrossSectionLink.Priority;
10  import org.opentrafficsim.road.network.lane.Lane;
11  
12  import nl.tudelft.simulation.language.Throw;
13  import nl.tudelft.simulation.language.d3.DirectedPoint;
14  
15  /**
16   * Default determination of priority based on link priority, or right-hand traffic. Note that this class is stateful as the
17   * priorities are cached. So each conflict pair should receive a separate {@code DefaultConflictRule}. This rule is only for use
18   * on merge and crossing conflicts. For split conflicts there is a separate rule {@code SplitConflictRule}.
19   * <p>
20   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
22   * <p>
23   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 26 jan. 2017 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
27   */
28  public class DefaultConflictRule implements ConflictRule
29  {
30  
31      /** Priority per conflict. */
32      private Map<Conflict, ConflictPriority> map = null;
33  
34      // Throw.whenNull(priority1, "Conflict rule may not be null.");
35      // Throw.whenNull(priority2, "Conflict rule may not be null.");
36      // if (priority1.equals(ConflictPriority.SPLIT) || priority2.equals(ConflictPriority.SPLIT))
37      // {
38      // // Split with split (on split)
39      // Throw.when(!priority1.equals(ConflictPriority.SPLIT) || !priority2.equals(ConflictPriority.SPLIT),
40      // NetworkException.class, "Both conflict rules should be split for conflict type split.");
41      // }
42      // else
43      // {
44      // // Priority with give-way/stop
45      // boolean check1 = priority1.equals(ConflictPriority.PRIORITY) && !priority2.equals(ConflictPriority.GIVE_WAY)
46      // && !priority2.equals(ConflictPriority.STOP);
47      // boolean check2 = priority2.equals(ConflictPriority.PRIORITY) && !priority1.equals(ConflictPriority.GIVE_WAY)
48      // && !priority1.equals(ConflictPriority.STOP);
49      // boolean check3 =
50      // priority1.equals(ConflictPriority.GIVE_WAY) && !priority2.equals(ConflictPriority.PRIORITY);
51      // boolean check4 =
52      // priority2.equals(ConflictPriority.GIVE_WAY) && !priority1.equals(ConflictPriority.PRIORITY);
53      // boolean check5 = priority1.equals(ConflictPriority.STOP) && !priority2.equals(ConflictPriority.PRIORITY);
54      // boolean check6 = priority2.equals(ConflictPriority.STOP) && !priority1.equals(ConflictPriority.PRIORITY);
55      // Throw.when(check1 || check2 || check3 || check4 || check5 || check6, NetworkException.class,
56      // "Conflict rules need to be a combination of 'PRIORITY' and 'GIVE_WAY' or 'STOP', "
57      // + "if any of these types is used.");
58      // // All-stop with all-stop
59      // boolean check7 =
60      // priority1.equals(ConflictPriority.ALL_STOP) && !priority2.equals(ConflictPriority.ALL_STOP);
61      // boolean check8 =
62      // priority2.equals(ConflictPriority.ALL_STOP) && !priority1.equals(ConflictPriority.ALL_STOP);
63      // Throw.when(check7 || check8, NetworkException.class,
64      // "Conflict rule 'ALL_STOP' can only be combined with a conflict rule 'ALL_STOP'.");
65      // // No split
66      // Throw.when(priority1.equals(ConflictPriority.SPLIT) || priority2.equals(ConflictPriority.SPLIT),
67      // NetworkException.class, "Conflict rule 'SPLIT' may only be used on conflicts of type SPLIT.");
68      // }
69  
70      /** {@inheritDoc} */
71      @Override
72      public ConflictPriority determinePriority(final Conflict conflict)
73      {
74          if (this.map == null)
75          {
76              ConflictPriority[] conflictPriorities = getConflictRules(conflict.getLane(), conflict.getLongitudinalPosition(),
77                      conflict.getOtherConflict().getLane(), conflict.getOtherConflict().getLongitudinalPosition(),
78                      conflict.getConflictType());
79              this.map = new HashMap<>();
80              this.map.put(conflict, conflictPriorities[0]);
81              this.map.put(conflict.getOtherConflict(), conflictPriorities[1]);
82          }
83          Throw.when(!this.map.containsKey(conflict), IllegalArgumentException.class,
84                  "Conflict %s is not related to a conflict that was used before in the same conflict rule.", conflict);
85          return this.map.get(conflict);
86      }
87  
88      /**
89       * Determine conflict rules.
90       * @param lane1 lane 1
91       * @param longitudinalPosition1 position 1
92       * @param lane2 lane 2
93       * @param longitudinalPosition2 position 2
94       * @param conflictType conflict type
95       * @return conflict rule 1 and 2
96       */
97      private static ConflictPriority[] getConflictRules(final Lane lane1, final Length longitudinalPosition1, final Lane lane2,
98              final Length longitudinalPosition2, final ConflictType conflictType)
99      {
100         Throw.when(conflictType.equals(ConflictType.SPLIT), UnsupportedOperationException.class,
101                 "DefaultConflictRule is not for use on a split conflict. Use SplitConflictRule instead.");
102         ConflictPriority[] conflictRules = new ConflictPriority[2];
103         Priority priority1 = lane1.getParentLink().getPriority();
104         Priority priority2 = lane2.getParentLink().getPriority();
105         if (priority1.isAllStop() && priority2.isAllStop())
106         {
107             conflictRules[0] = ConflictPriority.ALL_STOP;
108             conflictRules[1] = ConflictPriority.ALL_STOP;
109         }
110         else if (priority1.equals(priority2))
111         {
112             // Based on right- or left-hand traffic
113             DirectedPoint p1;
114             DirectedPoint p2;
115             try
116             {
117                 p1 = lane1.getCenterLine().getLocation(longitudinalPosition1);
118                 p2 = lane2.getCenterLine().getLocation(longitudinalPosition2);
119             }
120             catch (OTSGeometryException exception)
121             {
122                 throw new RuntimeException("Conflict position is not on its lane.", exception);
123             }
124             double diff = p2.getRotZ() - p1.getRotZ();
125             while (diff > Math.PI)
126             {
127                 diff -= 2 * Math.PI;
128             }
129             while (diff < -Math.PI)
130             {
131                 diff += 2 * Math.PI;
132             }
133             if (diff > 0.0)
134             {
135                 // 2 comes from the right
136                 conflictRules[0] = priority1.isStop() ? ConflictPriority.STOP : ConflictPriority.GIVE_WAY;
137                 conflictRules[1] = ConflictPriority.PRIORITY;
138             }
139             else
140             {
141                 // 1 comes from the right
142                 conflictRules[0] = ConflictPriority.PRIORITY;
143                 conflictRules[1] = priority2.isStop() ? ConflictPriority.STOP : ConflictPriority.GIVE_WAY;
144             }
145         }
146         else if (priority1.isPriority() && (priority2.isNone() || priority2.isStop()))
147         {
148             conflictRules[0] = ConflictPriority.PRIORITY;
149             conflictRules[1] = priority2.isStop() ? ConflictPriority.STOP : ConflictPriority.GIVE_WAY;
150         }
151         else if (priority2.isPriority() && (priority1.isNone() || priority1.isStop()))
152         {
153             conflictRules[0] = priority1.isStop() ? ConflictPriority.STOP : ConflictPriority.GIVE_WAY;
154             conflictRules[1] = ConflictPriority.PRIORITY;
155         }
156         else if (priority1.isNone() && priority2.isStop())
157         {
158             conflictRules[0] = ConflictPriority.PRIORITY;
159             conflictRules[1] = ConflictPriority.STOP;
160         }
161         else if (priority2.isNone() && priority1.isStop())
162         {
163             conflictRules[0] = ConflictPriority.STOP;
164             conflictRules[1] = ConflictPriority.PRIORITY;
165         }
166         else
167         {
168             throw new RuntimeException(
169                     "Could not sort out conflict priority from link priorities " + priority1 + " and " + priority2);
170         }
171         return conflictRules;
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public ConflictRule clone(final OTSSimulatorInterface newSimulator)
177     {
178         return new DefaultConflictRule();
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public String toString()
184     {
185         return "DefaultConflictRule";
186     }
187 
188 }