例: 制約による市の値チェッカーの実装

値チェッカーの実装の例としては、指定された市に基づいて州を検査する値チェッカーを制約を使用して作成するケースが挙げられます。

このタスクについて

この例では、指定された市に基づいて州を検査する市の値チェッカーを実装します。 この検査を組み込むために、制約を実装します。

手順

市の値チェッカーを制約を使用して実装するには、以下のようにします。

  1. IlrConstraintValueChecker クラスを拡張します。 このクラスを使用して、同じ語句内にある別の値に従って値を検査します。
    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. StateCityValueChecker を値チェッカーとして提供する値情報を宣言します。
    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. この値情報プロパティーを保持する BOM メンバーに対してカスタム・プロパティーを宣言することによって、制約のある値とその参照を関連付けます。

    例えば、BOM クラス Customer に メソッド void setBillingAddress(java.lang.String state, java.lang.String city) が ある場合は、以下のカスタム・プロパティーを定義する必要があります。

    constraint[1] = 0

    valueInfo[1] = sample.valueInfo.StateCity

  4. 以下のように拡張ポイント ilog.rules.studio.model.brl.valueInfos を宣言することで、値情報を Rule Designer に統合します。

    ID: sample.valueInfo.StateCity

    キー: sample.valueInfo.StateCity

    クラス: sample.StateCityValueInfo