Example: Show or Hide an Attribute

Example: Show or Hide an Attribute

This example shows how to show or hide an attribute control based on the value of another attribute control without the need for submitting the screen.

Create a project

First create a project that comprises a screen with two questions so we can hide/show one question based on the value of the other question.

Create the rulebase

To create the rulebase, do the following:

  1. Create a new project inside Oracle Policy Modeling.
  2. Add a new properties file.
  3. Open it and add the text attribute "the person's gender" to the global entity. Right click on the attribute and Copy Attribute ID.
  4. Add the text attribute "the person's name" to the global entity. In the Gender attribute field, paste the attribute ID from the above step. This gives automatic access to gender attribute values male and female.
  5. Create a new Word rule document and open it.
  6. Add the following rule to the Word document:

the person is eligible for maternity leave if

       the person is pregnant

  1. Click on Shortcut Rule in the Oracle Policy Modeling tab, add a rule to infer that if the gender is male then the person cannot be pregnant:

shortcut rule

the person is not pregnant if

       the person's gender = "male"

  1. Compile the Word document and correct any errors.
  2. Close the Word document.
  3. Open the properties file. Right click in the Attributes area and Generate Public Names.
  4. Rename the automatically generated public names as below:
    1. the person is pregnant -> person_pregnant.
    2. the person is eligible for maternity leave -> eligible_maternity_leave.

Create the screens

To create the screens, do the following:

  1. Inside the project, create a new screens file and open it.
  2. Create a question screen to collect a person's name and find out the gender of the person and whether they are pregnant or not. These attributes will be collected on the same question screen.
    1. a text control for "the person's name".
    2. "the person's gender" should have a radio buttons of two items - "male" and "female".
    3. "the person is pregnant" should be a regular boolean question.
  3. Create a summary screen to find out whether the person is eligible for maternity leave or not.
    1. Add the goal "the person is eligible for maternity leave"
  4. Build & debug to ensure the rulebase is working correctly. If there are any errors, fix them before proceeding with the steps below.

 

We will now add some custom properties that will tell us how the visibility of one attribute is dictated by another one. Based on whether the person chooses their gender as male or female, the question about whether the person is pregnant or nor either hidden or shown. If male, the question about being pregnant is no longer applicable. We do this by adding the following three custom properties.

 

Next, go to File -> Project Properties -> Screen and add the following property name-value pairs to the screen:

 

Name

Default_value

AttributeToCheck gender
AttributeToShowOrHide person_pregnant
ExpectedAttributeValue

female

 

The sample rulebase can be found at the following location in either the Java or .NET runtime zip file: examples\rulebases\source\ShowHideAttributeExample.zip.

Modify the velocity templates

The Velocity templates need to be modified to recognize when a person chooses answer to one question, and based on that either hide or show the second question. The files for this example can be found at examples\web-determinations\custom-show-hide-control in both the Java and .NET runtime zip files

 

The customization comprises the following files:

File

Description

templates/investigation/form.vm This is the modified version of form.vm from the web-determinations template. This collects the custom properties set for a screen. If the control being displayed is the same as the one associated with AttributeToCheck, it attaches a click handler to the control. When an attribute value is selected, the click handler gets fired and sets the visibility of the control associated with AttributeToShowOrHide based on the selected value and the expected value.
templates/javascript/clickHandler.js Javascript code for attaching the click handler and controlling the visibility.

templates/question_screen.vm

Modified to include the javascript file.

 

Run the example

To run the example use the ShowHideAttributeExample project under examples\rulebases\source in the runtime zip file, do the following:

  1. Open the project in Oracle Policy Modeling and Build and Debug with screens; this will create a Release directory.
  2. Copy over the templates directory (in the Java or .NET runtime zip file) from:
    examples\web-determinations\custom-show-hide-control\src
    to:
    examples\rulebases\ShowHideAttributeExample\Release\web-determinations\WEB-INF\classes
  3. Build and (Debug or Run) with screens