Rule Builder > Debugging and Profiling > Debugging > Adding Test Objects to Working Memory

Before you can get your rules or rules in a ruleflow to fire and test them, you need to initialize the working memory and the ruleset parameters of the engine with specific objects. These objects provide the information state required by the rules that enable them to fire.

You can add objects to working memory, declare ruleset parameters, prepare object iterators (for tasks using sequential mode only) to test rules and ruleflows.

Objects can be declared to the engine before execution using the ilrmain function available in the Rule Builder. The ilrmain function is similar to the main method of a Java application. An example of such a function is given below; this example initializes the working memory so that it contains two objects:

function void ilrmain(Object o) {

shoppingCart cart = new shoppingCart();
//Build an object,
// but don't insert it yet because it needs to be modified first.
cart.addItem(new Item(ItemType.DVD, 60.0));
cart.addItem(new Item(ItemType.GAME, 50.0));

insert Customer() {
// This insert syntax allows you to call a constructor and
// directly access the local object scope before inserting it.
setCategory("Silver");
cart.setCustomer(this);
}

insert cart;
// Note: This insert syntax allows you to insert only
// objects that have been previously built.

execute();
// Note: The ilrmain function calls engine execution. It can
// call it several times and can also process some of the results
// after execution.
}

These objects should lead to the execution of the following BAL rule:

 If 
  •  the shoppingCart contains more than $100
    
  •  and the customer category is silver
    
  • Then
  •    change the customer category to Gold
    
  •    and apply a 15% discount
    
  • In a case where the rules or ruleflow require ruleset parameters instead, a modified ilrmain function could look like the following:

    function void ilrmain(Object o) {

    ShoppingCart cart = new ShoppingCart();
    cart.addItem(new Item(ItemType.DVD, 60.0));
    cart.addItem(new Item(ItemType.GAME, 50.0));

    Customer customer = new Customer();
    customer.setCategory("Silver");

    cart.setCustomer(customer);

    IlrParameterMap params = new IlrParameterMap();
    // Note: Create a parameter map.
    params.setParameter("customer", customer);
    // Note: Populate this map with the in and inout ruleset parameters.
    params.setParameter("shoppingCart", cart);

    setParameterMap(params);
    // Note: Set the parameters for the following engine executions.

    IlrParameterMap results = execute();
    // Note: The engine also returns a map for the results
    // (out an inout) parameters.

    out.println("Discount is " + results.getIntValue("discount") + "%");
    }

    See Also

    Adding Functions to a Top-Level Package | Executing Rules in Sequential Mode


    Customer Support | Copyright © 1987-2004 ILOG S.A. All rights reserved. Legal terms. PREVIOUS   NEXT