001: package org.drools.reteoo;
002:
003: import java.lang.reflect.Method;
004:
005: import junit.framework.TestCase;
006:
007: import org.drools.Cheese;
008: import org.drools.base.ClassFieldExtractorCache;
009: import org.drools.base.ValueType;
010: import org.drools.base.evaluators.Operator;
011: import org.drools.base.evaluators.StringFactory;
012: import org.drools.base.field.ObjectFieldImpl;
013: import org.drools.common.InternalFactHandle;
014: import org.drools.common.InternalWorkingMemory;
015: import org.drools.rule.LiteralConstraint;
016: import org.drools.rule.PredicateConstraint;
017: import org.drools.spi.FieldExtractor;
018: import org.drools.spi.PropagationContext;
019:
020: public class CompositeObjectSinkAdapterTest extends TestCase {
021:
022: public int la;
023: public int blah;
024: public String wah;
025:
026: public void testBeta() {
027: final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
028: final MockBetaNode beta = new MockBetaNode(0, null, null);
029: ad.addObjectSink(beta);
030: assertEquals(1, ad.getSinks().length);
031: assertEquals(beta, ad.getSinks()[0]);
032:
033: assertEquals(1, ad.otherSinks.size());
034: assertEquals(beta, ad.otherSinks.getFirst());
035:
036: assertNull(ad.hashableSinks);
037: assertNull(ad.hashedFieldIndexes);
038: assertNull(ad.hashedSinkMap);
039:
040: ad.removeObjectSink(beta);
041: assertNull(ad.otherSinks);
042: assertEquals(0, ad.getSinks().length);
043: }
044:
045: public void testAlphaWithPredicate() {
046: final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
047: final AlphaNode al = new AlphaNode(0, new PredicateConstraint(
048: null, null), null, true, 3);
049: ad.addObjectSink(al);
050:
051: assertEquals(1, ad.getSinks().length);
052: assertEquals(1, ad.otherSinks.size());
053: assertEquals(al, ad.otherSinks.getFirst());
054:
055: ad.removeObjectSink(al);
056: assertEquals(0, ad.getSinks().length);
057: assertNull(ad.otherSinks);
058:
059: }
060:
061: public void testSingleAlpha() {
062:
063: final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
064: final LiteralConstraint lit = new LiteralConstraint(
065: new MockExtractor(), StringFactory.getInstance()
066: .getEvaluator(Operator.EQUAL),
067: new ObjectFieldImpl("stilton"));
068: final AlphaNode al = new AlphaNode(0, lit,
069: new MockObjectSource(0), true, 3);
070:
071: ad.addObjectSink(al);
072:
073: assertNull(ad.otherSinks);
074: assertNotNull(ad.hashedFieldIndexes);
075: assertEquals(1, ad.hashableSinks.size());
076: assertEquals(al, ad.getSinks()[0]);
077:
078: ad.removeObjectSink(al);
079: assertNull(ad.otherSinks);
080: assertNull(ad.hashableSinks);
081:
082: }
083:
084: public void testDoubleAlphaWithBeta() {
085:
086: final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
087: final LiteralConstraint lit = new LiteralConstraint(
088: new MockExtractor(), StringFactory.getInstance()
089: .getEvaluator(Operator.EQUAL),
090: new ObjectFieldImpl("stilton"));
091: final AlphaNode al = new AlphaNode(0, lit,
092: new MockObjectSource(0), true, 3);
093:
094: ad.addObjectSink(al);
095:
096: assertNull(ad.otherSinks);
097: assertNotNull(ad.hashedFieldIndexes);
098: assertEquals(1, ad.hashableSinks.size());
099: assertEquals(al, ad.getSinks()[0]);
100:
101: final LiteralConstraint lit2 = new LiteralConstraint(
102: new MockExtractor(), StringFactory.getInstance()
103: .getEvaluator(Operator.EQUAL),
104: new ObjectFieldImpl("cheddar"));
105: final AlphaNode al2 = new AlphaNode(1, lit2,
106: new MockObjectSource(0), true, 3);
107:
108: ad.addObjectSink(al2);
109:
110: assertNull(ad.otherSinks);
111: assertEquals(2, ad.hashableSinks.size());
112: assertEquals(al, ad.getSinks()[0]);
113: assertEquals(al2, ad.getSinks()[1]);
114:
115: //add a beta, just for good measure, make sure it leaves others alone
116: final MockBetaNode beta = new MockBetaNode(0, null, null);
117: ad.addObjectSink(beta);
118: assertNotNull(ad.otherSinks);
119: assertEquals(2, ad.hashableSinks.size());
120:
121: assertEquals(1, ad.otherSinks.size());
122: assertEquals(beta, ad.otherSinks.getFirst());
123:
124: ad.removeObjectSink(beta);
125: assertNull(ad.otherSinks);
126: assertEquals(2, ad.hashableSinks.size());
127:
128: }
129:
130: public void testTripleAlpha() {
131: final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
132: final LiteralConstraint lit = new LiteralConstraint(
133: new MockExtractor(), StringFactory.getInstance()
134: .getEvaluator(Operator.EQUAL),
135: new ObjectFieldImpl("stilton"));
136: final AlphaNode al = new AlphaNode(0, lit,
137: new MockObjectSource(0), true, 3);
138:
139: ad.addObjectSink(al);
140:
141: assertNull(ad.otherSinks);
142: assertNotNull(ad.hashedFieldIndexes);
143: assertEquals(1, ad.hashableSinks.size());
144: assertEquals(al, ad.getSinks()[0]);
145:
146: final LiteralConstraint lit2 = new LiteralConstraint(
147: new MockExtractor(), StringFactory.getInstance()
148: .getEvaluator(Operator.EQUAL),
149: new ObjectFieldImpl("cheddar"));
150: final AlphaNode al2 = new AlphaNode(1, lit2,
151: new MockObjectSource(1), true, 3);
152:
153: ad.addObjectSink(al2);
154:
155: assertNull(ad.hashedSinkMap);
156: assertEquals(2, ad.hashableSinks.size());
157:
158: final LiteralConstraint lit3 = new LiteralConstraint(
159: new MockExtractor(), StringFactory.getInstance()
160: .getEvaluator(Operator.EQUAL),
161: new ObjectFieldImpl("stinky"));
162: final AlphaNode al3 = new AlphaNode(1, lit3,
163: new MockObjectSource(2), true, 3);
164: ad.addObjectSink(al3);
165:
166: //this should now be nicely hashed.
167: assertNotNull(ad.hashedSinkMap);
168: assertNull(ad.hashableSinks);
169:
170: //now remove one, check the hashing is undone
171: ad.removeObjectSink(al2);
172: assertNotNull(ad.hashableSinks);
173: assertEquals(2, ad.hashableSinks.size());
174: assertNull(ad.hashedSinkMap);
175:
176: }
177:
178: public void testPropagationWithNullValue() {
179:
180: final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
181: FieldExtractor extractor = ClassFieldExtractorCache
182: .getExtractor(Cheese.class, "type", this .getClass()
183: .getClassLoader());
184: final LiteralConstraint lit1 = new LiteralConstraint(extractor,
185: StringFactory.getInstance()
186: .getEvaluator(Operator.EQUAL),
187: new ObjectFieldImpl("stilton"));
188: final AlphaNode al1 = new AlphaNode(0, lit1,
189: new MockObjectSource(0), true, 3);
190:
191: final LiteralConstraint lit2 = new LiteralConstraint(extractor,
192: StringFactory.getInstance()
193: .getEvaluator(Operator.EQUAL),
194: new ObjectFieldImpl("brie"));
195: final AlphaNode al2 = new AlphaNode(0, lit2,
196: new MockObjectSource(0), true, 3);
197:
198: final LiteralConstraint lit3 = new LiteralConstraint(extractor,
199: StringFactory.getInstance()
200: .getEvaluator(Operator.EQUAL),
201: new ObjectFieldImpl("muzzarela"));
202: final AlphaNode al3 = new AlphaNode(0, lit3,
203: new MockObjectSource(0), true, 3);
204:
205: ad.addObjectSink(al1);
206: ad.addObjectSink(al2);
207: ad.addObjectSink(al3);
208:
209: InternalFactHandle handle = new ReteooFactHandleFactory()
210: .newFactHandle(new Cheese());
211: try {
212: ad.propagateAssertObject(handle, null, null);
213: } catch (RuntimeException e) {
214: fail("Not supposed to throw any exception: "
215: + e.getMessage());
216: }
217:
218: }
219:
220: static class MockExtractor implements FieldExtractor {
221:
222: public int getIndex() {
223: // Auto-generated method stub
224: return 0;
225: }
226:
227: public boolean getBooleanValue(
228: InternalWorkingMemory workingMemory, final Object object) {
229: // Auto-generated method stub
230: return false;
231: }
232:
233: public byte getByteValue(InternalWorkingMemory workingMemory,
234: final Object object) {
235: // Auto-generated method stub
236: return 0;
237: }
238:
239: public char getCharValue(InternalWorkingMemory workingMemory,
240: final Object object) {
241: // Auto-generated method stub
242: return 0;
243: }
244:
245: public double getDoubleValue(
246: InternalWorkingMemory workingMemory, final Object object) {
247: // Auto-generated method stub
248: return 0;
249: }
250:
251: public Class getExtractToClass() {
252: // Auto-generated method stub
253: return null;
254: }
255:
256: public String getExtractToClassName() {
257: // Auto-generated method stub
258: return null;
259: }
260:
261: public float getFloatValue(InternalWorkingMemory workingMemory,
262: final Object object) {
263: // Auto-generated method stub
264: return 0;
265: }
266:
267: public int getIntValue(InternalWorkingMemory workingMemory,
268: final Object object) {
269: // Auto-generated method stub
270: return 0;
271: }
272:
273: public long getLongValue(InternalWorkingMemory workingMemory,
274: final Object object) {
275: // Auto-generated method stub
276: return 0;
277: }
278:
279: public Method getNativeReadMethod() {
280: // Auto-generated method stub
281: return null;
282: }
283:
284: public short getShortValue(InternalWorkingMemory workingMemory,
285: final Object object) {
286: // Auto-generated method stub
287: return 0;
288: }
289:
290: public Object getValue(InternalWorkingMemory workingMemory,
291: final Object object) {
292: // Auto-generated method stub
293: return null;
294: }
295:
296: public boolean isNullValue(final Object object,
297: InternalWorkingMemory workingMemory) {
298: return false;
299: }
300:
301: public ValueType getValueType() {
302: // Auto-generated method stub
303: return null;
304: }
305:
306: public int getHashCode(InternalWorkingMemory workingMemory,
307: final Object object) {
308: return 0;
309: }
310:
311: public boolean isGlobal() {
312: // TODO Auto-generated method stub
313: return false;
314: }
315:
316: public boolean isNullValue(InternalWorkingMemory workingMemory,
317: Object object) {
318: // TODO Auto-generated method stub
319: return false;
320: }
321:
322: }
323:
324: static class MockBetaNode extends BetaNode {
325:
326: MockBetaNode(final int id, final TupleSource leftInput,
327: final ObjectSource rightInput) {
328: super (id, leftInput, rightInput);
329: // Auto-generated constructor stub
330: }
331:
332: public void updateSink(final TupleSink sink,
333: final PropagationContext context,
334: final InternalWorkingMemory workingMemory) {
335: // Auto-generated method stub
336:
337: }
338:
339: public void assertTuple(final ReteTuple tuple,
340: final PropagationContext context,
341: final InternalWorkingMemory workingMemory) {
342: // Auto-generated method stub
343:
344: }
345:
346: public void retractTuple(final ReteTuple tuple,
347: final PropagationContext context,
348: final InternalWorkingMemory workingMemory) {
349: // Auto-generated method stub
350:
351: }
352:
353: public void assertObject(final InternalFactHandle handle,
354: final PropagationContext context,
355: final InternalWorkingMemory workingMemory) {
356: // Auto-generated method stub
357:
358: }
359:
360: public void retractObject(final InternalFactHandle handle,
361: final PropagationContext context,
362: final InternalWorkingMemory workingMemory) {
363: // Auto-generated method stub
364:
365: }
366:
367: }
368: }
|