---
title: "Drools高级技巧：Accumulate函数"
author: "hxpwork"
date: "2007-07-19T00:34:00Z"
tags: []
categories: ["企业架构"]
slug: "drools-accumulate"
draft: false
lastmod: "2007-07-19T00:34:00Z"
id: 102540
url: "https://hxpwork.iteye.com/blog/102540"
---

**Accumulate函数 (Edson Tirelli) &lt;o:p&gt;&lt;/o:p&gt;**

作者： Mark Proctor &lt;o:p&gt;&lt;/o:p&gt;

当我们接近完成发布版，一切事情趋于它最终的形状。这个星期是将Accumulate条件元素完成的日子。对于不了解它的人，Accumulate是Drools4.0中非常强大的一个条件元素。它允许你对数据集进行操作&lt;o:p&gt;&lt;/o:p&gt;

通常它的语法如下：&lt;o:p&gt;&lt;/o:p&gt;

**ResultPattern**( *fieldconstraint\** )  
**from accumulate** ( **SourcePattern**( *fieldconstraint\** )  
                  **init**( *code* )  
                  **action**( *code* )  
                  **reverse**( *code* )  
                  **result**( *code* ) )&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

基本上，**accumulate**所做的是执行***init***代码块一次，然后迭代所有匹配**SourcePattern**的fact，对每一个fact执行***action***代码块并且最后执行***result***代码块一次。所获得的结果按照**ResultPattern**匹配，结果为真时满足条件。**Reverse**代码块是可选的，它的功能是当之前被**SourcePattern**匹配的fact被删除或修改时改善执行性能。

&lt;o:p&gt;&lt;/o:p&gt;

好了，没有什么比一个例子更能说明情况了

规则：对包括至少价值100元以上的玩具的订单给以10%的折扣&lt;o:p&gt;&lt;/o:p&gt;

**rule** **"Discount for orders that include US$100,00 of toys"**  
**when**  
$o : Order()  
$toysTotal : Number( doubleValue &gt; 100 )  
  **from accumulate**( OrderItem( order == $o, type == "toy", $value : value ),  
                     **init**( double total = 0; ),  
                   **action**( total += $value; ),  
                   **reverse**( total -= $value; ), // 如果有订单被删除，则从总计中除去金额  
                   **result**( new Double( total ) ) )  
**then**  
 $o.setDiscountPercentage( 10 );  
**end**&lt;o:p&gt;&lt;/o:p&gt;

你可以从上面的例子看到，**accumulate**是非常灵活和强大的。每一个代码块可以是Java或MVEL代码块，可以在这里进行任何操作。

&lt;o:p&gt;&lt;/o:p&gt;

但是，有些人会说：“***好，accumulate是很灵活和强大，但是我不想为上面这样的常见操作不断编写代码。”***因此，这是我们这个星期正在进行的工作。我们非常希望你可以对常见的情况以更简单的方式来使用accumulate。因此Accumulate功能被继续改进。

&lt;o:p&gt;&lt;/o:p&gt;

你现在可以使用预定义的函数来简化accumulate常见情况的使用。例如，上面的规则使用accumulate执行一个值累加。同样的规则可以写成这样：

&lt;o:p&gt;&lt;/o:p&gt;

**rule** **"Discount for orders that include US$100,00 of toys"**  
**when**  
$o : Order()  
$toysTotal : Number( doubleValue &gt; 100 )  
             **from accumulate**( OrderItem( order == $o, type == "toy", $value : value ),  
                              **sum**( $value ) )  
**then**  
$o.setDiscountPercentage( 10 );  
**end**&lt;o:p&gt;&lt;/o:p&gt;

现在更简单了。***如果你希望建立一个可以告诉你为每一个部门提升X%的工资会为你带来多大的花费的规则。如下示例：***&lt;o:p&gt;&lt;/o:p&gt;

**rule** "Total raise"  
**when**  
$dpt : Department( $raise : raise )  
$total : Number()  
         **from accumulate**( Employee( dept == $dpt, $salary : salary ),  
                            **sum**( $salary * $raise ) )  
