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