A JSON-based rule language

From Wiki
Jump to: navigation, search



Rules and rule sets


Rule: On any occurrence of a DOM click on an element with class note having as first child an ul element, change the first child background color to blue.

 "appliesTo": ["http://www.yahoo.com", "http://www.google.com/"],
 "eventExpression": { "eventType": "click",
                       "eventTarget": "$X"
 "conditions": [
    "$X:Element( class == 'note',$Y:firstChild)",
    "$Y:Node(nodeName == 'ul')",
   ["changeBackground($Y, 'blue')"]

The above rule is a JSON object with the following structure:

  • id - a required string-valued attribute. The identifier of the rule. This should be unique in the scope of a rule set
  • appliesTo - an optional array containing the URL's of the pages on which this rule must only apply. If this array is empty then rule will be executed on any URL.
  • eventExpression - an optional property capturing the triggering event for this rule. Its value is a JSON object with the following structure:
    • eventType - a string-valued attribute. Its value is the identification string of any DOM Event types (such as load or mousedown or DOMNodeInserted ...)
    • eventTarget - a property capturing the target of the corresponding event type. It value is a JSONTerm capturing this target. In the above example the eventTarget value is a rule variable bound to the DOM element on which the event occurs.
  • conditions - a required array encoding rule conditions. If this array is empty, then by default the conditions will be evaluated to true.
  • actions - a required array of JavaScript function call. These actions will be executed sequentially using the array default order. If this array is empty, no JavaScript function call is performed.

Event Expressions


JSON Rules event expressions are related to the Event interface specification in Document Object Model. An EventExpression is a JSON object with the following properties (have the same meaning as in the W3C DOM Events specification):

  • eventType - is a string attribute capturing the name of the event. This event type can be a DOM Event type (See List of Complete Event Types) but also a user-defined JavaScript event.
  • eventTarget - is a JSONTerm. In the case when the event type is a user-defined JavaScript event, the target can be any JavaScript object in JSON notation.

Properties of event expressions are matched at the run time against the incoming DOM events or user-defined events. Their values are used in the rule conditions and actions.

// When click event is raised, if the event cames from an anchor element, and 
//its href attribute matches a specific regular expression [1] 
//then call append()</code> with the message subject as parameter.
  "eventExpression": { "eventType": "click",
                       "eventTarget": "$X"
  "conditions":" ["$X:HTMLAnchorElement(tagName == 'a',
                 href == 'match(showMessage\?fid=Inbox)'"],

The Condition Language

A rule condition is always a conjunction of atoms. Empty conditions are interpreted as true conditions.


JavaScript Boolean conditions

The JavaScriptBooleanCondition the simplest conditional atom. Any JavaScript Boolean expression is allowed.



The Description is a simplified version of the Drools pattern conditional.


There are two types of descriptions:

  • PropertyRestriction: describes a set of value restrictions to properties of the JSONTerm that are bound to it.

The variable $N is bound to any DOM element:


The variable $N is bound to any DOM element having the class attribute equal with "note":


The variable $N is bound to any input element having the value greater than 25:

$N:Element(nodeName=="input",  nodeValue > 25)

Assuming we have the following <form> element:

<form name="myform" id="myform" action="next.jsp" >
 <input type="text" id="postalCode" name="postalCode" size="15" /> 
 <input type="button" value="check" />

Then following pattern will bound the variable $N to the <input> element with id="postalCode" if the value of this element is a 5 digit number:

$N:Element(tagName=="input", id=="postalCode", nodeValue==RegExp("/^\d{5}$/"))
  • PropertyBinding: performs a variable binding to a property of a specific JSONTerm.

In the below example, the rule variable $Y is bound to the value of the class attribute of a DOM element:


XPath conditions

The XPathCondition: is checking the membership of a DOM Node against a NodeList resulting from the evaluation of an XPath expression.


Consider a HTML page that contains two tables:

T1:row 1,cell 1 T1:row 1,cell 2
T1:row 2,cell 1 T1:row 2,cell 2
T2:row 1,cell 1 T2:row 1,cell 2
T2:row 2,cell 1 T2:row 2,cell 2

A condition checking that the content of the first row of the second table is "T2:row1, cell 1", might be:

      "textContent":"T2:row1, cell 1"
 "xPath": "html//table//tr"

Rule Actions

JSON Rules are compliant with the OMG Production Rule Representation[2]. The below table specifies the mapping between JSON rules actions and PRR actions. However the readers should notice that JSON Rules allows any valid JavaScript function call as a rule action.

PRR Standard Actions JSON Rules Example
AssignExp change properties of an element document.getElementById('Item25').setAttribute('class', 'perishable')
InvokeExp any JavaScript function call alert(message)
AssertExp insert a DOM node document.insertBefore(child, newChild)
RetractExp remove a DOM node removeChild(child)
UpdateExp update a DOM node replaceChild(newChild, oldChild)


  1. rudimentary check that the link is an inbox Yahoo message link
  2. See http://www.omg.org/docs/dtc/07-11-04.pdf
Personal tools