Interface ConstraintInterface

All Known Implementing Classes:
ParameterTypes

public interface ConstraintInterface
In order to define default constraints within a Parameter Type, an enum is available. This interface supplies easy access to the values of this enum. To use this interface, simply implement it as below. The value POSITIVE is a property of this interface pointing to the enum field POSITIVE. As a result, the value that is set for X 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 the enum 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 the check method of its super. An example is given below. The method should throw a ParameterException whenever a constraint is not met. The static throwIf 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 the Parameters. These checks can only be performed if the other parameter is present in the Parameters. Checks with other parameter type values should always check whether Parameters 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 empty Parameters is forwarded. At construction of a Parameter Type, no Parameters is available. Checks are also invoked when value are set into Parameters, in which case that Parameters 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 the check() method depends on the super Parameter Type. For example:
  • double for ParameterTypeDouble
  • int for ParameterTypeInteger
  • Speed for ParameterTypeSpeed
  • Length for ParameterTypeLength
  • T for ParameterType<T>
Note that ParameterTypeBoolean has no check method as checks on booleans are senseless.

Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Alexander Verbraeck, Wouter Schakel