<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
accumulate still returns objects, as we don't allow patterns against
primitives. maybe for special use cases we can allot $i : int() from
accumulate (......) in the future.<br>
<br>
Btw did you try the sequential engine mode in your stateless approach?<br>
<br>
Mark<br>
Edson Tirelli wrote:
<blockquote
 cite="mid:e6dd5ba30707221230s447222ffn4bc7f19eeb3df645@mail.gmail.com"
 type="cite"><br>
&nbsp;&nbsp;&nbsp; Geoffrey,<br>
  <br>
&nbsp;&nbsp;&nbsp; The "sum" function that is shipped with drools always uses double
internally, but all numeric accumulate functions must have return type
Number to avoid class cast exceptions... I must write that in the
docs... so, recommendation is to do:
  <br>
  <br>
&nbsp;&nbsp;&nbsp;&nbsp; Number( $total : intValue ) from accumulate(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hop($distance : distance ), // distance is an int<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum($distance)<br>
&nbsp;&nbsp;&nbsp;&nbsp; );<br>
  <br>
&nbsp;&nbsp;&nbsp; Now, having said that, the decision to use double is because we
needed a one size fits all to ship with drools. On the other hand I
tried to make as simple as possible to plug new or replace built-in
functions.&nbsp; So, if you want a sum function that uses only integers, you
can easily develop and plug it your own. Since I just wrote the docs on
how to do it, may I ask you please to read them and provide a feedback
if they are good, need more info, shall I write them in another way,
etc?
  <br>
&nbsp;&nbsp;&nbsp; BTW, when I say it is really simple to create another sum function,
I mean, you can do it in about 20 minutes. Really!<br>
  <br>
&nbsp;&nbsp;&nbsp; Find attached the HTML doc page I'm talking about.<br>
  <br>
&nbsp;&nbsp;&nbsp; []s<br>
&nbsp;&nbsp;&nbsp; Edson
  <br>
  <br>
  <br>
  <div><span class="gmail_quote">2007/7/22, Geoffrey De Smet &lt;<a
 moz-do-not-send="true" href="mailto:ge0ffrey.spam@gmail.com"
 target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
ge0ffrey.spam@gmail.com</a>&gt;:</span>
  <blockquote class="gmail_quote"
 style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi
guys,<br>
    <br>
I finally got around to experimenting with the accumulate support in
    <br>
LocalSearchSolver. Thanks for implementing it :)<br>
    <br>
I use nothing but int's in my calculations,<br>
but I get a ClassCastExceptions, as sum() always returns a Double and<br>
setSoftConstraintsBroken(int) failes.
    <br>
    <br>
In a previous benchmarks I 've proven that summing integers in double's<br>
instead of int's hurts performance for 10% or more (which is rather
big).<br>
    <br>
Is there a sumInteger() available (or do I need to write it)? Or can it
    <br>
be overloaded over sum() in a much cleaner way?<br>
    <br>
    <br>
    <br>
rule "ScoreCalculator"<br>
&nbsp;&nbsp; when<br>
&nbsp;&nbsp;&nbsp;&nbsp; $scoreFact : ScoreFact(); // singleton<br>
&nbsp;&nbsp;&nbsp;&nbsp; $total : Integer() from accumulate(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hop($distance : distance ), // distance is an int
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum($distance)<br>
&nbsp;&nbsp;&nbsp;&nbsp; );<br>
&nbsp;&nbsp; then<br>
&nbsp;&nbsp;&nbsp;&nbsp; $scoreFact.setSoftConstraintsBroken($total);<br>
end<br>
    <br>
    <br>
svn is still here, till it's ready for drools:<br>
    <a moz-do-not-send="true"
 href="https://taseree.svn.sourceforge.net/svnroot/taseree/trunk"
 target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">https://taseree.svn.sourceforge.net/svnroot/taseree/trunk</a><br>
    <br>
--<br>
With kind regards,<br>
Geoffrey De Smet<br>
    <br>
_______________________________________________<br>
rules-dev mailing list<br>
    <a moz-do-not-send="true" href="mailto:rules-dev@lists.jboss.org"
 target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">rules-dev@lists.jboss.org</a><br>
    <a moz-do-not-send="true"
 href="https://lists.jboss.org/mailman/listinfo/rules-dev"
 target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">https://lists.jboss.org/mailman/listinfo/rules-dev</a><br>
  </blockquote>
  </div>
  <br>
  <br clear="all">
  <br>
