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.io.PrintWriter;
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.jdom.Comment;
028: import org.jdom.Element;
029: import org.mandarax.kernel.ClauseSet;
030: import org.mandarax.kernel.ExtendedKnowledgeBase;
031: import org.mandarax.kernel.Fact;
032: import org.mandarax.kernel.KnowledgeBase;
033: import org.mandarax.kernel.LogicFactory;
034: import org.mandarax.kernel.Query;
035: import org.mandarax.kernel.Rule;
036: import org.mandarax.sql.SQLClauseSet;
037: import org.mandarax.xkb.XKBException;
038:
039: /**
040: * An adapter class for knowledge bases. Supports comparators.
041: * @see org.mandarax.kernel.KnowledgeBase
042: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
043: * @version 3.4 <7 March 05>
044: * @since 2.1
045: * @deprecated from v 3.4 - support for new features such as validation will not be added to XKB, please use ZKB instead
046: */
047:
048: public class XMLAdapter4KnowledgeBases2 extends AbstractXMLAdapter {
049: public static final String KNOWLEDGE_BASE = "knowledge_base";
050:
051: /**
052: * Export an object, i.e., convert it to an element in the DOM.
053: * @param obj an object
054: * @param driver the generic driver
055: * @param cache a cache used in order to associate the same
056: * id with various occurences of the same object
057: * @exception an XKBException is thrown if export fails
058: */
059: public Element exportObject(Object obj, GenericDriver driver,
060: Map cache) throws XKBException {
061: check(obj, KnowledgeBase.class);
062: int ftl = driver.getFaultToleranceLevel();
063: PrintWriter out = driver.getLogWriter();
064: KnowledgeBase kb = (KnowledgeBase) obj;
065: Element e = new Element(KNOWLEDGE_BASE);
066: List clauseSets = kb.getClauseSets();
067:
068: // export comparator
069: Comparator comp = null;
070: if (kb instanceof ExtendedKnowledgeBase
071: && (comp = ((ExtendedKnowledgeBase) kb).getComparator()) != null) {
072: Element eComp = exportObject(comp,
073: GenericDriver.COMPARATOR, driver, cache);
074: e.addContent(eComp);
075: }
076:
077: // export knowledge
078: for (Iterator it = clauseSets.iterator(); it.hasNext();) {
079: ClauseSet cs = (ClauseSet) it.next();
080: String kindOfObject = getClauseSetType(cs);
081: if (kindOfObject == null) {
082: String msg = "Cannot figure out what kind of clause set the following object is: "
083: + cs;
084: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
085: throw new XKBException(msg);
086: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
087: else {
088: out.println("Warning");
089: out.println(msg);
090: out.println("The clause is ignored");
091: LOG_XKB.warn(msg);
092: LOG_XKB.warn("The clause is ignored");
093: }
094: } else {
095: try {
096: Element el = exportObject(cs, kindOfObject, driver,
097: cache);
098: e.addContent(el);
099: } catch (XKBException x) {
100: String msg = "Problems exporting the knowledge base, export failed for "
101: + cs;
102: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
103: throw new XKBException(msg);
104: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
105: else {
106: out.println(msg);
107: out.println("See log for details");
108: e.addContent(new Comment(msg));
109: LOG_XKB.error(msg, x);
110: }
111: }
112:
113: }
114: }
115: // export queries
116: for (Iterator it = kb.queryNames(); it.hasNext();) {
117: Query q = kb.getQuery((String) it.next());
118: try {
119: Element el = exportObject(q, GenericDriver.QUERY,
120: driver, cache);
121: e.addContent(el);
122: } catch (XKBException x) {
123: String msg = "Problems exporting the knowledge base, export failed for query "
124: + q;
125: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
126: throw new XKBException(msg);
127: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
128: else {
129: out.println(msg);
130: out.println("See log for details");
131: e.addContent(new Comment(msg));
132: LOG_XKB.error(msg, x);
133: }
134: }
135: }
136: return e;
137: }
138:
139: /**
140: * Get the type of clause set, e.g. GenericDriver.FACT or GenericDriver.RULE.
141: * @param cs a clause set
142: * @return a string
143: */
144: protected String getClauseSetType(ClauseSet cs) {
145: if (cs instanceof Rule)
146: return GenericDriver.RULE;
147: if (cs instanceof Fact)
148: return GenericDriver.FACT;
149: if (cs instanceof SQLClauseSet)
150: return GenericDriver.SQL_CLAUSE_SET;
151: return null;
152: }
153:
154: /**
155: * Build an object from an XML element.
156: * @param e an element
157: * @param driver the generic driver
158: * @param cache a cache used to identify objects that have the same id
159: * @param lfactory the logic factory used to create objects
160: * @exception an XKBException is thrown if export fails
161: */
162: public Object importObject(Element e, GenericDriver driver,
163: Map cache, LogicFactory lfactory) throws XKBException {
164: KnowledgeBase kb = new org.mandarax.reference.AdvancedKnowledgeBase();
165: int ftl = driver.getFaultToleranceLevel();
166: PrintWriter out = driver.getLogWriter();
167: List children = e.getChildren();
168: for (Iterator it = children.iterator(); it.hasNext();) {
169: Element child = (Element) it.next();
170:
171: if (child.getName().equals(GenericDriver.COMPARATOR)) {
172: try {
173: Object obj = driver.getAdapterByTagName(
174: GenericDriver.COMPARATOR).importObject(
175: child, driver, cache, lfactory);
176: if (obj != null)
177: ((ExtendedKnowledgeBase) kb)
178: .setComparator((Comparator) obj);
179: } catch (XKBException x) {
180: String msg = "Problems importing the comparator, root cause: "
181: + x.getMessage();
182: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
183: throw new XKBException(msg);
184: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
185: else {
186: out.println(msg);
187: out.println("See log for details");
188: LOG_XKB.error(msg, x);
189: }
190: }
191: } else {
192: XMLAdapter adapter = driver.getAdapterByTagName(child
193: .getName());
194: try {
195: Object obj = adapter.importObject(child, driver,
196: cache, lfactory);
197: if (obj instanceof ClauseSet)
198: kb.add((ClauseSet) obj);
199: else if (obj instanceof Query)
200: kb.addQuery((Query) obj);
201: else
202: throw new XKBException(
203: "Can only import clause sets and queries into repository, import for object failed: "
204: + obj);
205: } catch (XKBException x) {
206: String msg = "Problems importing the knowledge base, root cause: "
207: + x.getMessage();
208: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
209: throw new XKBException(msg);
210: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
211: else {
212: out.println(msg);
213: out.println("See log for details");
214: LOG_XKB.error(msg, x);
215: }
216: }
217: }
218: }
219: return kb;
220: }
221:
222: /**
223: * Get the name of the associated tag (element).
224: * @return a string
225: */
226: public String getTagName() {
227: return KNOWLEDGE_BASE;
228: }
229:
230: /**
231: * Get the kind of object the adapter can export/import.
232: * @return a string
233: */
234: public String getKindOfObject() {
235: return GenericDriver.KNOWLEDGE_BASE;
236: }
237: }
|