<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>JSON Rules - The JavaScript Rule Engine</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Emilian Pascalau</string-name>
          <email>emilian.pascalau@hpi.uni-potsdam.de</email>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Adrian Giurca</string-name>
          <email>giurca@tu-cottbus.de</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Brandenburg University of Technology</institution>
          ,
          <country country="DE">Germany</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Hasso Plattner Institute</institution>
          ,
          <country country="DE">Germany</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>TOOL PRESENTATION: There is a considerable browser potential in being able to easily wire together di erent services into new functionality. Usually, developers use JavaScript or related technologies to do browser programming. This short paper presents, JSON Rules, a JavaScript rule engine running Event-Condition-Action rules triggered by Document-Object-Model Events.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>
        The Rule Engine implementing the JSON Rules [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] language was designed to
ful ll at least the following requirements:
{ create and execute rules in a Web browser
{ support for ECA and PR rules
{ forward chaining rule engine, in uenced by the RETE algorithm;
{ process atomic event-facts;
{ the Working Memory contains beside regular facts, event facts.
      </p>
    </sec>
    <sec id="sec-2">
      <title>The JSON Rules language</title>
      <p>
        The language was initially introduced in [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. JSON notation combined with
JavaScript function calls o ers large capabilities to express various kinds of rules.
Recall that we deal both with production rules and with Event-Condition-Action
(ECA) rules i.e. rules of the form
RuleID: ON EventExpression IF C1 &amp;&amp; ... &amp;&amp; Cn DO [A1, ..., Am]
where the event part is optional and denotes an event expression matching the
triggering events of the rule; C1, ... Cn are boolean conditions using a Drools
like syntax and [A1, ... Am] is a sequence of actions.
2.1
      </p>
      <sec id="sec-2-1">
        <title>Ontology of events - DOM events</title>
        <p>The JSON event expression is related to the Event interface speci cation in
DOM Level 3 Events1, therefore the properties of this expression have the same
meaning as in the Event speci cation. At runtime these properties of this
expression are matched against the incoming DOM events and their values can be
processed in the rule conditions and actions.</p>
        <p>Example 1 (ECA Rule).
{"id": "rule101",
"appliesTo": ["http://mail.yahoo.com/"],
"eventExpression": {"type": "click",</p>
        <p>"target": "$X"
},
"condition":[
"$X:HTMLAnchorElement($hrefVal:href)",
"new RegExp(/showMessage\?fid=Inbox/).test($hrefVal)"
],
"actions":["append($X.textContent)"]
}
3</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>The Engine</title>
      <p>There is an important di erence between the actual rule engines and the
JavaScript Rule Engine implementing the JSON Rules language for at least two
reasons: events facts are not static facts that require usual operation such as:
delete, update on the Working Memory but they are dynamic facts. They are
dynamically consumed based on the appearance time. Second the whole engine is
a live system: it is reactive because reacts based on events and it is proactive
for by itself produces events.</p>
      <p>The project is hosted on Google Code platform2.
3.1</p>
      <sec id="sec-3-1">
        <title>How you can use the engine</title>
        <p>The engine is programmed in JavaScript and can be used as any JavaScript
framework. Basically, the lifetime of the rule engine is in the scope of the lifetime
of the current DOM inside the browser. Simple steps to make it run are:
1. Load the engine in your page:
&lt;script type="text/javascript"</p>
        <p>src="http://www.domain.com/jsonRulesEngine_Version.js"&gt;
&lt;/script&gt;
1 http://www.w3.org/TR/DOM-Level-3-Events/
2 http://jsonrules.googlecode.com
2. Create an instance of the engine:</p>
        <p>var jsonRulesEngine=new org.jsonrules.JSONRulesMainSystem();
3. Run the engine by calling run() with the URI of location of the repository
as input parameter:
When the engine and the rulesets are available, the main things that happen
are:
{ When an event is raised, the EventManager catches that event. Then the</p>
        <p>EventManager checks the ActionProcessor state.
{ If the ActionProcessor is running, then the EventManager stores the event
in the queue of events that the InferenceEngine must later on process.
{ However if the ActionProcessor is idle then the EventManager sends a
message to the InferenceEngine containing the queue of events that must be
processed. The InferenceEngine responds back to the EventManager, and
informs it that it has received/consumed the queue such that the EventManager
can reset its own queue.
{ Events are processed one by one. For each event rules triggered by that
event will be matched against the WorkingMemory. The action of each
executable rule is added to the list of executable actions (to be processed by
the ActionProcessor) according with possible priority of rules.
{ The list of executable actions it is send to the ActionProcessor, to execute
them. Any JavaScript functions can be called in the rule actions' part.
4</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>Conclusions References</title>
      <p>This paper describes shortly the general ideas behind an ECA rule-based and
forward chaining engine for browsers.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <given-names>Adrian</given-names>
            <surname>Giurca</surname>
          </string-name>
          and
          <string-name>
            <given-names>Emilian</given-names>
            <surname>Pascalau</surname>
          </string-name>
          .
          <source>JSON Rules</source>
          .
          <source>In Proceedings of the Proceedings of 4th Knowledge Engineering and Software Engineering</source>
          ,
          <string-name>
            <surname>KESE</surname>
          </string-name>
          <year>2008</year>
          , volume
          <volume>425</volume>
          , pages
          <fpage>7</fpage>
          <lpage>{</lpage>
          18. CEUR Workshop Proceedings,
          <year>2008</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>Emilian</given-names>
            <surname>Pascalau</surname>
          </string-name>
          and
          <string-name>
            <given-names>Adrian</given-names>
            <surname>Giurca</surname>
          </string-name>
          .
          <article-title>A Rule-Based Approach of Creating and Executing Mashups</article-title>
          .
          <source>In Proceedings of the 9th IFIP Conference on e-Business</source>
          , eServices, and e-Society (
          <year>I3E 2009</year>
          ), LNCS. Springer,
          <year>2009</year>
          . forthcoming.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>