-- <br>
&nbsp;&nbsp;Edson Tirelli<br>
&nbsp;&nbsp;Software Engineer - JBoss Rules Core Developer<br>
&nbsp;&nbsp;Office: +55 11 3529-6000<br>
&nbsp;&nbsp;Mobile: +55 11 9287-5646<br>
&nbsp;&nbsp;JBoss, a division of Red Hat @ <a moz-do-not-send="true"
 href="http://www.jboss.com" target="_blank"
 onclick="return top.js.OpenExtLink(window,event,this)">
www.jboss.com</a>
  <br>
  <hr size="4" width="90%"><br>
  <meta http-equiv="Content-Type" content="text/html; ">
  <title>3.6.&nbsp;Advanced Conditional Elements</title>
  <link rel="stylesheet" href="../shared/css/html.css" type="text/css">
  <meta name="generator"
 content="DocBook XSL Stylesheets Vsnapshot_6797">
  <link rel="start" href="title.html" title="Drools">
  <link rel="up" href="ch03.html" title="Chapter&nbsp;3.&nbsp;The Rule Language">
  <link rel="prev" href="ch03s05.html" title="3.5.&nbsp;Rule">
  <link rel="next" href="ch03s07.html" title="3.7.&nbsp;Query">
  <base xmlns="http://www.w3.org/TR/xhtml1/transitional" target="body">
  <div xmlns="" class="navheader">
  <table _base_target="body" summary="Navigation header" width="100%">
    <tbody _base_target="body">
      <tr _base_target="body">
        <th _base_target="body" colspan="3" align="center">3.6.&nbsp;Advanced
Conditional Elements</th>
      </tr>
      <tr _base_target="body">
        <td _base_target="body" align="left" width="20%"><a
 moz-do-not-send="true" accesskey="p" href="ch03s05.html">Prev</a>&nbsp;</td>
        <th _base_target="body" align="center" width="60%">Chapter&nbsp;3.&nbsp;The
Rule Language</th>
        <td _base_target="body" align="right" width="20%">&nbsp;<a
 moz-do-not-send="true" accesskey="n" href="ch03s07.html">Next</a></td>
      </tr>
    </tbody>
  </table>
  <hr></div>
  <div xmlns="" class="section" lang="en">
  <div class="titlepage">
  <div>
  <div>
  <h2 class="title" style="clear: both;"><a moz-do-not-send="true"
 name="d0e2677"></a>3.6.&nbsp;Advanced Conditional Elements</h2>
  </div>
  </div>
  </div>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
  <h3 class="title">Note</h3>
  <p><em class="replaceable"><code>(updated to Drools 4.0)</code></em></p>
  </div>
  <p>Drools 4.0 introduces a whole new set of conditional elements in
order to support full First Order Logic expressiveness, as well as some
facilities for handling collections of facts. This section will detail
the following new Conditional Elements:</p>
  <div class="itemizedlist">
  <ul type="disc">
    <li>
      <p>from</p>
    </li>
    <li>
      <p>collect</p>
    </li>
    <li>
      <p>accumulate</p>
    </li>
    <li>
      <p>forall</p>
    </li>
  </ul>
  </div>
  <div class="section" lang="en">
  <div class="titlepage">
  <div>
  <div>
  <h3 class="title"><a moz-do-not-send="true" name="d0e2699"></a>3.6.1.&nbsp;From</h3>
  </div>
  </div>
  </div>
  <p>The <span class="bold"><strong>from</strong></span> Conditional
Element allows users to specify a source for patterns to reason over.
This allows the engine to reason over data not in the Working Memory.
This could be a sub-field on a bound variable or the results of a
method call. It is a powerful construction that allows out of the box
integration with other application components and frameworks. One
common example is the integration with data retrieved on-demand from
databases using hibernate named queries.</p>
  <p>The expression used to define the object source is any expression
that follows regular MVEL syntax. I.e., it allows you to easily use
object property navigation, execute method calls and access maps and
collections elements.</p>
  <p>Here is a simple example of reasoning and binding on another
pattern sub-field:</p>
  <pre class="programlisting">rule "validate zipcode"
when
    Person( $personAddress : address ) 
    Address( zipcode == "23920W") from $personAddress 
then
    # zip code is ok
end
  </pre>
  <p>With all the flexibility from the new expressiveness in the Drools
engine you can slice and dice this problem many ways. This is the same
but shows how you can use a graph notation with the 'from':</p>
  <pre class="programlisting">rule "validate zipcode"
