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 java.io.Serializable;
021: import java.util.Map;
022:
023: import org.drools.WorkingMemory;
024: import org.drools.base.mvel.DroolsMVELFactory;
025: import org.drools.common.InternalFactHandle;
026: import org.drools.rule.Declaration;
027: import org.drools.spi.Accumulator;
028: import org.drools.spi.Tuple;
029: import org.mvel.MVEL;
030:
031: /**
032: * An MVEL accumulator function executor implementation
033: *
034: * @author etirelli
035: */
036: public class MVELAccumulatorFunctionExecutor implements Accumulator {
037:
038: private static final long serialVersionUID = 400L;
039:
040: private final Object dummy = new Object();
041: private final DroolsMVELFactory model;
042: private final Serializable expression;
043: private final AccumulateFunction function;
044:
045: public MVELAccumulatorFunctionExecutor(
046: final DroolsMVELFactory factory,
047: final Serializable expression,
048: final AccumulateFunction function) {
049: super ();
050: this .model = factory;
051: this .expression = expression;
052: this .function = function;
053: }
054:
055: /* (non-Javadoc)
056: * @see org.drools.spi.Accumulator#createContext()
057: */
058: public Object createContext() {
059: return this .function.createContext();
060: }
061:
062: /* (non-Javadoc)
063: * @see org.drools.spi.Accumulator#init(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
064: */
065: public void init(Object workingMemoryContext, Object context,
066: Tuple leftTuple, Declaration[] declarations,
067: WorkingMemory workingMemory) throws Exception {
068: this .function.init(context);
069: }
070:
071: /* (non-Javadoc)
072: * @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)
073: */
074: public void accumulate(Object workingMemoryContext, Object context,
075: Tuple leftTuple, InternalFactHandle handle,
076: Declaration[] declarations,
077: Declaration[] innerDeclarations, WorkingMemory workingMemory)
078: throws Exception {
079: DroolsMVELFactory factory = (DroolsMVELFactory) workingMemoryContext;
080: factory.setContext(leftTuple, null, handle.getObject(),
081: workingMemory, null);
082: final Object value = MVEL.executeExpression(this .expression,
083: this .dummy, factory);
084: this .function.accumulate(context, value);
085: }
086:
087: public void reverse(Object workingMemoryContext, Object context,
088: Tuple leftTuple, InternalFactHandle handle,
089: Declaration[] declarations,
090: Declaration[] innerDeclarations, WorkingMemory workingMemory)
091: throws Exception {
092: DroolsMVELFactory factory = (DroolsMVELFactory) workingMemoryContext;
093: factory.setContext(leftTuple, null, handle.getObject(),
094: workingMemory, null);
095: final Object value = MVEL.executeExpression(this .expression,
096: this .dummy, factory);
097: this .function.reverse(context, value);
098: }
099:
100: /* (non-Javadoc)
101: * @see org.drools.spi.Accumulator#getResult(java.lang.Object, org.drools.spi.Tuple, org.drools.rule.Declaration[], org.drools.WorkingMemory)
102: */
103: public Object getResult(Object workingMemoryContext,
104: Object context, Tuple leftTuple,
105: Declaration[] declarations, WorkingMemory workingMemory)
106: throws Exception {
107: return this .function.getResult(context);
108: }
109:
110: public boolean supportsReverse() {
111: return this .function.supportsReverse();
112: }
113:
114: public Object createWorkingMemoryContext() {
115: return this.model.clone();
116: }
117:
118: }
|