01: package org.drools.base.dataproviders;
02:
03: import java.io.Serializable;
04: import java.util.Collection;
05: import java.util.Collections;
06: import java.util.Iterator;
07:
08: import org.drools.WorkingMemory;
09: import org.drools.base.mvel.DroolsMVELFactory;
10: import org.drools.rule.Declaration;
11: import org.drools.spi.DataProvider;
12: import org.drools.spi.PropagationContext;
13: import org.drools.spi.Tuple;
14: import org.mvel.MVEL;
15:
16: public class MVELDataProvider implements DataProvider, Serializable {
17:
18: private final Serializable expression;
19: private final DroolsMVELFactory factory;
20:
21: public MVELDataProvider(final Serializable expression,
22: final DroolsMVELFactory factory) {
23: this .expression = expression;
24: this .factory = factory;
25: }
26:
27: public Declaration[] getRequiredDeclarations() {
28: return new Declaration[] {};
29: //return factory.getRequiredDeclarations();
30: }
31:
32: public Iterator getResults(final Tuple tuple,
33: final WorkingMemory wm, final PropagationContext ctx) {
34: this .factory.setContext(tuple, null, null, wm, null);
35:
36: //this.expression.
37: final Object result = MVEL.executeExpression(this .expression,
38: this .factory);
39: if (result instanceof Collection) {
40: return ((Collection) result).iterator();
41: } else if (result instanceof Iterator) {
42: return (Iterator) result;
43: } else {
44: return Collections.singletonList(result).iterator();
45: }
46: }
47: }
|