when
    $p : Person( ) 
    $a : Address( zipcode == "23920W") from $p.address 
then
    # zip code is ok
end
  </pre>
  <p>Previous examples were reasoning over a single pattern. The <span
 class="bold"><strong>from</strong></span> CE also support object
sources that return a collection of objects. In that case, <span
 class="bold"><strong>from</strong></span> will iterate over all
objects in the collection and try to match each of them individually.
For instance, if we want a rule that applies 10% discount to each item
in an order, we could do:</p>
  <pre class="programlisting">rule "apply 10% discount to all items over US$ 100,00 in an order"
when
    $order : Order()
    $item  : OrderItem( value &gt; 100 ) from $order.items
then
    # apply discount to $item
end
  </pre>
  <p>The above example will cause the rule to fire once for each item
whose value is greater than 100 for each given order.</p>
  <p>The next example shows how we can reason over the results of a
hibernate query. The Restaurant pattern will reason over and bind with
each result in turn:</p>
  </div>
  <div class="section" lang="en">
  <div class="titlepage">
  <div>
  <div>
  <h3 class="title"><a moz-do-not-send="true" name="d0e2733"></a>3.6.2.&nbsp;Collect</h3>
  </div>
  </div>
  </div>
  <p>The <span class="bold"><strong>collect</strong></span>
Conditional Element allows rules to reason over collection of objects
collected from the given source or from the working memory. A simple
example:</p>
  <pre class="programlisting">import java.util.ArrayList

rule "Raise priority if system has more than 3 pending alarms"
when
    $system : System()
    $alarms : ArrayList( size &gt;= 3 )
              from collect( Alarm( system == $system, status == 'pending' ) )
then
    # Raise priority, because system $system has
    # 3 or more alarms pending. The pending alarms
    # are $alarms.
end
  </pre>
  <p>In the above example, the rule will look for all pending alarms in
the working memory for each given system and group them in ArrayLists.
If 3 or more alarms are found for a given system, the rule will fire.</p>
  <p>The <span class="bold"><strong>collect</strong></span> CE result
pattern can be any concrete class that implements tha
java.util.Collection interface and provides a default no-arg public
constructor. I.e., you can use default java collections like ArrayList,
LinkedList, HashSet, etc, or your own class, as long as it implements
the java.util.Collection interface and provide a default no-arg public
constructor.</p>
  <p>Both source and result patterns can be constrained as any other
pattern.</p>
  <p>Variables bound before the <span class="bold"><strong>collect</strong></span>
CE are in the scope of both source and result patterns and as so, you
can use them to constrain both your source and result patterns.
Although, the <span class="emphasis"><em>collect( ... )</em></span> is
a scope delimiter for bindings, meaning that any binding made inside of
it, is not available for use outside of it.</p>
  <p>Collect accepts nested <span class="bold"><strong>from</strong></span>
elements, so the following example is a valid use of <span class="bold"><strong>collect</strong></span>:</p>
  <pre class="programlisting">import java.util.LinkedList;

rule "Send a message to all mothers"
when
    $town : Town( name == 'Paris' )
    $mothers : LinkedList() 
               from collect( Person( gender == 'F', children &gt; 0 ) 
                             from $town.getPeople() 
                           )
then
    # send a message to all mothers
end
  </pre>
  </div>
  <div class="section" lang="en">
  <div class="titlepage">
  <div>
  <div>
  <h3 class="title"><a moz-do-not-send="true" name="d0e2770"></a>3.6.3.&nbsp;Accumulate</h3>
  </div>
  </div>
  </div>
  <p>The <span class="bold"><strong>accumulate</strong></span>
Conditional Element is a more flexible and powerful form of <span
 class="bold"><strong>collect</strong></span> Conditional Element, in
the sense that it can be used to do what <span class="bold"><strong>collect</strong></span>
CE does and also do things that <span class="bold"><strong>collect</strong></span>
CE is not capable to do. Basically what it does is it allows a rule to
iterate over a collection of objects, executing custom actions for each
of the elements, and at the end return a result object.</p>
  <p>The general syntax of the <span class="bold"><strong>accumulate</strong></span>
CE is:</p>
  <pre class="programlisting"><em class="replaceable"><code>&lt;result pattern&gt;</code></em> from accumulate( <em
 class="replaceable"><code>&lt;source pattern&gt;</code></em>,
                                  init( <em class="replaceable"><code>&lt;init code&gt;</code></em> ),
                                  action( <em class="replaceable"><code>&lt;action code&gt;</code></em> ),
                                  reverse( <em class="replaceable"><code>&lt;reverse code&gt;</code></em> ),
                                  result( <em class="replaceable"><code>&lt;result expression&gt;</code></em> ) )
  </pre>
  <p>The meaning of each of the elements is the following:</p>
  <div class="itemizedlist">
  <ul type="disc">
    <li>
      <p><span class="bold"><strong>&lt;source pattern&gt;</strong></span>:
