Logo

RuleX Action Rule User Guide

Action rules in RuleX are used to update XML, and are written against XML schemas. An action is written against a context in an XML schema. A context can be a complex or simple type in the schema.

If writing rules against JSON schema, for type read definition, for element read property.

The actions language uses OCL expressions for conditions and queries. You can find information on OCL constructs in the RuleX OCL User Guide.

Comments can be added to action rules. Inline comments are prefixed by //. Multiline comments are surrounded by /** and **/.

Actions

Set
Create
Copy
Delete
Add
If then
Foreach
Compound


Set

Sets the value of an XML element. The assignment operator is :

element : value;

Example 1: Setting a string element

address : 'New York';

sets the address element defined by the schema <xs:element name="address" type="xs:string"/> to the literal 'New York'.

Example 2: Setting an integer element

amount : 10;

sets the amount element defined by the schema <xs:element name="amount" type="xs:integer"/> to the value 10.

Example 3: Setting a complexType element

payment : anotherPayment;

sets the element payment to the value of anotherPayment. The 'anotherPayment' element must already exist and be of the same type.



Create

Creates an XML element.

type name : type.create();

The type can be a complex type, a simple type or an XML schema built-in type. The name is used to access the created XML element.

Datetime and date elements can be created based on today's date, and dates relative to today's date. The W3 formats for date and dateTime are used: CCYY-MM-DD and [-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]. Where different date formats are required, the created dates can be reformatted using string manipulation functions. See Examples 5, 6 and 7.

Example 1: Creating a string element

string aString : string.create();

creates an element of type string.

Example 2: Creating a complexType element

Payment aPayment : Payment.create();

based on the schema below, creates an element of type Payment.

<xs:complexType name="Payment">
 ...
</xs:complexType>


Example 3: Creating nested elements

Payment aPayment : Payment.create();
Person aPayer : Person.create();
Person aReceiver : Person.create();
aPayment.payer : aPayer;
aPayment.receiver : aReceiver;


based on the schema below, creates an element of type Payment, creates elements aPayer and aReceiver of type Person, and sets the payer and receiver elements in the aPayment element.

<xs:complexType name="Person">
 ...
</xs:complexType>
<xs:complexType name="Payment">
 <xs:sequence>
  <xs:element name="payer" type="Person"/>
  <xs:element name="recipient" type="Person"/>
  ...
 </xs:sequence>
</xs:complexType>


Example 4: Creating a complexType element & creating and setting simple and built-in types within the element

Person aPerson : Person.create();
aPerson.name : 'David';
aPerson.bankAccount : '1234567';


based on the schema below, creates an element of type Person and assigns values to the child elements, name and bankAccount. If a child element is not assigned a value, it is not created.

<xs:complexType name="Person">
 <xs:sequence>
  <xs:element name="name" type="xs:string"/>
  <xs:element name="bankAccount" type="xs:string"/>
 </xs:sequence>
</xs:complexType>




Example 5: Creating a date or datetime element based on today's date

dateTime aDateTime : dateTime.today();

creates a dateTime equal to the current timestamp e.g. 2015-02-24T14:31:00.

date aDate : date.today();

creates a date equal to the current date e.g. 2015-02-24.



Example 6: Creating a date element with value relative to today's date

date aDate : date.today(10);

creates a date 10 days in the future. To create a date in the past, use a negative number.



Example 7: Creating a date element with integer format CCYYMMDD

integer aDate : date.today().toString().replaceAll("-","").toInteger();

creates a date based on today's date and converts to a formatted integer e.g. 20150224.



Copy

Copies a complexType XML element.

type name : type.copy(element);

The name is used to access the copied XML element.

Example 1: Copying a complexType element

Person aPerson : Person.copy(anExistingPersonElement);

does a full copy of anExistingPersonElement.



Delete

Deletes an XML element.

element.delete();

Example 1: Deleting a child element

payment.delete();

deletes a payment element from its parent.

Example 2: Deleting a grandchild element

payment.payer.delete();

deletes the payer element contained in the payment element.



Add

Adds an XML child element to a parent element. The element being added must already exist.

Add is used in the case where a parent element can contain more than one instances of the same child element (maxOccurs is greater than 1 for the child element).

parentElement.add(id);

Example: Adding an element

payment.add(aPayment);

based on the XML schema below, adds 'aPayment' as a payment child element.

<xs:complexType name="Payments">
 <xs:sequence>
   <xs:element name="payment" type="Payment" maxOccurs="unbounded"/>
 </xs:sequence>
</xs:complexType>




If then

The If then statement is used to perform actions if a certain condition is met. The condition is specified using OCL.

The else...endif portion of the statement is optional.

if (ocl invariant) then
  Action;
else
  Action;
endif;


Example

if (membershipType = 'recurring contract') then
  fee : 10;
else
  fee : 20;
endif;


sets the fee element to 10 for recurring constracts, and sets the fee to 20 otherwise.



Foreach

The foreach action performs an action on a collection of XML elements. The collection is created using an OCL query.

Foreach is used to perform the same action on all child elements of the same name in a parent element (maxOccurs is greater than 1 for the child element).

foreach value in (OCL query)
  value.Action;
endeach;


Example 1

foreach p in (payment)
  p.amount : 10;
endeach;


loops through every payment in the parent element and sets the payment amount to 10.

Example 2

foreach i in (Set{1 .. 10})
  Payment aPayment : Payment.create();
  payment.add(aPayment);
endeach;


creates ten elements of type Payment, and adds each element to a collection named payment.



Compound

Compound actions are used to execute a group of actions sequentially.

Action;
Action;
...


Example

foreach p in (payment)
  p.amount : 10;
  p.currency : 'EUR';
endeach;


loops through every payment in the parent element and sets the payment amount to 10 and the currency to EUR.



Copyright © 2015 Nomos Software