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
21
22
23
24
25
26
27 public final class Transformer
28 {
29
30 private Transformer()
31 {
32
33 }
34
35
36
37
38
39
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
58
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
80
81
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
102
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
127
128
129
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 }