the source pattern is a regular pattern that the engine will try to
match against each of the source objects.</p>
    </li>
    <li>
      <p><span class="bold"><strong>&lt;init code&gt;</strong></span>:
this is a semantic block of code in the selected dialect that will be
executed once for each tuple, before iterating over the source objects.</p>
    </li>
    <li>
      <p><span class="bold"><strong>&lt;action code&gt;</strong></span>:
this is a semantic block of code in the selected dialect that will be
executed for each of the source objects.</p>
    </li>
    <li>
      <p><span class="bold"><strong>&lt;reverse code&gt;</strong></span>:
this is an optional semantic block of code in the selected dialect that
if present will be executed for each source object that no longer
matches the source pattern. The objective of this code block is to
"undo" any calculation done in the &lt;action code&gt; block, so that
the engine can do decremental calculation when a source object is
modified or retracted, hugely improving performance of these operations.</p>
    </li>
    <li>
      <p><span class="bold"><strong>&lt;result expression&gt;</strong></span>:
this is a semantic expression in the selected dialect that is executed
after all source objects are iterated.</p>
    </li>
    <li>
      <p><span class="bold"><strong>&lt;result pattern&gt;</strong></span>:
this is a regular pattern that the engine tries to match against the
object returned from the &lt;result expression&gt;. If it matches, the <span
 class="bold"><strong>accumulate</strong></span> conditional element
evaluates to <span class="bold"><strong>true</strong></span> and the
engine proceeds with the evaluation of the next CE in the rule. If it
does not matches, the <span class="bold"><strong>accumulate</strong></span>
CE evaluates to <span class="bold"><strong>false</strong></span> and
the engine stops evaluating CEs for that rule.</p>
    </li>
  </ul>
  </div>
  <p>It is easier to understand if we look at an example:</p>
  <pre class="programlisting">rule "Apply 10% discount to orders over US$ 100,00"
when
    $order : Order()
    $total : Number( doubleValue &gt; 100 ) 
             from accumulate( OrderItem( order == $order, $value : value ),
                              init( double total = 0; ),
                              action( total += $value; ),
                              reverse( total -= $value; ),
                              result( total ) )
then
    # apply discount to $order
end
  </pre>
  <p>In the above example, for each Order() in the working memory, the
engine will execute the <span class="bold"><strong>init code</strong></span>
initializing the total variable to zero. Then it will iterate over all
OrderItem() objects for that order, executing the <span class="bold"><strong>action</strong></span>
for each one (in the example, it will sum the value of all items into
the total variable). After iterating over all OrderItem, it will return
the value corresponding to the <span class="bold"><strong>result
expression</strong></span> (in the above example, the value of the
total variable). Finally, the engine will try to match the result with
the Number() pattern and if the double value is greater than 100, the
rule will fire.</p>
  <p>The example used java as the semantic dialect, and as such, note
that the usage of ';' is mandatory in the init, action and reverse code
blocks. The result is an expression and as such, it does not admit ';'.
If the user uses any other dialect, he must comply to that dialect
specific syntax.</p>
  <p>As mentioned before, the <span class="bold"><strong>reverse code</strong></span>
is optional, but it is strongly recommended that the user writes it in
order to benefit from the <span class="emphasis"><em>improved
performance on update and retracts</em></span>.</p>
  <p>The <span class="bold"><strong>accumulate</strong></span> CE can
be used to execute any action on source objects. The following example
instantiates and populates a custom object:</p>
  <pre class="programlisting">rule "Accumulate using custom objects"
when
    $person   : Person( $likes : likes )
    $cheesery : Cheesery( totalAmount &gt; 100 )
                from accumulate( $cheese : Cheese( type == $likes ),
                                 init( Cheesery cheesery = new Cheesery(); ),
                                 action( cheesery.addCheese( $cheese ); ),
                                 reverse( cheesery.removeCheese( $cheese ); ),
                                 result( cheesery ) );
then
    // do something
end</pre>
  <div class="section" lang="en">
  <div class="titlepage">
  <div>
  <div>
  <h4 class="title"><a moz-do-not-send="true" name="d0e2888"></a>3.6.3.1.&nbsp;Accumulate
