[jboss-svn-commits] JBL Code SVN: r13603 - labs/jbossrules/trunk/documentation/manual/en/Chapter-Rule_Language.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jul 18 08:28:19 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-07-18 08:28:19 -0400 (Wed, 18 Jul 2007)
New Revision: 13603

Modified:
   labs/jbossrules/trunk/documentation/manual/en/Chapter-Rule_Language/Section-Rule.xml
Log:
-more updated on the Rule language spec.

Modified: labs/jbossrules/trunk/documentation/manual/en/Chapter-Rule_Language/Section-Rule.xml
===================================================================
--- labs/jbossrules/trunk/documentation/manual/en/Chapter-Rule_Language/Section-Rule.xml	2007-07-18 11:49:27 UTC (rev 13602)
+++ labs/jbossrules/trunk/documentation/manual/en/Chapter-Rule_Language/Section-Rule.xml	2007-07-18 12:28:19 UTC (rev 13603)
@@ -278,11 +278,11 @@
 
     <para>As mentioned in <xref linkend="RuleLanguage-LHS" os="" /> the LHS is
     made up of one or more Conditional Elements. The Pattern elment is the
-    most important Conditional Element and as such has it's own section. As
-    you already know a forward chaining rule engine is often referred to as a
-    Pattern Matching engine, because at the heart it matches the specified
-    Patterns against the available data. So lets take a look at what a Pattern
-    is.</para>
+    most important Conditional Element, which is why we have given it a
+    dedicated section. As you already know a forward chaining rule engine is
+    often referred to as a Pattern Matching engine, because at the heart it
+    matches the specified Patterns against the available data. So lets take a
+    look at what a Pattern is.</para>
 
     <figure>
       <title>Pattern</title>
@@ -299,7 +299,7 @@
     </figure>
 
     <para>At the simplest, with no constraints, it simply matches against a
-    type, in the following casae the type is "Cheese". This meanst the pattern
+    type, in the following case the type is "Cheese". This meanst the pattern
     will match against all Cheese objects in the working memory.</para>
 
     <example>
@@ -308,8 +308,11 @@
       <programlisting>Cheese( )</programlisting>
     </example>
 
-    <para>To be able to reference, from the consequence, the object that the
-    pattern matches against we use a pattern binding variable.</para>
+    <para>To be able to refer to the object, from other patterns or in the
+    consequence, use a pattern binding variable '$c'. While the the variable
+    is prefixed with a $ symbol, it is optional, but can be useful in complex
+    rules so an easier differentiation can be made between variables and
+    fields.</para>
 
     <example>
       <title>Pattern</title>
@@ -317,11 +320,78 @@
       <programlisting>$c : Cheese( )</programlisting>
     </example>
 
+    <para>Inside of the pattern parenthesis is where all the action happens.
+    Drools 4.0 brings a large, and almost bewildering, range and combination
+    of constraints.</para>
+
+    <figure>
+      <title>Constraints</title>
+
+      <mediaobject>
+        <imageobject>
+          <imagedata align="center" fileref="constraints.svg" format="SVG"
+                     role="" />
+        </imageobject>
+
+        <imageobject>
+          <imagedata align="center" fileref="constraints.png" format="PNG"
+                     role="" />
+        </imageobject>
+      </mediaobject>
+    </figure>
+
+    <figure>
+      <title>Constraint</title>
+
+      <mediaobject>
+        <imageobject>
+          <imagedata align="center" fileref="constraint.svg" format="SVG"
+                     role="" />
+        </imageobject>
+
+        <imageobject>
+          <imagedata align="center" fileref="constraint.png" format="PNG"
+                     role="" />
+        </imageobject>
+      </mediaobject>
+    </figure>
+
+    <figure>
+      <title>groupConstraint</title>
+
+      <mediaobject>
+        <imageobject>
+          <imagedata align="center" fileref="constraintGroup.svg" format="SVG"
+                     role="" />
+        </imageobject>
+
+        <imageobject>
+          <imagedata align="center" fileref="constraintGroup.png" format="PNG"
+                     role="" />
+        </imageobject>
+      </mediaobject>
+    </figure>
+
+    <figure>
+      <title>groupConstraint</title>
+
+      <mediaobject>
+        <imageobject>
+          <imagedata align="center" fileref="fieldConstraint.svg" format="SVG"
+                     role="" />
+        </imageobject>
+
+        <imageobject>
+          <imagedata align="center" fileref="fieldConstraint.png" format="PNG"
+                     role="" />
+        </imageobject>
+      </mediaobject>
+    </figure>
+
     <example>
       <title>Pattern</title>
 
