A JSON-based rule language
From Wiki
Contents |
The first Example
Rule: For all elements with class note having as first child a ul element, change the first child background color to blue.
{
"id":"rule101",
"appliesTo": ["http://www.yahoo.com", "http://www.google.com/"],
"condition":
"
$X:Element( class==’note’,$Y:firstChild)
&&
($Y.nodeName == ’ul’)
",
"actions":
["changeBackground($Y, ’blue’)"]
}
The Language
- Based on JSON (any rule is a JSON object)
- Uses a Drools-like condition language
- Uses JavaScript function calls as actions
- Supports both Production Rules and Event-Condition-Action Rules
UML model of JSON Rule
The Condition Language
A rule condition is always a conjunction of atoms. Empty conditions are interpreted as true conditions.
- JavaScriptBooleanCondition the simplest conditional atom. Any JavaScript boolean expression is allowed.
- Description: related to the syntax of Drools pattern conditional.
- XPathCondition
Atom types (Conditions)
JavaScriptBooleanCondition
- the simplest conditional atom;
- any JavaScript boolean expression is allowed.
Example:
window.find(’rule’) document.getElementById(’id’).value==10
Descriptions
- related to the syntax of Drools pattern conditional;
- offers two types of constraints:
-
PropertyRestriction: describes a set of value restrictions to properties of the JSONTerm that are bound to it. -
PropertyBinding: performs a variable binding of a property belonging to the related JSONTerm.
-
Descriptions - UML Model
XPathCondition
Example:
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:
{"nodeName":"tr",
"firstChild":{"nodeName":"td",
"textContent":"T2:row1, cell 1"
}
} in "html//table//tr"
XPathCondition - UML Model
Actions
- standard actions from OMG Production Rule Representation (PRR);
- any user-defined JavaScript functions can be called in the rule actions part.
| PRR Standard Actions | JSON Rules |
AssignExp
| change properties of an element |
InvokeExp
| any JavaScript function call |
AssertExp
| insert a DOM node |
RetractExp
| remove a DOM node |
UpdateExp
| update a DOM node |
EventExpression
- JSON event expressions are related to the Event interface specification in W3C DOM Level 2 Events
- the properties of these expressions have the same meaning as in the W3C specification
- properties of event expressions are matched against the incoming Dom events at the runtime. Their values can be processed in the rule conditions and actions.
EventExpression Example
Rule: When click event is raised, if the event cames from an a element, and if the href property matches a specific regular expression[1] then call append() with the message subject as parameter.
{"id":"rule102",
"appliesTo":["http://mail.yahoo.com/"],
"eventExpression": { "eventType": "click",
"eventTarget": "$X"
},
"condition":" ($X.nodeName == ’a’,
$X.href==’match(showMessage\?fid=Inbox)’)",
"actions":["append($X.textContent)"]
}
EventExpression - UML Model
- ↑ rudimentary check that the link is an inbox Yahoo message link

