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