Interface ConstraintInterface
-
- All Known Implementing Classes:
ParameterTypes
public interface ConstraintInterfaceIn order to define default constraints within a Parameter Type, anenumis available. This interface supplies easy access to the values of thisenum. To use this interface, simply implement it as below. The valuePOSITIVEis a property of this interface pointing to theenumfieldPOSITIVE. As a result, the value that is set forXis 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 theenumfields 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 thecheckmethod of its super. An example is given below. The method should throw aParameterExceptionwhenever a constraint is not met. The staticthrowIfmethod 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 whetherParameterscontains 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 emptyParametersis forwarded. At construction of a Parameter Type, noParametersis available. Checks are also invoked when value are set intoParameters, in which case thatParametersforwards 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:
doubleforParameterTypeDoubleintforParameterTypeIntegerSpeedforParameterTypeSpeedLengthforParameterTypeLengthTforParameterType<T>
ParameterTypeBooleanhas 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<java.lang.Number>ATLEASTONEstatic Constraint<java.lang.Number>NEGATIVEstatic Constraint<java.lang.Number>NEGATIVEZEROstatic Constraint<java.lang.Number>NONZEROstatic Constraint<java.lang.Number>POSITIVEstatic Constraint<java.lang.Number>POSITIVEZEROstatic Constraint<java.lang.Number>UNITINTERVAL
-
-
-
Field Detail
-
POSITIVE
static final Constraint<java.lang.Number> POSITIVE
-
NEGATIVE
static final Constraint<java.lang.Number> NEGATIVE
-
POSITIVEZERO
static final Constraint<java.lang.Number> POSITIVEZERO
-
NEGATIVEZERO
static final Constraint<java.lang.Number> NEGATIVEZERO
-
NONZERO
static final Constraint<java.lang.Number> NONZERO
-
ATLEASTONE
static final Constraint<java.lang.Number> ATLEASTONE
-
UNITINTERVAL
static final Constraint<java.lang.Number> UNITINTERVAL
-
-