Interface ConstraintInterface
-
- All Known Implementing Classes:
ParameterTypes
public interface ConstraintInterface
In order to define default constraints within a Parameter Type, anenum
is available. This interface supplies easy access to the values of thisenum
. To use this interface, simply implement it as below. The valuePOSITIVE
is a property of this interface pointing to theenum
fieldPOSITIVE
. As a result, the value that is set forX
is checked to be above zero. Note that model and parameterType do not have to be defined in the same class.public class myModel implements ConstraintInterface { public static final ParameterTypeLength X = new ParameterTypeLength("x", "My x parameter.", POSITIVE); // ... model that uses parameter of type X. }
Another way to access theenum
fields is to import them, e.g.:import static org.opentrafficsim.core.gtu.drivercharacteristics.AbstractParameterType.Constraint.POSITIVE;
In order to implement custom checks, any Parameter Type must extend thecheck
method of its super. An example is given below. The method should throw aParameterException
whenever a constraint is not met. The staticthrowIf
method is used to do this. The first check is a simple check on the SI value being above 2. The second check compares the value with the value of another parameter in theParameters
. These checks can only be performed if the other parameter is present in theParameters
. Checks with other parameter type values should always check whetherParameters
contains the other parameter type. i.e.params.contains()
.
public static final ParameterTypeLength X = new ParameterTypeLength("x", "My x parameter.") { public void check(Length value, Parameters params) throws ParameterException { Throw.when(value.si <= 2, ParameterException.class, "Value of X is not above 2."); Throw.when(params.contains(Y) && value.si > params.getParameter(Y).si, ParameterException.class, "Value of X is larger than value of Y."); } };
Checks are invoked on default values (if given), in which case an emptyParameters
is forwarded. At construction of a Parameter Type, noParameters
is available. Checks are also invoked when value are set intoParameters
, in which case thatParameters
forwards itself. Even still, if in the above case X is set before Y, the check is never performed. Therefore, Y should also compare itself to X. Two parameters that are checked with each other, should both implement a check which is consistent with and mirrored to the the others' check.
The type of the first argument in thecheck()
method depends on the super Parameter Type. For example:
double
forParameterTypeDouble
int
forParameterTypeInteger
Speed
forParameterTypeSpeed
Length
forParameterTypeLength
T
forParameterType<T>
ParameterTypeBoolean
has no check method as checks on booleans are senseless.
Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.- Version:
- $Revision$, $LastChangedDate$, by $Author$, initial version Apr 13, 2016
- Author:
- Alexander Verbraeck, Wouter Schakel
-
-
Field Summary
Fields Modifier and Type Field Description static Constraint<Number>
ATLEASTONE
static Constraint<Number>
NEGATIVE
static Constraint<Number>
NEGATIVEZERO
static Constraint<Number>
NONZERO
static Constraint<Number>
POSITIVE
static Constraint<Number>
POSITIVEZERO
static Constraint<Number>
UNITINTERVAL
-
-
-
Field Detail
-
POSITIVE
static final Constraint<Number> POSITIVE
-
NEGATIVE
static final Constraint<Number> NEGATIVE
-
POSITIVEZERO
static final Constraint<Number> POSITIVEZERO
-
NEGATIVEZERO
static final Constraint<Number> NEGATIVEZERO
-
NONZERO
static final Constraint<Number> NONZERO
-
ATLEASTONE
static final Constraint<Number> ATLEASTONE
-
UNITINTERVAL
static final Constraint<Number> UNITINTERVAL
-
-