Create an Extension Rule

In this example, you define an extension rule that calculates the effect of applying different discounts to the list price of an item, in order to arrive at the sale price.

The formula used in this example to calculate the discount is simple, but you can use this technique to construct more complex extension rule functionality, through the script in your rule text.

The tasks in this example are:

  1. Define supplemental features for list price, discount, and sale price.

  2. Create an extension rule, with the discount feature node as the base node.

  3. Write a Groovy script that defines a discounting function and applies it to the supplemental features.

  4. Define event and argument bindings for the function.

  5. Test the rule.

Caution: This simplified example is for instructional purposes only. The pricing information displayed in Configurator at run time is normally provided by integration with Oracle Pricing.

Defining Supplemental Features

The supplemental features are used for the end user inputs for list price and discount, and the calculated output of the sale price.

  1. On the Overview page of the Configurator Models work area, select Create from the Actions menu, to create a new workspace. Set the Effective Start Date to tomorrow's date.

  2. Open the workspace. On the Workspace page, select Select and Add Models from the Actions menu.

  3. On the Select and Add: Models page, search for a model, select it, then click OK, to add it to the workspace. Ignore any warning about drafts in other workspaces.

  4. On the Workspace page, click the model's name to open it for editing.

  5. On the Edit Configurator Model page, select the root node of the model, and select Create Decimal Feature from the Actions menu.

  6. Create the following decimal features, as shown in these tables.

    Field

    Value

    Name

    List Price

    Minimum

    20,000

    Maximum

    60.000

    Field

    Value

    Name

    Discount

    Minimum

    0

    Maximum

    10

    Field

    Value

    Name

    Sale Price

    Minimum

    0

    Maximum

    100.000

  7. Click Save.

Creating the Extension Rule

The extension rule will apply the discount to the list price, and put the result in the sale price.

  1. On the Rules tab of the Edit Configurator Model page, select Create Extension Rule from the Rules toolbar.

  2. Enter a Name for the extension rule, such as Apply Discount.

  3. In the Structure palette, expand the model tree, select the node Discount, then right-click and select Set as Base Node from the context menu.

  4. Click Save.

Writing the Rule Text

The behavior of the extension rule is defined in the script entered in the Rule Text field, which is written in the Groovy scripting language.

  1. In the Rule Text field, enter the following script.

    // Import the needed Configurator interfaces
    import oracle.apps.scm.configurator.runtime.core.IConfiguration
    import oracle.apps.scm.configurator.runtime.core.IBomModelInstance
    import oracle.apps.scm.configurator.runtime.core.IDecimalFeature
    
    // Define the discounting function.
    def applyDiscount ( p_listPrice, p_discount ) {
    
    // Get values of nodes from arguments.
       double listPrice = ((IDecimalFeature)p_listPrice).getValue()
       double discount = ((IDecimalFeature)p_discount).getValue()
       double salePrice = 0
    
    // Calculate the price.
        salePrice = listPrice - ( listPrice * ( discount / 100 ) )
    
    // Get the node whose value will be set.
        IConfiguration config = cxEvent.getConfiguration()
        IBomModelInstance root = config.getRootBomModel()
        IDecimalFeature salePriceNode = root.getChildByName("Sale Price")
    
    // Set the value.
        ((IDecimalFeature)salePriceNode).setDecimalValue(salePrice)
    }
  2. Click Save.

  3. Click Validate.

  4. You should receive the error message The rule is invalid. The extension rule must have at least one event binding defined.

Defining the Event Binding

To make the script execute, you must bind it to a configurator event. So now you add an event binding to the rule.

  1. In the Event Bindings table, click Create

  2. In the new row for the event binding, from the Event list, select postValueChange.

  3. From the Event Scope list, select Base node.

  4. From the Class list, select ScriptClass.

  5. From the Method list, select applyDiscount. The arguments p_listPrice and p_discount are displayed with the function name.

  6. Click Save.

Defining the Argument Bindings

When you selected the applyDiscount method in the Event Bindings table, the Argument Bindings table should have automatically appeared, populated with a row for each argument

  1. In the Argument Bindings table, select the row for the argument named p_listPrice.

  2. From the Specification column for that row, select Model node.

  3. Expand the model tree in the Structure pane, and select the node List Price, then right-click the node and select Set as Argument Value.

  4. Click Validate.

  5. You should receive the error message The rule is invalid. The node referenced in the argument p_discount bound to the event postValueChange was not found.

  6. Repeat the preceding argument binding steps for the argument p_discount and the node Discount.

  7. Click Validate.

  8. You should receive the information message No errors were detected.

  9. Click Save and Compile.

  10. You should receive the confirmation message Model compilation has completed without errors..

Testing the Model

Test the model to verify the functionality of the extension rule.

  1. On the top of the Edit Configurator Model page, select Test Model.

  2. In the Test Model dialog box, ensure that User Interface is set to Default, then click OK.

  3. On the Test Model page, fields for the three decimal features that you added should appear, in the sequence that you created them:

    • List Price

    • Discount

    • Sale Price

  4. In the List Price field, enter the value 30,000. Notice that you can only enter values between the minimum and maximum that you defined when you created the decimal feature.

  5. In the Discount field, enter the value 5. Notice again that the allowable values are between the minimum and maximum that you defined.

  6. When you press Enter in the Discount field, or tab out of it, notice that the value of the Sale Price field changes, from empty to 28500.

  7. Enter a different value in the Discount field, and notice the changes in the Sale price field. This change happens because you bound the execution of the extension rule to changes in the value of Discount.

  8. Enter a different value in the List Price field, and notice that there is no change in the Sale price, because there is no binding in the rule to changes in the value of List Price.

  9. Click Finish to end the test session.