001: package org.drools.reteoo;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.drools.Agenda;
023: import org.drools.DroolsTestCase;
024: import org.drools.RuleBase;
025: import org.drools.RuleBaseFactory;
026: import org.drools.WorkingMemory;
027: import org.drools.common.DefaultFactHandle;
028: import org.drools.common.InternalWorkingMemory;
029: import org.drools.common.PropagationContextImpl;
030: import org.drools.reteoo.ReteooBuilder.IdGenerator;
031: import org.drools.rule.Rule;
032: import org.drools.spi.Duration;
033: import org.drools.spi.KnowledgeHelper;
034: import org.drools.spi.PropagationContext;
035: import org.drools.spi.Tuple;
036:
037: /**
038: * @author mproctor
039: */
040:
041: public class SchedulerTest extends DroolsTestCase {
042: public void testScheduledActivation() throws Exception {
043: ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
044: .newRuleBase();
045: IdGenerator idGenerator = ruleBase.getReteooBuilder()
046: .getIdGenerator();
047: InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase
048: .newStatefulSession();
049:
050: final Rule rule = new Rule("test-rule");
051: final RuleTerminalNode node = new RuleTerminalNode(idGenerator
052: .getNextId(), new MockTupleSource(idGenerator
053: .getNextId()), rule, rule.getLhs());
054: final List data = new ArrayList();
055:
056: // add consequence
057: rule.setConsequence(new org.drools.spi.Consequence() {
058: /**
059: *
060: */
061: private static final long serialVersionUID = 400L;
062:
063: public void evaluate(final KnowledgeHelper knowledgeHelper,
064: final WorkingMemory workingMemory) {
065: data.add("tested");
066: }
067: });
068:
069: /* 1/10th of a second */
070: final Duration duration = new Duration() {
071: /**
072: *
073: */
074: private static final long serialVersionUID = 400L;
075:
076: public long getDuration(Tuple tuple) {
077: return 100;
078: }
079:
080: };
081: rule.setDuration(duration);
082:
083: final PropagationContext context = new PropagationContextImpl(
084: 0, PropagationContext.ASSERTION, null, null);
085:
086: final ReteTuple tuple = new ReteTuple(new DefaultFactHandle(1,
087: "cheese"));
088:
089: assertEquals(0, data.size());
090:
091: node.assertTuple(tuple, context, workingMemory);
092:
093: // sleep for 300ms
094: Thread.sleep(300);
095:
096: // now check for update
097: assertEquals(1, data.size());
098: }
099:
100: public void testDoLoopScheduledActivation() throws Exception {
101: ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
102: .newRuleBase();
103: IdGenerator idGenerator = ruleBase.getReteooBuilder()
104: .getIdGenerator();
105:
106: final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase
107: .newStatefulSession();
108: final Agenda agenda = workingMemory.getAgenda();
109:
110: final Rule rule = new Rule("test-rule");
111: final RuleTerminalNode node = new RuleTerminalNode(idGenerator
112: .getNextId(), new MockTupleSource(idGenerator
113: .getNextId()), rule, rule.getLhs());
114: final List data = new ArrayList();
115:
116: /* 1/10th of a second */
117: final Duration duration = new Duration() {
118: /**
119: *
120: */
121: private static final long serialVersionUID = 400L;
122:
123: public long getDuration(Tuple tuple) {
124: return 100;
125: }
126:
127: };
128:
129: rule.setDuration(duration);
130:
131: // add consequence
132: rule.setConsequence(new org.drools.spi.Consequence() {
133: /**
134: *
135: */
136: private static final long serialVersionUID = 400L;
137:
138: public void evaluate(final KnowledgeHelper knowledgeHelper,
139: final WorkingMemory workingMemory) {
140: /* on first invoke add another one to the agenda */
141: if (data.size() < 3) {
142: final PropagationContext context2 = new PropagationContextImpl(
143: 0, 0, rule, knowledgeHelper.getActivation());
144: final ReteTuple tuple2 = new ReteTuple(
145: new DefaultFactHandle(2, "cheese"));
146: node.assertTuple(tuple2, context2,
147: (ReteooWorkingMemory) workingMemory);
148: }
149: data.add("tested");
150: }
151: });
152:
153: final PropagationContext context1 = new PropagationContextImpl(
154: 0, PropagationContext.ASSERTION, null, null);
155:
156: final ReteTuple tuple1 = new ReteTuple(new DefaultFactHandle(1,
157: "cheese"));
158:
159: node.assertTuple(tuple1, context1, workingMemory);
160:
161: assertEquals(0, data.size());
162:
163: // sleep for 0.5 seconds
164: Thread.sleep(500);
165:
166: // now check for update
167: assertEquals(4, data.size());
168:
169: }
170:
171: public void testNoLoopScheduledActivation() throws Exception {
172: ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
173: .newRuleBase();
174: IdGenerator idGenerator = ruleBase.getReteooBuilder()
175: .getIdGenerator();
176:
177: final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase
178: .newStatefulSession();
179: final Agenda agenda = workingMemory.getAgenda();
180:
181: final Rule rule = new Rule("test-rule");
182: final List data = new ArrayList();
183:
184: final RuleTerminalNode node = new RuleTerminalNode(idGenerator
185: .getNextId(), new MockTupleSource(idGenerator
186: .getNextId()), rule, rule.getLhs());
187:
188: /* 1/10th of a second */
189: final Duration duration = new Duration() {
190: /**
191: *
192: */
193: private static final long serialVersionUID = 400L;
194:
195: public long getDuration(Tuple tuple) {
196: return 100;
197: }
198:
199: };
200:
201: rule.setDuration(duration);
202: rule.setNoLoop(true);
203:
204: // add consequence
205: rule.setConsequence(new org.drools.spi.Consequence() {
206: /**
207: *
208: */
209: private static final long serialVersionUID = 400L;
210:
211: public void evaluate(final KnowledgeHelper knowledgeHelper,
212: final WorkingMemory workingMemory) {
213: /* on first invoke add another one to the agenda */
214: if (data.size() < 5) {
215: final PropagationContext context2 = new PropagationContextImpl(
216: 0, 0, rule, knowledgeHelper.getActivation());
217: final ReteTuple tuple2 = new ReteTuple(
218: new DefaultFactHandle(2, "cheese"));
219: node.assertTuple(tuple2, context2,
220: (ReteooWorkingMemory) workingMemory);
221: }
222: data.add("tested");
223: }
224: });
225:
226: final PropagationContext context1 = new PropagationContextImpl(
227: 0, PropagationContext.ASSERTION, null, null);
228:
229: final ReteTuple tuple1 = new ReteTuple(new DefaultFactHandle(1,
230: "cheese"));
231: node.assertTuple(tuple1, context1, workingMemory);
232: assertEquals(0, data.size());
233:
234: // sleep for 0.5 seconds
235: Thread.sleep(500);
236:
237: // now check for update
238: assertEquals(1, data.size());
239:
240: }
241: //
242: // public void testExceptionHandler() throws Exception {
243: // RuleBase ruleBase = new RuleBaseImpl();
244: //
245: // WorkingMemoryImpl workingMemory = (WorkingMemoryImpl) ruleBase.newWorkingMemory();
246: // Agenda agenda = workingMemory.getAgenda();
247: // final Scheduler scheduler = Scheduler.getInstance();
248: //
249: // final Rule rule = new Rule( "test-rule" );
250: //
251: // // add consequence
252: // rule.setConsequence( new org.drools.spi.Consequence() {
253: // public void invoke(Activation activation,
254: // WorkingMemory workingMemory) throws ConsequenceException {
255: // throw new ConsequenceException( "not enough cheese",
256: // rule );
257: // }
258: // } );
259: //
260: // /* 1/10th of a second */
261: // Duration duration = new Duration() {
262: // public long getDuration(Tuple tuple) {
263: // return 100;
264: // }
265: //
266: // };
267: // rule.setDuration( duration );
268: //
269: // final List data = new ArrayList();
270: //
271: // PropagationContext context = new PropagationContextImpl( 0,
272: // PropagationContext.ASSERTION,
273: // null,
274: // null );
275: //
276: // ReteTuple tuple = new ReteTuple( 0,
277: // new FactHandleImpl( 1 ),
278: // workingMemory );
279: //
280: // assertEquals( 0,
281: // data.size() );
282: //
283: // AsyncExceptionHandler handler = new AsyncExceptionHandler() {
284: // public void handleException(WorkingMemory workingMemory,
285: // ConsequenceException exception) {
286: // data.add( "tested" );
287: // }
288: // };
289: // workingMemory.setAsyncExceptionHandler( handler );
290: //
291: // assertLength( 0,
292: // data );
293: //
294: // agenda.addToAgenda( tuple,
295: // context,
296: // rule );
297: //
298: // // sleep for 2 seconds
299: // Thread.sleep( 300 );
300: //
301: // // now check for update
302: // assertLength( 1,
303: // data );
304: // }
305: }
|