1
2 package org.drools.reteoo.impl;
3
4 import org.drools.reteoo.MockTupleSource;
5 import org.drools.rule.Declaration;
6 import org.drools.semantics.java.ClassObjectType;
7
8 import junit.framework.TestCase;
9
10 import java.util.Set;
11
12 public class JoinMemoryTest extends TestCase
13 {
14 private Declaration joinDecl1;
15 private Declaration joinDecl2;
16
17 private ReteTuple tuple1;
18 private ReteTuple tuple2;
19 private ReteTuple tuple3;
20 private ReteTuple tuple4;
21
22 private MockTupleSource leftInput1;
23 private MockTupleSource rightInput1;
24
25 private JoinNodeImpl oneColumnJoinNode;
26 private JoinMemoryImpl oneColumnJoinMemory;
27
28 private MockTupleSource leftInput2;
29 private MockTupleSource rightInput2;
30
31 private JoinNodeImpl twoColumnJoinNode;
32 private JoinMemoryImpl twoColumnJoinMemory;
33
34 public JoinMemoryTest(String name)
35 {
36 super( name );
37 }
38
39 public void setUp()
40 {
41 this.joinDecl1 = new Declaration( new ClassObjectType( Object.class ),
42 "one" );
43
44 this.joinDecl2 = new Declaration( new ClassObjectType( Object.class ),
45 "two" );
46
47 this.tuple1 = new ReteTuple();
48 this.tuple2 = new ReteTuple();
49 this.tuple3 = new ReteTuple();
50 this.tuple4 = new ReteTuple();
51
52 // | cheese | toast |
53 this.tuple1.putOtherColumn( this.joinDecl1,
54 "one-cheese" );
55
56 this.tuple1.putOtherColumn( this.joinDecl2,
57 "two-toast" );
58
59 // | cheese | sneeze |
60 this.tuple2.putOtherColumn( this.joinDecl1,
61 "one-cheese" );
62
63 this.tuple2.putOtherColumn( this.joinDecl2,
64 "two-sneeze" );
65
66 // | toast | sneeze |
67 this.tuple3.putOtherColumn( this.joinDecl1,
68 "one-toast" );
69
70 this.tuple3.putOtherColumn( this.joinDecl2,
71 "two-sneeze" );
72
73 // | cheese | sneeze |
74 this.tuple4.putOtherColumn( this.joinDecl1,
75 "one-cheese" );
76
77 this.tuple4.putOtherColumn( this.joinDecl2,
78 "two-sneeze" );
79
80 // ----------------------------------------
81
82 this.leftInput1 = new MockTupleSource();
83 this.rightInput1 = new MockTupleSource();
84
85 this.leftInput1.addTupleDeclaration( this.joinDecl1 );
86 this.rightInput1.addTupleDeclaration( this.joinDecl1 );
87
88 this.oneColumnJoinNode = new JoinNodeImpl( this.leftInput1,
89 this.rightInput1 );
90
91 this.oneColumnJoinMemory = new JoinMemoryImpl( this.oneColumnJoinNode );
92
93 // ----------------------------------------
94
95 this.leftInput2 = new MockTupleSource();
96 this.rightInput2 = new MockTupleSource();
97
98 this.leftInput2.addTupleDeclaration( this.joinDecl1 );
99 this.leftInput2.addTupleDeclaration( this.joinDecl2 );
100 this.rightInput2.addTupleDeclaration( this.joinDecl1 );
101 this.rightInput2.addTupleDeclaration( this.joinDecl2 );
102
103 this.twoColumnJoinNode = new JoinNodeImpl( this.leftInput2,
104 this.rightInput2 );
105
106 this.twoColumnJoinMemory = new JoinMemoryImpl( this.twoColumnJoinNode );
107
108 }
109
110 public void tearDown()
111 {
112 this.joinDecl1 = null;
113 this.joinDecl2 = null;
114 }
115
116 /*** If two Tuples having multiple common join columns contain
117 * the same values in thoses columns, then a new joined tuple
118 * MUST be created an returned.
119 */
120 public void testMultiColumnAttemptJoinSingleTuple()
121 {
122 ReteTuple tuple = null;
123
124 // ----------------------------------------
125
126 tuple = this.twoColumnJoinMemory.attemptJoin( this.tuple1,
127 this.tuple2 );
128
129 assertNull( tuple );
130
131 // ----------------------------------------
132
133 tuple = this.twoColumnJoinMemory.attemptJoin( this.tuple1,
134 this.tuple3 );
135
136 assertNull( tuple );
137
138 // ----------------------------------------
139
140 tuple = this.twoColumnJoinMemory.attemptJoin( this.tuple1,
141 this.tuple4 );
142
143 assertNull( tuple );
144
145 // ----------------------------------------
146
147 tuple = this.twoColumnJoinMemory.attemptJoin( this.tuple2,
148 this.tuple4 );
149
150 assertNotNull( tuple );
151
152 assertEquals( "one-cheese",
153 tuple.get( this.joinDecl1 ) );
154
155 assertEquals( "two-sneeze",
156 tuple.get( this.joinDecl2 ) );
157 }
158
159 /*** If two Tuples having a common join column contain the
160 * same values in that column, then a new joined tuple
161 * MUST be created an returned.
162 */
163 public void testSingleColumnAttemptJoinSingleTuple()
164 {
165 ReteTuple tuple = null;
166
167 // ----------------------------------------
168
169 tuple = this.oneColumnJoinMemory.attemptJoin( this.tuple1,
170 this.tuple2 );
171
172 assertNotNull( tuple );
173
174 assertEquals( "one-cheese",
175 tuple.get( this.joinDecl1 ) );
176
177 // ----------------------------------------
178
179 tuple = this.oneColumnJoinMemory.attemptJoin( this.tuple1,
180 this.tuple3 );
181
182 assertNull( tuple );
183
184 // ----------------------------------------
185
186 tuple = this.oneColumnJoinMemory.attemptJoin( this.tuple1,
187 this.tuple4 );
188
189 assertNotNull( tuple );
190
191 assertEquals( "one-cheese",
192 tuple.get( this.joinDecl1 ) );
193
194 // ----------------------------------------
195
196 tuple = this.oneColumnJoinMemory.attemptJoin( this.tuple2,
197 this.tuple3 );
198
199 assertNull( tuple );
200
201 // ----------------------------------------
202
203 tuple = this.oneColumnJoinMemory.attemptJoin( this.tuple2,
204 this.tuple4 );
205
206 assertNotNull( tuple );
207
208 assertEquals( "one-cheese",
209 tuple.get( this.joinDecl1 ) );
210 }
211
212 /*** A Tuple added to one side of the memory MUST attempt a
213 * join against all tuples in the other side memory, returning
214 * all successfully joined tuples.
215 */
216 /*
217 public void testAddTuples()
218 {
219 Set joined = null;
220
221 joined = this.oneColumnJoinMemory.addLeftTuple( this.tuple1 );
222
223 assertEquals( 0,
224 joined.size() );
225
226 joined = this.oneColumnJoinMemory.addRightTuple( this.tuple2 );
227
228 assertEquals( 1,
229 joined.size() );
230
231 joined = this.oneColumnJoinMemory.addLeftTuple( this.tuple2 );
232
233 assertEquals( 1,
234 joined.size() );
235
236 assertEquals( 2,
237 this.oneColumnJoinMemory.getLeftTuples().size() );
238
239 assertEquals( 1,
240 this.oneColumnJoinMemory.getRightTuples().size() );
241 }
242 */
243 }
This page was automatically generated by Maven