[jboss-svn-commits] JBL Code SVN: r12765 - in labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base: accumulators and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 21 21:19:46 EDT 2007


Author: tirelli
Date: 2007-06-21 21:19:45 -0400 (Thu, 21 Jun 2007)
New Revision: 12765

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AccumulateFunction.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AverageAccumulator.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/JavaAccumulatorFunctionExecutor.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MVELAccumulatorFunctionExecutor.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MaxAccumulator.java
Log:
JBRULES-941: adding support to pluggable accumulate functions

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AccumulateFunction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AccumulateFunction.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AccumulateFunction.java	2007-06-22 01:19:45 UTC (rev 12765)
@@ -0,0 +1,74 @@
+/*
+ * 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 20, 2007
+ */
+package org.drools.base.accumulators;
+
+/**
+ * An interface for accumulate external function implementations
+ * 
+ * @author etirelli
+ *
+ */
+public interface AccumulateFunction {
+
+    /**
+     * Creates and returns a new context object
+     * @return
+     */
+    public Object createContext();
+
+    /**
+     * Initializes the accumulator
+     * @param context
+     * @throws Exception
+     */
+    public void init(Object context) throws Exception;
+
+    /**
+     * Executes the accumulation action
+     * @param context
+     * @param value
+     */
+    public void accumulate(Object context,
+                           Object value);
+
+    /**
+     * Reverses the accumulation action
+     * @param context
+     * @param value
+     * @throws Exception
+     */
+    public void reverse(Object context,
+                        Object value) throws Exception;
+
+    /**
+     * Returns the current value in this accumulation session
+     * 
+     * @param context
+     * @return
+     * @throws Exception
+     */
+    public Object getResult(Object context) throws Exception;
+
+    /**
+     * True if the function supports reverse. False otherwise.
+     * 
+     * @return
+     */
+    public boolean supportsReverse();
+
+}
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AverageAccumulator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AverageAccumulator.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/AverageAccumulator.java	2007-06-22 01:19:45 UTC (rev 12765)
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+
+/**
+ * An implementation of an accumulator capable of calculating average values
+ * 
+ * @author etirelli
+ *
+ */
+public class AverageAccumulator 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;
+    }
+
+}

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/JavaAccumulatorFunctionExecutor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/JavaAccumulatorFunctionExecutor.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/JavaAccumulatorFunctionExecutor.java	2007-06-22 01:19:45 UTC (rev 12765)
@@ -0,0 +1,118 @@
+/*
+ * 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 20, 2007
+ */
+package org.drools.base.accumulators;
+
+import org.drools.WorkingMemory;
+import org.drools.common.InternalFactHandle;
+import org.drools.rule.Declaration;
+import org.drools.spi.Accumulator;
+import org.drools.spi.ReturnValueExpression;
+import org.drools.spi.Tuple;
+
+/**
+ * An MVEL accumulator function executor implementation
+ * 
+ * @author etirelli
+ */
+public class JavaAccumulatorFunctionExecutor
+    implements
+    Accumulator {
+
+    private static final long           serialVersionUID = 1081306132058101176L;
+
+    private ReturnValueExpression expression;
+    private final AccumulateFunction   function;
+
+    public JavaAccumulatorFunctionExecutor(final AccumulateFunction function) {
+        super();
+        this.function = function;
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#createContext()
+     */
+    public Object createContext() {
+        return this.function.createContext();
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#init(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
+     */
+    public void init(Object context,
+                     Tuple leftTuple,
+                     Declaration[] declarations,
+                     WorkingMemory workingMemory) throws Exception {
+        this.function.init( context );
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#accumulate(java.lang.Object, org.drools.spi.Tuple, org.drools.common.InternalFactHandle, org.drools.rule.Declaration[], org.drools.rule.Declaration[], org.drools.WorkingMemory)
+     */
+    public void accumulate(Object context,
+                           Tuple leftTuple,
+                           InternalFactHandle handle,
+                           Declaration[] declarations,
+                           Declaration[] innerDeclarations,
+                           WorkingMemory workingMemory) throws Exception {
+        final Object value = this.expression.evaluate( handle.getObject(),
+                                                       leftTuple,
+                                                       declarations,
+                                                       innerDeclarations,
+                                                       workingMemory ).getValue();
+        this.function.accumulate( context,
+                                  value );
+    }
+
+    public void reverse(Object context,
+                        Tuple leftTuple,
+                        InternalFactHandle handle,
+                        Declaration[] declarations,
+                        Declaration[] innerDeclarations,
+                        WorkingMemory workingMemory) throws Exception {
+        final Object value = this.expression.evaluate( handle.getObject(),
+                                                       leftTuple,
+                                                       declarations,
+                                                       innerDeclarations,
+                                                       workingMemory ).getValue();
+        this.function.reverse( context,
+                               value );
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#getResult(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
+     */
+    public Object getResult(Object context,
+                            Tuple leftTuple,
+                            Declaration[] declarations,
+                            WorkingMemory workingMemory) throws Exception {
+        return this.function.getResult( context );
+    }
+
+    public boolean supportsReverse() {
+        return this.function.supportsReverse();
+    }
+
+    public ReturnValueExpression getExpression() {
+        return expression;
+    }
+
+    public void setExpression(ReturnValueExpression expression) {
+        this.expression = expression;
+    }
+
+}

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MVELAccumulatorFunctionExecutor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MVELAccumulatorFunctionExecutor.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MVELAccumulatorFunctionExecutor.java	2007-06-22 01:19:45 UTC (rev 12765)
@@ -0,0 +1,122 @@
+/*
+ * 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 20, 2007
+ */
+package org.drools.base.accumulators;
+
+import java.io.Serializable;
+
+import org.drools.WorkingMemory;
+import org.drools.base.mvel.DroolsMVELFactory;
+import org.drools.common.InternalFactHandle;
+import org.drools.rule.Declaration;
+import org.drools.spi.Accumulator;
+import org.drools.spi.Tuple;
+import org.mvel.MVEL;
+
+/**
+ * An MVEL accumulator function executor implementation
+ * 
+ * @author etirelli
+ */
+public class MVELAccumulatorFunctionExecutor
+    implements
+    Accumulator {
+
+    private static final long         serialVersionUID = 1081306132058101176L;
+
+    private final Object              dummy            = new Object();
+    private final DroolsMVELFactory   factory;
+    private final Serializable        expression;
+    private final AccumulateFunction function;
+
+    public MVELAccumulatorFunctionExecutor(final DroolsMVELFactory factory,
+                                           final Serializable expression,
+                                           final AccumulateFunction function) {
+        super();
+        this.factory = factory;
+        this.expression = expression;
+        this.function = function;
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#createContext()
+     */
+    public Object createContext() {
+        return this.function.createContext();
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#init(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
+     */
+    public void init(Object context,
+                     Tuple leftTuple,
+                     Declaration[] declarations,
+                     WorkingMemory workingMemory) throws Exception {
+        this.function.init( context );
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#accumulate(java.lang.Object, org.drools.spi.Tuple, org.drools.common.InternalFactHandle, org.drools.rule.Declaration[], org.drools.rule.Declaration[], org.drools.WorkingMemory)
+     */
+    public void accumulate(Object context,
+                           Tuple leftTuple,
+                           InternalFactHandle handle,
+                           Declaration[] declarations,
+                           Declaration[] innerDeclarations,
+                           WorkingMemory workingMemory) throws Exception {
+        this.factory.setContext( leftTuple,
+                                 null,
+                                 handle.getObject(),
+                                 workingMemory );
+        final Object value = MVEL.executeExpression( this.expression,
+                                                     this.dummy,
+                                                     this.factory );
+        this.function.accumulate( context,
+                                  value );
+    }
+
+    public void reverse(Object context,
+                        Tuple leftTuple,
+                        InternalFactHandle handle,
+                        Declaration[] declarations,
+                        Declaration[] innerDeclarations,
+                        WorkingMemory workingMemory) throws Exception {
+        this.factory.setContext( leftTuple,
+                                 null,
+                                 handle.getObject(),
+                                 workingMemory );
+        final Object value = MVEL.executeExpression( this.expression,
+                                                     this.dummy,
+                                                     this.factory );
+        this.function.reverse( context, value );
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.Accumulator#getResult(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
+     */
+    public Object getResult(Object context,
+                            Tuple leftTuple,
+                            Declaration[] declarations,
+                            WorkingMemory workingMemory) throws Exception {
+        return this.function.getResult( context );
+    }
+
+    public boolean supportsReverse() {
+        return this.function.supportsReverse();
+    }
+
+}

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MaxAccumulator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MaxAccumulator.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/accumulators/MaxAccumulator.java	2007-06-22 01:19:45 UTC (rev 12765)
@@ -0,0 +1,79 @@
+/*
+ * 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;
+
+
+/**
+ * An implementation of an accumulator capable of calculating average values
+ * 
+ * @author etirelli
+ *
+ */
+public class MaxAccumulator implements AccumulateFunction {
+
+    protected static class MaxData {
+        public double max = Double.MIN_VALUE;
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.base.accumulators.AccumulateFunction#createContext()
+     */
+    public Object createContext() {
+        return new MaxData();
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.base.accumulators.AccumulateFunction#init(java.lang.Object)
+     */
+    public void init(Object context) throws Exception {
+        MaxData data = (MaxData) context;
+        data.max = 0;
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.base.accumulators.AccumulateFunction#accumulate(java.lang.Object, java.lang.Object)
+     */
+    public void accumulate(Object context,
+                           Object value) {
+        MaxData data = (MaxData) context;
+        data.max = Math.max( data.max, ((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 {
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.base.accumulators.AccumulateFunction#getResult(java.lang.Object)
+     */
+    public Object getResult(Object context) throws Exception {
+        MaxData data = (MaxData) context;
+        return new Double( data.max );
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.base.accumulators.AccumulateFunction#supportsReverse()
+     */
+    public boolean supportsReverse() {
+        return false;
+    }
+
+}




More information about the jboss-svn-commits mailing list