001: package org.drools.brms.server.util;
002:
003: import java.io.InputStream;
004: import java.io.InputStreamReader;
005: import java.io.Reader;
006:
007: import junit.framework.TestCase;
008:
009: import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
010: import org.drools.brms.client.modeldriven.brl.ActionFieldValue;
011: import org.drools.brms.client.modeldriven.brl.ActionInsertFact;
012: import org.drools.brms.client.modeldriven.brl.ActionRetractFact;
013: import org.drools.brms.client.modeldriven.brl.ActionUpdateField;
014: import org.drools.brms.client.modeldriven.brl.CompositeFactPattern;
015: import org.drools.brms.client.modeldriven.brl.CompositeFieldConstraint;
016: import org.drools.brms.client.modeldriven.brl.ConnectiveConstraint;
017: import org.drools.brms.client.modeldriven.brl.DSLSentence;
018: import org.drools.brms.client.modeldriven.brl.FactPattern;
019: import org.drools.brms.client.modeldriven.brl.ISingleFieldConstraint;
020: import org.drools.brms.client.modeldriven.brl.RuleAttribute;
021: import org.drools.brms.client.modeldriven.brl.RuleModel;
022: import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
023:
024: public class BRLPersitenceTest extends TestCase {
025:
026: public void testGenerateEmptyXML() {
027: final BRLPersistence p = BRXMLPersistence.getInstance();
028: final String xml = p.marshal(new RuleModel());
029: assertNotNull(xml);
030: assertFalse(xml.equals(""));
031:
032: assertTrue(xml.startsWith("<rule>"));
033: assertTrue(xml.endsWith("</rule>"));
034: }
035:
036: public void testBasics() {
037: final BRLPersistence p = BRXMLPersistence.getInstance();
038: final RuleModel m = new RuleModel();
039: m.addLhsItem(new FactPattern("Person"));
040: m.addLhsItem(new FactPattern("Accident"));
041: m.addAttribute(new RuleAttribute("no-loop", "true"));
042:
043: m.addRhsItem(new ActionInsertFact("Report"));
044: m.name = "my rule";
045: final String xml = p.marshal(m);
046: //System.out.println(xml);
047: assertTrue(xml.indexOf("Person") > -1);
048: assertTrue(xml.indexOf("Accident") > -1);
049: assertTrue(xml.indexOf("no-loop") > -1);
050: assertTrue(xml.indexOf("org.drools") == -1);
051:
052: }
053:
054: public void testMoreComplexRendering() {
055: final BRLPersistence p = BRXMLPersistence.getInstance();
056: final RuleModel m = getComplexModel();
057:
058: final String xml = p.marshal(m);
059: System.out.println(xml);
060:
061: assertTrue(xml.indexOf("org.drools") == -1);
062:
063: }
064:
065: public void testRoundTrip() {
066: final RuleModel m = getComplexModel();
067:
068: final String xml = BRXMLPersistence.getInstance().marshal(m);
069:
070: final RuleModel m2 = BRXMLPersistence.getInstance().unmarshal(
071: xml);
072: assertNotNull(m2);
073: assertEquals(m.name, m2.name);
074: assertEquals(m.lhs.length, m2.lhs.length);
075: assertEquals(m.rhs.length, m2.rhs.length);
076: assertEquals(1, m.attributes.length);
077:
078: final RuleAttribute at = m.attributes[0];
079: assertEquals("no-loop", at.attributeName);
080: assertEquals("true", at.value);
081:
082: final String newXML = BRXMLPersistence.getInstance()
083: .marshal(m2);
084: assertEquals(xml, newXML);
085:
086: }
087:
088: public void testCompositeConstraintsRoundTrip() throws Exception {
089: RuleModel m = new RuleModel();
090: m.name = "with composite";
091:
092: FactPattern p1 = new FactPattern("Person");
093: p1.boundName = "p1";
094: m.addLhsItem(p1);
095:
096: FactPattern p = new FactPattern("Goober");
097: m.addLhsItem(p);
098: CompositeFieldConstraint comp = new CompositeFieldConstraint();
099: comp.compositeJunctionType = CompositeFieldConstraint.COMPOSITE_TYPE_OR;
100: p.addConstraint(comp);
101:
102: final SingleFieldConstraint X = new SingleFieldConstraint();
103: X.fieldName = "goo";
104: X.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
105: X.value = "foo";
106: X.operator = "==";
107: X.connectives = new ConnectiveConstraint[1];
108: X.connectives[0] = new ConnectiveConstraint();
109: X.connectives[0].constraintValueType = ConnectiveConstraint.TYPE_LITERAL;
110: X.connectives[0].operator = "|| ==";
111: X.connectives[0].value = "bar";
112: comp.addConstraint(X);
113:
114: final SingleFieldConstraint Y = new SingleFieldConstraint();
115: Y.fieldName = "goo2";
116: Y.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
117: Y.value = "foo";
118: Y.operator = "==";
119: comp.addConstraint(Y);
120:
121: CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
122: comp2.compositeJunctionType = CompositeFieldConstraint.COMPOSITE_TYPE_AND;
123: final SingleFieldConstraint Q1 = new SingleFieldConstraint();
124: Q1.fieldName = "goo";
125: Q1.operator = "==";
126: Q1.value = "whee";
127: Q1.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
128:
129: comp2.addConstraint(Q1);
130:
131: final SingleFieldConstraint Q2 = new SingleFieldConstraint();
132: Q2.fieldName = "gabba";
133: Q2.operator = "==";
134: Q2.value = "whee";
135: Q2.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
136:
137: comp2.addConstraint(Q2);
138:
139: //now nest it
140: comp.addConstraint(comp2);
141:
142: final SingleFieldConstraint Z = new SingleFieldConstraint();
143: Z.fieldName = "goo3";
144: Z.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
145: Z.value = "foo";
146: Z.operator = "==";
147:
148: p.addConstraint(Z);
149:
150: ActionInsertFact ass = new ActionInsertFact("Whee");
151: m.addRhsItem(ass);
152:
153: String xml = BRXMLPersistence.getInstance().marshal(m);
154: //System.err.println(xml);
155:
156: RuleModel m2 = BRXMLPersistence.getInstance().unmarshal(xml);
157: assertNotNull(m2);
158: assertEquals("with composite", m2.name);
159:
160: assertEquals(m2.lhs.length, m.lhs.length);
161: assertEquals(m2.rhs.length, m.rhs.length);
162:
163: }
164:
165: /**
166: * This will verify that we can load an old BRL change. If this fails,
167: * then backwards compatability is broken.
168: */
169: public void testBackwardsCompat() throws Exception {
170: RuleModel m2 = BRXMLPersistence.getInstance().unmarshal(
171: loadResource("existing_brl.xml"));
172:
173: assertNotNull(m2);
174: assertEquals(3, m2.rhs.length);
175: }
176:
177: private String loadResource(final String name) throws Exception {
178:
179: // System.err.println( getClass().getResource( name ) );
180: final InputStream in = getClass().getResourceAsStream(name);
181:
182: final Reader reader = new InputStreamReader(in);
183:
184: final StringBuffer text = new StringBuffer();
185:
186: final char[] buf = new char[1024];
187: int len = 0;
188:
189: while ((len = reader.read(buf)) >= 0) {
190: text.append(buf, 0, len);
191: }
192:
193: return text.toString();
194: }
195:
196: private RuleModel getComplexModel() {
197: final RuleModel m = new RuleModel();
198:
199: m.addAttribute(new RuleAttribute("no-loop", "true"));
200:
201: final FactPattern pat = new FactPattern();
202: pat.boundName = "p1";
203: pat.factType = "Person";
204: final SingleFieldConstraint con = new SingleFieldConstraint();
205: con.fieldBinding = "f1";
206: con.fieldName = "age";
207: con.operator = "<";
208: con.value = "42";
209: pat.addConstraint(con);
210:
211: m.addLhsItem(pat);
212:
213: final CompositeFactPattern comp = new CompositeFactPattern(
214: "not");
215: comp.addFactPattern(new FactPattern("Cancel"));
216: m.addLhsItem(comp);
217:
218: final ActionUpdateField set = new ActionUpdateField();
219: set.variable = "p1";
220: set.addFieldValue(new ActionFieldValue("status", "rejected",
221: SuggestionCompletionEngine.TYPE_STRING));
222: m.addRhsItem(set);
223:
224: final ActionRetractFact ret = new ActionRetractFact("p1");
225: m.addRhsItem(ret);
226:
227: final DSLSentence sen = new DSLSentence();
228: sen.sentence = "Send an email to {administrator}";
229:
230: m.addRhsItem(sen);
231: return m;
232: }
233:
234: public void testLoadEmpty() {
235: RuleModel m = BRXMLPersistence.getInstance().unmarshal(null);
236: assertNotNull(m);
237:
238: m = BRXMLPersistence.getInstance().unmarshal("");
239: assertNotNull(m);
240: }
241:
242: }
|