call

Calls out to a static Java method to perform a complex calculation.

The call expression declares:

The Java method must be on a class which is on the classpath at rule set validation time. The first argument to the method must be a Session object, and the remaining arguments must match those specified in the rule set.

warning: You should ensure that any Java code invoked by a call expression does not attempt to mutate any values on rule object attributes.

In general, CER rule sets use immutable data types, but it is possible to use your own mutable Java classes as data types; if so, it is your responsibility to ensure that no invoked code causes the value of a custom Java data type to be modified, as doing so could mean that previously-performed calculations would now be "wrong".

package curam.creole.example;

import curam.creole.execution.RuleObject;
import curam.creole.execution.session.Session;

public class Statics {

  /**
   * Calculates a person's favorite color.
   *
   * This calculation is too complex for rules and so has been
   * coded in java.
   *
   * @param session
   *          The rule session
   * @param person
   *          the person
   * @return the calculated favorite color of the specified person
   */
  public static String calculateFavoriteColor(
      final Session session, final RuleObject person) {

    // Note that the retrieval of the attribute value must be
    // cast to the correct type
    final String name =
        (String) person.getAttributeValue("name").getValue();
    final Number age =
        (Number) person.getAttributeValue("age").getValue();

    final String ageString = age.toString();
    // Calculate the person's favorite color according
    // to the digits in their age and their name
    if (ageString.contains("5") || ageString.contains("7")) {
      return "Blue";
    } else if (name.contains("z")) {
      return "Purple";
    } else {
      return "Green";
    }

  }
}
<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_call"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
"http://www.curamsoftware.com/CreoleRulesSchema.xsd">
  <Class name="Person">

    <Attribute name="age">
      <type>
        <javaclass name="Number"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="name">
      <type>
        <javaclass name="String"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="favoriteColor">
      <type>
        <javaclass name="String"/>
      </type>
      <derivation>
        <!-- Call a java static method
             to perform the calculation -->
        <call class="curam.creole.example.Statics"
          method="calculateFavoriteColor">
          <type>
            <javaclass name="String"/>
          </type>
          <arguments>
            <!-- Pass in this person
                 as an argument to the
                 static method -->
            <this/>
          </arguments>
        </call>
      </derivation>
    </Attribute>

  </Class>

</RuleSet>
CAUTION:
Since Cúram V6, CER and the Dependency Manager support the automatic recalculation of CER-calculated values if their dependencies change.

If you change the implementation of a static method, CER and the Dependency Manager will not automatically know to recalculate attribute values that were calculated using the old version of your static method.

Once a static method has been used in a production environment for stored attribute values, rather than changing the implementation you should instead create a new static method (with the required new implementation), and change your rule sets to use the new static method. When you publish your rule set changes to point to the new static method, CER and the Dependency Manager will automatically recalculate all instances of the affected attribute values.