22
JSON Rules Language Adrian Giurca

JSON Rules Language

  • View
    5.295

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: JSON Rules Language

JSON Rules Language

Adrian Giurca

Page 2: JSON Rules Language

Goals

1. 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

Page 3: JSON Rules Language

Main Requirements

1. Rules must run in the Web browser2. Event-Condition-Action (ECA) Rules3. The events vocabulary must include DOM Events and

JavaScript user-defined events4. Rule conditions must address the information in the

pages where the user works5. Rule actions should be freely designed by the user6. Rule internal representation should be fast plugable to

JavaScript – JSON notation7. Rule language syntax should be close to browsers

developers experience8. Rules XML syntax should be an extension of

Rule Interchange Format for ECA rules.

Page 4: JSON Rules Language

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)

Page 5: JSON Rules Language

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')"] }

Page 6: JSON Rules Language

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.

Page 7: JSON Rules Language

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="myp">Content</p>as a result the variable $X is bound to the DOM node value like:{"nodeName":"p", "nodeValue":"Content", "attributes":{"id":"myp"}, ...}

Page 8: JSON Rules Language

Supported Event Types

• All DOM Level 2 Events i.e.o HTML Events (load, unload, abort, error, select,

change, submit reset, ...)o UI Events (DOMFocusIn, DOMFocusOut, ...)o Mouse Events (click, mousedown, mouseup,

mouseover, ...)o Mutation Events (DOMNodeInserted,

DOMNodeRemoved, DOMSubtreeModified, ...)• DOM Level 3 Events as much they are supported

by browsers

Page 9: JSON Rules Language

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)

Page 10: JSON Rules Language

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.{ "tagName":"ul" "id":"container","title":"Publication List","lang":"en", "dir":"ltr", "className":"pubs"}• More complex terms are allowed too:{ "tagName":"ul" "id":{"variable":{"name":"$Y"}},...}

Page 11: JSON Rules Language

Conditions (1)

Page 12: JSON Rules Language

JavaScript Boolean Conditions

document.getElementById($id).nodeValue == 10

cart.totalAmount() > 150

Page 13: JSON Rules Language

XPath conditions{"variable":{"name":"$X"},"xPath":"html//table//tr"}

{"node":{"nodeValue":"JSON Rules"},"xPath": /store/book[23]/title"}

{"node":{"nodeName":"tr","firstChild":{ "nodeName":"td","textContent":"T2:row1, cell 1"}},"xPath": "html//table//tr"}

Page 14: JSON Rules Language

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 == "input", nodeType == "text",$Y:nodeValue)or, shorter,$X : Input(type == "radio", $Y:value)• Assuming <input id="name" name="name" type="text" value="Doe"/>$Y will be bound to the string "Doe"

Page 15: JSON Rules Language

More on Descriptions

$N:Input( id=="postalCode",nodeValue==RegExp("/^\d{5}$/"))

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

• Descriptions can be much more complex

Page 16: JSON Rules Language

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

Page 17: JSON Rules Language

Rule Actions

• JSON Rules allows any valid JavaScript function call as a rule action.

• For example "actions":["append($X.nodeValue)"]

assuming that $X is bound to the value {"nodeValue":"JSON Rules"}

the performed JavaScript call is:append("JSON Rules")

Page 18: JSON Rules Language

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 callalert(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)

Page 19: JSON Rules Language

Intelligent and adaptive Web user interfaces

• Tip: "more than 3 times" 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.

{ "id":"fast101", "appliesTo":["http://www.example.com""],"eventExpression":{ "eventType":"click", "eventTarget":"$X"}, "conditions": [ "$X:A( $Id:id, class = = 'menuItem')", "count("click",$Id) >= 3"], "actions": ["fastMenu('add',$X)"] }

Page 20: JSON Rules Language

Implement Web Business ProcessesIf the user loads financial news, offers him a three months subscription to Financial Times

{ "id":"fast101", "appliesTo":['http://www.example.com'"],"eventExpression":{"eventType":"DOMNodeInserted", "eventTarget":"$X"}, "conditions": [ "$Y:Div(id == 'currentFeedContent')","$X:Div(?Y:relatedNode, $Z:childNodes)", "$C:Div(class='itemcontent')","org.jsonrules.builtin.pred.member($C, $Z)","$C.firstChild.nodeValue=='businessNews'"], "actions": ["showAdd({"s":"ft","d":"3","p":"30"})"] }

Page 21: JSON Rules Language

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)

{"description":{"type":"HTMLElement", "binding":{"variable":{"name":"$X"}},"constraints":[{ "propertyRestriction":{ "property":"class","operator":"EQ","value":"note"}},{"propertyBinding":{"property":"firstChild","variable":{"name":"$Y"}}}]}}

Page 22: JSON Rules Language

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