001: package org.drools.brms.modeldriven;
002:
003: import java.util.List;
004:
005: import junit.framework.TestCase;
006:
007: import org.drools.brms.client.modeldriven.brl.ActionRetractFact;
008: import org.drools.brms.client.modeldriven.brl.ActionSetField;
009: import org.drools.brms.client.modeldriven.brl.CompositeFactPattern;
010: import org.drools.brms.client.modeldriven.brl.CompositeFieldConstraint;
011: import org.drools.brms.client.modeldriven.brl.ConnectiveConstraint;
012: import org.drools.brms.client.modeldriven.brl.DSLSentence;
013: import org.drools.brms.client.modeldriven.brl.FactPattern;
014: import org.drools.brms.client.modeldriven.brl.IAction;
015: import org.drools.brms.client.modeldriven.brl.IPattern;
016: import org.drools.brms.client.modeldriven.brl.ISingleFieldConstraint;
017: import org.drools.brms.client.modeldriven.brl.RuleAttribute;
018: import org.drools.brms.client.modeldriven.brl.RuleModel;
019: import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
020:
021: import com.thoughtworks.xstream.XStream;
022:
023: public class RuleModelTest extends TestCase {
024:
025: public void testBoundFactFinder() {
026: final RuleModel model = new RuleModel();
027:
028: assertNull(model.getBoundFact("x"));
029: model.lhs = new IPattern[3];
030:
031: final FactPattern x = new FactPattern("Car");
032: model.lhs[0] = x;
033: x.boundName = "x";
034:
035: assertNotNull(model.getBoundFact("x"));
036: assertEquals(x, model.getBoundFact("x"));
037:
038: final FactPattern y = new FactPattern("Car");
039: model.lhs[1] = y;
040: y.boundName = "y";
041:
042: final FactPattern other = new FactPattern("House");
043: model.lhs[2] = other;
044:
045: assertEquals(y, model.getBoundFact("y"));
046: assertEquals(x, model.getBoundFact("x"));
047:
048: model.rhs = new IAction[1];
049: final ActionSetField set = new ActionSetField();
050: set.variable = "x";
051: model.rhs[0] = set;
052:
053: assertTrue(model.isBoundFactUsed("x"));
054: assertFalse(model.isBoundFactUsed("y"));
055:
056: assertEquals(3, model.lhs.length);
057: assertFalse(model.removeLhsItem(0));
058: assertEquals(3, model.lhs.length);
059:
060: final ActionRetractFact fact = new ActionRetractFact("q");
061: model.rhs[0] = fact;
062: assertTrue(model.isBoundFactUsed("q"));
063: assertFalse(model.isBoundFactUsed("x"));
064:
065: final XStream xt = new XStream();
066: xt.alias("rule", RuleModel.class);
067: xt.alias("fact", FactPattern.class);
068: xt.alias("retract", ActionRetractFact.class);
069:
070: final String brl = xt.toXML(model);
071:
072: System.out.println(brl);
073: }
074:
075: public void testScopedVariables() {
076:
077: //setup the data...
078:
079: final RuleModel model = new RuleModel();
080: model.lhs = new IPattern[3];
081: final FactPattern x = new FactPattern("Car");
082: model.lhs[0] = x;
083: x.boundName = "x";
084:
085: final FactPattern y = new FactPattern("Car");
086: model.lhs[1] = y;
087: y.boundName = "y";
088: final SingleFieldConstraint[] cons = new SingleFieldConstraint[2];
089: y.constraintList = new CompositeFieldConstraint();
090: y.constraintList.constraints = cons;
091: cons[0] = new SingleFieldConstraint("age");
092: cons[1] = new SingleFieldConstraint("make");
093: cons[0].fieldBinding = "qbc";
094: cons[0].connectives = new ConnectiveConstraint[1];
095: cons[0].connectives[0] = new ConnectiveConstraint("&", "x");
096: cons[0].connectives[0].constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
097:
098: final FactPattern other = new FactPattern("House");
099: model.lhs[2] = other;
100: other.boundName = "q";
101: final SingleFieldConstraint[] cons2 = new SingleFieldConstraint[1];
102: cons2[0] = new SingleFieldConstraint();
103: other.constraintList = new CompositeFieldConstraint();
104: other.constraintList.constraints = cons2;
105:
106: //check the results for correct scope
107: List vars = model.getBoundVariablesInScope(cons[0]);
108: assertEquals(1, vars.size());
109: assertEquals("x", vars.get(0));
110:
111: vars = model.getBoundVariablesInScope(cons[0].connectives[0]);
112: assertEquals(1, vars.size());
113: assertEquals("x", vars.get(0));
114:
115: vars = model.getBoundVariablesInScope(cons[1]);
116: assertEquals(2, vars.size());
117: assertEquals("x", vars.get(0));
118: assertEquals("qbc", vars.get(1));
119:
120: vars = model.getBoundVariablesInScope(cons[0]);
121: assertEquals(1, vars.size());
122: assertEquals("x", vars.get(0));
123:
124: vars = model.getBoundVariablesInScope(cons2[0]);
125: assertEquals(3, vars.size());
126: assertEquals("x", vars.get(0));
127: assertEquals("qbc", vars.get(1));
128: assertEquals("y", vars.get(2));
129: }
130:
131: public void testScopedVariablesWithCompositeFact() {
132: RuleModel m = new RuleModel();
133: FactPattern p = new FactPattern();
134: CompositeFieldConstraint cf = new CompositeFieldConstraint();
135: cf.addConstraint(new SingleFieldConstraint("x"));
136: p.addConstraint(cf);
137: SingleFieldConstraint sf = new SingleFieldConstraint("q");
138: sf.fieldBinding = "abc";
139:
140: p.addConstraint(sf);
141: SingleFieldConstraint sf2 = new SingleFieldConstraint("q");
142: sf2.fieldBinding = "qed";
143: cf.addConstraint(sf2);
144: m.addLhsItem(p);
145:
146: List vars = m.getAllVariables();
147: assertEquals(1, vars.size());
148: assertEquals("abc", vars.get(0));
149: }
150:
151: public void testBindingList() {
152: final RuleModel model = new RuleModel();
153:
154: model.lhs = new IPattern[3];
155: final FactPattern x = new FactPattern("Car");
156: model.lhs[0] = x;
157: x.boundName = "x";
158:
159: final FactPattern y = new FactPattern("Car");
160: model.lhs[1] = y;
161: y.boundName = "y";
162:
163: final FactPattern other = new FactPattern("House");
164: model.lhs[2] = other;
165:
166: final List b = model.getBoundFacts();
167: assertEquals(2, b.size());
168:
169: assertEquals("x", b.get(0));
170: assertEquals("y", b.get(1));
171:
172: }
173:
174: public void testAllVariableBindings() {
175: final RuleModel model = new RuleModel();
176: model.lhs = new IPattern[2];
177: final FactPattern x = new FactPattern("Car");
178: model.lhs[0] = x;
179: x.boundName = "boundFact";
180:
181: SingleFieldConstraint sfc = new SingleFieldConstraint("q");
182: x.addConstraint(sfc);
183: sfc.fieldBinding = "field1";
184:
185: SingleFieldConstraint sfc2 = new SingleFieldConstraint("q");
186: x.addConstraint(sfc2);
187: sfc2.fieldBinding = "field2";
188:
189: model.lhs[1] = new CompositeFactPattern();
190:
191: List vars = model.getAllVariables();
192: assertEquals(3, vars.size());
193: assertEquals("boundFact", vars.get(0));
194: assertEquals("field1", vars.get(1));
195: assertEquals("field2", vars.get(2));
196:
197: assertTrue(model.isVariableNameUsed("field2"));
198:
199: }
200:
201: public void testGetVariableNameForRHS() {
202: RuleModel m = new RuleModel();
203: m.name = "blah";
204:
205: FactPattern pat = new FactPattern();
206: pat.boundName = "pat";
207: pat.factType = "Person";
208:
209: m.addLhsItem(pat);
210:
211: List l = m.getAllVariables();
212: assertEquals(1, l.size());
213: assertEquals("pat", l.get(0));
214:
215: }
216:
217: public void testRemoveItemLhs() {
218: final RuleModel model = new RuleModel();
219:
220: model.lhs = new IPattern[3];
221: final FactPattern x = new FactPattern("Car");
222: model.lhs[0] = x;
223: x.boundName = "x";
224:
225: final FactPattern y = new FactPattern("Car");
226: model.lhs[1] = y;
227: y.boundName = "y";
228:
229: final FactPattern other = new FactPattern("House");
230: model.lhs[2] = other;
231:
232: assertEquals(3, model.lhs.length);
233: assertEquals(x, model.lhs[0]);
234:
235: model.removeLhsItem(0);
236:
237: assertEquals(2, model.lhs.length);
238: assertEquals(y, model.lhs[0]);
239: }
240:
241: public void testRemoveItemRhs() {
242: final RuleModel model = new RuleModel();
243:
244: model.rhs = new IAction[3];
245: final ActionRetractFact r0 = new ActionRetractFact("x");
246: final ActionRetractFact r1 = new ActionRetractFact("y");
247: final ActionRetractFact r2 = new ActionRetractFact("z");
248:
249: model.rhs[0] = r0;
250: model.rhs[1] = r1;
251: model.rhs[2] = r2;
252:
253: model.removeRhsItem(1);
254:
255: assertEquals(2, model.rhs.length);
256: assertEquals(r0, model.rhs[0]);
257: assertEquals(r2, model.rhs[1]);
258: }
259:
260: public void testAddItemLhs() {
261: final RuleModel model = new RuleModel();
262: final FactPattern x = new FactPattern();
263: model.addLhsItem(x);
264: assertEquals(1, model.lhs.length);
265:
266: final FactPattern y = new FactPattern();
267: model.addLhsItem(y);
268:
269: assertEquals(2, model.lhs.length);
270: assertEquals(x, model.lhs[0]);
271: assertEquals(y, model.lhs[1]);
272:
273: }
274:
275: public void testAddItemRhs() {
276: final RuleModel model = new RuleModel();
277: final IAction a0 = new ActionSetField();
278: final IAction a1 = new ActionSetField();
279:
280: model.addRhsItem(a0);
281:
282: assertEquals(1, model.rhs.length);
283: model.addRhsItem(a1);
284:
285: assertEquals(2, model.rhs.length);
286:
287: assertEquals(a0, model.rhs[0]);
288: assertEquals(a1, model.rhs[1]);
289: }
290:
291: public void testAttributes() {
292: final RuleModel m = new RuleModel();
293: final RuleAttribute at = new RuleAttribute("salience", "42");
294: m.addAttribute(at);
295: assertEquals(1, m.attributes.length);
296: assertEquals(at, m.attributes[0]);
297:
298: final RuleAttribute at2 = new RuleAttribute("agenda-group", "x");
299: m.addAttribute(at2);
300: assertEquals(2, m.attributes.length);
301: assertEquals(at2, m.attributes[1]);
302:
303: m.removeAttribute(0);
304: assertEquals(1, m.attributes.length);
305: assertEquals(at2, m.attributes[0]);
306: }
307:
308: public void testIsDSLEnhanced() throws Exception {
309: RuleModel m = new RuleModel();
310:
311: assertFalse(m.hasDSLSentences());
312:
313: m.addLhsItem(new FactPattern());
314: assertFalse(m.hasDSLSentences());
315:
316: m.addRhsItem(new ActionSetField("q"));
317:
318: assertFalse(m.hasDSLSentences());
319:
320: m.addLhsItem(new DSLSentence());
321: assertTrue(m.hasDSLSentences());
322:
323: m.addRhsItem(new DSLSentence());
324: assertTrue(m.hasDSLSentences());
325:
326: m = new RuleModel();
327:
328: m.addLhsItem(new DSLSentence());
329: assertTrue(m.hasDSLSentences());
330:
331: m = new RuleModel();
332: m.addRhsItem(new DSLSentence());
333: assertTrue(m.hasDSLSentences());
334:
335: }
336:
337: }
|