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.Map;
022:
023: import org.jdom.Element;
024: import org.mandarax.kernel.Fact;
025: import org.mandarax.kernel.LogicFactory;
026: import org.mandarax.kernel.Predicate;
027: import org.mandarax.kernel.Term;
028: import org.mandarax.xkb.XKBException;
029:
030: /**
031: * An adapter class for facts. New is the support for additional properties.
032: * @see org.mandarax.kernel.Fact
033: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
034: * @version 3.4 <7 March 05>
035: * @since 2.0
036: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
037: */
038:
039: public class XMLAdapter4Facts2 extends AbstractXMLAdapter {
040: public static final String TERMS = "terms";
041: public static final String FACT = "atom";
042: public static final String OPERATOR = "_opr";
043:
044: /**
045: * Export an object, i.e., convert it to an element in the DOM.
046: * @param obj an object
047: * @param driver the generic driver
048: * @param cache a cache used in order to associate the same
049: * id with various occurences of the same object
050: * @exception an XKBException is thrown if export fails
051: */
052: public Element exportObject(Object obj, GenericDriver driver,
053: Map cache) throws XKBException {
054: check(obj, Fact.class);
055: Fact fact = (Fact) obj;
056: Element e = new Element(FACT);
057: // add predicate
058: Predicate p = fact.getPredicate();
059: Element eo = new Element(OPERATOR);
060: e.addContent(eo);
061: Element ep = exportObject(p, getPredicateType(p), driver, cache);
062: eo.addContent(ep);
063: // add terms
064: Term[] terms = fact.getTerms();
065: Element eTerms = new Element(TERMS);
066: e.addContent(eTerms);
067: for (int i = 0; i < terms.length; i++) {
068: Element et = exportObject(terms[i], getTermType(terms[i]),
069: driver, cache);
070: eTerms.addContent(et);
071: }
072: addProperties(e, fact);
073: return e;
074: }
075:
076: /**
077: * Build an object from an XML element.
078: * @param e an element
079: * @param driver the generic driver
080: * @param cache a cache used to identify objects that have the same id
081: * @param lfactory the logic factory used to create objects
082: * @exception an XKBException is thrown if import fails
083: */
084: public Object importObject(Element e, GenericDriver driver,
085: Map cache, LogicFactory lfactory) throws XKBException {
086: // the first element is predicate
087: Element eOperator = e.getChild(OPERATOR);
088: Element ePredicate = (Element) eOperator.getChildren().get(0);
089: XMLAdapter adapter = driver.getAdapterByTagName(ePredicate
090: .getName());
091: Predicate predicate = (Predicate) adapter.importObject(
092: ePredicate, driver, cache, lfactory);
093: // get the terms
094: Element eTerm = (Element) e.getChild(TERMS);
095: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
096: cache, lfactory, Term.class);
097: // assemble fact
098: Fact f = lfactory.createFact(predicate, terms);
099: loadProperties(e, f);
100: return f;
101: }
102:
103: /**
104: * Get the name of the associated tag (element).
105: * @return a string
106: */
107: public String getTagName() {
108: return FACT;
109: }
110:
111: /**
112: * Get the kind of object the adapter can export/import.
113: * @return a string
114: */
115: public String getKindOfObject() {
116: return GenericDriver.FACT;
117: }
118:
119: }
|