001: package org.drools.rule;
002:
003: import org.drools.RuntimeDroolsException;
004:
005: import junit.framework.Assert;
006: import junit.framework.TestCase;
007:
008: public class GroupElementTest extends TestCase {
009:
010: public void testPackNestedAnd() {
011: final GroupElement and1 = GroupElementFactory.newAndInstance();
012: final Pattern pattern1 = new Pattern(0, null);
013: and1.addChild(pattern1);
014:
015: final Pattern pattern2 = new Pattern(0, null);
016: and1.addChild(pattern2);
017:
018: assertEquals(2, and1.getChildren().size());
019: assertSame(pattern1, and1.getChildren().get(0));
020: assertSame(pattern2, and1.getChildren().get(1));
021:
022: final GroupElement and2 = GroupElementFactory.newAndInstance();
023: and2.addChild(and1);
024:
025: and2.pack();
026: assertEquals(2, and2.getChildren().size());
027: assertSame(pattern1, and2.getChildren().get(0));
028: assertSame(pattern2, and2.getChildren().get(1));
029: }
030:
031: public void testPackNestedOr() {
032: final GroupElement or1 = GroupElementFactory.newOrInstance();
033: final Pattern pattern1 = new Pattern(0, null);
034: or1.addChild(pattern1);
035:
036: final Pattern pattern2 = new Pattern(0, null);
037: or1.addChild(pattern2);
038:
039: assertEquals(2, or1.getChildren().size());
040: assertSame(pattern1, or1.getChildren().get(0));
041: assertSame(pattern2, or1.getChildren().get(1));
042:
043: final GroupElement or2 = GroupElementFactory.newOrInstance();
044: or2.addChild(or1);
045:
046: or2.pack();
047:
048: assertEquals(2, or2.getChildren().size());
049: assertSame(pattern1, or2.getChildren().get(0));
050: assertSame(pattern2, or2.getChildren().get(1));
051: }
052:
053: public void testPackNestedExists() {
054: final GroupElement exists1 = GroupElementFactory
055: .newExistsInstance();
056: final Pattern pattern1 = new Pattern(0, null);
057: exists1.addChild(pattern1);
058:
059: assertEquals(1, exists1.getChildren().size());
060: assertSame(pattern1, exists1.getChildren().get(0));
061:
062: final GroupElement exists2 = GroupElementFactory
063: .newExistsInstance();
064: exists2.addChild(exists1);
065:
066: exists2.pack();
067:
068: assertEquals(1, exists2.getChildren().size());
069: assertSame(pattern1, exists2.getChildren().get(0));
070: }
071:
072: public void testAddMultipleChildsIntoNot() {
073: final GroupElement not = GroupElementFactory.newNotInstance();
074:
075: final Pattern pattern1 = new Pattern(0, null);
076: try {
077: not.addChild(pattern1);
078: } catch (final RuntimeDroolsException rde) {
079: Assert
080: .fail("Adding a single child is not supposed to throw Exception for NOT GE: "
081: + rde.getMessage());
082: }
083:
084: final Pattern pattern2 = new Pattern(0, null);
085: try {
086: not.addChild(pattern2);
087: Assert
088: .fail("Adding a second child into a NOT GE should throw Exception");
089: } catch (final RuntimeDroolsException rde) {
090: // everything is fine
091: }
092: }
093:
094: public void testAddSingleBranchAnd() {
095: final GroupElement and1 = GroupElementFactory.newAndInstance();
096: final Pattern pattern = new Pattern(0, null);
097: and1.addChild(pattern);
098: assertEquals(1, and1.getChildren().size());
099: assertSame(pattern, and1.getChildren().get(0));
100:
101: final GroupElement or1 = GroupElementFactory.newOrInstance();
102: or1.addChild(and1);
103:
104: or1.pack();
105: assertEquals(1, or1.getChildren().size());
106: assertSame(pattern, or1.getChildren().get(0));
107: }
108:
109: public void testAddSingleBranchOr() {
110: final GroupElement or1 = GroupElementFactory.newOrInstance();
111: final Pattern pattern = new Pattern(0, null);
112: or1.addChild(pattern);
113: assertEquals(1, or1.getChildren().size());
114: assertSame(pattern, or1.getChildren().get(0));
115:
116: final GroupElement and1 = GroupElementFactory.newAndInstance();
117: and1.addChild(or1);
118:
119: and1.pack();
120: assertEquals(1, and1.getChildren().size());
121: assertSame(pattern, and1.getChildren().get(0));
122: }
123:
124: /**
125: * This test tests deep nested structures, and shall transform this:
126: *
127: * AND2
128: * |
129: * OR3
130: * |
131: * OR2
132: * |
133: * AND1
134: * |
135: * OR1
136: * / \
137: * C1 C2
138: *
139: * Into this:
140: *
141: * OR1
142: * / \
143: * C1 C2
144: *
145: */
146: public void testDeepNestedStructure() {
147: final GroupElement or1 = GroupElementFactory.newOrInstance();
148: final Pattern pattern1 = new Pattern(0, null);
149: or1.addChild(pattern1);
150:
151: final Pattern pattern2 = new Pattern(0, null);
152: or1.addChild(pattern2);
153:
154: final GroupElement and1 = GroupElementFactory.newAndInstance();
155: and1.addChild(or1);
156: assertEquals(1, and1.getChildren().size());
157: assertSame(or1, and1.getChildren().get(0));
158:
159: assertSame(pattern1, or1.getChildren().get(0));
160: assertSame(pattern2, or1.getChildren().get(1));
161:
162: final GroupElement or2 = GroupElementFactory.newOrInstance();
163: or2.addChild(and1);
164:
165: assertEquals(1, or2.getChildren().size());
166: assertSame(and1, or2.getChildren().get(0));
167:
168: final GroupElement or3 = GroupElementFactory.newOrInstance();
169: or3.addChild(or2);
170:
171: assertEquals(1, or2.getChildren().size());
172: assertSame(or2, or3.getChildren().get(0));
173:
174: final GroupElement and2 = GroupElementFactory.newAndInstance();
175: and2.addChild(or3);
176:
177: assertEquals(1, and2.getChildren().size());
178: assertSame(or3, and2.getChildren().get(0));
179:
180: // Now pack the structure
181: and2.pack();
182:
183: // and2 now is in fact transformed into an OR
184: assertEquals(GroupElement.OR, and2.getType());
185:
186: assertEquals(2, and2.getChildren().size());
187:
188: assertSame(pattern1, and2.getChildren().get(0));
189: assertSame(pattern2, and2.getChildren().get(1));
190:
191: }
192:
193: /**
194: * This test tests deep nested structures, and shall transform this:
195: *
196: * AND2
197: * / \
198: * OR3 C3
199: * |
200: * OR2
201: * |
202: * AND1
203: * |
204: * OR1
205: * / \
206: * C1 C2
207: *
208: * Into this:
209: *
210: * AND2
211: * / \
212: * OR1 C3
213: * / \
214: * C1 C2
215: *
216: */
217: public void testDeepNestedStructureWithMultipleElementsInRoot() {
218: final GroupElement or1 = GroupElementFactory.newOrInstance();
219: final Pattern pattern1 = new Pattern(0, null);
220: or1.addChild(pattern1);
221:
222: final Pattern pattern2 = new Pattern(0, null);
223: or1.addChild(pattern2);
224:
225: final GroupElement and1 = GroupElementFactory.newAndInstance();
226: and1.addChild(or1);
227: assertEquals(1, and1.getChildren().size());
228: assertSame(or1, and1.getChildren().get(0));
229:
230: assertSame(pattern1, or1.getChildren().get(0));
231: assertSame(pattern2, or1.getChildren().get(1));
232:
233: final GroupElement or2 = GroupElementFactory.newOrInstance();
234: or2.addChild(and1);
235:
236: assertEquals(1, or2.getChildren().size());
237: assertSame(and1, or2.getChildren().get(0));
238:
239: final GroupElement or3 = GroupElementFactory.newOrInstance();
240: or3.addChild(or2);
241:
242: assertEquals(1, or2.getChildren().size());
243: assertSame(or2, or3.getChildren().get(0));
244:
245: final GroupElement and2 = GroupElementFactory.newAndInstance();
246: and2.addChild(or3);
247:
248: final Pattern pattern3 = new Pattern(0, null);
249: and2.addChild(pattern3);
250:
251: assertEquals(2, and2.getChildren().size());
252: assertSame(or3, and2.getChildren().get(0));
253: assertSame(pattern3, and2.getChildren().get(1));
254:
255: // Now pack the structure
256: and2.pack();
257:
258: // and2 now is in fact transformed into an OR
259: assertTrue(and2.isAnd());
260:
261: assertEquals(2, and2.getChildren().size());
262:
263: // order must be the same
264: assertSame(or1, and2.getChildren().get(0));
265: assertSame(pattern3, and2.getChildren().get(1));
266:
267: assertEquals(2, or1.getChildren().size());
268: assertSame(pattern1, or1.getChildren().get(0));
269: assertSame(pattern2, or1.getChildren().get(1));
270:
271: }
272:
273: }
|