JSON Rules Language


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

JSON Rules Language

  1. 1. JSON Rules Language Adrian Giurca
  2. 2. Goals <ul><ul><li>To implement intelligent and adaptive Web user interfaces </li></ul></ul><ul><ul><ul><ul><li>Whenever the user clicks more than 3 times a menu item add this </li></ul></ul></ul></ul><ul><ul><ul><ul><li>item to the fast access menu items. </li></ul></ul></ul></ul><ul><ul><li>To model Web business processes involving user activities in the browser </li></ul></ul><ul><ul><ul><ul><li>If the user loads financial news, offers him a three months </li></ul></ul></ul></ul><ul><ul><ul><ul><li>subscription to Financial Times </li></ul></ul></ul></ul><ul><ul><li>To allow rule-based reasoning with semantic data inside HTML pages (reasoning with RDFa) </li></ul></ul><ul><ul><ul><ul><li>Whenever the user clicks on an RDFa annotated publication show him </li></ul></ul></ul></ul><ul><ul><ul><ul><li>related publications from the same foaf:topic </li></ul></ul></ul></ul><ul><ul><li>To create intelligent mashups – rule-based mashups </li></ul></ul><ul><ul><ul><ul><li>If the user reads Reuters news about swine flu, deliver him </li></ul></ul></ul></ul><ul><ul><ul><ul><li>similar video news from CNN and possibly update his swine flu </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Google Map </li></ul></ul></ul></ul>
  3. 3. Main Requirements <ul><ul><li>Rules must run in the Web browser </li></ul></ul><ul><ul><li>Event-Condition-Action (ECA) Rules </li></ul></ul><ul><ul><li>The events vocabulary must include DOM Events and JavaScript user-defined events </li></ul></ul><ul><ul><li>Rule conditions must address the information in the pages where the user works </li></ul></ul><ul><ul><li>Rule actions should be freely designed by the user </li></ul></ul><ul><ul><li>Rule internal representation should be fast plugable to JavaScript – JSON notation </li></ul></ul><ul><ul><li>Rule language syntax should be close to browsers developers experience </li></ul></ul><ul><ul><li>Rules XML syntax should be an extension of Rule Interchange Format for ECA rules. </li></ul></ul>
  4. 4. Short Roadmap (1) Adrian Giurca and Emilian Pascalau. JSON Rules . In G. J. Nalepa and J. Baumeister (Eds.) Proceedings of 4th Knowledge Engineering and Software Engineering , KESE 2008 , collocated with KI 2008 , September 23, 2008, Kaiserlautern, Germany, CEUR vol 425 . (2) JSON Rules on Web Technologies Wiki (3) Emilian Pascalau, Adrian Giurca. Towards enabling SaaS for Business Rules . In Proceedings of The International Workshop on Intelligent Service Management ( ISM2009 ) , Lecture Notes in Informatics (LNI) Vol. P-147, pp. 207-222, collocated with Sabre 2009 , March 23-24, 2009, Leipzig, Germany. (4) See http://code.google.com/p/jsonrules (5) Emilian Pascalau, Adrian Giurca. A Lightweight Architecture of an ECA Rule Engine for Web Browsers. In Proceedings of 5th Knowledge Engineering and Software Engineering, KESE 2009 , collocated with KI 2009 . September 15, 2009, Paderborn, Germany. (6) Emilian Pascalau, Adrian Giurca. JSON Rules - The JavaScript Rule Engine . Software demo at Knowledge Engineering and Software Engineering, KESE 2009 , September 15, 2009, Paderborn, Germany. (7) Emilian Pascalau, Adrian Giurca. A Rule-Based Approach of Creating and Executing Mashups . 9th IFIP Conference on e-Business, e-Services, and e-Society, (I3E 2009). September 23-25, 2009, Nancy, France. <ul><ul><li>(September 2008) First version of the rule language (1) </li></ul></ul><ul><ul><li>(September 2008) An online public wiki is available (2) </li></ul></ul><ul><ul><li>(March 2009) A Proposal for a rules registry (3) </li></ul></ul><ul><ul><li>(August 2009) First version of the rule engine is available (4) </li></ul></ul><ul><ul><li>(September 2009) The engine architecture to be shown (5) and a use case is described (7) </li></ul></ul><ul><ul><li>(September 2009) JSON Rules is discussed in his tutorial </li></ul></ul><ul><ul><li>(September 2009) The tutorial and a software demo at KI2009 (6) </li></ul></ul>
  5. 5. Event-Condition-Action (ECA) Rules <ul><ul><li>One event expression as triggering event </li></ul></ul><ul><ul><li>An optional collection of atoms (conjunction) as conditions . </li></ul></ul><ul><ul><li>one or more actions to be performed </li></ul></ul><ul><ul><li>id , required, in the scope of a ruleset </li></ul></ul><ul><ul><li>priority , optional, default 1, used for conflict resolution </li></ul></ul><ul><ul><li>appliesTo – an Array of URLs on which the rule can be executed </li></ul></ul>{ &quot;id&quot;:&quot;rule101&quot;, &quot;appliesTo&quot;: [&quot;http://www.yahoo.com&quot;, &quot;http://www.google.com/&quot;], &quot;eventExpression&quot;: { &quot;eventType&quot;: &quot;click&quot;, &quot;eventTarget&quot;: &quot;$X&quot; }, &quot;conditions&quot;: [ &quot;$X:HTMLElement( class == 'note', $Y:firstChild)&quot;, &quot;$Y.tagName == 'ul'&quot; ], &quot;actions&quot;: [&quot;changeBackground($Y, 'blue')&quot;] }
  6. 6. Event Expressions (1) <ul><ul><li>Event expressions are designed to capture the logic of DOM events, (follows the interface Event introduced in DOM Level 2) i.e. they catch the information of any DOM Event occurrence . </li></ul></ul><ul><ul><li>eventType - is an attribute capturing the type 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. </li></ul></ul><ul><ul><li>eventTarget - is a JSONTerm. When the event type is a user-defined JavaScript event, the target can be any JavaScript object in JSON notation. </li></ul></ul>
  7. 7. Event Expressions (2) <ul><ul><li>Event expressions are matched against any occurring DOM event. The following Event Expression </li></ul></ul><ul><li>&quot;eventExpression&quot;:{ </li></ul><ul><li>&quot;eventType&quot;:&quot;click&quot;, </li></ul><ul><li>&quot;eventTarget&quot;: &quot;$X&quot; </li></ul><ul><li>}, </li></ul><ul><li>triggers a rule whenever a click event occurs on a specific element </li></ul><ul><li>e.g. </li></ul><ul><li><p id=&quot;myp&quot;>Content</p> </li></ul><ul><li>as a result the variable $X is bound to the DOM node value like: </li></ul><ul><li>{&quot;nodeName&quot;:&quot;p&quot;, &quot;nodeValue&quot;:&quot;Content&quot;, &quot;attributes&quot;:{&quot;id&quot;:&quot;myp&quot;}, ...} </li></ul>
  8. 8. Supported Event Types <ul><ul><li>All DOM Level 2 Events i.e. </li></ul></ul><ul><ul><ul><li>HTML Events (load, unload, abort, error, select, change, submit reset, ...) </li></ul></ul></ul><ul><ul><ul><li>UI Events (DOMFocusIn, DOMFocusOut, ...) </li></ul></ul></ul><ul><ul><ul><li>Mouse Events (click, mousedown, mouseup, mouseover, ...) </li></ul></ul></ul><ul><ul><ul><li>Mutation Events (DOMNodeInserted, DOMNodeRemoved, DOMSubtreeModified, ...) </li></ul></ul></ul><ul><ul><li>DOM Level 3 Events as much they are supported by browsers </li></ul></ul>
  9. 9. The Logic of JSON Rule Event Expressions <ul><ul><li>JSON Rules event expressions captures only atomic events without duration . </li></ul></ul><ul><ul><li>Events are consumed immediately they occur. </li></ul></ul><ul><ul><li>There is no support for periodic events </li></ul></ul><ul><ul><li>There is no support for event history </li></ul></ul><ul><ul><li>No events algebra is introduced i.e. no support for composite events. </li></ul></ul><ul><ul><li>DOM Event bubbling is not part of the JSON Rules Engine logic (read more on Part III) </li></ul></ul>
  10. 10. JSONTerm <ul><ul><li>A JSONTerm is either a DOM Node (constant) or any of its subclasses or a variable </li></ul></ul><ul><ul><li>$X is a JSONTerm </li></ul></ul><ul><ul><li>An HTMLElement is a JSONTerm i.e. </li></ul></ul><ul><li>{ &quot;tagName&quot;:&quot;ul&quot; &quot;id&quot;:&quot;container&quot;, </li></ul><ul><li>&quot;title&quot;:&quot;Publication List&quot;, </li></ul><ul><li>&quot;lang&quot;:&quot;en&quot;, &quot;dir&quot;:&quot;ltr&quot;, </li></ul><ul><li>&quot;className&quot;:&quot;pubs&quot; </li></ul><ul><li>} </li></ul><ul><ul><li>More complex terms are allowed too: </li></ul></ul><ul><li>{ &quot;tagName&quot;:&quot;ul&quot; &quot;id&quot;:{&quot;variable&quot;:{&quot;name&quot;:&quot;$Y&quot;}},...} </li></ul>
  11. 11. Conditions (1)
  12. 12. JavaScript Boolean Conditions document.getElementById($id).nodeValue == 10 cart.totalAmount() > 150
  13. 13. XPath conditions { &quot;variable&quot;:{&quot;name&quot;:&quot;$X&quot;}, &quot;xPath&quot;:&quot;html//table//tr&quot; } { &quot;node&quot;:{&quot;nodeValue&quot;:&quot;JSON Rules&quot;}, &quot;xPath&quot;: /store/book[23]/title&quot; } {&quot;node&quot;:{ &quot;nodeName&quot;:&quot;tr&quot;, &quot;firstChild&quot;:{ &quot;nodeName&quot;:&quot;td&quot;, &quot;textContent&quot;:&quot;T2:row1, cell 1&quot; } }, &quot;xPath&quot;: &quot;html//table//tr&quot; }
  14. 14. Descriptions <ul><ul><li>inspired from Drools descriptions </li></ul></ul><ul><ul><li>$X is matched against text input elements and the variable $Y is bound to the input value </li></ul></ul><ul><li>$X:Element( </li></ul><ul><li>tagName == &quot;input&quot;, </li></ul><ul><li>nodeType == &quot;text&quot;, </li></ul><ul><li>$Y:nodeValue </li></ul><ul><li>) </li></ul><ul><li>or, shorter, </li></ul><ul><li>$X : Input(type == &quot;radio&quot;, $Y:value) </li></ul><ul><ul><li>Assuming </li></ul></ul><ul><li><input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; value=&quot;Doe&quot;/> </li></ul><ul><li>$Y will be bound to the string &quot;Doe&quot; </li></ul>
  15. 15. More on Descriptions $N:Input( id==&quot;postalCode&quot;, nodeValue==RegExp(&quot;/^d{5}$/&quot;) ) (The pattern will bound the variable $N to the <input> element with id=&quot;postalCode&quot; if the value of this element is a 5 digit number) <ul><ul><li>Descriptions can be much more complex </li></ul></ul>
  16. 16. Built-ins <ul><ul><li>JSON Rules develops a library of built-in predicates used in rule conditions (work in progress) </li></ul></ul><ul><ul><li>Basically they help to maintain the declarative design of rules (otherwise built-ins can be avoided by using plain JavaScript Boolean expressions) </li></ul></ul><ul><ul><li>JSON Rules built-ins are compatible with W3C Rule Interchange Format built-ins </li></ul></ul>
  17. 17. Rule Actions <ul><ul><li>JSON Rules allows any valid JavaScript function call as a rule action. </li></ul></ul><ul><ul><li>For example </li></ul></ul><ul><li>&quot;actions&quot;:[&quot;append($X.nodeValue)&quot;] </li></ul><ul><li>assuming that $X is bound to the value </li></ul><ul><li>{&quot;nodeValue&quot;:&quot;JSON Rules&quot;} </li></ul><ul><li>the performed JavaScript call is: </li></ul><ul><li>append(&quot;JSON Rules&quot;) </li></ul>
  18. 18. More on Rule Actions <ul><ul><li>JSON Rules actions are compliant with the OMG Production Rule Representation . However JSON Rules allows any valid JavaScript function call as a rule action. </li></ul></ul>PRR Standard Actions JSON Rules Example AssignExp change properties of an element document.getElementById('Item25').setAttribute('class', 'note') InvokeExp 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)
  19. 19. Intelligent and adaptive Web user interfaces <ul><ul><li>Tip: &quot;more than 3 times&quot; refers to the current browser session (learn more details in the Part III of the tutorial). </li></ul></ul><ul><ul><li>Remember: events are consumed immediately they occur . </li></ul></ul><ul><ul><li>This version does not maintain any event history! </li></ul></ul>Whenever the user clicks more than 3 times the same menu item add this item to the fast access menu items. { &quot;id&quot;:&quot;fast101&quot;, &quot;appliesTo&quot;:[&quot;http://www.example.com&quot;&quot;], &quot;eventExpression&quot;:{ &quot;eventType&quot;:&quot;click&quot;, &quot;eventTarget&quot;:&quot;$X&quot;}, &quot;conditions&quot;: [ &quot;$X:A( $Id:id, class = = 'menuItem')&quot;, &quot;count(&quot;click&quot;,$Id) >= 3&quot; ], &quot;actions&quot;: [&quot;fastMenu('add',$X)&quot;] }
  20. 20. Implement Web Business Processes If the user loads financial news, offers him a three months subscription to Financial Times { &quot;id&quot;:&quot;fast101&quot;, &quot;appliesTo&quot;:['http://www.example.com'&quot;], &quot;eventExpression&quot;:{&quot;eventType&quot;:&quot;DOMNodeInserted&quot;, &quot;eventTarget&quot;:&quot;$X&quot;}, &quot;conditions&quot;: [ &quot;$Y:Div(id == 'currentFeedContent')&quot;, &quot;$X:Div(?Y:relatedNode, $Z:childNodes)&quot;, &quot;$C:Div(class='itemcontent')&quot;, &quot;org.jsonrules.builtin.pred.member($C, $Z)&quot;, &quot;$C.firstChild.nodeValue=='businessNews'&quot; ], &quot;actions&quot;: [&quot;showAdd({&quot;s&quot;:&quot;ft&quot;,&quot;d&quot;:&quot;3&quot;,&quot;p&quot;:&quot;30&quot;})&quot;] }
  21. 21. JSON Rules in JSON Notation <ul><ul><li>Rules serialize to JSON objects. </li></ul></ul><ul><ul><li>JSON objects are native objects with the rule engine works </li></ul></ul>$X:HTMLElement( class == 'note', $Y:firstChild ) {&quot;description&quot;:{ &quot;type&quot;:&quot;HTMLElement&quot;, &quot;binding&quot;:{ &quot;variable&quot;:{&quot;name&quot;:&quot;$X&quot;}}, &quot;constraints&quot;:[{ &quot;propertyRestriction&quot;:{ &quot;property&quot;:&quot;class&quot;, &quot;operator&quot;:&quot;EQ&quot;, &quot;value&quot;:&quot;note&quot; } }, {&quot;propertyBinding&quot;:{ &quot;property&quot;:&quot;firstChild&quot;, &quot;variable&quot;:{&quot;name&quot;:&quot;$Y&quot;} } }] } }
  22. 22. Summary <ul><ul><li>The browser can be enriched with rule-based reasoning </li></ul></ul><ul><ul><li>JSON Rules offers such a solution (see Part III of this tutorial) </li></ul></ul><ul><ul><li>Various types of business rules can be implemented </li></ul></ul><ul><ul><li>Event-Condition-Action rules are essential for rule-based reasoning in the browser </li></ul></ul><ul><ul><li>JSON Rules can implement client-side mashups </li></ul></ul>