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.
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 1.6
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 XMLAdapter4Rules 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(), GenericDriver.FACT,
068: 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: return e;
077: }
078:
079: /**
080: * Build an object from an XML element.
081: * @param e an element
082: * @param driver the generic driver
083: * @param cache a cache used to identify objects that have the same id
084: * @param lfactory the logic factory used to create objects
085: * @exception an XKBException is thrown if import fails
086: */
087: public Object importObject(Element e, GenericDriver driver,
088: Map cache, LogicFactory lfactory) throws XKBException {
089: // get body
090: Element eBody = e.getChild(BODY);
091: Element eConn = (Element) eBody.getChildren().get(0);
092: boolean isOrConnected = OR.equals(eConn.getName());
093: List body = new Vector();
094: XMLAdapter adapter4Facts = null;
095: List children = eConn.getChildren();
096: for (Iterator it = children.iterator(); it.hasNext();) {
097: Element eFact = (Element) it.next();
098: if (adapter4Facts == null)
099: adapter4Facts = driver.getAdapterByTagName(eFact
100: .getName());
101: Fact premisse = (Fact) adapter4Facts.importObject(eFact,
102: driver, cache, lfactory);
103: body.add(premisse);
104:
105: }
106: // get head
107: Element eHead = e.getChild(HEAD);
108: Fact head = (Fact) importChild(eHead, GenericDriver.FACT,
109: driver, cache, lfactory);
110:
111: // build rule
112: return lfactory.createRule(body, head, isOrConnected);
113: }
114:
115: /**
116: * Get the name of the associated tag (element).
117: * @return a string
118: */
119: public String getTagName() {
120: return RULE;
121: }
122:
123: /**
124: * Get the kind of object the adapter can export/import.
125: * @return a string
126: */
127: public String getKindOfObject() {
128: return GenericDriver.RULE;
129: }
130: }
|