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