Functions</h4>
  </div>
  </div>
  </div>
  <p>The accumulate CE is a very powerful CE, but it gets real
declarative and easy to use when using predefined functions that are
known as Accumulate Functions. They work exactly like accumulate, but
instead of explicitly writing custom code in every accumulate CE, the
user can use predefined code for common operations.</p>
  <p>For instance, the rule to apply discount on orders written in the
previous section, could be written in the following way, using
Accumulate Functions:</p>
  <pre class="programlisting">rule "Apply 10% discount to orders over US$ 100,00"
when
    $order : Order()
    $total : Number( doubleValue &gt; 100 ) 
             from accumulate( OrderItem( order == $order, $value : value ),
                              sum( $value ) )
then
    # apply discount to $order
end
  </pre>
  <p>In the above example, sum is an AccumulateFunction and will sum
the $value of all OrderItems and return the result.</p>
  <p>Drools 4.0 ships with the following built in accumulate functions:</p>
  <div class="itemizedlist">
  <ul type="disc">
    <li>
      <p>average</p>
    </li>
    <li>
      <p>min</p>
    </li>
    <li>
      <p>max</p>
    </li>
    <li>
      <p>count</p>
    </li>
    <li>
      <p>sum</p>
    </li>
  </ul>
  </div>
  <p>These common functions accept any expression as input. For
instance, if someone wants to calculate the average profit on all items
of an order, a rule could be written using the average function:</p>
  <pre class="programlisting">rule "Average profit"
when
    $order : Order()
    $profit : Number() 
              from accumulate( OrderItem( order == $order, $cost : cost, $price : price )
                               average( 1 - $cost / $price ) )
then
    # average profit for $order is $profit
end
  </pre>
  <p>Accumulate Functions are all pluggable. That means that if needed,
custom, domain specific functions can easily be added to the engine and
rules can start to use them without any restrictions. To implement a
new Accumulate Functions all one needs to do is to create a java class
that implements the org.drools.base.acumulators.AccumulateFunction
interface and add a line to the configuration file or set a system
property to let the engine know about the new function. As an example
of an Accumulate Function implementation, the following is the
implementation of the "average" function:</p>
  <pre class="programlisting">/*
 * Copyright 2007 JBoss Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      <a class="moz-txt-link-freetext" href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created on Jun 21, 2007
 */
package org.drools.base.accumulators;


/**
 * An implementation of an accumulator capable of calculating average values
 * 
 * @author etirelli
 *
 */
public class AverageAccumulateFunction implements AccumulateFunction {

    protected static class AverageData {
        public int    count = 0;
        public double total = 0;
    }

    /* (non-Javadoc)
     * @see org.drools.base.accumulators.AccumulateFunction#createContext()
     */
    public Object createContext() {
        return new AverageData();
    }

    /* (non-Javadoc)
     * @see org.drools.base.accumulators.AccumulateFunction#init(java.lang.Object)
     */
    public void init(Object context) throws Exception {
        AverageData data = (AverageData) context;
        data.count = 0;
        data.total = 0;
    }

    /* (non-Javadoc)
     * @see org.drools.base.accumulators.AccumulateFunction#accumulate(java.lang.Object, java.lang.Object)
     */
    public void accumulate(Object context,
                           Object value) {
        AverageData data = (AverageData) context;
        data.count++;
        data.total += ((Number) value).doubleValue();
    }

    /* (non-Javadoc)
     * @see org.drools.base.accumulators.AccumulateFunction#reverse(java.lang.Object, java.lang.Object)
     */
    public void reverse(Object context,
                        Object value) throws Exception {
        AverageData data = (AverageData) context;
        data.count--;
        data.total -= ((Number) value).doubleValue();
    }

    /* (non-Javadoc)
     * @see org.drools.base.accumulators.AccumulateFunction#getResult(java.lang.Object)
     */
    public Object getResult(Object context) throws Exception {
        AverageData data = (AverageData) context;
        return new Double( data.count == 0 ? 0 : data.total / data.count );
    }

    /* (non-Javadoc)
     * @see org.drools.base.accumulators.AccumulateFunction#supportsReverse()
     */
    public boolean supportsReverse() {
        return true;
    }

}
  </pre>
  <p>The code for the function is very simple, as we could expect, as
