001: /*
002: * Copyright 2005 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:
017: package org.drools.rule;
018:
019: import java.util.Collections;
020: import java.util.Map;
021:
022: import org.drools.RuntimeDroolsException;
023: import org.drools.WorkingMemory;
024: import org.drools.common.InternalFactHandle;
025: import org.drools.spi.Accumulator;
026: import org.drools.spi.Tuple;
027:
028: /**
029: * A class to represent the Accumulate CE
030: */
031: public class Accumulate extends ConditionalElement implements
032: PatternSource {
033:
034: private static final long serialVersionUID = 400L;
035:
036: private Accumulator accumulator;
037: private Pattern sourcePattern;
038: private Declaration[] requiredDeclarations;
039: private Declaration[] innerDeclarations;
040:
041: public Accumulate(final Pattern sourcePattern) {
042:
043: this (sourcePattern, new Declaration[0], new Declaration[0],
044: null);
045: }
046:
047: public Accumulate(final Pattern sourcePattern,
048: final Declaration[] requiredDeclarations,
049: final Declaration[] innerDeclarations) {
050:
051: this (sourcePattern, requiredDeclarations, innerDeclarations,
052: null);
053: }
054:
055: public Accumulate(final Pattern sourcePattern,
056: final Declaration[] requiredDeclarations,
057: final Declaration[] innerDeclarations,
058: final Accumulator accumulator) {
059:
060: this .sourcePattern = sourcePattern;
061: this .requiredDeclarations = requiredDeclarations;
062: this .innerDeclarations = innerDeclarations;
063: this .accumulator = accumulator;
064: }
065:
066: public Accumulator getAccumulator() {
067: return this .accumulator;
068: }
069:
070: public void setAccumulator(final Accumulator accumulator) {
071: this .accumulator = accumulator;
072: }
073:
074: public Object createContext() {
075: return this .accumulator.createContext();
076: }
077:
078: /**
079: * Executes the initialization block of code
080: *
081: * @param leftTuple tuple causing the rule fire
082: * @param declarations previous declarations
083: * @param workingMemory
084: * @throws Exception
085: */
086: public void init(final Object workingMemoryContext,
087: final Object context, final Tuple leftTuple,
088: final WorkingMemory workingMemory) {
089: try {
090: this .accumulator
091: .init(workingMemoryContext, context, leftTuple,
092: this .requiredDeclarations, workingMemory);
093: } catch (final Exception e) {
094: throw new RuntimeDroolsException(e);
095: }
096: }
097:
098: /**
099: * Executes the accumulate (action) code for the given fact handle
100: *
101: * @param leftTuple
102: * @param handle
103: * @param declarations
104: * @param innerDeclarations
105: * @param workingMemory
106: * @throws Exception
107: */
108: public void accumulate(final Object workingMemoryContext,
109: final Object context, final Tuple leftTuple,
110: final InternalFactHandle handle,
111: final WorkingMemory workingMemory) {
112: try {
113: this .accumulator.accumulate(workingMemoryContext, context,
114: leftTuple, handle, this .requiredDeclarations,
115: this .innerDeclarations, workingMemory);
116: } catch (final Exception e) {
117: throw new RuntimeDroolsException(e);
118: }
119: }
120:
121: /**
122: * Executes the reverse (action) code for the given fact handle
123: *
124: * @param leftTuple
125: * @param handle
126: * @param declarations
127: * @param innerDeclarations
128: * @param workingMemory
129: * @throws Exception
130: */
131: public void reverse(final Object workingMemoryContext,
132: final Object context, final Tuple leftTuple,
133: final InternalFactHandle handle,
134: final WorkingMemory workingMemory) {
135: try {
136: this .accumulator.reverse(workingMemoryContext, context,
137: leftTuple, handle, this .requiredDeclarations,
138: this .innerDeclarations, workingMemory);
139: } catch (final Exception e) {
140: throw new RuntimeDroolsException(e);
141: }
142: }
143:
144: /**
145: * Gets the result of the accummulation
146: *
147: * @param leftTuple
148: * @param declarations
149: * @param workingMemory
150: * @return
151: * @throws Exception
152: */
153: public Object getResult(final Object workingMemoryContext,
154: final Object context, final Tuple leftTuple,
155: final WorkingMemory workingMemory) {
156: try {
157: return this .accumulator.getResult(workingMemoryContext,
158: context, leftTuple, this .requiredDeclarations,
159: workingMemory);
160: } catch (final Exception e) {
161: throw new RuntimeDroolsException(e);
162: }
163: }
164:
165: /**
166: * Returns true if this accumulate supports reverse
167: * @return
168: */
169: public boolean supportsReverse() {
170: return this .accumulator.supportsReverse();
171: }
172:
173: public Object clone() {
174: return new Accumulate(this .sourcePattern,
175: this .requiredDeclarations, this .innerDeclarations,
176: this .accumulator);
177: }
178:
179: public Pattern getSourcePattern() {
180: return this .sourcePattern;
181: }
182:
183: public Map getInnerDeclarations() {
184: return this .sourcePattern.getInnerDeclarations();
185: }
186:
187: public Map getOuterDeclarations() {
188: return Collections.EMPTY_MAP;
189: }
190:
191: /**
192: * @inheritDoc
193: */
194: public Declaration resolveDeclaration(final String identifier) {
195: return (Declaration) this .sourcePattern.getInnerDeclarations()
196: .get(identifier);
197: }
198:
199: public Object createWorkingMemoryContext() {
200: return this.accumulator.createWorkingMemoryContext();
201: }
202:
203: }
|