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.LogicFactory;
023: import org.mandarax.kernel.Predicate;
024: import org.mandarax.kernel.Prerequisite;
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 prerequisites.
031: * @see org.mandarax.kernel.Prerequisite
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 Adapter4Prerequisites_1_1 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, Prerequisite.class);
050: Prerequisite prereq = (Prerequisite) obj;
051: Element e = new Element(PREREQUISITE);
052: // negation
053: e.setAttribute(NAF, prereq.isNegatedAF() ? Boolean
054: .toString(true) : Boolean.toString(false));
055: // add predicate
056: Predicate p = prereq.getPredicate();
057: Element ep = exportObject(p, PREDICATE, driver, ops);
058: e.addContent(ep);
059: // add terms
060: Term[] terms = prereq.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, prereq);
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: String negatedAsString = e.getAttributeValue(NAF);
085: boolean negated = negatedAsString != null
086: && negatedAsString.equals(Boolean.toString(true));
087: Element ePredicate = e.getChild(PREDICATE);
088: Adapter adapter = driver.getAdapter(PREDICATE);
089: Predicate predicate = (Predicate) adapter.importObject(
090: ePredicate, driver, ops, lfactory);
091: // get the terms
092: Element eTerm = (Element) e.getChild(TERMS);
093: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
094: ops, lfactory, Term.class);
095: // assemble prerequisite
096: Prerequisite prereq = lfactory.createPrerequisite(predicate,
097: terms, negated);
098: importProperties(e, prereq);
099: return prereq;
100: }
101:
102: /**
103: * Get the name of the associated tag (element).
104: * @return a string
105: */
106: public String getTagName() {
107: return PREREQUISITE;
108: }
109:
110: /**
111: * Get the type of the term, e.g. GenericDriver.COMPLEX_TERM or GenericDriver.VARIABLE_TERM.
112: * @param t a term
113: * @return a string
114: */
115: private String getTermType(Term t) {
116: if (t instanceof org.mandarax.kernel.VariableTerm)
117: return VARIABLE_TERM;
118: if (t instanceof org.mandarax.kernel.ConstantTerm)
119: return CONSTANT_TERM;
120: if (t instanceof org.mandarax.kernel.ComplexTerm)
121: return COMPLEX_TERM;
122: return null;
123: }
124:
125: /**
126: * Print the DTD associated with this adapter on a string buffer.
127: * @param out the buffer to print on.
128: */
129: public void printDTD(StringBuffer out) {
130: out.append("<!ELEMENT prereq (predicate,terms,properties?)>\n");
131: out.append("<!ATTLIST prereq naf (true | false) \"false\">\n");
132: }
133: }
|