Using Java Beans Asserted as Facts

1-12 Oracle Fusion Middleware Language Reference Guide for Oracle Business Rules In Oracle Business Rules RL Language, the effective start and end dates and the active property are only applied to rules and do not apply for rulesets. The effective start and end date properties of a rule can be specified in the rule. For example, rule myrule2 { active = true; effectiveDateForm = Rule.EDFORM_DATETIME: effectiveStartDate = JavaDate.fromDateTimeString2008-11-01; effectiveEndDate = JavaDate.fromDateTimeString2008-11-16; if fact Foo { . . } } If you use the RuleSession Java API, you can access the effective start and end date. Setting a property from RL Language requires a long expression or several statements. For example, given a ruleset: ruleset MyRules { rule myRule { if fact foo { }} } To set the active property, use the following: Rule r = getRuleSession.getRulesetMyRules.getRulemyRule; r.setActivefalse;

1.6 Integrating RL Language Programs with Java Programs

This section describes integrating RL Language programs with Java programs. This section covers the following topics: ■ Using Java Beans Asserted as Facts ■ Using RuleSession Objects in Java Applications

1.6.1 Using Java Beans Asserted as Facts

Example 1–16 shows the Java source for a simple bean. Use the javac command to compile the bean, example.Person shown in Example 1–16 into a directory tree. The following shows how an RL Language command-line can be started that can access this Java bean: java -classpath ORACLE_HOMEsoamodulesoracle.rules_11.1.1rl.jar;BeanPath oracle.rules.rl.session.CommandLine -p RL See Also: Working with Rules SDK Decision Point API in the Oracle Business Rules Users Guide Rules Programming Concepts 1-13 Where BeanPath is the classpath component to any supplied Java Bean classes. Example 1–16 Java Source for Person Bean Class package example; import java.util.; public class Person { private String firstName; private String lastName; private Set nicknames = new HashSet; public PersonString first, String last, String[] nick { firstName = first; lastName = last; for int i = 0; i nick.length; ++i nicknames.addnick[i]; } public Person {} public String getFirstName {return firstName;} public void setFirstNameString first {firstName = first;} public String getLastName {return lastName;} public void setLastNameString last {lastName = last;} public Set getNicknames {return nicknames;} } Example 1–17 shows how the RL Language command-line can execute an RL Language program that uses example.Person. The import statement, as in Java, allows a reference to the Person class using Person instead of example.Person. Rules reference the Person bean class and its properties and methods. In order to create a Person fact you must assert a Java Person bean. Example 1–17 uses the new operator to create an array of Person objects, named people. The people array is declared final so that reset does not create more people. The numPeople variable is not declared final so that reset re-invokes the assertPeople function and re-asserts the Person facts using the existing Person objects. Example 1–17 Ruleset Using Person Bean Class ruleset main { import example.Person; import java.util.; rule hasNickNames { if fact Personnicknames: var nns p nns.isEmpty { accessing properties as fields: printlnp.firstName + + p.lastName + has nicknames:; Iterator i = nns.iterator; while i.hasNext { printlni.next; } } } rule noNickNames { if fact Personnicknames: var nns p nns.isEmpty { 1-14 Oracle Fusion Middleware Language Reference Guide for Oracle Business Rules accessing properties with getters: printlnp.getFirstName + + p.getLastName + does not have nicknames; } } final Person[] people = new Person[] { new PersonRobert, Smith, new String[] { Bob, Rob }, using constructor new PersonfirstName: Joe, lastName: Schmoe using attribute value pairs }; function assertPeoplePerson[] people returns int { for int i = 0; i people.length; ++i { assertpeople[i]; } return people.length; } int numPeople = assertPeoplepeople; run; } Note the following when working with Java beans as facts: 1. The fact operator can include a pattern that matches or retrieves the bean properties. The properties are defined by getter and setter methods in the bean class. 2. The new operator can include a pattern that sets property values after invoking the default no-argument constructor, or can pass arguments to a user-defined constructor. 3. Outside of the fact and new operators, the bean properties may be referenced or updated using getter and setter methods, or using the property name as if it were a field. 4. If a bean has both a property and a field with the same name, then the field cannot be referenced in RL Language. If Example 1–18 executes using the same RuleSession following the execution of Example 1–17 , the output is identical to the Example 1–17 results both person facts are reasserted. Example 1–18 Using Reset with a RuleSession reset; run;

1.6.2 Using RuleSession Objects in Java Applications