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 org.jdom.Element;
022: import org.mandarax.kernel.Fact;
023: import org.mandarax.kernel.LogicFactory;
024: import org.mandarax.kernel.Predicate;
025: import org.mandarax.kernel.Term;
026: import org.mandarax.zkb.ObjectPersistencyService;
027: import org.mandarax.zkb.ZKBException;
028:
029: /**
030: * An adapter class for facts.
031: * @see org.mandarax.kernel.Fact
032: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
033: * @version 3.4 <7 March 05>
034: * @since 2.3
035: */
036:
037: public class Adapter4Facts_1_1 extends AbstractAdapter {
038:
039: /**
040: * Export an object, i.e., convert it to an element in the DOM.
041: * @param obj an object
042: * @param driver the generic driver
043: * @param ops the object persistency service
044: * @exception a ZKBException is thrown if export fails
045: */
046: public Element exportObject(Object obj, GenericDriver driver,
047: ObjectPersistencyService ops) throws ZKBException {
048: check(obj, Fact.class);
049: Fact fact = (Fact) obj;
050: Element e = new Element(FACT);
051: // add predicate
052: Predicate p = fact.getPredicate();
053: Element ep = exportObject(p, PREDICATE, driver, ops);
054: e.addContent(ep);
055: // add terms
056: Term[] terms = fact.getTerms();
057: Element eTerms = new Element(TERMS);
058: e.addContent(eTerms);
059: for (int i = 0; i < terms.length; i++) {
060: Element et = exportObject(terms[i], getTermType(terms[i]),
061: driver, ops);
062: eTerms.addContent(et);
063: }
064: exportProperties(e, fact);
065: return e;
066: }
067:
068: /**
069: * Build an object from an XML element.
070: * @param e an element
071: * @param driver the generic driver
072: * @param ops the object persistency service
073: * @param lfactory the logic factory used to create objects
074: * @exception a ZKBException is thrown if import fails
075: */
076: public Object importObject(Element e, GenericDriver driver,
077: ObjectPersistencyService ops, LogicFactory lfactory)
078: throws ZKBException {
079: // the first element is predicate
080: Element ePredicate = e.getChild(PREDICATE);
081: Adapter adapter = driver.getAdapter(PREDICATE);
082: Predicate predicate = (Predicate) adapter.importObject(
083: ePredicate, driver, ops, lfactory);
084: // get the terms
085: Element eTerm = (Element) e.getChild(TERMS);
086: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
087: ops, lfactory, Term.class);
088: // assemble fact
089: Fact fact = lfactory.createFact(predicate, terms);
090: importProperties(e, fact);
091: return fact;
092: }
093:
094: /**
095: * Get the name of the associated tag (element).
096: * @return a string
097: */
098: public String getTagName() {
099: return FACT;
100: }
101:
102: /**
103: * Get the type of the term, e.g. GenericDriver.COMPLEX_TERM or GenericDriver.VARIABLE_TERM.
104: * @param t a term
105: * @return a string
106: */
107: private String getTermType(Term t) {
108: if (t instanceof org.mandarax.kernel.VariableTerm)
109: return VARIABLE_TERM;
110: if (t instanceof org.mandarax.kernel.ConstantTerm)
111: return CONSTANT_TERM;
112: if (t instanceof org.mandarax.kernel.ComplexTerm)
113: return COMPLEX_TERM;
114: return null;
115: }
116:
117: /**
118: * Print the DTD associated with this adapter on a string buffer.
119: * @param out the buffer to print on.
120: */
121: public void printDTD(StringBuffer out) {
122: out.append("<!ELEMENT atom (predicate,terms,properties?)>\n");
123: out.append("<!ELEMENT terms (var|ind|complex_term)*>\n");
124: }
125: }
|