JSON Rules Language

  • 4,590 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,590
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
72
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. JSON Rules Language Adrian Giurca
  • 2. Goals
      • To implement intelligent and adaptive Web user interfaces
          • Whenever the user clicks more than 3 times a menu item add this
          • item to the fast access menu items.
      • To model Web business processes involving user activities in the browser
          • If the user loads financial news, offers him a three months
          • subscription to Financial Times
      • To allow rule-based reasoning with semantic data inside HTML pages (reasoning with RDFa)
          • Whenever the user clicks on an RDFa annotated publication show him
          • related publications from the same foaf:topic
      • To create intelligent mashups – rule-based mashups
          • If the user reads Reuters news about swine flu, deliver him
          • similar video news from CNN and possibly update his swine flu
          • Google Map
  • 3. Main Requirements
      • Rules must run in the Web browser
      • Event-Condition-Action (ECA) Rules
      • The events vocabulary must include DOM Events and JavaScript user-defined events
      • Rule conditions must address the information in the pages where the user works
      • Rule actions should be freely designed by the user
      • Rule internal representation should be fast plugable to JavaScript – JSON notation
      • Rule language syntax should be close to browsers developers experience
      • Rules XML syntax should be an extension of Rule Interchange Format for ECA rules.
  • 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.
      • (September 2008) First version of the rule language (1)
      • (September 2008) An online public wiki is available (2)
      • (March 2009) A Proposal for a rules registry (3)
      • (August 2009) First version of the rule engine is available (4)
      • (September 2009) The engine architecture to be shown (5) and a use case is described (7)
      • (September 2009) JSON Rules is discussed in his tutorial
      • (September 2009) The tutorial and a software demo at KI2009 (6)
  • 5. Event-Condition-Action (ECA) Rules
      • One event expression as triggering event
      • An optional collection of atoms (conjunction) as conditions .
      • one or more actions to be performed
      • id , required, in the scope of a ruleset
      • priority , optional, default 1, used for conflict resolution
      • appliesTo – an Array of URLs on which the rule can be executed
    { "id":"rule101", "appliesTo": ["http://www.yahoo.com", "http://www.google.com/"], "eventExpression": { "eventType": "click", "eventTarget": "$X" }, "conditions": [ "$X:HTMLElement( class == 'note', $Y:firstChild)", "$Y.tagName == 'ul'" ], "actions": ["changeBackground($Y, 'blue')"] }
  • 6. Event Expressions (1)
      • 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 .
      • 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.
      • eventTarget - is a JSONTerm. When the event type is a user-defined JavaScript event, the target can be any JavaScript object in JSON notation.
  • 7. Event Expressions (2)
      • Event expressions are matched against any occurring DOM event. The following Event Expression
    • "eventExpression":{
    • "eventType":"click",
    • "eventTarget": "$X"
    • },
    • triggers a rule whenever a click event occurs on a specific element
    • e.g.
    • <p id=&quot;myp&quot;>Content</p>
    • as a result the variable $X is bound to the DOM node value like:
    • {&quot;nodeName&quot;:&quot;p&quot;, &quot;nodeValue&quot;:&quot;Content&quot;, &quot;attributes&quot;:{&quot;id&quot;:&quot;myp&quot;}, ...}
  • 8. Supported Event Types
      • All DOM Level 2 Events i.e.
        • HTML Events (load, unload, abort, error, select, change, submit reset, ...)
        • UI Events (DOMFocusIn, DOMFocusOut, ...)
        • Mouse Events (click, mousedown, mouseup, mouseover, ...)
        • Mutation Events (DOMNodeInserted, DOMNodeRemoved, DOMSubtreeModified, ...)
      • DOM Level 3 Events as much they are supported by browsers
  • 9. The Logic of JSON Rule Event Expressions
      • JSON Rules event expressions captures only atomic events without duration .
      • Events are consumed immediately they occur.
      • There is no support for periodic events
      • There is no support for event history
      • No events algebra is introduced i.e. no support for composite events.
      • DOM Event bubbling is not part of the JSON Rules Engine logic (read more on Part III)
  • 10. JSONTerm
      • A JSONTerm is either a DOM Node (constant) or any of its subclasses or a variable
      • $X is a JSONTerm
      • An HTMLElement is a JSONTerm i.e.
    • { &quot;tagName&quot;:&quot;ul&quot; &quot;id&quot;:&quot;container&quot;,
    • &quot;title&quot;:&quot;Publication List&quot;,
    • &quot;lang&quot;:&quot;en&quot;, &quot;dir&quot;:&quot;ltr&quot;,
    • &quot;className&quot;:&quot;pubs&quot;
    • }
      • More complex terms are allowed too:
    • { &quot;tagName&quot;:&quot;ul&quot; &quot;id&quot;:{&quot;variable&quot;:{&quot;name&quot;:&quot;$Y&quot;}},...}
  • 11. Conditions (1)
  • 12. JavaScript Boolean Conditions document.getElementById($id).nodeValue == 10 cart.totalAmount() > 150
  • 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. Descriptions
      • inspired from Drools descriptions
      • $X is matched against text input elements and the variable $Y is bound to the input value
    • $X:Element(
    • tagName == &quot;input&quot;,
    • nodeType == &quot;text&quot;,
    • $Y:nodeValue
    • )
    • or, shorter,
    • $X : Input(type == &quot;radio&quot;, $Y:value)
      • Assuming
    • <input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; value=&quot;Doe&quot;/>
    • $Y will be bound to the string &quot;Doe&quot;
  • 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)
      • Descriptions can be much more complex
  • 16. Built-ins
      • JSON Rules develops a library of built-in predicates used in rule conditions (work in progress)
      • Basically they help to maintain the declarative design of rules (otherwise built-ins can be avoided by using plain JavaScript Boolean expressions)
      • JSON Rules built-ins are compatible with W3C Rule Interchange Format built-ins
  • 17. Rule Actions
      • JSON Rules allows any valid JavaScript function call as a rule action.
      • For example
    • &quot;actions&quot;:[&quot;append($X.nodeValue)&quot;]
    • assuming that $X is bound to the value
    • {&quot;nodeValue&quot;:&quot;JSON Rules&quot;}
    • the performed JavaScript call is:
    • append(&quot;JSON Rules&quot;)
  • 18. More on Rule Actions
      • JSON Rules actions are compliant with the OMG Production Rule Representation . However 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', '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. Intelligent and adaptive Web user interfaces
      • Tip: &quot;more than 3 times&quot; refers to the current browser session (learn more details in the Part III of the tutorial).
      • Remember: events are consumed immediately they occur .
      • This version does not maintain any event history!
    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. 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. JSON Rules in JSON Notation
      • Rules serialize to JSON objects.
      • JSON objects are native objects with the rule engine works
    $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. Summary
      • The browser can be enriched with rule-based reasoning
      • JSON Rules offers such a solution (see Part III of this tutorial)
      • Various types of business rules can be implemented
      • Event-Condition-Action rules are essential for rule-based reasoning in the browser
      • JSON Rules can implement client-side mashups