01: package org.drools.brms.server.util;
02:
03: import org.drools.brms.client.modeldriven.brl.ActionFieldValue;
04: import org.drools.brms.client.modeldriven.brl.ActionInsertFact;
05: import org.drools.brms.client.modeldriven.brl.ActionInsertLogicalFact;
06: import org.drools.brms.client.modeldriven.brl.ActionRetractFact;
07: import org.drools.brms.client.modeldriven.brl.ActionSetField;
08: import org.drools.brms.client.modeldriven.brl.ActionUpdateField;
09: import org.drools.brms.client.modeldriven.brl.CompositeFactPattern;
10: import org.drools.brms.client.modeldriven.brl.CompositeFieldConstraint;
11: import org.drools.brms.client.modeldriven.brl.ConnectiveConstraint;
12: import org.drools.brms.client.modeldriven.brl.DSLSentence;
13: import org.drools.brms.client.modeldriven.brl.FactPattern;
14: import org.drools.brms.client.modeldriven.brl.RuleAttribute;
15: import org.drools.brms.client.modeldriven.brl.RuleModel;
16: import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
17:
18: import com.thoughtworks.xstream.XStream;
19: import com.thoughtworks.xstream.io.xml.DomDriver;
20:
21: /**
22: * This class persists the rule model to XML and back.
23: *
24: * This is the 'brl' xml format (Business Rule Language).
25: *
26: * @author Michael Neale
27: */
28: public class BRXMLPersistence implements BRLPersistence {
29:
30: private XStream xt;
31: private static final BRLPersistence INSTANCE = new BRXMLPersistence();
32:
33: private BRXMLPersistence() {
34: this .xt = new XStream(new DomDriver());
35:
36: this .xt.alias("rule", RuleModel.class);
37: this .xt.alias("fact", FactPattern.class);
38: this .xt.alias("retract", ActionRetractFact.class);
39: this .xt.alias("assert", ActionInsertFact.class);
40: this .xt.alias("modify", ActionUpdateField.class);
41: this .xt.alias("setField", ActionSetField.class);
42: this .xt.alias("dslSentence", DSLSentence.class);
43: this .xt.alias("compositePattern", CompositeFactPattern.class);
44: this .xt.alias("attribute", RuleAttribute.class);
45:
46: this .xt.alias("fieldValue", ActionFieldValue.class);
47: this .xt.alias("connectiveConstraint",
48: ConnectiveConstraint.class);
49: this .xt.alias("fieldConstraint", SingleFieldConstraint.class);
50:
51: this .xt.alias("compositeConstraint",
52: CompositeFieldConstraint.class);
53:
54: this .xt.alias("assertLogical", ActionInsertLogicalFact.class);
55:
56: }
57:
58: public static BRLPersistence getInstance() {
59: return INSTANCE;
60: }
61:
62: /* (non-Javadoc)
63: * @see org.drools.brms.server.util.BRLPersistence#toXML(org.drools.brms.client.modeldriven.brl.RuleModel)
64: */
65: public String marshal(final RuleModel model) {
66: return this .xt.toXML(model);
67: }
68:
69: /* (non-Javadoc)
70: * @see org.drools.brms.server.util.BRLPersistence#toModel(java.lang.String)
71: */
72: public RuleModel unmarshal(final String xml) {
73: if (xml == null) {
74: return new RuleModel();
75: }
76: if (xml.trim().equals("")) {
77: return new RuleModel();
78: }
79: return (RuleModel) this.xt.fromXML(xml);
80: }
81:
82: }
|