**then**  
$dpt.setSalaryIncreaseValue( $total );  
**end**&lt;o:p&gt;&lt;/o:p&gt;

这样，你可以将使用任何表达式作为参数传递到accumulate功能。我们为你增加了大多数最常用的功能如：**sum**, **average**, **count**, **min**, **max**等等。&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

但是你会说：***“我喜欢这些函数，但是我希望规则能够使用我自己定义的函数。我可以实现这些函数并提供给他们，这样他们就不用重复的编写同样代码了吗？”***

我们的答案是: ***"******当然可以!"*** ;)

我们使用可以达到的最简单的方式来使**Accumulate函数**具有可插入性，因此你可以很简单的提供新的函数来给你的用户使用。例如，假设你有一个非常复杂的计算，需要获得一个指定股票交易操作的费用。你的用户正在你的专家系统中编写规则，使得它可以建议哪一种操作方式是更有利可图的，并且他们已经有一些规则需要计算这样的股票交易操作的费用。&lt;o:p&gt;&lt;/o:p&gt;

要开发一个新的accumulate函数，你唯一要做的事情是开发一个实现了[AccumulateFunction](http://www.proxyurls.com/b3Jn/amJvc3M/dmlld3Zj/aHR0cDovL3ZpZXd2Yy5qYm9zcy5vcmcvY2dpLWJpbi92aWV3dmMuY2dpL2xhYnMvbGFicy9qYm9zc3J1bGVzL3RydW5rL2Ryb29scy1jb3JlL3NyYy9tYWluL2phdmEvb3JnL2Ryb29scy9iYXNlL2FjY3VtdWxhdG9ycy9BY2N1bXVsYXRlRnVuY3Rpb24uamF2YT92aWV3PW1hcmt1cA/69/0/)接口的java类。这个接口有一个方法与Accumulate的每一个操作（*init,* *action, reverse* and *result*）进行交互。可以用来实现类似计算平均数这样的函数（代码附后）。&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

最后，要在系统里使用你的自定义函数，你可以调用一个API(addAccumulateFunction())或者定义一个属性。这个属性可以在配制文件中或者作为系统属性定义，例如：

&lt;o:p&gt;&lt;/o:p&gt;

**drools.accumulate.function.average = org.drools.base.accumulators.AverageAccumulateFunction**&lt;o:p&gt;&lt;/o:p&gt;

就这么简单，希望你能感到满意。

Happy Drooling!&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

&lt;o:p&gt; &lt;/o:p&gt;

附录：平均数函数实现代码

```
/*
```

```
 * 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
```

```
 * 
```

```
 *      http://www.apache.org/licenses/LICENSE-2.0
```

```
 * 
```

```
 * 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;
```

```
<o:p> </o:p>
```

```
<o:p> </o:p>
```

```
/**
```

```
 * An implementation of an accumulator capable of calculating average values
```

```
 * 
```

```
 * @author etirelli
```

```
 *
```

```
 */
```

```
public class AverageAccumulateFunction implements AccumulateFunction {
```

```
<o:p> </o:p>
```

```
    protected static class AverageData {
```

```
        public int    count = 0;
```

```
        public double total = 0;
```

```
    }
```

```
<o:p> </o:p>
```

```
    /* (non-Javadoc)
```

```
     * @see org.drools.base.accumulators.AccumulateFunction#createContext()
```

```
     */
```

```
    public Object createContext() {
```

```
        return new AverageData();
```

```
    }
```

```
<o:p> </o:p>
```

```
    /* (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;
```

```
    }
```

```
<o:p> </o:p>
```

```
    /* (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();
```

```
    }
```

```
<o:p> </o:p>
```

```
    /* (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();
```

```
    }
```

```
<o:p> </o:p>
```

```
    /* (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 );
```

```
    }
```

```
<o:p> </o:p>
```

```
    /* (non-Javadoc)
```

```
     * @see org.drools.base.accumulators.AccumulateFunction#supportsReverse()
```

```
     */
```

```
    public boolean supportsReverse() {
```

```
        return true;
```

```
    }
```

```
<o:p> </o:p>
```

```
}
```

&lt;o:p&gt; &lt;/o:p&gt;