Example: Implementing a city value checker with a constraint

As an example of implementing a value checker, you can create a value checker that checks a state based on a given city, using a constraint.

About this task

In this example you implement a city value checker that checks a state based on a given city. To include the check, you implement a constraint.

Procedure

To implement a city value checker with a constraint:

  1. Extend the class IlrConstraintValueChecker. You use this class to check a value according to another value within the same phrase.
    public class CityValueChecker extends IlrConstraintValueChecker {
      
      // Returns true if the proposed city (value) is owned by 
      // the state given in reference (ref) 
      protected boolean accept(Object ref, Object value) {
        City city = <retrieve the city by its name>(value.toString());
        if (city != null) {
          return city.state.getName().equals(ref.toString());
        }
        return false;
      }
    
      // Checks if the city exists 
      public boolean check(Object value, 
                           IlrSyntaxTree.Node node,
                           IlrValueError valueError) {
        if (super.check(value, node, valueError)) { 
      // Checks the constraint, the city according to the state
      // if the city exists, the state can not be filled and in 
      // that case the constraint is not checked
        boolean exists =
             StateCityValueProvider.getCityByName(value.toString()) != null;
        if (!exists) {
      // If the city does not exist fill the value error argument
            valueError.setReason("This city does not exist");
          }
          return exists;
        } else {
          return false;
        }
      }
    }
  2. Declare a value info that provides the StateCityValueChecker as value checker.
    public class StateCityValueInfo implements IlrValueInfo {
      ...
      public String getValueDescriptor() {
           return IlrValueDescriptorConstants.STRING;
       }
      
      private StateCityValueChecker valueChecker;
      
      public IlrValueChecker getValueChecker() {
        if (valueChecker == null)
          valueChecker = new StateCityValueChecker();
        return valueChecker;
      }
    ...
    }
  3. Associate the constrained value with its reference by declaring a custom property on the BOM member that holds the value info property.

    For example, if a BOM class Customer has a method void setBillingAddress(java.lang.String state, java.lang.String city), you must define the following custom properties:

    constraint[1] = 0

    valueInfo[1] = sample.valueInfo.StateCity

  4. Integrate the value info in Rule Designer by declaring an extension point ilog.rules.studio.model.brl.valueInfos:

    ID: sample.valueInfo.StateCity

    Key: sample.valueInfo.StateCity

    Class: sample.StateCityValueInfo