1 package org.opentrafficsim.road.gtu.perception.mental;
2
3 import static org.opentrafficsim.base.parameters.constraint.NumericConstraint.POSITIVEZERO;
4
5 import java.util.LinkedHashMap;
6 import java.util.LinkedHashSet;
7 import java.util.Map;
8 import java.util.Optional;
9 import java.util.Set;
10
11 import org.djutils.exceptions.Throw;
12 import org.djutils.immutablecollections.Immutable;
13 import org.djutils.immutablecollections.ImmutableLinkedHashSet;
14 import org.djutils.immutablecollections.ImmutableSet;
15 import org.opentrafficsim.base.parameters.ParameterException;
16 import org.opentrafficsim.base.parameters.ParameterTypeDouble;
17 import org.opentrafficsim.base.parameters.Parameters;
18 import org.opentrafficsim.road.gtu.perception.LanePerception;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class SumFuller<T extends Task> extends Fuller
43 {
44
45
46 public static final ParameterTypeDouble TS_CRIT =
47 new ParameterTypeDouble("TScrit", "Critical task saturation", 0.8, POSITIVEZERO)
48 {
49 @Override
50 public void check(final Double value, final Parameters params) throws ParameterException
51 {
52 Optional<Double> tsMax = params.getOptionalParameter(TS_MAX);
53 Throw.when(tsMax.isPresent() && value > tsMax.get(), ParameterException.class,
54 "Value for TS_CRIT should not be larger than TS_MAX.");
55 }
56 };
57
58
59 public static final ParameterTypeDouble TS_MAX =
60 new ParameterTypeDouble("TSmax", "Maximum task saturation", 2.0, POSITIVEZERO)
61 {
62 @Override
63 public void check(final Double value, final Parameters params) throws ParameterException
64 {
65 Optional<Double> tsCrit = params.getOptionalParameter(TS_CRIT);
66 Throw.when(tsCrit.isPresent() && value < tsCrit.get(), ParameterException.class,
67 "Value for TS_MAX should not be smaller than TS_CRIT.");
68 }
69 };
70
71
72 private final Set<T> tasks;
73
74
75 private final Map<String, T> taskMap;
76
77
78
79
80
81
82 public SumFuller(final Set<T> tasks, final Set<BehavioralAdaptation> behavioralAdapatations)
83 {
84 super(behavioralAdapatations);
85 Throw.whenNull(tasks, "Tasks may not be null.");
86 this.tasks = new LinkedHashSet<>();
87 this.tasks.addAll(tasks);
88 this.taskMap = new LinkedHashMap<>();
89 tasks.forEach((t) -> this.taskMap.put(t.getId(), t));
90 }
91
92 @Override
93 protected double getTotalTaskDemand(final LanePerception perception) throws ParameterException
94 {
95 double taskDemand = 0.0;
96 for (T task : this.tasks)
97 {
98 taskDemand += task.getTaskDemand(perception);
99 }
100 return taskDemand;
101 }
102
103
104
105
106
107 public void addTask(final T task)
108 {
109 this.tasks.add(task);
110 }
111
112
113
114
115
116 public void removeTask(final T task)
117 {
118 this.tasks.remove(task);
119 }
120
121 @Override
122 public ImmutableSet<T> getTasks()
123 {
124 return new ImmutableLinkedHashSet<>(this.tasks, Immutable.WRAP);
125 }
126
127 @Override
128 public Optional<Task> getTask(final String taskId)
129 {
130 return Optional.ofNullable(this.taskMap.get(taskId));
131 }
132
133 @Override
134 public String toString()
135 {
136 return "SumFuller [tasks=" + this.tasks + "]";
137 }
138
139 }