1 package org.opentrafficsim.core.gtu;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.Acceleration;
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.djutils.exceptions.Throw;
9 import org.opentrafficsim.base.parameters.ParameterException;
10 import org.opentrafficsim.core.distributions.ConstantGenerator;
11 import org.opentrafficsim.core.distributions.Generator;
12 import org.opentrafficsim.core.distributions.ProbabilityException;
13
14
15
16
17
18
19
20
21
22
23 public class GtuTemplate implements Serializable, Generator<GtuCharacteristics>
24 {
25
26 private static final long serialVersionUID = 20141230L;
27
28
29 private final GtuType gtuType;
30
31
32 private final Generator<Length> lengthGenerator;
33
34
35 private final Generator<Length> widthGenerator;
36
37
38 private final Generator<Speed> maximumSpeedGenerator;
39
40
41 private final Generator<Acceleration> maxAcceleration;
42
43
44 private final Generator<Acceleration> maxDeceleration;
45
46
47
48
49
50
51
52
53
54
55
56 public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator,
57 final Generator<Length> widthGenerator, final Generator<Speed> maximumSpeedGenerator) throws NullPointerException
58 {
59 this(gtuType, lengthGenerator, widthGenerator, maximumSpeedGenerator,
60 new ConstantGenerator<>(Acceleration.instantiateSI(3.0)),
61 new ConstantGenerator<>(Acceleration.instantiateSI(-8.0)));
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator,
77 final Generator<Length> widthGenerator, final Generator<Speed> maximumSpeedGenerator,
78 final Generator<Acceleration> maximumAccelerationGenerator,
79 final Generator<Acceleration> maximumDecelerationGenerator) throws NullPointerException
80 {
81 Throw.whenNull(gtuType, "gtuType is null");
82 Throw.whenNull(lengthGenerator, "lengthGenerator is null");
83 Throw.whenNull(widthGenerator, "widthGenerator is null");
84 Throw.whenNull(maximumSpeedGenerator, "maximumSpeedGenerator is null");
85 Throw.whenNull(maximumAccelerationGenerator, "maximumAccelerationGenerator is null");
86 Throw.whenNull(maximumDecelerationGenerator, "maximumDecelerationGenerator is null");
87
88 this.gtuType = gtuType;
89 this.lengthGenerator = lengthGenerator;
90 this.widthGenerator = widthGenerator;
91 this.maximumSpeedGenerator = maximumSpeedGenerator;
92 this.maxAcceleration = maximumAccelerationGenerator;
93 this.maxDeceleration = maximumDecelerationGenerator;
94 }
95
96
97
98
99
100
101
102 public GtuTemplate copyForGtuType(final GtuType newGtuType)
103 {
104 return new GtuTemplate(newGtuType, this.lengthGenerator, this.widthGenerator, this.maximumSpeedGenerator,
105 this.maxAcceleration, this.maxDeceleration);
106 }
107
108
109
110
111
112
113
114 @Override
115 @SuppressWarnings("checkstyle:designforextension")
116 public GtuCharacteristics draw() throws ProbabilityException, ParameterException
117 {
118 Acceleration acceleration = this.maxAcceleration.draw();
119 Acceleration deceleration = this.maxDeceleration.draw();
120 Throw.when(acceleration.si <= 0, IllegalArgumentException.class, "Acceleration should be above 0.");
121 Throw.when(deceleration.si >= 0, IllegalArgumentException.class, "Deceleration should be below 0.");
122 Length length = this.lengthGenerator.draw();
123 return new GtuCharacteristics(this.gtuType, length, this.widthGenerator.draw(), this.maximumSpeedGenerator.draw(),
124 acceleration, deceleration, length.times(0.75));
125 }
126
127
128
129
130 @SuppressWarnings("checkstyle:designforextension")
131 public GtuType getGtuType()
132 {
133 return this.gtuType;
134 }
135
136
137 @Override
138 @SuppressWarnings("checkstyle:designforextension")
139 public String toString()
140 {
141 return String.format("TemplateGTUType [%s, %s, %s, %s]", this.gtuType, this.lengthGenerator, this.widthGenerator,
142 this.maximumSpeedGenerator);
143 }
144
145 }