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 java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.jdom.Element;
026: import org.mandarax.kernel.DerivationEventListener;
027: import org.mandarax.kernel.Fact;
028: import org.mandarax.kernel.LogicFactory;
029: import org.mandarax.kernel.Query;
030: import org.mandarax.zkb.ObjectPersistencyService;
031: import org.mandarax.zkb.ZKBException;
032:
033: /**
034: * An adapter class for queries.
035: * @see org.mandarax.kernel.Query
036: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037: * @version 3.4 <7 March 05>
038: * @since 2.3
039: */
040:
041: public class Adapter4Queries_1_1 extends AbstractAdapter {
042:
043: /**
044: * Export an object, i.e., convert it to an element in the DOM.
045: * @param obj an object
046: * @param driver the generic driver
047: * @param ops the object persistency service
048: * @exception a XKBException is thrown if export fails
049: */
050: public Element exportObject(Object obj, GenericDriver driver,
051: ObjectPersistencyService ops) throws ZKBException {
052: check(obj, Query.class);
053: Query query = (Query) obj;
054: Element e = new Element(QUERY);
055: String queryName = query.getName();
056: if (queryName == null)
057: queryName = "";
058: e.setAttribute(NAME, queryName);
059: Fact[] queryFacts = query.getFacts();
060: for (int i = 0; i < queryFacts.length; i++) {
061: Element el = exportObject(queryFacts[i],
062: TagAndAttributeNames.FACT, driver, ops);
063: e.addContent(el);
064: }
065: exportProperties(e, query);
066: // new in 3.2 : add derivation event listeners
067: DerivationEventListener[] listeners = query
068: .getDerivationEventListeners();
069: if (listeners != null) {
070: for (int i = 0; i < listeners.length; i++) {
071: Element el = exportObject(listeners[i],
072: TagAndAttributeNames.DERIVATION_LISTENER,
073: driver, ops);
074: e.addContent(el);
075: }
076: }
077: return e;
078: }
079:
080: /**
081: * Build an object from an XML element.
082: * @param e an element
083: * @param driver the generic driver
084: * @param ops the object persistency service
085: * @param lfactory the logic factory used to create objects
086: * @exception a XKBException is thrown if import fails
087: */
088: public Object importObject(Element e, GenericDriver driver,
089: ObjectPersistencyService ops, LogicFactory lfactory)
090: throws ZKBException {
091: List list = new ArrayList();
092: Adapter adapter4Facts = null;
093: List children = e.getChildren(FACT);
094: for (Iterator it = children.iterator(); it.hasNext();) {
095: Element eFact = (Element) it.next();
096: if (adapter4Facts == null)
097: adapter4Facts = driver.getAdapter(eFact.getName());
098: Fact queryFact = (Fact) adapter4Facts.importObject(eFact,
099: driver, ops, lfactory);
100: list.add(queryFact);
101: }
102: Fact[] facts = new Fact[list.size()];
103: for (int i = 0; i < list.size(); i++)
104: facts[i] = (Fact) list.get(i);
105: String queryName = e.getAttributeValue(NAME);
106:
107: // new in 3.2 support for derivation listeners
108: list.clear();
109: children = e.getChildren(DERIVATION_LISTENER);
110: Adapter adapter4Listeners = null;
111: for (Iterator it = children.iterator(); it.hasNext();) {
112: Element eListeners = (Element) it.next();
113: if (adapter4Listeners == null)
114: adapter4Listeners = driver.getAdapter(eListeners
115: .getName());
116: DerivationEventListener l = (DerivationEventListener) adapter4Listeners
117: .importObject(eListeners, driver, ops, lfactory);
118: if (l == null)
119: LOG_ZKB
120: .warn("Problem importing derivation event listeners, check sourcecode and classpath");
121: else
122: list.add(l);
123: }
124: DerivationEventListener[] listeners = new DerivationEventListener[list
125: .size()];
126: for (int i = 0; i < list.size(); i++)
127: listeners[i] = (DerivationEventListener) list.get(i);
128:
129: // build query
130: Query q = lfactory.createQuery(facts, queryName == null ? ""
131: : queryName);
132: if (queryName == null)
133: q.setName(q.toString());
134: q.setDerivationEventListeners(listeners);
135: importProperties(e, q);
136: return q;
137: }
138:
139: /**
140: * Get the name of the associated tag (element).
141: * @return a string
142: */
143: public String getTagName() {
144: return QUERY;
145: }
146:
147: /**
148: * Print the DTD associated with this adapter on a string buffer.
149: * @param out the buffer to print on.
150: */
151: public void printDTD(StringBuffer out) {
152: out.append("<!ELEMENT query (atom*,properties?)>\n");
153: out.append("<!ATTLIST query name CDATA #REQUIRED>\n");
154: }
155:
156: }
|