001: package org.mandarax.xkb.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.Map;
024: import java.util.Vector;
025:
026: import org.jdom.Element;
027: import org.mandarax.kernel.Fact;
028: import org.mandarax.kernel.LogicFactory;
029: import org.mandarax.kernel.Rule;
030: import org.mandarax.xkb.XKBException;
031:
032: /**
033: * An adapter class for rules. Supports negated prerequisites.
034: * @see org.mandarax.kernel.Rule
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 2.0
038: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
039: */
040:
041: public class XMLAdapter4Rules2 extends AbstractXMLAdapter {
042: public static final String RULE = "imp";
043: public static final String BODY = "_body";
044: public static final String HEAD = "_head";
045: public static final String OR = "or";
046: public static final String AND = "and";
047: public static final String CONNECTIVE = "connective";
048:
049: /**
050: * Export an object, i.e., convert it to an element in the DOM.
051: * @param obj an object
052: * @param driver the generic driver
053: * @param cache a cache used in order to associate the same
054: * id with various occurences of the same object
055: * @exception an XKBException is thrown if export fails
056: */
057: public Element exportObject(Object obj, GenericDriver driver,
058: Map cache) throws XKBException {
059: check(obj, Rule.class);
060: Rule rule = (Rule) obj;
061: Element e = new Element(RULE);
062: Element eConn = new Element(rule.isBodyOrConnected() ? OR : AND);
063: Element eBody = new Element(BODY);
064: eBody.addContent(eConn);
065: e.addContent(eBody);
066: for (Iterator it = rule.getBody().iterator(); it.hasNext();) {
067: Element el = exportObject(it.next(),
068: GenericDriver.PREREQUISITE, driver, cache);
069: eConn.addContent(el);
070: }
071: Element eHead = new Element(HEAD);
072: e.addContent(eHead);
073: Element el = exportObject(rule.getHead(), GenericDriver.FACT,
074: driver, cache);
075: eHead.addContent(el);
076: addProperties(e, rule);
077: return e;
078: }
079:
080: /**
081: * Build an object from an XML element.
082: * @param e an element
083: * @param driver the generic driver
084: * @param cache a cache used to identify objects that have the same id
085: * @param lfactory the logic factory used to create objects
086: * @exception an XKBException is thrown if import fails
087: */
088: public Object importObject(Element e, GenericDriver driver,
089: Map cache, LogicFactory lfactory) throws XKBException {
090: // get body
091: Element eBody = e.getChild(BODY);
092: Element eConn = (Element) eBody.getChildren().get(0);
093: boolean isOrConnected = OR.equals(eConn.getName());
094: List body = new Vector();
095: XMLAdapter adapter4Prereqs = null;
096: List children = eConn.getChildren();
097: for (Iterator it = children.iterator(); it.hasNext();) {
098: Element ePrereq = (Element) it.next();
099: if (adapter4Prereqs == null)
100: adapter4Prereqs = driver.getAdapterByTagName(ePrereq
101: .getName());
102: Fact premisse = (Fact) adapter4Prereqs.importObject(
103: ePrereq, driver, cache, lfactory);
104: body.add(premisse);
105:
106: }
107: // get head
108: Element eHead = e.getChild(HEAD);
109: Fact head = (Fact) importChild(eHead, GenericDriver.FACT,
110: driver, cache, lfactory);
111:
112: // build rule
113: Rule r = lfactory.createRule(body, head, isOrConnected);
114: loadProperties(e, r);
115: return r;
116: }
117:
118: /**
119: * Get the name of the associated tag (element).
120: * @return a string
121: */
122: public String getTagName() {
123: return RULE;
124: }
125:
126: /**
127: * Get the kind of object the adapter can export/import.
128: * @return a string
129: */
130: public String getKindOfObject() {
131: return GenericDriver.RULE;
132: }
133: }
|