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.ComplexTerm;
023: import org.mandarax.kernel.Function;
024: import org.mandarax.kernel.LogicFactory;
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 complex terms.
031: * @see org.mandarax.kernel.ComplexTerm
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.2
035: */
036:
037: public class Adapter4ComplexTerms extends AbstractAdapter {
038: public static final String TERMS = "terms";
039:
040: /**
041: * Export an object, i.e., convert it to an element in the DOM.
042: * @param obj an object
043: * @param driver the generic driver
044: * @param ops the object persistency service
045: * @exception a ZKBException is thrown if export fails
046: */
047: public Element exportObject(Object obj, GenericDriver driver,
048: ObjectPersistencyService ops) throws ZKBException {
049: check(obj, ComplexTerm.class);
050: ComplexTerm term = (ComplexTerm) obj;
051: Element e = new Element(COMPLEX_TERM);
052: this .setTypeAttribute(e, term);
053: // add predicate
054: Function f = term.getFunction();
055: Element ef = exportObject(f, FUNCTION, driver, ops);
056: e.addContent(ef);
057: // add terms
058: Term[] terms = term.getTerms();
059: Element eTerms = new Element(TERMS);
060: e.addContent(eTerms);
061: for (int i = 0; i < terms.length; i++) {
062: Element et = exportObject(terms[i], getTermType(terms[i]),
063: driver, ops);
064: eTerms.addContent(et);
065: }
066: return e;
067: }
068:
069: /**
070: * Get the type of the term, e.g. GenericDriver.COMPLEX_TERM or GenericDriver.VARIABLE_TERM.
071: * @param t a term
072: * @return a string
073: */
074: private String getTermType(Term t) {
075: if (t instanceof org.mandarax.kernel.VariableTerm)
076: return VARIABLE_TERM;
077: if (t instanceof org.mandarax.kernel.ConstantTerm)
078: return CONSTANT_TERM;
079: if (t instanceof org.mandarax.kernel.ComplexTerm)
080: return COMPLEX_TERM;
081: return null;
082: }
083:
084: /**
085: * Build an object from an XML element.
086: * @param e an element
087: * @param driver the generic driver
088: * @param ops the object persistency service
089: * @param lfactory the logic factory used to create objects
090: * @exception a ZKBException is thrown if import fails
091: */
092: public Object importObject(Element e, GenericDriver driver,
093: ObjectPersistencyService ops, LogicFactory lfactory)
094: throws ZKBException {
095: // the first element is functions
096: Element eFunction = (Element) e.getChildren().get(0);
097: Adapter adapter = driver.getAdapter(eFunction.getName());
098: Function function = (Function) adapter.importObject(eFunction,
099: driver, ops, lfactory);
100: // get the terms
101: Element eTerm = (Element) e.getChild(TERMS);
102: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
103: ops, lfactory, Term.class);
104: return lfactory.createComplexTerm(function, terms);
105:
106: }
107:
108: /**
109: * Get the name of the associated tag (element).
110: * @return a string
111: */
112: public String getTagName() {
113: return COMPLEX_TERM;
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: out.append("<!ELEMENT complex_term (function,terms)>\n");
122: out.append("<!ATTLIST complex_term type CDATA #IMPLIED>\n");
123:
124: }
125:
126: }
|