001: package org.mandarax.xkb.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 java.util.Map;
022:
023: import org.jdom.Element;
024: import org.mandarax.kernel.Fact;
025: import org.mandarax.kernel.LogicFactory;
026: import org.mandarax.kernel.Predicate;
027: import org.mandarax.kernel.Prerequisite;
028: import org.mandarax.kernel.Term;
029: import org.mandarax.lib.Cut;
030: import org.mandarax.xkb.XKBException;
031:
032: /**
033: * An adapter class for prerequisites.
034: * @see org.mandarax.kernel.Prerequisite
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 2.0
038: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
039: */
040:
041: public class XMLAdapter4Prerequisites extends AbstractXMLAdapter {
042: public static final String TERMS = "terms";
043: public static final String PREREQUISITE = "prereq";
044: public static final String OPERATOR = "_opr";
045: public static final String NEGATED_AF = "neg_af";
046:
047: /**
048: * Export an object, i.e., convert it to an element in the DOM.
049: * @param obj an object
050: * @param driver the generic driver
051: * @param cache a cache used in order to associate the same
052: * id with various occurences of the same object
053: * @exception an XKBException is thrown if export fails
054: */
055: public Element exportObject(Object obj, GenericDriver driver,
056: Map cache) throws XKBException {
057: Prerequisite prereq = null;
058: // extra support for versions prior 2.0: old version have used Fact in rule bodies,
059: // we support this but warn - if obj is a Fact, convert it to a prerequisite
060: if (obj instanceof Fact) {
061: if (obj instanceof Prerequisite) {
062: prereq = (Prerequisite) obj;
063: } else {
064: LOG_XKB
065: .warn("Exporting fact from rule body - rule bodies should contain instances of Prerequisite "
066: + obj);
067: Fact fact = (Fact) obj;
068: prereq = lfactory.createPrerequisite(fact
069: .getPredicate(), fact.getTerms(), false);
070: }
071: } else
072: check(obj, Prerequisite.class);
073: Element e = new Element(PREREQUISITE);
074: e
075: .setAttribute(NEGATED_AF, String.valueOf(prereq
076: .isNegatedAF()));
077: // add predicate
078: Predicate p = prereq.getPredicate();
079: Element eo = new Element(OPERATOR);
080: e.addContent(eo);
081: Element ep = exportObject(p, getPredicateType(p), driver, cache);
082: eo.addContent(ep);
083: // add terms
084: Term[] terms = prereq.getTerms();
085: Element eTerms = new Element(TERMS);
086: e.addContent(eTerms);
087: for (int i = 0; i < terms.length; i++) {
088: Element et = exportObject(terms[i], getTermType(terms[i]),
089: driver, cache);
090: eTerms.addContent(et);
091: }
092: addProperties(e, prereq);
093: return e;
094: }
095:
096: /**
097: * Build an object from an XML element.
098: * @param e an element
099: * @param driver the generic driver
100: * @param cache a cache used to identify objects that have the same id
101: * @param lfactory the logic factory used to create objects
102: * @exception an XKBException is thrown if import fails
103: */
104: public Object importObject(Element e, GenericDriver driver,
105: Map cache, LogicFactory lfactory) throws XKBException {
106: // the first element is predicate
107: Element eOperator = e.getChild(OPERATOR);
108: Element ePredicate = (Element) eOperator.getChildren().get(0);
109: XMLAdapter adapter = driver.getAdapterByTagName(ePredicate
110: .getName());
111: Predicate predicate = (Predicate) adapter.importObject(
112: ePredicate, driver, cache, lfactory);
113: // special handling for cut
114: if (predicate instanceof Cut)
115: return lfactory.createCut();
116:
117: // get the terms
118: Element eTerm = (Element) e.getChild(TERMS);
119: Term[] terms = (Term[]) importChildren(eTerm, null, driver,
120: cache, lfactory, Term.class);
121: // negation
122: boolean neg = "true".equals(e.getAttributeValue(NEGATED_AF));
123: // assemble prerequisite
124: Prerequisite prereq = lfactory.createPrerequisite(predicate,
125: terms, neg);
126: loadProperties(e, prereq);
127: return prereq;
128: }
129:
130: /**
131: * Get the name of the associated tag (element).
132: * @return a string
133: */
134: public String getTagName() {
135: return PREREQUISITE;
136: }
137:
138: /**
139: * Get the kind of object the adapter can export/import.
140: * @return a string
141: */
142: public String getKindOfObject() {
143: return GenericDriver.PREREQUISITE;
144: }
145:
146: }
|