1 package org.opentrafficsim.road.gtu.lane.perception.mental;
2
3 import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVE;
4 import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVEZERO;
5
6 import java.util.LinkedHashSet;
7 import java.util.Set;
8
9 import org.djutils.exceptions.Throw;
10 import org.djutils.exceptions.Try;
11 import org.djutils.immutablecollections.Immutable;
12 import org.djutils.immutablecollections.ImmutableLinkedHashSet;
13 import org.djutils.immutablecollections.ImmutableSet;
14 import org.opentrafficsim.base.parameters.ParameterException;
15 import org.opentrafficsim.base.parameters.ParameterTypeDouble;
16 import org.opentrafficsim.base.parameters.Parameters;
17 import org.opentrafficsim.core.gtu.GTUException;
18 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
19 import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
20 import org.opentrafficsim.road.gtu.lane.perception.mental.TaskManager.SummativeTaskManager;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class Fuller implements Mental
45 {
46
47
48
49
50 public static final ParameterTypeDouble TC = new ParameterTypeDouble("TC", "Task capability", 1.0, POSITIVE);
51
52
53 public static final ParameterTypeDouble TS_CRIT =
54 new ParameterTypeDouble("TScrit", "Critical task saturation", 0.8, POSITIVEZERO)
55 {
56
57 private static final long serialVersionUID = 20180403L;
58
59
60 @Override
61 public void check(final Double value, final Parameters params) throws ParameterException
62 {
63 Double tsMax = params.getParameterOrNull(TS_MAX);
64 Throw.when(tsMax != null && value > tsMax, ParameterException.class,
65 "Value for TS_CRIT should not be larger than TS_MAX.");
66 }
67 };
68
69
70 public static final ParameterTypeDouble TS_MAX =
71 new ParameterTypeDouble("TSmax", "Maximum task saturation", 2.0, POSITIVEZERO)
72 {
73
74 private static final long serialVersionUID = 20180403L;
75
76
77 @Override
78 public void check(final Double value, final Parameters params) throws ParameterException
79 {
80 Double tsCrit = params.getParameterOrNull(TS_CRIT);
81 Throw.when(tsCrit != null && value < tsCrit, ParameterException.class,
82 "Value for TS_MAX should not be smaller than TS_CRIT.");
83 }
84 };
85
86
87 public static final ParameterTypeDouble TS = new ParameterTypeDouble("TS", "Task saturation", 0.0, POSITIVEZERO);
88
89
90
91
92 private final Set<Task> tasks;
93
94
95 private final Set<BehavioralAdaptation> behavioralAdapatations;
96
97
98 private final TaskManager taskManager;
99
100
101
102
103
104
105 public Fuller(final Set<? extends Task> tasks, final Set<BehavioralAdaptation> behavioralAdapatations)
106 {
107 this(tasks, behavioralAdapatations, new SummativeTaskManager());
108 }
109
110
111
112
113
114
115
116 public Fuller(final Set<? extends Task> tasks, final Set<BehavioralAdaptation> behavioralAdapatations,
117 final TaskManager taskManager)
118 {
119 Throw.whenNull(tasks, "Tasks may not be null.");
120 Throw.whenNull(behavioralAdapatations, "Behavioral adaptations may not be null.");
121 this.tasks = new LinkedHashSet<>();
122 this.tasks.addAll(tasks);
123 this.behavioralAdapatations = behavioralAdapatations;
124 this.taskManager = taskManager;
125 }
126
127
128
129
130
131 public void addTask(final Task task)
132 {
133 this.tasks.add(task);
134 }
135
136
137
138
139
140 public void removeTask(final Task task)
141 {
142 this.tasks.remove(task);
143 }
144
145
146
147
148
149 public ImmutableSet<Task> getTasks()
150 {
151 return new ImmutableLinkedHashSet<>(this.tasks, Immutable.WRAP);
152 }
153
154
155 @Override
156 public void apply(final LanePerception perception) throws ParameterException, GTUException
157 {
158 LaneBasedGTU gtu = Try.assign(() -> perception.getGtu(), "Could not obtain GTU.");
159 Parameters parameters = gtu.getParameters();
160 double taskDemand = 0.0;
161
162
163 this.taskManager.manage(this.tasks, perception, gtu, parameters);
164 for (Task task : this.tasks)
165 {
166 taskDemand += (task.getTaskDemand() - task.getAnticipationReliance());
167 }
168 double taskSaturation = taskDemand / parameters.getParameter(TC);
169 parameters.setParameter(TS, taskSaturation);
170
171 for (BehavioralAdaptation behavioralAdapatation : this.behavioralAdapatations)
172 {
173 behavioralAdapatation.adapt(parameters, taskSaturation);
174 }
175
176
177
178 }
179
180
181 @Override
182 public String toString()
183 {
184 return "Fuller [tasks=" + this.tasks + ", behavioralAdapatations=" + this.behavioralAdapatations + "]";
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199 @FunctionalInterface
200 public interface BehavioralAdaptation
201 {
202
203
204
205
206
207
208 void adapt(Parameters parameters, double taskSaturation) throws ParameterException;
209 }
210
211 }