1
2 package org.drools.reteoo;
3
4 import org.drools.reteoo.impl.ReteImpl;
5 import org.drools.rule.Rule;
6 import org.drools.rule.Declaration;
7 import org.drools.rule.Extraction;
8 import org.drools.spi.ObjectType;
9 import org.drools.spi.InstrumentedCondition;
10 import org.drools.spi.InstrumentedExtractor;
11 import org.drools.semantics.java.ClassObjectType;
12
13 import junit.framework.TestCase;
14
15 import java.util.Set;
16 import java.util.HashSet;
17 import java.util.Iterator;
18
19 public class BuilderTest extends TestCase
20 {
21 private ReteImpl rete;
22 private Builder builder;
23
24 private ObjectType stringType;
25 private ObjectType objectType;
26
27 private Declaration stringDecl;
28 private Declaration objectDecl;
29
30 private Rule rule1;
31
32 public BuilderTest(String name)
33 {
34 super( name );
35 }
36
37 public void setUp()
38 {
39 this.rete = new ReteImpl();
40 this.builder = new Builder( this.rete );
41
42 this.stringType = new ClassObjectType( String.class );
43 this.objectType = new ClassObjectType( Object.class );
44
45 this.rule1 = new Rule( "cheese" );
46
47 this.stringDecl = new Declaration( this.stringType,
48 "string" );
49
50 this.objectDecl = new Declaration( this.objectType,
51 "object" );
52
53 this.rule1.addParameterDeclaration( this.stringDecl );
54
55 this.rule1.addParameterDeclaration( this.objectDecl );
56 }
57
58 public void tearDown()
59 {
60 this.rete = null;
61 this.builder = null;
62
63 this.stringType = null;
64 this.objectType = null;
65
66 this.rule1 = null;
67 }
68
69 public void testCreateParameterNodes()
70 {
71 Set nodes = this.builder.createParameterNodes( this.rule1 );
72
73 assertEquals( 2,
74 nodes.size() );
75
76 Set decls = new HashSet();
77
78 Iterator nodeIter = nodes.iterator();
79 ParameterNode eachNode = null;
80
81 while ( nodeIter.hasNext() )
82 {
83 eachNode = (ParameterNode) nodeIter.next();
84
85 decls.add( eachNode.getDeclaration() );
86 }
87
88 assertEquals( 2,
89 decls.size() );
90
91 assertTrue( decls.contains( this.stringDecl ) );
92 assertTrue( decls.contains( this.objectDecl ) );
93 }
94
95 public void testMatches()
96 {
97 Set decls = new HashSet();
98
99 InstrumentedCondition cond = new InstrumentedCondition();
100
101 cond.addDeclaration( this.stringDecl );
102 cond.addDeclaration( this.objectDecl );
103
104 assertTrue( ! this.builder.matches( cond,
105 decls ) );
106
107 decls.add( this.stringDecl );
108
109 assertTrue( ! this.builder.matches( cond,
110 decls ) );
111
112 decls.add( this.objectDecl );
113
114 assertTrue( this.builder.matches( cond,
115 decls ) );
116 }
117
118 public void testFindMatchingTupleSourceForExtraction()
119 {
120 Set sources = new HashSet();
121
122 MockTupleSource source = null;
123
124 InstrumentedExtractor extractor = null;
125
126 Extraction extract = null;
127
128 TupleSource found = null;
129
130 // ----------------------------------------
131 // ----------------------------------------
132
133 source = new MockTupleSource();
134
135 source.addTupleDeclaration( this.objectDecl );
136
137 sources.add( source );
138
139 extractor = new InstrumentedExtractor();
140
141 extractor.addDeclaration( this.stringDecl );
142
143 extract = new Extraction( this.objectDecl,
144 extractor );
145
146 found = this.builder.findMatchingTupleSourceForExtraction( extract,
147 sources );
148
149 assertNull( found );
150
151 // ----------------------------------------
152 // ----------------------------------------
153
154 sources.clear();
155
156 source = new MockTupleSource();
157
158 source.addTupleDeclaration( this.objectDecl );
159
160 source.addTupleDeclaration( this.stringDecl );
161
162 sources.add( source );
163
164 found = this.builder.findMatchingTupleSourceForExtraction( extract,
165 sources );
166
167 assertNull( found );
168
169 // ----------------------------------------
170 // ----------------------------------------
171
172 sources.clear();
173
174 source = new MockTupleSource();
175
176 source.addTupleDeclaration( this.stringDecl );
177
178 sources.add( source );
179
180 found = this.builder.findMatchingTupleSourceForExtraction( extract,
181 sources );
182
183 assertNotNull( found );
184
185 assertSame( source,
186 found );
187 }
188
189 public void testFindMatchingTupleSourceForCondition()
190 {
191 Set sources = new HashSet();
192
193 MockTupleSource source = null;
194
195 // ----------------------------------------
196 // ----------------------------------------
197
198 source = new MockTupleSource();
199
200 source.addTupleDeclaration( this.stringDecl );
201
202 sources.add( source );
203
204 InstrumentedCondition cond = new InstrumentedCondition();
205
206 cond.addDeclaration( this.stringDecl );
207
208 TupleSource found = null;
209
210 found = this.builder.findMatchingTupleSourceForCondition( cond,
211 sources );
212
213 assertNotNull( found );
214
215 assertSame( source,
216 found );
217
218 // ----------------------------------------
219 // ----------------------------------------
220
221 sources.clear();
222
223 source = new MockTupleSource();
224
225 source.addTupleDeclaration( this.objectDecl );
226
227 found = this.builder.findMatchingTupleSourceForCondition( cond,
228 sources );
229
230 assertNull( found );
231
232 // ----------------------------------------
233 // ----------------------------------------
234
235 cond.addDeclaration( this.objectDecl );
236
237 sources.clear();
238
239 source = new MockTupleSource();
240
241 source.addTupleDeclaration( this.objectDecl );
242 source.addTupleDeclaration( this.stringDecl );
243
244 sources.add( source );
245
246 found = this.builder.findMatchingTupleSourceForCondition( cond,
247 sources );
248
249 assertNotNull( found );
250
251 assertSame( source,
252 found );
253 }
254 }
This page was automatically generated by Maven