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: }
|