Source Code Cross Referenced for AgendaEventSupportTest.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » event » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Rule Engine » drolls Rule Engine » org.drools.event 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.