1 package org.opentrafficsim.demo;
2
3 import java.util.List;
4
5 import org.djunits.value.vdouble.scalar.Acceleration;
6 import org.djunits.value.vdouble.scalar.Duration;
7 import org.djunits.value.vdouble.scalar.Length;
8 import org.opentrafficsim.base.modelproperties.CompoundProperty;
9 import org.opentrafficsim.base.modelproperties.Property;
10 import org.opentrafficsim.base.modelproperties.PropertyException;
11 import org.opentrafficsim.base.modelproperties.SelectionProperty;
12 import org.opentrafficsim.core.gtu.GTUException;
13 import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
14 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlannerFactory;
15 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingDirectedChangeTacticalPlannerFactory;
16 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlannerFactory;
17 import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractIDM;
18 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
19 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMOld;
20 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
21 import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
22 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Altruistic;
23 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
24 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
25 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLMRSPerceptionFactory;
26 import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
27 import org.opentrafficsim.road.gtu.lane.tactical.toledo.ToledoFactory;
28 import org.opentrafficsim.road.gtu.lane.tactical.util.TrafficLightUtil;
29 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
30 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
31 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
32 import org.opentrafficsim.road.modelproperties.IDMPropertySet;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public final class PropertiesParser
47 {
48
49 private PropertiesParser()
50 {
51
52 }
53
54
55
56
57
58
59
60
61 public static GTUFollowingModelOld parseGTUFollowingModelOld(final List<Property<?>> properties, final String gtuType)
62 throws PropertyException
63 {
64
65 String carFollowingModelName = null;
66 CompoundProperty propertyContainer = new CompoundProperty("", "", "", properties, false, 0);
67 Property<?> cfmp = propertyContainer.findByKey("CarFollowingModel");
68 if (null == cfmp)
69 {
70 throw new PropertyException("Cannot find \"Car following model\" property");
71 }
72 if (cfmp instanceof SelectionProperty)
73 {
74 carFollowingModelName = ((SelectionProperty) cfmp).getValue();
75 }
76 else
77 {
78 throw new Error("\"Car following model\" property has wrong type");
79 }
80
81
82 for (Property<?> ap : new CompoundProperty("", "", "", properties, false, 0))
83 {
84 if (ap instanceof CompoundProperty)
85 {
86 CompoundProperty cp = (CompoundProperty) ap;
87 if (ap.getKey().contains("IDM"))
88 {
89 Acceleration a = IDMPropertySet.getA(cp);
90 Acceleration b = IDMPropertySet.getB(cp);
91 Length s0 = IDMPropertySet.getS0(cp);
92 Duration tSafe = IDMPropertySet.getTSafe(cp);
93 GTUFollowingModelOld gtuFollowingModel = null;
94 if (carFollowingModelName.equals("IDM"))
95 {
96 gtuFollowingModel = new IDMOld(a, b, s0, tSafe, 1.0);
97 }
98 else if (carFollowingModelName.equals("IDM+"))
99 {
100 gtuFollowingModel = new IDMPlusOld(a, b, s0, tSafe, 1.0);
101 }
102 else
103 {
104 throw new PropertyException("Unknown gtu following model: " + carFollowingModelName);
105 }
106 if (ap.getKey().contains(gtuType))
107 {
108 return gtuFollowingModel;
109 }
110 }
111 }
112 }
113 throw new PropertyException("Cannot determine GTU following model for GTU type " + gtuType);
114 }
115
116
117
118
119
120
121
122 public static LaneChangeModel parseLaneChangeModel(final List<Property<?>> properties) throws PropertyException
123 {
124 CompoundProperty propertyContainer = new CompoundProperty("", "", "", properties, false, 0);
125 Property<?> cfmp = propertyContainer.findByKey("LaneChanging");
126 if (null == cfmp)
127 {
128 throw new PropertyException("Cannot find \"Lane changing\" property");
129 }
130 if (cfmp instanceof SelectionProperty)
131 {
132 String laneChangeModelName = ((SelectionProperty) cfmp).getValue();
133 if ("Egoistic".equals(laneChangeModelName))
134 {
135 return new Egoistic();
136 }
137 else if ("Altruistic".equals(laneChangeModelName))
138 {
139 return new Altruistic();
140 }
141 throw new PropertyException("Lane changing " + laneChangeModelName + " not implemented");
142 }
143 throw new PropertyException("\"Lane changing\" property has wrong type");
144 }
145
146
147
148
149
150
151
152
153
154
155 public static LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> parseStrategicalPlannerFactory(
156 final List<Property<?>> properties, final GTUFollowingModelOld gtuFollowingModel,
157 final LaneChangeModel laneChangeModel) throws PropertyException, GTUException
158 {
159 for (Property<?> ap : new CompoundProperty("", "", "", properties, false, 0))
160 {
161 if (ap instanceof SelectionProperty)
162 {
163 SelectionProperty sp = (SelectionProperty) ap;
164 if ("TacticalPlanner".equals(sp.getKey()))
165 {
166 String tacticalPlannerName = sp.getValue();
167 if ("IDM".equals(tacticalPlannerName))
168 {
169 return new LaneBasedStrategicalRoutePlannerFactory(
170 new LaneBasedGTUFollowingTacticalPlannerFactory(gtuFollowingModel));
171 }
172 else if ("MOBIL/IDM".equals(tacticalPlannerName))
173 {
174 return new LaneBasedStrategicalRoutePlannerFactory(
175 new LaneBasedCFLCTacticalPlannerFactory(gtuFollowingModel, laneChangeModel));
176 }
177 else if ("DIRECTED/IDM".equals(tacticalPlannerName))
178 {
179 return new LaneBasedStrategicalRoutePlannerFactory(
180 new LaneBasedGTUFollowingDirectedChangeTacticalPlannerFactory(gtuFollowingModel));
181 }
182 else if ("LMRS".equals(tacticalPlannerName))
183 {
184
185 BehavioralCharacteristics defaultBehavioralCFCharacteristics = new BehavioralCharacteristics();
186 defaultBehavioralCFCharacteristics.setDefaultParameters(AbstractIDM.class);
187 defaultBehavioralCFCharacteristics.setDefaultParameters(TrafficLightUtil.class);
188 return new LaneBasedStrategicalRoutePlannerFactory(
189 new LMRSFactory(new IDMPlusFactory(), defaultBehavioralCFCharacteristics,
190 new DefaultLMRSPerceptionFactory()));
191 }
192 else if ("Toledo".equals(tacticalPlannerName))
193 {
194 return new LaneBasedStrategicalRoutePlannerFactory(new ToledoFactory());
195 }
196 else
197 {
198 throw new PropertyException("Don't know how to create a " + tacticalPlannerName + " tactical planner");
199 }
200 }
201 }
202 }
203 throw new PropertyException("No TacticalPlanner key found in the properties");
204 }
205
206 }