例: Rule Designer での州別の市に関する値プロバイダーの実装

値プロバイダーの実装の例としては、市を表すロールまたは語句に対する値プロバイダーを宣言して、それに対する値情報を宣言するケースが挙げられます。

このタスクについて

この例では、市を表すロールまたは語句に対して値プロバイダーを宣言します。 市は、州別に階層構造で事前定義されています。

手順

州別の市の値プロバイダーを実装するには、以下のようにします。

  1. クラス City を定義し、IlrAbstractValueProvider を拡張して、 クラス StateCityValueProvider を作成します。
    public interface Named {
        String getName();
    }
    
    // Represents a city, this city is owned by a state
    public class City implements Named {
      public final State state;
      public final String name;
          
      public City(State state, String name) {
        this.state = state;
        this.name = name;
      }    
      
      public String getName() {
        return name;
      }
    }
    
    // Represents a state, a state has a list of cities
    public class State implements Named {
      private final String name;
      public City[] cities;
      
      public State(String name) {      
        this.name = name;		  
      }
      
      public String getName() {
        return name;
      }
    }
    
    // Represents a hierarchical predefined set of values for cities
    // The hierarchy has two levels, at the root level there are states, 
    // and each state has a list of cities as children 
    public class StateCityValueProvider extends IlrAbstractValueProvider {
      
      // The predefined list of states
      public static State[] STATES;
      
      // Returns the root level of the hierarchy (all the states)
      public Object[] getValues() {
        return STATES;
      }
    
      // Returns the children of a predefined value, 
      // in this case only a state can have children
      public Object[] getChildren(Object parent) {
        return parent instanceof State ? ((State) parent).cities : null;
      }
    
      // Returns the parent of a predefined value, in this case only a city 
      // can have a parent (a state)
      public Object getParent(Object element) {
        return element instanceof City ? ((City) element).state : null;
      }
    
      // Returns true, if a predefined value has children, 
      // in this case only a state can have children
      public boolean hasChildren(Object element) {
        return element instanceof State;
      }
    
      // Returns the text to be displayed 
      // in the drop-down list for that predefined value
      public String getDisplayText(Object element, Locale locale) {
        return ((Named) element).getName();
      }
      
      // Returns the text to be inserted in 
      // the text of the rule for that predefined value.
      // In this case only a city value can be inserted.
      public String getText(Object element, Locale locale) {
        return element instanceof City ? ('"' + ((City) element).name + '"') : 
    null;
      }
      
      // The predefined set of cities is not exclusive
      public boolean isExclusive() {
        return false;
      }  
    }
  2. 以下のように、StateCityValueProvider を値プロバイダーとして提供する値情報を宣言します。
    public class StateCityValueInfo implements IlrValueInfo {
      ...
      public String getValueDescriptor() {
           return IlrValueDescriptorConstants.STRING;
       }
      
      private StateCityValueProvider valueProvider;
      
      public IlrValueProvider getValueProvider() {
        if (valueProvider == null)
          valueProvider = new StateCityValueProvider();
        return valueProvider;
      }
    ...
    }
  3. この値情報を BOM メンバーに付加します。

    例えば、クラス Customer と、このカスタマーに 市を割り当てるメソッド void setCity(String city) がある場合、 以下のように、このメソッドにカスタム・プロパティー valueInfo[<arg index>] を定義します。

    valueInfo[0] = sample.valueInfo.StateCity
  4. 以下を使用して拡張ポイント ilog.rules.studio.model.brl.valueInfos を宣言することで、値情報を Rule Designer に統合します。

    ID: sample.valueInfo.StateCity

    キー: sample.valueInfo.StateCity

    クラス: sample.StateCityValueInfo