001: package org.drools.event;
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.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.drools.Cheese;
024: import org.drools.FactHandle;
025: import org.drools.RuleBase;
026: import org.drools.RuleBaseFactory;
027: import org.drools.WorkingMemory;
028: import org.drools.base.ClassFieldExtractor;
029: import org.drools.base.ClassFieldExtractorCache;
030: import org.drools.base.ClassObjectType;
031: import org.drools.base.FieldFactory;
032: import org.drools.base.ShadowProxy;
033: import org.drools.base.ValueType;
034: import org.drools.base.evaluators.Operator;
035: import org.drools.rule.Pattern;
036: import org.drools.rule.LiteralConstraint;
037: import org.drools.rule.Rule;
038: import org.drools.rule.Package;
039: import org.drools.spi.Consequence;
040: import org.drools.spi.Evaluator;
041: import org.drools.spi.FieldValue;
042: import org.drools.spi.KnowledgeHelper;
043:
044: import junit.framework.TestCase;
045:
046: /**
047: * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
048: */
049: public class AgendaEventSupportTest extends TestCase {
050: public void testIsSerializable() {
051: assertTrue(Serializable.class
052: .isAssignableFrom(AgendaEventSupport.class));
053: }
054:
055: public void testAgendaEventListener() throws Exception {
056: final RuleBase rb = RuleBaseFactory.newRuleBase();
057:
058: // create a simpe package with one rule to test the events
059: final Package pkg = new Package("org.drools.test");
060: final Rule rule = new Rule("test1");
061: rule.setAgendaGroup("test group");
062: final ClassObjectType cheeseObjectType = new ClassObjectType(
063: Cheese.class);
064: final Pattern pattern = new Pattern(0, cheeseObjectType);
065:
066: final ClassFieldExtractor extractor = ClassFieldExtractorCache
067: .getExtractor(Cheese.class, "type", getClass()
068: .getClassLoader());
069:
070: final FieldValue field = FieldFactory.getFieldValue("cheddar");
071:
072: final Evaluator evaluator = ValueType.STRING_TYPE
073: .getEvaluator(Operator.EQUAL);
074:
075: final LiteralConstraint constraint = new LiteralConstraint(
076: extractor, evaluator, field);
077: pattern.addConstraint(constraint);
078: rule.addPattern(pattern);
079:
080: rule.setConsequence(new Consequence() {
081: public void evaluate(final KnowledgeHelper knowledgeHelper,
082: final WorkingMemory workingMemory) throws Exception {
083: }
084: });
085: pkg.addRule(rule);
086: rb.addPackage(pkg);
087:
088: // create a new working memory and add an AgendaEventListener
089: final WorkingMemory wm = rb.newStatefulSession();
090: final List agendaList = new ArrayList();
091: final AgendaEventListener agendaEventListener = new AgendaEventListener() {
092:
093: public void activationCancelled(
094: ActivationCancelledEvent event,
095: WorkingMemory workingMemory) {
096: agendaList.add(event);
097:
098: }
099:
100: public void activationCreated(ActivationCreatedEvent event,
101: WorkingMemory workingMemory) {
102: agendaList.add(event);
103: }
104:
105: public void afterActivationFired(
106: AfterActivationFiredEvent event,
107: WorkingMemory workingMemory) {
108: agendaList.add(event);
109: }
110:
111: public void agendaGroupPopped(AgendaGroupPoppedEvent event,
112: WorkingMemory workingMemory) {
113: agendaList.add(event);
114: }
115:
116: public void agendaGroupPushed(AgendaGroupPushedEvent event,
117: WorkingMemory workingMemory) {
118: agendaList.add(event);
119: }
120:
121: public void beforeActivationFired(
122: BeforeActivationFiredEvent event,
123: WorkingMemory workingMemory) {
124: agendaList.add(event);
125: }
126: };
127: wm.addEventListener(agendaEventListener);
128:
129: // assert the cheese fact
130: final Cheese cheddar = new Cheese("cheddar", 15);
131: FactHandle cheddarHandle = wm.insert(cheddar);
132:
133: // should be one ActivationCreatedEvent
134: assertEquals(1, agendaList.size());
135: ActivationCreatedEvent createdEvent = (ActivationCreatedEvent) agendaList
136: .get(0);
137: assertSame(cheddar, unwrapShadow(createdEvent.getActivation()
138: .getTuple().get(0).getObject()));
139: agendaList.clear();
140:
141: // update results in a ActivationCancelledEvent and an ActivationCreatedEvent, note the object is always resolvable
142: cheddar.setPrice(14);
143: wm.update(cheddarHandle, cheddar);
144: assertEquals(2, agendaList.size());
145: ActivationCancelledEvent cancelledEvent = (ActivationCancelledEvent) agendaList
146: .get(0);
147: assertSame(cheddar, unwrapShadow(cancelledEvent.getActivation()
148: .getTuple().get(0).getObject()));
149: createdEvent = (ActivationCreatedEvent) agendaList.get(1);
150: assertSame(cheddar, unwrapShadow(createdEvent.getActivation()
151: .getTuple().get(0).getObject()));
152: agendaList.clear();
153:
154: // retract results in a ActivationCancelledEvent, noe the object is not resolveable now as it no longer exists
155: wm.retract(cheddarHandle);
156: assertEquals(1, agendaList.size());
157: cancelledEvent = (ActivationCancelledEvent) agendaList.get(0);
158: assertNull(cancelledEvent.getActivation().getTuple().get(0)
159: .getObject());
160:
161: // re-assert the fact so we can test the agenda group events
162: cheddarHandle = wm.insert(cheddar);
163: agendaList.clear();
164:
165: // setFocus results in an AgendaGroupPushedEvent
166: wm.setFocus("test group");
167: assertEquals(1, agendaList.size());
168: final AgendaGroupPushedEvent pushedEvent = (AgendaGroupPushedEvent) agendaList
169: .get(0);
170: assertEquals("test group", pushedEvent.getAgendaGroup()
171: .getName());
172: agendaList.clear();
173:
174: // fireAllRules results in a BeforeActivationFiredEvent and an AfterActivationFiredEvent
175: // the AgendaGroup becomes empty, which results in a popped event.
176: wm.fireAllRules();
177: assertEquals(3, agendaList.size());
178: final BeforeActivationFiredEvent beforeEvent = (BeforeActivationFiredEvent) agendaList
179: .get(0);
180: assertSame(cheddar, unwrapShadow(beforeEvent.getActivation()
181: .getTuple().get(0).getObject()));
182: final AfterActivationFiredEvent afterEvent = (AfterActivationFiredEvent) agendaList
183: .get(1);
184: assertSame(cheddar, unwrapShadow(afterEvent.getActivation()
185: .getTuple().get(0).getObject()));
186: final AgendaGroupPoppedEvent poppedEvent = (AgendaGroupPoppedEvent) agendaList
187: .get(2);
188: assertEquals("test group", poppedEvent.getAgendaGroup()
189: .getName());
190: }
191:
192: private Object unwrapShadow(Object object) {
193: if (object instanceof ShadowProxy) {
194: return ((ShadowProxy) object).getShadowedObject();
195: } else {
196: return object;
197: }
198: }
199: }
|