Chapter 35. Defining enumerations for drop-down lists in rule assets
Chapter 35. Defining enumerations for drop-down lists in rule assets Enumeration definitions in Business Central determine the possible values of fields for conditions or actions in guided rules, guided rule templates, and guided decision tables. An enumeration definition contains a fact.field mapping to a list of supported values that are displayed as a drop-down list in the relevant field of a rule asset. When a user selects a field that is based on the same fact and field as the enumeration definition, the drop-down list of defined values is displayed. You can define enumerations in Business Central or in the DRL source for your Red Hat Decision Manager project. Procedure In Business Central, go to Menu Design Projects and click the project name. Click Add Asset Enumeration . Enter an informative Enumeration name and select the appropriate Package . The package that you specify must be the same package where the required data objects and relevant rule assets have been assigned or will be assigned. Click Ok to create the enumeration. The new enumeration is now listed in the Enumeration Definitions panel of the Project Explorer . In the Model tab of the enumerations designer, click Add enum and define the following values for the enumeration: Fact : Specify an existing data object within the same package of your project with which you want to associate this enumeration. Open the Data Objects panel in the Project Explorer to view the available data objects, or create the relevant data object as a new asset if needed. Field : Specify an existing field identifier that you defined as part of the data object that you selected for the Fact . Open the Data Objects panel in the Project Explorer to select the relevant data object and view the list of available Identifier options. You can create the relevant identifier for the data object if needed. Context : Specify a list of values in the format ['string1','string2','string3'] or [integer1,integer2,integer3] that you want to map to the Fact and Field definitions. These values will be displayed as a drop-down list for the relevant field of the rule asset. For example, the following enumeration defines the drop-down values for applicant credit rating in a loan application decision service: Figure 35.1. Example enumeration for applicant credit rating in Business Central Example enumeration for applicant credit rating in the DRL source In this example, for any guided rule, guided rule template, or guided decision table that is in the same package of the project and that uses the Applicant data object and the creditRating field, the configured values are available as drop-down options: Figure 35.2. Example enumeration drop-down options in a guided rule or guided rule template Figure 35.3. Example enumeration drop-down options in a guided decision table 35.1. Advanced enumeration options for rule assets For advanced use cases with enumeration definitions in your Red Hat Decision Manager project, consider the following extended options for defining enumerations: Mapping between DRL values and values in Business Central If you want the enumeration values to appear differently or more completely in the Business Central interface than they appear in the DRL source, use a mapping in the format 'fact.field' : ['sourceValue1=UIValue1','sourceValue2=UIValue2', ... ] for your enumeration definition values. For example, in the following enumeration definition for loan status, the options A or D are used in the DRL file but the options Approved or Declined are displayed in Business Central: Enumeration value dependencies If you want the selected value in one drop-down list to determine the available options in a subsequent drop-down list, use the format 'fact.fieldB[fieldA=value1]' : ['value2', 'value3', ... ] for your enumeration definition. For example, in the following enumeration definition for insurance policies, the policyType field accepts the values Home or Car . The type of policy that the user selects determines the policy coverage field options that are then available: Note Enumeration dependencies are not applied across rule conditions and actions. For example, in this insurance policy use case, the selected policy in the rule condition does not determine the available coverage options in the rule actions, if applicable. External data sources in enumerations If you want to retrieve a list of enumeration values from an external data source instead of defining the values directly in the enumeration definition, on the class path of your project, add a helper class that returns a java.util.List list of strings. In the enumeration definition, instead of specifying a list of values, identify the helper class that you configured to retrieve the values externally. For example, in the following enumeration definition for loan applicant region, instead of defining applicant regions explicitly in the format 'Applicant.region' : ['country1', 'country2', ... ] , the enumeration uses a helper class that returns the list of values defined externally: In this example, a DataHelper class contains a getListOfRegions() method that returns a list of strings. The enumerations are loaded in the drop-down list for the relevant field in the rule asset. You can also load dependent enumeration definitions dynamically from a helper class by identifying the dependent field as usual and enclosing the call to the helper class within quotation marks: If you want to load all enumeration data entirely from an external data source, such as a relational database, you can implement a Java class that returns a Map<String, List<String>> map. The key of the map is the fact.field mapping and the value is a java.util.List<String> list of values. For example, the following Java class defines loan applicant regions for the related enumeration: public class SampleDataSource { public Map<String, List<String>> loadData() { Map data = new HashMap(); List d = new ArrayList(); d.add("AU"); d.add("DE"); d.add("ES"); d.add("UK"); d.add("US"); ... data.put("Applicant.region", d); return data; } } The following enumeration definition correlates to this example Java class. The enumeration contains no references to fact or field names because they are defined in the Java class: The = operator enables Business Central to load all enumeration data from the helper class. The helper methods are statically evaluated when the enumeration definition is requested for use in an editor. Note Defining an enumeration without a fact and field definition is currently not supported in Business Central. To define the enumeration for the associated Java class in this way, use the DRL source in your Red Hat Decision Manager project.
'Applicant.creditRating' : ['AA', 'OK', 'Sub prime'] 'Loan.status' : ['A=Approved','D=Declined'] 'Insurance.policyType' : ['Home', 'Car'] 'Insurance.coverage[policyType=Home]' : ['property', 'liability'] 'Insurance.coverage[policyType=Car]' : ['collision', 'fullCoverage'] 'Applicant.region' : (new com.mycompany.DataHelper()).getListOfRegions() 'Applicant.region[countryCode]' : '(new com.mycompany.DataHelper()).getListOfRegions("@{countryCode}")' public class SampleDataSource { public Map<String, List<String>> loadData() { Map data = new HashMap(); List d = new ArrayList(); d.add("AU"); d.add("DE"); d.add("ES"); d.add("UK"); d.add("US"); data.put("Applicant.region", d); return data; } } =(new SampleDataSource()).loadData()