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.2
035: */
036:
037: public class Adapter4Prerequisites 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, Prerequisite.class);
051: Prerequisite prereq = (Prerequisite) obj;
052: Element e = new Element(PREREQUISITE);
053: // negation
054: e.setAttribute(NAF, prereq.isNegatedAF() ? Boolean
055: .toString(true) : Boolean.toString(false));
056: // add predicate
057: Predicate p = prereq.getPredicate();
058: Element eo = new Element(OPERATOR);
059: e.addContent(eo);
060: Element ep = exportObject(p, PREDICATE, driver, ops);
061: eo.addContent(ep);
062: // add terms
063: Term[] terms = prereq.getTerms();
064: Element eTerms = new Element(TERMS);
065: e.addContent(eTerms);
066: for (int i = 0; i < terms.length; i++) {
067: Element et = exportObject(terms[i], getTermType(terms[i]),
068: driver, ops);
069: eTerms.addContent(et);
070: }
071: exportProperties(e, prereq);
072: return e;
073: }
074:
075: /**
076: * Build an object from an XML element.
077: * @param e an element
078: * @param driver the generic driver
079: * @param ops the object persistency service
080: * @param lfactory the logic factory used to create objects
081: * @exception a ZKBException is thrown if import fails
082: */
083: public Object importObject(Element e, GenericDriver driver,
084: ObjectPersistencyService ops, LogicFactory lfactory)
085: throws ZKBException {
086: // the first element is predicate
087: Element eOperator = e.getChild(OPERATOR);
088: String negatedAsString = e.getAttributeValue(NAF);
089: boolean negated = negatedAsString != null
090: && negatedAsString.equals(Boolean.toString(true));
091: Element ePredicate = (Element) eOperator.getChildren().get(0);
092: Adapter adapter = driver.getAdapter(PREDICATE);
093: Predicate predicate = (Predicate) adapter.importObject(
094: ePredicate, driver, ops, lfactory);
095: // get the terms
096: Element eTerm = (Element) e.getChild(TERMS);
097: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
098: ops, lfactory, Term.class);
099: // assemble prerequisite
100: Prerequisite prereq = lfactory.createPrerequisite(predicate,
101: terms, negated);
102: importProperties(e, prereq);
103: return prereq;
104: }
105:
106: /**
107: * Get the name of the associated tag (element).
108: * @return a string
109: */
110: public String getTagName() {
111: return PREREQUISITE;
112: }
113:
114: /**
115: * Get the type of the term, e.g. GenericDriver.COMPLEX_TERM or GenericDriver.VARIABLE_TERM.
116: * @param t a term
117: * @return a string
118: */
119: private String getTermType(Term t) {
120: if (t instanceof org.mandarax.kernel.VariableTerm)
121: return VARIABLE_TERM;
122: if (t instanceof org.mandarax.kernel.ConstantTerm)
123: return CONSTANT_TERM;
124: if (t instanceof org.mandarax.kernel.ComplexTerm)
125: return COMPLEX_TERM;
126: return null;
127: }
128:
129: /**
130: * Print the DTD associated with this adapter on a string buffer.
131: * @param out the buffer to print on.
132: */
133: public void printDTD(StringBuffer out) {
134: out.append("<!ELEMENT prereq (_opr,terms,properties?)>\n");
135: out.append("<!ATTLIST prereq naf (true | false) \"false\">\n");
136: out.append("<!ELEMENT _opr (predicate)>\n");
137: }
138: }
|