001: package org.mandarax.zkb.framework;
002:
003: /**
004: * Copyright (C) 1999-2004 Jens Dietrich (mailto:mandarax@jbdietrich.com)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Vector;
024:
025: import org.jdom.Element;
026: import org.mandarax.kernel.Fact;
027: import org.mandarax.kernel.LogicFactory;
028: import org.mandarax.kernel.Prerequisite;
029: import org.mandarax.kernel.Rule;
030: import org.mandarax.zkb.ObjectPersistencyService;
031: import org.mandarax.zkb.ZKBException;
032:
033: /**
034: * An adapter class for rules.
035: * @see org.mandarax.kernel.Rule
036: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037: * @version 3.4 <7 March 05>
038: * @since 2.2
039: */
040:
041: public class Adapter4Rules extends AbstractAdapter {
042:
043: /**
044: * Export an object, i.e., convert it to an element in the DOM.
045: * @param obj an object
046: * @param driver the generic driver
047: * @param ops the object persistency service
048: * @exception a ZKBException is thrown if export fails
049: */
050: public Element exportObject(Object obj, GenericDriver driver,
051: ObjectPersistencyService ops) throws ZKBException {
052: check(obj, Rule.class);
053: Rule rule = (Rule) obj;
054: Element e = new Element(RULE);
055: Element eConn = new Element(rule.isBodyOrConnected() ? OR : AND);
056: Element eBody = new Element(BODY);
057: eBody.addContent(eConn);
058: e.addContent(eBody);
059: for (Iterator it = rule.getBody().iterator(); it.hasNext();) {
060: Element el = exportObject(it.next(), PREREQUISITE, driver,
061: ops);
062: eConn.addContent(el);
063: }
064: Element eHead = new Element(HEAD);
065: e.addContent(eHead);
066: Element el = exportObject(rule.getHead(),
067: TagAndAttributeNames.FACT, driver, ops);
068: eHead.addContent(el);
069: exportProperties(e, rule);
070: return e;
071: }
072:
073: /**
074: * Build an object from an XML element.
075: * @param e an element
076: * @param driver the generic driver
077: * @param ops the object persistency service
078: * @param lfactory the logic factory used to create objects
079: * @exception a ZKBException is thrown if import fails
080: */
081: public Object importObject(Element e, GenericDriver driver,
082: ObjectPersistencyService ops, LogicFactory lfactory)
083: throws ZKBException {
084: // get body
085: Element eBody = e.getChild(BODY);
086: Element eConn = (Element) eBody.getChildren().get(0);
087: boolean isOrConnected = OR.equals(eConn.getName());
088: List body = new Vector();
089: Adapter adapter4Prereq = driver.getAdapter(PREREQUISITE);
090: List children = eConn.getChildren();
091: for (Iterator it = children.iterator(); it.hasNext();) {
092: Element eFact = (Element) it.next();
093: Prerequisite premisse = (Prerequisite) adapter4Prereq
094: .importObject(eFact, driver, ops, lfactory);
095: body.add(premisse);
096: }
097: // get head
098: Element eHead = e.getChild(HEAD);
099: Fact head = (Fact) importChild(eHead, FACT, driver, ops,
100: lfactory);
101:
102: // build rule
103: Rule rule = lfactory.createRule(body, head, isOrConnected);
104: importProperties(e, rule);
105: return rule;
106: }
107:
108: /**
109: * Get the name of the associated tag (element).
110: * @return a string
111: */
112: public String getTagName() {
113: return RULE;
114: }
115:
116: /**
117: * Print the DTD associated with this adapter on a string buffer.
118: * @param out the buffer to print on.
119: */
120: public void printDTD(StringBuffer out) {
121: // warning : properties are declared here and only here
122: // this is sensitive w.r.t. the order in which adapters are registered
123: // here we assume that this is the first adapter registered that needs property support
124: out.append("<!ELEMENT rule (_body,_head,properties?)>\n");
125: out.append("<!ELEMENT _body (or|and|atom*)>\n");
126: out.append("<!ELEMENT or (prereq*)>\n");
127: out.append("<!ELEMENT and (prereq*)>\n");
128: out.append("<!ELEMENT _head (atom)>\n");
129: out.append("<!ELEMENT properties (property*)>\n");
130: out.append("<!ELEMENT property EMPTY>\n");
131: out.append("<!ATTLIST property key CDATA #REQUIRED>\n");
132: out.append("<!ATTLIST property value CDATA #REQUIRED>\n");
133: }
134:
135: }
|