001: /*
002: * Copyright 2007 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * Created on Jun 20, 2007
017: */
018: package org.drools.base.accumulators;
019:
020: import org.drools.WorkingMemory;
021: import org.drools.common.InternalFactHandle;
022: import org.drools.rule.Declaration;
023: import org.drools.spi.Accumulator;
024: import org.drools.spi.ReturnValueExpression;
025: import org.drools.spi.Tuple;
026:
027: /**
028: * An MVEL accumulator function executor implementation
029: *
030: * @author etirelli
031: */
032: public class JavaAccumulatorFunctionExecutor implements Accumulator {
033:
034: private static final long serialVersionUID = 400L;
035:
036: private ReturnValueExpression expression;
037: private final AccumulateFunction function;
038:
039: public JavaAccumulatorFunctionExecutor(
040: final AccumulateFunction function) {
041: super ();
042: this .function = function;
043: }
044:
045: /* (non-Javadoc)
046: * @see org.drools.spi.Accumulator#createContext()
047: */
048: public Object createContext() {
049: return this .function.createContext();
050: }
051:
052: /* (non-Javadoc)
053: * @see org.drools.spi.Accumulator#init(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
054: */
055: public void init(Object workingMemoryContext, Object context,
056: Tuple leftTuple, Declaration[] declarations,
057: WorkingMemory workingMemory) throws Exception {
058: this .function.init(context);
059: }
060:
061: /* (non-Javadoc)
062: * @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)
063: */
064: public void accumulate(Object workingMemoryContext, Object context,
065: Tuple leftTuple, InternalFactHandle handle,
066: Declaration[] declarations,
067: Declaration[] innerDeclarations, WorkingMemory workingMemory)
068: throws Exception {
069: final Object value = this .expression.evaluate(
070: handle.getObject(), leftTuple, declarations,
071: innerDeclarations, workingMemory).getValue();
072: this .function.accumulate(context, value);
073: }
074:
075: public void reverse(Object workingMemoryContext, Object context,
076: Tuple leftTuple, InternalFactHandle handle,
077: Declaration[] declarations,
078: Declaration[] innerDeclarations, WorkingMemory workingMemory)
079: throws Exception {
080: final Object value = this .expression.evaluate(
081: handle.getObject(), leftTuple, declarations,
082: innerDeclarations, workingMemory).getValue();
083: this .function.reverse(context, value);
084: }
085:
086: /* (non-Javadoc)
087: * @see org.drools.spi.Accumulator#getResult(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
088: */
089: public Object getResult(Object workingMemoryContext,
090: Object context, Tuple leftTuple,
091: Declaration[] declarations, WorkingMemory workingMemory)
092: throws Exception {
093: return this .function.getResult(context);
094: }
095:
096: public boolean supportsReverse() {
097: return this .function.supportsReverse();
098: }
099:
100: public ReturnValueExpression getExpression() {
101: return expression;
102: }
103:
104: public void setExpression(ReturnValueExpression expression) {
105: this .expression = expression;
106: }
107:
108: public Object createWorkingMemoryContext() {
109: // no working memory context needed
110: return null;
111: }
112: }
|