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.io.PrintWriter;
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.List;
025: import org.jdom.Comment;
026: import org.jdom.Element;
027: import org.mandarax.kernel.ClauseSet;
028: import org.mandarax.kernel.ExtendedKnowledgeBase;
029: import org.mandarax.kernel.Fact;
030: import org.mandarax.kernel.KnowledgeBase;
031: import org.mandarax.kernel.LogicFactory;
032: import org.mandarax.kernel.Query;
033: import org.mandarax.kernel.Rule;
034: import org.mandarax.sql.SQLClauseSet;
035: import org.mandarax.zkb.ObjectPersistencyService;
036: import org.mandarax.zkb.ZKBException;
037: import org.mandarax.kernel.validation.*;
038:
039: /**
040: * An adapter class for knowledge bases.
041: * New in this driver is the support for test cases.
042: * @see org.mandarax.kernel.KnowledgeBase
043: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
044: * @version 3.4 <7 March 05>
045: * @since 3.4
046: */
047:
048: public class Adapter4KnowledgeBases_2_0 extends AbstractAdapter {
049:
050: // constant for comparator attribute
051:
052: /**
053: * Export an object, i.e., convert it to an element in the DOM.
054: * @param obj an object
055: * @param driver the generic driver
056: * @param ops the object persistency service
057: * @exception a ZKBException is thrown if export fails
058: */
059: public Element exportObject(Object obj, GenericDriver driver,
060: ObjectPersistencyService ops) throws ZKBException {
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:
067: e.setAttribute(CLASS, kb.getClass().getName());
068:
069: // export comparator
070: if (kb instanceof ExtendedKnowledgeBase) {
071: Comparator comp = ((ExtendedKnowledgeBase) kb)
072: .getComparator();
073: if (comp != null) {
074: e
075: .addContent(exportObject(comp, COMPARATOR,
076: driver, ops));
077: }
078: }
079:
080: // export knowledge
081: List clauseSets = kb.getClauseSets();
082: for (Iterator it = clauseSets.iterator(); it.hasNext();) {
083: ClauseSet cs = (ClauseSet) it.next();
084: String kindOfObject = getClauseSetType(cs);
085: if (kindOfObject == null) {
086: String msg = "Cannot figure out what kind of clause set the following object is: "
087: + cs;
088: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
089: throw new ZKBException(msg);
090: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
091: else {
092: out.println("Warning");
093: out.println(msg);
094: out.println("The clause is ignored");
095: LOG_XKB.warn(msg);
096: LOG_XKB.warn("The clause is ignored");
097: }
098: } else {
099: try {
100: Element el = exportObject(cs, kindOfObject, driver,
101: ops);
102: e.addContent(el);
103: } catch (ZKBException x) {
104: String msg = "Problems exporting the knowledge base, export failed for "
105: + cs;
106: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
107: throw new ZKBException(msg);
108: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
109: else {
110: if (out != null) {
111: out.println(msg);
112: out.println("See log for details");
113: }
114: e.addContent(new Comment(msg));
115: LOG_XKB.error(msg, x);
116: }
117: }
118:
119: }
120: }
121: // export queries
122: for (Iterator it = kb.queryNames(); it.hasNext();) {
123: Query q = kb.getQuery((String) it.next());
124: try {
125: Element el = exportObject(q, GenericDriver.QUERY,
126: driver, ops);
127: e.addContent(el);
128: } catch (ZKBException x) {
129: String msg = "Problems exporting the knowledge base, export failed for query "
130: + q;
131: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
132: throw new ZKBException(msg);
133: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
134: else {
135: out.println(msg);
136: out.println("See log for details");
137: e.addContent(new Comment(msg));
138: LOG_XKB.error(msg, x);
139: }
140: }
141: }
142:
143: // export test cases
144: for (Iterator it = kb.testcases(); it.hasNext();) {
145: TestCase tc = (TestCase) it.next();
146: try {
147: Element el = exportObject(tc, GenericDriver.TESTCASE,
148: driver, ops);
149: e.addContent(el);
150: } catch (ZKBException x) {
151: String msg = "Problems exporting the knowledge base, export failed for test case "
152: + tc;
153: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
154: throw new ZKBException(msg);
155: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
156: else {
157: out.println(msg);
158: out.println("See log for details");
159: e.addContent(new Comment(msg));
160: LOG_XKB.error(msg, x);
161: }
162: }
163: }
164:
165: return e;
166: }
167:
168: /**
169: * Build an object from an XML element.
170: * @param e an element
171: * @param driver the generic driver
172: * @param ops the object persistency service
173: * @param lfactory the logic factory used to create objects
174: * @exception a ZKBException is thrown if export fails
175: */
176: public Object importObject(Element e, GenericDriver driver,
177: ObjectPersistencyService ops, LogicFactory lfactory)
178: throws ZKBException {
179: // try to instanciate saved class
180: KnowledgeBase kb = null;
181: String implClassName = e.getAttributeValue(CLASS);
182: if (implClassName != null) {
183: try {
184: Class implClass = Class.forName(implClassName);
185: kb = (KnowledgeBase) implClass.newInstance();
186: } catch (ClassNotFoundException x) {
187: LOG_ZKB.error("Cannot find class " + implClassName
188: + ", will use default", x);
189: } catch (IllegalAccessException x) {
190: LOG_ZKB.error("Cannot instanciate class "
191: + implClassName + ", will use default", x);
192: } catch (InstantiationException x) {
193: LOG_ZKB.error("Cannot instanciate class "
194: + implClassName + ", will use default", x);
195: }
196: }
197: if (kb == null)
198: kb = new org.mandarax.reference.AdvancedKnowledgeBase();
199:
200: int ftl = driver.getFaultToleranceLevel();
201: PrintWriter out = driver.getLogWriter();
202: List children = e.getChildren();
203: for (Iterator it = children.iterator(); it.hasNext();) {
204: Element child = (Element) it.next();
205: Adapter adapter = driver.getAdapter(child.getName());
206: if (child.getName().equals(COMPARATOR)) {
207: Comparator comp = (Comparator) adapter.importObject(
208: child, driver, ops, lfactory);
209: if (kb instanceof ExtendedKnowledgeBase)
210: ((ExtendedKnowledgeBase) kb).setComparator(comp);
211: } else {
212: try {
213: Object obj = adapter.importObject(child, driver,
214: ops, lfactory);
215: if (obj instanceof ClauseSet)
216: kb.add((ClauseSet) obj);
217: else if (obj instanceof Query)
218: kb.addQuery((Query) obj);
219: else if (obj instanceof TestCase)
220: kb.addTestCase((TestCase) obj);
221: else
222: throw new ZKBException(
223: "Can only import clause sets and queries into repository, import for object failed: "
224: + obj);
225: } catch (Exception x) {
226: String msg = "Problems importing the knowledge base, import failed for element "
227: + e.getText();
228: if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
229: throw new ZKBException(msg);
230: // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
231: else {
232: out.println(msg == null ? "null" : msg);
233: out.println("See log for details");
234: LOG_XKB.error(msg, x);
235: }
236: }
237: }
238: }
239: return kb;
240: }
241:
242: /**
243: * Get the name of the associated tag (element).
244: * @return a string
245: */
246: public String getTagName() {
247: return KNOWLEDGE_BASE;
248: }
249:
250: /**
251: * Get the kind of object the adapter can export/import.
252: * @return a string
253: */
254: public String getKindOfObject() {
255: return KNOWLEDGE_BASE;
256: }
257:
258: /**
259: * Print the DTD associated with this adapter on a string buffer.
260: * @param out the buffer to print on.
261: */
262: public void printDTD(StringBuffer out) {
263: out
264: .append("<!ELEMENT knowledgebase (comparator?,(atom|rule|sql_clause_set|custom_clause_set|query)*)>\n");
265: out.append("<!ATTLIST knowledgebase class CDATA #IMPLIED>\n");
266: }
267:
268: }
|