-      <programlisting>Cheese( )
-Cheese( type == "stilton", price &lt; 10 )</programlisting>
+      <programlisting>Cheese( type == "stilton", price &lt; 10 )</programlisting>
     </example>
 
     <para>A Rule consists of Field Constraints on one or more Object Types.
@@ -725,10 +795,13 @@
 
         <para>A <indexterm>
             <primary>Predicate</primary>
-          </indexterm>Predicate constraint can use any valid expression as
-        long as it evaluated to a primitive boolean - avoid using any Drools
-        keywords as Declaration identifiers. Previously bound declarations can
-        be used in the expression. </para>
+          </indexterm>Predicate constraint can use any valid Java expression
+        as long as it evaluated to a primitive boolean - avoid using any
+        Drools keywords as Declaration identifiers. Previously bound
+        declarations can be used in the expression. Functions used in a
+        Predicate Constraint must return time constant results. All bound
+        primitive declarations are boxed, there is currently no auto-unboxing
+        (if you use java 5, this is all automatic).</para>
 
         <para>This example will find all pairs of male/femal people where the
         male is 2 years older than the female.</para>
@@ -737,13 +810,8 @@
           <title>Return Value operator</title>
 
           <programlisting>Person( girlAge : age, sex = "F" )
-Person( girlAge == age + 2, sex == 'M' )
-
-//can also be written as:
-
-p: Person()
-
-Person( p.girlAge == age + 2, sex == 'M' )</programlisting>
+Person( boyAge : age -&gt; ( girlAge.intValue() == boyAge.intValue() + 2 ), sex = 'M' )
+</programlisting>
         </example>
       </section>
 
@@ -773,7 +841,9 @@
         primitives - avoid using any Drools keywords as Declaration
         identifiers. Functions used in a Return value Constraint must return
         time constant results. Previously bound declarations can be used in
-        the expression. </para>
+        the expression. All bound primitive declarations are boxed, there is
+        currently no auto-unboxing. The returned value must be boxed if its a
+        primitive..</para>
 
         <para>Like the Predicate example this will find all pairs of
         male/femal people where the male is 2 years older than the female.
@@ -784,7 +854,7 @@
           <title>Return Value operator</title>
 
           <programlisting>Person( girlAge : age, sex == "F" )
-Person( age == ( girlAge + 2) , sex == 'M' )
+Person( age == ( new Integer(girlAge.intValue() + 2) ), sex == 'M' )
 </programlisting>
         </example>
       </section>
@@ -932,12 +1002,7 @@
         <programlisting>p1 : Parameter() 
 p2 : Parameter()
 eval( p1.getList().containsKey(p2.getItem()) )
-eval( isValid(p1, p2) ) //this is how you call a function in the LHS - a function called "isValid"
-
-
-//there is also "inline eval":
-
-p1: Parameber( eval(list.containsKey("42")) )</programlisting>
+eval( isValid(p1, p2) ) //this is how you call a function in the LHS - a function called "isValid"</programlisting>
       </example>
     </section>
 
@@ -1072,5 +1137,25 @@
     </example>
   </section>
 
+  <section>
+    <title>A note on autoboxing and primitive types</title>
 
+    <para>Java 5 supports autoboxing and unboxing between primitives of
+    appropriate types. This makes for very convenient and easy to read code.
+    However, as drools runs in J2SE 1.4 as well, we can't rely on this. Thus
+    we have to autobox at times. Fields that are referred to are autoboxed in
+    the corresponding object type automatically (if they are already an
+    object, then there is no change). However, it is important to note that
+    they are not "unboxed" automatically. Thus if you bind to a field that is
+    an "int" in your object model, it will behave like an Object in the rule
+    (ie predicates, return value constraints and the RHS).</para>
+
+    <para>As a general rule, if possible, make your fields object types (at
+    least until java 5), or at least think of your fields as object types even
+    if they are not to start with). Another special note, is that for return
+    value constraints, the return value snippet of code must return an Object
+    (not a primitive). Now, I bet you can't wait for Java 5 to be the minimum
+    ! The fact that not quite *everything* in java is an object causes
+    headaches like this (keep those tablets handy).</para>
+  </section>
 </section>
\ No newline at end of file




More information about the jboss-svn-commits mailing list