Reference > Rule Languages > ILOG Rule Language > IRL Keywords > from

Summary

A statement that supports relations between objects. This keyword is used in rule conditions.

Syntax

condition from expression; 

Description

The from statement is used in the condition part of a rule to express the relations between objects. The statement returns true when the from condition matches an object that is returned by the from expression.

Using the from statement, you can directly access objects not only in the working memory but also objects that are linked to other objects by fields or by method invocations. Methods that are attached directly to the context object may be called in the expression part of the statement.

The from statement has performance advantages, since the pattern matching is done on a reduced number of objects and not on all the objects in the working memory (see the first example below).

The from statement may also be used to access objects in a database. Using SQL queries in the expression part of the statement, you can retrieve information from a relational database (see the second example below).

Examples

rule FindBookStore {
   when {
      ?s: Store(city == London);
      ?d: Department(type == Books) from ?s.department;
   }
   then {
      System.out.println(
                "The following book store/department was found: "
                + ?s.address);
   }
}

The rule FindBookStore returns bookstores or stores with a book department in the city of London. The first condition provides the stores in London in variable ?s. The second condition uses this reduced set of stores to find which store has a book department. If the when part is successful, the action part will print the store(s) found.

rule FindCarOwner {
   when {
      CarToMove(?l:license);
      ?c: Car(license == ?l);
      ?p: Person(?n:name) from ?c.getOwner();
   }
   then {
      System.out.println("Contact " + ?n + " at telephone " 
             ?p.telephone + " to move a " + ?c.model + 
             " with license: " + ?l);
   }
}

The rule FindCarOwner finds the owner of a car using an SQL query to a relational database where employee information resides. The first condition matches a CarToMove object and returns a license number in variable ?l. The second condition returns, in variable ?c, a Car object found using ?l, the license number. The from statement uses the Person object to return a name in variable ?n, which matches the name of the car owner. The car owner is retrieved using an SQL query, which is launched by the getOwner() method. The action part of the rule prints the person's name and telephone number as well as the car model and license number.

See Also

collect, exists, in, not