001: package org.drools.reteoo;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.drools.ObjectFilter;
010: import org.drools.StatelessSession;
011: import org.drools.StatelessSessionResult;
012: import org.drools.WorkingMemory;
013: import org.drools.base.MapGlobalResolver;
014: import org.drools.common.InternalRuleBase;
015: import org.drools.common.InternalWorkingMemory;
016: import org.drools.concurrent.AssertObject;
017: import org.drools.concurrent.AssertObjects;
018: import org.drools.concurrent.CommandExecutor;
019: import org.drools.concurrent.ExecutorService;
020: import org.drools.concurrent.FireAllRules;
021: import org.drools.concurrent.Future;
022: import org.drools.event.AgendaEventListener;
023: import org.drools.event.AgendaEventSupport;
024: import org.drools.event.RuleFlowEventListener;
025: import org.drools.event.RuleFlowEventSupport;
026: import org.drools.event.WorkingMemoryEventListener;
027: import org.drools.event.WorkingMemoryEventSupport;
028: import org.drools.reteoo.ReteooRuleBase.InitialFactHandleDummyObject;
029: import org.drools.reteoo.ReteooWorkingMemory.WorkingMemoryReteAssertAction;
030: import org.drools.spi.AgendaFilter;
031: import org.drools.spi.GlobalResolver;
032:
033: public class ReteooStatelessSession implements StatelessSession {
034: //private WorkingMemory workingMemory;
035:
036: private InternalRuleBase ruleBase;
037: private AgendaFilter agendaFilter;
038: private GlobalResolver globalResolver = new MapGlobalResolver();
039:
040: /** The eventSupport */
041: protected WorkingMemoryEventSupport workingMemoryEventSupport = new WorkingMemoryEventSupport();
042:
043: protected AgendaEventSupport agendaEventSupport = new AgendaEventSupport();
044:
045: protected RuleFlowEventSupport ruleFlowEventSupport = new RuleFlowEventSupport();
046:
047: public ReteooStatelessSession(final InternalRuleBase ruleBase) {
048: this .ruleBase = ruleBase;
049: }
050:
051: public InternalWorkingMemory newWorkingMemory() {
052: synchronized (this .ruleBase.getPackagesMap()) {
053: InternalWorkingMemory wm = new ReteooWorkingMemory(
054: this .ruleBase.nextWorkingMemoryCounter(),
055: this .ruleBase);
056:
057: wm.setGlobalResolver(this .globalResolver);
058: wm
059: .setWorkingMemoryEventSupport(this .workingMemoryEventSupport);
060: wm.setAgendaEventSupport(this .agendaEventSupport);
061: wm.setRuleFlowEventSupport(ruleFlowEventSupport);
062:
063: final InitialFactHandle handle = new InitialFactHandle(wm
064: .getFactHandleFactory().newFactHandle(
065: new InitialFactHandleDummyObject()));
066:
067: wm
068: .queueWorkingMemoryAction(new WorkingMemoryReteAssertAction(
069: handle, false, true, null, null));
070: return wm;
071: }
072: }
073:
074: public void addEventListener(
075: final WorkingMemoryEventListener listener) {
076: this .workingMemoryEventSupport.addEventListener(listener);
077: }
078:
079: public void removeEventListener(
080: final WorkingMemoryEventListener listener) {
081: this .workingMemoryEventSupport.removeEventListener(listener);
082: }
083:
084: public List getWorkingMemoryEventListeners() {
085: return this .workingMemoryEventSupport.getEventListeners();
086: }
087:
088: public void addEventListener(final AgendaEventListener listener) {
089: this .agendaEventSupport.addEventListener(listener);
090: }
091:
092: public void removeEventListener(final AgendaEventListener listener) {
093: this .agendaEventSupport.removeEventListener(listener);
094: }
095:
096: public List getAgendaEventListeners() {
097: return this .agendaEventSupport.getEventListeners();
098: }
099:
100: public void addEventListener(final RuleFlowEventListener listener) {
101: this .ruleFlowEventSupport.addEventListener(listener);
102: }
103:
104: public void removeEventListener(final RuleFlowEventListener listener) {
105: this .ruleFlowEventSupport.removeEventListener(listener);
106: }
107:
108: public List getRuleFlowEventListeners() {
109: return this .ruleFlowEventSupport.getEventListeners();
110: }
111:
112: public void setAgendaFilter(AgendaFilter agendaFilter) {
113: this .agendaFilter = agendaFilter;
114: }
115:
116: public void setGlobal(String identifier, Object value) {
117: this .globalResolver.setGlobal(identifier, value);
118: }
119:
120: public void setGlobalResolver(GlobalResolver globalResolver) {
121: this .globalResolver = globalResolver;
122: }
123:
124: public void execute(Object object) {
125: InternalWorkingMemory wm = newWorkingMemory();
126:
127: wm.insert(object);
128: wm.fireAllRules(this .agendaFilter);
129: }
130:
131: public void execute(Object[] array) {
132: InternalWorkingMemory wm = newWorkingMemory();
133:
134: for (int i = 0, length = array.length; i < length; i++) {
135: wm.insert(array[i]);
136: }
137: wm.fireAllRules(this .agendaFilter);
138: }
139:
140: public void execute(Collection collection) {
141: InternalWorkingMemory wm = newWorkingMemory();
142:
143: for (Iterator it = collection.iterator(); it.hasNext();) {
144: wm.insert(it.next());
145: }
146: wm.fireAllRules(this .agendaFilter);
147: }
148:
149: public void asyncExecute(final Object object) {
150: InternalWorkingMemory wm = newWorkingMemory();
151:
152: final AssertObject assertObject = new AssertObject(object);
153: ExecutorService executor = this .ruleBase.getConfiguration()
154: .getExecutorService();
155: executor.setCommandExecutor(new CommandExecutor(wm));
156: executor.submit(assertObject);
157: executor.submit(new FireAllRules(this .agendaFilter));
158: }
159:
160: public void asyncExecute(final Object[] array) {
161: InternalWorkingMemory wm = newWorkingMemory();
162:
163: final AssertObjects assertObjects = new AssertObjects(array);
164: ExecutorService executor = this .ruleBase.getConfiguration()
165: .getExecutorService();
166: executor.setCommandExecutor(new CommandExecutor(wm));
167: executor.submit(assertObjects);
168: executor.submit(new FireAllRules(this .agendaFilter));
169: }
170:
171: public void asyncExecute(final Collection collection) {
172: InternalWorkingMemory wm = newWorkingMemory();
173:
174: final AssertObjects assertObjects = new AssertObjects(
175: collection);
176: ExecutorService executor = this .ruleBase.getConfiguration()
177: .getExecutorService();
178: executor.setCommandExecutor(new CommandExecutor(wm));
179: executor.submit(assertObjects);
180: executor.submit(new FireAllRules(this .agendaFilter));
181: }
182:
183: public StatelessSessionResult executeWithResults(Object object) {
184: InternalWorkingMemory wm = newWorkingMemory();
185:
186: wm.insert(object);
187: wm.fireAllRules(this .agendaFilter);
188: return new ReteStatelessSessionResult(wm);
189: }
190:
191: public StatelessSessionResult executeWithResults(Object[] array) {
192: InternalWorkingMemory wm = newWorkingMemory();
193:
194: for (int i = 0, length = array.length; i < length; i++) {
195: wm.insert(array[i]);
196: }
197: wm.fireAllRules(this .agendaFilter);
198: return new ReteStatelessSessionResult(wm);
199: }
200:
201: public StatelessSessionResult executeWithResults(
202: Collection collection) {
203: InternalWorkingMemory wm = newWorkingMemory();
204:
205: for (Iterator it = collection.iterator(); it.hasNext();) {
206: wm.insert(it.next());
207: }
208: wm.fireAllRules(this .agendaFilter);
209: return new ReteStatelessSessionResult(wm);
210: }
211: }
|