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.Iterator;
021: import java.util.Map;
022:
023: import junit.framework.Assert;
024: import junit.framework.TestCase;
025:
026: import org.drools.FactHandle;
027: import org.drools.QueryResult;
028: import org.drools.QueryResults;
029: import org.drools.RuleBaseFactory;
030: import org.drools.WorkingMemory;
031: import org.drools.base.ClassFieldExtractor;
032: import org.drools.base.ClassFieldExtractorCache;
033: import org.drools.base.ClassObjectType;
034: import org.drools.base.DroolsQuery;
035: import org.drools.base.FieldFactory;
036: import org.drools.base.ValueType;
037: import org.drools.base.evaluators.Operator;
038: import org.drools.rule.LiteralConstraint;
039: import org.drools.rule.Query;
040: import org.drools.spi.Evaluator;
041: import org.drools.spi.FieldValue;
042:
043: public class QueryTerminalNodeTest extends TestCase {
044: public void testQueryTerminalNode() {
045: final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
046: .newRuleBase();
047:
048: final Rete rete = ruleBase.getRete();
049:
050: final ClassObjectType queryObjectType = new ClassObjectType(
051: DroolsQuery.class);
052: final ObjectTypeNode queryObjectTypeNode = new ObjectTypeNode(
053: 1, queryObjectType, rete, 3);
054: queryObjectTypeNode.attach();
055:
056: ClassFieldExtractor extractor = ClassFieldExtractorCache
057: .getExtractor(DroolsQuery.class, "name",
058: DroolsQuery.class.getClassLoader());
059:
060: FieldValue field = FieldFactory.getFieldValue("query-1");
061:
062: final Evaluator evaluator = ValueType.STRING_TYPE
063: .getEvaluator(Operator.EQUAL);
064: LiteralConstraint constraint = new LiteralConstraint(extractor,
065: evaluator, field);
066:
067: AlphaNode alphaNode = new AlphaNode(2, constraint,
068: queryObjectTypeNode, true, 3);
069: alphaNode.attach();
070:
071: final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(
072: 3, alphaNode);
073: liaNode.attach();
074:
075: final ClassObjectType cheeseObjectType = new ClassObjectType(
076: Cheese.class);
077: final ObjectTypeNode cheeseObjectTypeNode = new ObjectTypeNode(
078: 4, cheeseObjectType, rete, 3);
079: cheeseObjectTypeNode.attach();
080:
081: extractor = ClassFieldExtractorCache.getExtractor(Cheese.class,
082: "type", getClass().getClassLoader());
083:
084: field = FieldFactory.getFieldValue("stilton");
085:
086: constraint = new LiteralConstraint(extractor, evaluator, field);
087:
088: alphaNode = new AlphaNode(5, constraint, cheeseObjectTypeNode,
089: true, 3);
090: alphaNode.attach();
091:
092: final JoinNode joinNode = new JoinNode(6, liaNode, alphaNode);
093: joinNode.attach();
094:
095: final Query query = new Query("query-1");
096:
097: final QueryTerminalNode queryNode = new QueryTerminalNode(7,
098: joinNode, query, query.getLhs());
099:
100: queryNode.attach();
101:
102: final org.drools.rule.Package pkg = new org.drools.rule.Package(
103: "com.drools.test");
104: pkg.addRule(query);
105:
106: try {
107: final Field pkgField = ruleBase.getClass().getSuperclass()
108: .getDeclaredField("pkgs");
109: pkgField.setAccessible(true);
110: final Map pkgs = (Map) pkgField.get(ruleBase);
111: pkgs.put(pkg.getName(), pkg);
112: } catch (final Exception e) {
113: Assert.fail("Should not throw any exception: "
114: + e.getMessage());
115: }
116:
117: final WorkingMemory workingMemory = ruleBase
118: .newStatefulSession();
119: QueryResults results = workingMemory.getQueryResults("query-1");
120:
121: assertEquals(0, results.size());
122:
123: final Cheese stilton1 = new Cheese("stilton", 100);
124: final FactHandle handle1 = workingMemory.insert(stilton1);
125:
126: results = workingMemory.getQueryResults("query-1");
127:
128: assertEquals(1, results.size());
129:
130: final Cheese cheddar = new Cheese("cheddar", 55);
131: workingMemory.insert(cheddar);
132:
133: results = workingMemory.getQueryResults("query-1");
134:
135: assertEquals(1, results.size());
136:
137: final Cheese stilton2 = new Cheese("stilton", 5);
138:
139: final FactHandle handle2 = workingMemory.insert(stilton2);
140:
141: results = workingMemory.getQueryResults("query-1");
142:
143: assertEquals(2, results.size());
144:
145: QueryResult result = results.get(0);
146: assertEquals(1, result.size());
147: assertEquals(stilton2, result.get(0));
148:
149: result = results.get(1);
150: assertEquals(1, result.size());
151: assertEquals(stilton1, result.get(0));
152:
153: int i = 0;
154: for (final Iterator it = results.iterator(); it.hasNext();) {
155: result = (QueryResult) it.next();
156: assertEquals(1, result.size());
157: if (i == 1) {
158: assertSame(stilton1, result.get(0));
159: } else {
160: assertSame(stilton2, result.get(0));
161: }
162: i++;
163: }
164:
165: workingMemory.retract(handle1);
166: results = workingMemory.getQueryResults("query-1");
167:
168: assertEquals(1, results.size());
169:
170: workingMemory.retract(handle2);
171: results = workingMemory.getQueryResults("query-1");
172:
173: assertEquals(0, results.size());
174:
175: }
176:
177: public class Cheese {
178: private String type;
179: private int price;
180:
181: public Cheese(final String type, final int price) {
182: super ();
183: this .type = type;
184: this .price = price;
185: }
186:
187: public int getPrice() {
188: return this .price;
189: }
190:
191: public String getType() {
192: return this .type;
193: }
194:
195: public String toString() {
196: return "[Cheese type='" + this .type + "' price='"
197: + this .price + "']";
198: }
199:
200: }
201: }
|