all the "dirty" integration work is done by the engine. Finally, to
plug the function into the engine, we added it to the configuration
file:</p>
  <pre class="programlisting">drools.accumulate.function.average = org.drools.base.accumulators.AverageAccumulateFunction
  </pre>
  <p>Where "drools.accumulate.function." is a prefix that must always
be used, "average" is how the function will be used in the rule file,
and "org.drools.base.accumulators.AverageAccumulateFunction" is the
fully qualified name of the class that implements the function behavior.</p>
  </div>
  </div>
  <div class="section" lang="en">
  <div class="titlepage">
  <div>
  <div>
  <h3 class="title"><a moz-do-not-send="true" name="d0e2931"></a>3.6.4.&nbsp;Forall</h3>
  </div>
  </div>
  </div>
  <p><span class="bold"><strong>Forall</strong></span> is the
Conditional Element that completes the First Order Logic support in
Drools. The syntax is very simple: </p>
  <pre class="programlisting">forall( <em class="replaceable"><code>&lt;select pattern&gt;</code></em> <em
 class="replaceable"><code>&lt;constraint patterns&gt;</code></em> )</pre>
  <p>The <span class="bold"><strong>forall</strong></span> Conditional
Element will evaluate to true when all facts that match the <em
 class="replaceable"><code>&lt;select pattern&gt;</code></em> match all
the <em class="replaceable"><code>&lt;constraint patterns&gt;</code></em>.
Example:</p>
  <pre class="programlisting">rule "All english buses are red"
when
    forall( $bus : Bus( type == 'english') 
                   Bus( this == $bus, color = 'red' ) )
then
    # all english buses are red
end
  </pre>
  <p>In the above rule, we "select" all Bus object whose type is
"english". Then, for each fact that matchs this pattern we evaluate the
following patterns and if they match, the forall CE will evaluate to
true. Another example:</p>
  <pre class="programlisting">rule "all employees have health and dental care programs"
when
    forall( $emp : Employee()
            HealthCare( employee == $emp )
            DentalCare( employee == $emp )
          )
then
    # all employees have health and dental care
end
  </pre>
  <p>Forall can be nested inside other CEs for complete expressiveness.
For instance, <span class="bold"><strong>forall</strong></span> can be
used inside a <span class="bold"><strong>not</strong></span> CE:</p>
  <pre class="programlisting">rule "not all employees have health and dental care"
when 
    not forall( $emp : Employee()
                HealthCare( employee == $emp )
                DentalCare( employee == $emp )
              )
then
    # not all employees have health and dental care
end
  </pre>
  <p>As a side note, forall Conditional Element is equivalent to
writing:</p>
  <pre class="programlisting">not( <em class="replaceable"><code>&lt;select pattern&gt;</code></em> and not ( and <em
 class="replaceable"><code>&lt;constraint patterns&gt;</code></em> ) )</pre>
  <p>Also, it is important to note that <span class="bold"><strong>forall
is a scope delimiter</strong></span>, so it can use any previously
bound variable, but no variable bound inside it will be available to
use outside of it.</p>
  </div>
  </div>
  <div xmlns="" class="navfooter">
  <hr>
  <table _base_target="body" summary="Navigation footer" width="100%">
    <tbody _base_target="body">
      <tr _base_target="body">
        <td _base_target="body" align="left" width="40%"><a
 moz-do-not-send="true" accesskey="p" href="ch03s05.html">Prev</a>&nbsp;</td>
        <td _base_target="body" align="center" width="20%"><a
 moz-do-not-send="true" accesskey="u" href="ch03.html">Up</a></td>
        <td _base_target="body" align="right" width="40%">&nbsp;<a
 moz-do-not-send="true" accesskey="n" href="ch03s07.html">Next</a></td>
      </tr>
      <tr _base_target="body">
        <td _base_target="body" align="left" valign="top" width="40%">3.5.&nbsp;Rule&nbsp;</td>
        <td _base_target="body" align="center" width="20%"><a
 moz-do-not-send="true" accesskey="h" href="title.html">Home</a>&nbsp;|&nbsp;<a
 moz-do-not-send="true" accesskey="t" href="bk01-toc.html">ToC</a></td>
        <td _base_target="body" align="right" valign="top" width="40%">&nbsp;3.7.&nbsp;Query</td>
      </tr>
    </tbody>
  </table>
  </div>
  <pre wrap="">
<hr size="4" width="90%">
_______________________________________________
rules-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-dev@lists.jboss.org">rules-dev@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-dev">https://lists.jboss.org/mailman/listinfo/rules-dev</a>
  </pre>
</blockquote>
<br>
</body>
</html>