View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.utils;
2   
3   import java.lang.reflect.Field;
4   
5   import org.djunits.value.vdouble.scalar.Duration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djutils.reflection.ClassUtil;
8   import org.opentrafficsim.core.gtu.RelativePosition;
9   import org.opentrafficsim.core.network.NetworkException;
10  import org.opentrafficsim.road.gtu.generator.CFBARoomChecker;
11  import org.opentrafficsim.road.gtu.generator.CFRoomChecker;
12  import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.RoomChecker;
13  import org.opentrafficsim.road.gtu.generator.TTCRoomChecker;
14  import org.opentrafficsim.road.gtu.generator.headway.ArrivalsHeadwayGenerator.HeadwayDistribution;
15  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
16  import org.opentrafficsim.xml.bindings.types.GTUPositionType;
17  import org.opentrafficsim.xml.bindings.types.LengthBeginEnd;
18  
19  /**
20   * Transformer contains common transformations between intermediate classes created by the JAXB Adapters and OTS objects. <br>
21   * <br>
22   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
23   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
24   * source code and binary code of this software is proprietary information of Delft University of Technology.
25   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
26   */
27  public final class Transformer
28  {
29      /** */
30      private Transformer()
31      {
32          // utility class
33      }
34  
35      /**
36       * Parse LengthBeginEnd for a Lane.
37       * @param lbe LengthBeginEnd; the begin, end, fraction, or offset from begin or end on the lane
38       * @param laneLength Length; the length of the lane
39       * @return the offset on the lane
40       */
41      public static Length parseLengthBeginEnd(final LengthBeginEnd lbe, final Length laneLength)
42      {
43          if (lbe.isAbsolute())
44          {
45              if (lbe.isBegin())
46                  return lbe.getOffset();
47              else
48                  return laneLength.minus(lbe.getOffset());
49          }
50          else
51          {
52              return laneLength.multiplyBy(lbe.getFraction());
53          }
54      }
55  
56      /**
57       * @param positionType GTUPositionType; the JAXB position to parse
58       * @return the corresponding OTS RelativePosition
59       */
60      public static RelativePosition.TYPE parseTriggerPosition(final GTUPositionType positionType)
61      {
62          switch (positionType)
63          {
64              case FRONT:
65                  return RelativePosition.FRONT;
66  
67              case REAR:
68                  return RelativePosition.REAR;
69  
70              case REFERENCE:
71                  return RelativePosition.REFERENCE;
72  
73              default:
74                  return RelativePosition.REFERENCE;
75          }
76      }
77  
78      /**
79       * @param lkpStr String; the lane keeping policy string.
80       * @return the lane keeping policy.
81       * @throws NetworkException in case of unknown policy.
82       */
83      public static LaneKeepingPolicy parseLaneKeepingPolicy(final String lkpStr) throws NetworkException
84      {
85          if (lkpStr.equals("KEEPRIGHT"))
86          {
87              return LaneKeepingPolicy.KEEPRIGHT;
88          }
89          else if (lkpStr.equals("KEEPLEFT"))
90          {
91              return LaneKeepingPolicy.KEEPLEFT;
92          }
93          else if (lkpStr.equals("KEEPLANE"))
94          {
95              return LaneKeepingPolicy.KEEPLANE;
96          }
97          throw new NetworkException("Unknown lane keeping policy string: " + lkpStr);
98      }
99      
100     /**
101      * @param v String; XML string value
102      * @return RoomChecker; parsed room checker
103      */
104     public static RoomChecker parseRoomChecker(final String v)
105     {
106         if (v == null)
107         {
108             return null;
109         }
110         if (v.equals("CF"))
111         {
112             return new CFRoomChecker();
113         }
114         else if (v.equals("CFBA"))
115         {
116             return new CFBARoomChecker();
117         }
118         else if (v.equals("TTC"))
119         {
120             return new TTCRoomChecker(Duration.createSI(10));
121         }
122         return new TTCRoomChecker(Duration.valueOf(v.substring(4, v.length() - 1)));
123     }
124 
125     /**
126      * @param v String; XML string value
127      * @return RoomChecker; parsed room checker
128      * @throws NoSuchFieldException if {@code HeadwayDistribution} does not have specified field
129      * @throws IllegalAccessException if the field is not accessible
130      */
131     public static HeadwayDistribution parseHeadwayDistribution(final String v)
132             throws NoSuchFieldException, IllegalAccessException
133     {
134         if (v == null)
135         {
136             return null;
137         }
138         Field field = ClassUtil.resolveField(HeadwayDistribution.class, v);
139         return (HeadwayDistribution) field.get(null);
140     }
141 }