Source Code Cross Referenced for LeftInputAdapterNodeTest.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » reteoo » 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.reteoo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.lang.reflect.Field;
020:        import java.util.List;
021:
022:        import org.drools.DroolsTestCase;
023:        import org.drools.RuleBaseFactory;
024:        import org.drools.common.BaseNode;
025:        import org.drools.common.DefaultFactHandle;
026:        import org.drools.common.InternalWorkingMemory;
027:        import org.drools.common.PropagationContextImpl;
028:        import org.drools.reteoo.ReteooBuilder.IdGenerator;
029:        import org.drools.spi.PropagationContext;
030:        import org.drools.spi.Tuple;
031:        import org.drools.util.FactHashTable;
032:
033:        public class LeftInputAdapterNodeTest extends DroolsTestCase {
034:
035:            public void testLeftInputAdapterNode() {
036:                final MockObjectSource source = new MockObjectSource(15);
037:                final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
038:                        23, source);
039:                assertEquals(23, liaNode.getId());
040:
041:                assertEquals(0, source.getAttached());
042:                source.attach();
043:                assertEquals(1, source.getAttached());
044:            }
045:
046:            /**
047:             * Tests the attaching of the LeftInputAdapterNode to an ObjectSource
048:             * @throws Exception
049:             */
050:            public void testAttach() throws Exception {
051:                final MockObjectSource source = new MockObjectSource(15);
052:
053:                final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
054:                        1, source);
055:                final Field field = ObjectSource.class.getDeclaredField("sink");
056:                field.setAccessible(true);
057:                ObjectSinkPropagator sink = (ObjectSinkPropagator) field
058:                        .get(source);
059:
060:                assertEquals(1, liaNode.getId());
061:                assertNotNull(sink);
062:
063:                liaNode.attach();
064:
065:                sink = (ObjectSinkPropagator) field.get(source);
066:
067:                assertEquals(1, sink.getSinks().length);
068:
069:                assertSame(liaNode, sink.getSinks()[0]);
070:            }
071:
072:            /**
073:             * Tests the assertion of objects into LeftInputAdapterNode
074:             * 
075:             * @throws Exception
076:             */
077:            public void testAssertObjectWithoutMemory() throws Exception {
078:                final PropagationContext context = new PropagationContextImpl(
079:                        0, PropagationContext.ASSERTION, null, null);
080:
081:                final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory(
082:                        1, (ReteooRuleBase) RuleBaseFactory.newRuleBase());
083:
084:                final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
085:                        1, new MockObjectSource(15));
086:                final MockTupleSink sink = new MockTupleSink();
087:                liaNode.addTupleSink(sink);
088:
089:                final Object string1 = "cheese";
090:
091:                // assert object
092:                final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory
093:                        .insert(string1);
094:                liaNode.assertObject(f0, context, workingMemory);
095:
096:                final List asserted = sink.getAsserted();
097:                assertLength(1, asserted);
098:                final Tuple tuple0 = (Tuple) ((Object[]) asserted.get(0))[0];
099:                assertSame(string1, workingMemory.getObject(tuple0.get(0)));
100:
101:            }
102:
103:            /**
104:             * Tests the assertion of objects into LeftInputAdapterNode
105:             * 
106:             * @throws Exception
107:             */
108:            public void testAssertObjectWithMemory() throws Exception {
109:                final PropagationContext context = new PropagationContextImpl(
110:                        0, PropagationContext.ASSERTION, null, null);
111:
112:                ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
113:                        .newRuleBase();
114:                IdGenerator idGenerator = ruleBase.getReteooBuilder()
115:                        .getIdGenerator();
116:                final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase
117:                        .newStatefulSession();
118:
119:                final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
120:                        idGenerator.getNextId(), new MockObjectSource(
121:                                idGenerator.getNextId()));
122:
123:                final MockTupleSink sink = new MockTupleSink();
124:                liaNode.addTupleSink(sink);
125:
126:                //force liaNode to have memory
127:                final Field field = BaseNode.class
128:                        .getDeclaredField("hasMemory");
129:                field.setAccessible(true);
130:                field.set(liaNode, new Boolean(true));
131:
132:                final Object string1 = "cheese";
133:
134:                // assert object
135:                final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory
136:                        .insert(string1);
137:                liaNode.assertObject(f0, context, workingMemory);
138:
139:                final List asserted = sink.getAsserted();
140:                assertLength(1, asserted);
141:                final Tuple tuple0 = (Tuple) ((Object[]) asserted.get(0))[0];
142:                assertSame(string1, workingMemory.getObject(tuple0.get(0)));
143:
144:                // check node memory
145:                final FactHashTable table = (FactHashTable) workingMemory
146:                        .getNodeMemory(liaNode);
147:                assertEquals(1, table.size());
148:                assertTrue(table.contains(f0));
149:
150:                // check memory works with multiple handles
151:                final DefaultFactHandle f1 = (DefaultFactHandle) workingMemory
152:                        .insert("test1");
153:                liaNode.assertObject(f1, context, workingMemory);
154:
155:                assertLength(2, asserted);
156:                final Tuple tuple1 = (Tuple) ((Object[]) asserted.get(1))[0];
157:
158:                assertEquals(2, table.size());
159:                assertTrue(table.contains(f1));
160:
161:                assertNotSame(tuple0, tuple1);
162:
163:            }
164:
165:            /**
166:             * Tests the retractions from a LeftInputAdapterNode.
167:             * Object Assertions result in tuple propagations, so we 
168:             * test the remove(...) method
169:             * @throws Exception
170:             */
171:            public void testRetractObjectWithoutMemory() throws Exception {
172:                final PropagationContext context = new PropagationContextImpl(
173:                        0, PropagationContext.ASSERTION, null, null);
174:
175:                final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory(
176:                        1, (ReteooRuleBase) RuleBaseFactory.newRuleBase());
177:
178:                final MockObjectSource source = new MockObjectSource(15);
179:
180:                final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
181:                        1, source);
182:                final MockTupleSink sink = new MockTupleSink();
183:                liaNode.addTupleSink(sink);
184:
185:                final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory
186:                        .insert("f1");
187:
188:                // assert object 
189:                liaNode.assertObject(f0, context, workingMemory);
190:
191:                final Tuple tuple = (Tuple) ((Object[]) sink.getAsserted().get(
192:                        0))[0];
193:
194:                liaNode.retractObject(f0, context, workingMemory);
195:
196:                assertEquals(tuple, ((Object[]) sink.getRetracted().get(0))[0]);
197:                assertNotSame(tuple, ((Object[]) sink.getRetracted().get(0))[0]);
198:
199:            }
200:
201:            public void testRetractObjectWithMemory() throws Exception {
202:                final PropagationContext context = new PropagationContextImpl(
203:                        0, PropagationContext.ASSERTION, null, null);
204:
205:                ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
206:                        .newRuleBase();
207:                IdGenerator idGenerator = ruleBase.getReteooBuilder()
208:                        .getIdGenerator();
209:                final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase
210:                        .newStatefulSession();
211:
212:                final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
213:                        idGenerator.getNextId(), new MockObjectSource(
214:                                idGenerator.getNextId()));
215:                //force liaNode to have memory
216:                final Field field = BaseNode.class
217:                        .getDeclaredField("hasMemory");
218:                field.setAccessible(true);
219:                field.set(liaNode, new Boolean(true));
220:
221:                final MockTupleSink sink = new MockTupleSink();
222:                liaNode.addTupleSink(sink);
223:
224:                final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory
225:                        .insert("f1");
226:
227:                // assert object
228:                liaNode.assertObject(f0, context, workingMemory);
229:
230:                final Tuple tuple = (Tuple) ((Object[]) sink.getAsserted().get(
231:                        0))[0];
232:
233:                final FactHashTable map = (FactHashTable) workingMemory
234:                        .getNodeMemory(liaNode);
235:                assertTrue(map.contains(f0));
236:
237:                liaNode.retractObject(f0, context, workingMemory);
238:
239:                assertFalse(map.contains(f0));
240:
241:                assertNotSame(tuple, ((Object[]) sink.getRetracted().get(0))[0]);
242:
243:            }
244:
245:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.