Example: Implementing a city by state value provider in Rule Designer

As an example of implementing a value provider, you can declare a value provider for a role or phrase that represents a city, and declare a value info for it.

About this task

In this example you declare a value provider for a role or phrase that represents a city. The predefined cities are structured hierarchically by state.

Procedure

To implement a city value provider by state:

  1. Define a class City and extend IlrAbstractValueProvider to create a StateCityValueProvider class:
    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. Declare a value info that provides the StateCityValueProvider as value provider:
    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. Attach the value info to the BOM member.

    For example, if you have a class Customer with a method that assigns a city to this customer void setCity(String city), define a custom property valueInfo[<arg index>] on this method:

    valueInfo[0] = sample.valueInfo.StateCity
  4. Integrate the value info into Rule Designer by declaring an extension point ilog.rules.studio.model.brl.valueInfos:

    ID: sample.valueInfo.StateCity

    Key: sample.valueInfo.StateCity

    Class: sample.StateCityValueInfo