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.*;
022: import org.mandarax.kernel.validation.*;
023: import org.jdom.Element;
024: import org.mandarax.kernel.*;
025: import org.mandarax.zkb.ObjectPersistencyService;
026: import org.mandarax.zkb.ZKBException;
027:
028: /**
029: * An adapter class for test cases.
030: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
031: * @version 3.4 <7 March 05>
032: * @since 3.4
033: */
034:
035: public class Adapter4TestCases_2_0 extends AbstractAdapter {
036:
037: /**
038: * Export an object, i.e., convert it to an element in the DOM.
039: * @param obj an object
040: * @param driver the generic driver
041: * @param ops the object persistency service
042: * @exception a ZKBException is thrown if export fails
043: */
044: public Element exportObject(Object obj, GenericDriver driver,
045: ObjectPersistencyService ops) throws ZKBException {
046: check(obj, TestCase.class);
047:
048: TestCase tc = (TestCase) obj;
049: Element e = new Element(TESTCASE);
050:
051: // query
052: Element eQ = this .exportObject(tc.getQuery(), QUERY, driver,
053: ops);
054: e.addContent(eQ);
055:
056: // assumptions
057: Element eAss = new Element(ASSUMPTIONS);
058: eAss.setAttribute(POLICY_TO_ADD_ASSUMPTIONS_TO_KB, String
059: .valueOf(tc.getPolicyToAddAssumptionsToKB()));
060: e.addContent(eAss);
061: ClauseSet[] assumptions = tc.getAssumptions();
062: for (int i = 0; i < assumptions.length; i++) {
063: ClauseSet cs = assumptions[i];
064: String kindOfObject = getClauseSetType(cs);
065: if (kindOfObject == null)
066: throw new ZKBException(
067: "Unknown clause set type inj test case assumptions "
068: + cs);
069: else {
070: Element el = exportObject(cs, kindOfObject, driver, ops);
071: eAss.addContent(el);
072: }
073: }
074:
075: // expected number of results
076: Element eNR = new Element(EXPECTED_NUMBER_OF_RESULTS);
077: eNR.addContent(String.valueOf(tc.getExpectedNumberOfResults()));
078: e.addContent(eNR);
079:
080: // expected outcome for ground queries
081: Element eGQ = new Element(EXPECTED_RESULT);
082: eGQ.addContent(String.valueOf(tc.isMustBeTrue()));
083: e.addContent(eGQ);
084:
085: // expected variable substitutions for non ground queries
086: java.util.Map[] expectedReplacements = tc
087: .getExpectedReplacements();
088: Element eRes = new Element(EXPECTED_REPLACEMENTS);
089: if (expectedReplacements != null) {
090: for (int i = 0; i < expectedReplacements.length; i++) {
091: java.util.Map expectedReplacement = expectedReplacements[i];
092: Element eR = new Element(EXPECTED_RESULT2);
093: eRes.addContent(eR);
094: for (Iterator iter = expectedReplacement.keySet()
095: .iterator(); iter.hasNext();) {
096: Element eRe = new Element(EXPECTED_REPLACEMENT);
097: VariableTerm var = (VariableTerm) iter.next();
098: Object expected = expectedReplacement.get(var);
099: Element eVar = this .exportObject(var,
100: VARIABLE_TERM, driver, ops);
101: Element eObj = new Element(EXPECTED_OBJECT);
102: this .assignURI(eObj, ops, expected);
103: eRe.addContent(eVar);
104: eRe.addContent(eObj);
105: eR.addContent(eRe);
106: }
107: }
108: }
109: e.addContent(eRes);
110:
111: // additional properties
112: exportProperties(e, tc);
113:
114: return e;
115: }
116:
117: /**
118: * Build an object from an XML element.
119: * @param e an element
120: * @param driver the generic driver
121: * @param ops the object persistency service
122: * @param lfactory the logic factory used to create objects
123: * @exception a ZKBException is thrown if import fails
124: */
125: public Object importObject(Element e, GenericDriver driver,
126: ObjectPersistencyService ops, LogicFactory lfactory)
127: throws ZKBException {
128:
129: // query
130: Query query = (Query) this .importChild(e, QUERY, driver, ops,
131: lfactory);
132:
133: // get assumptions
134: Element eAss = e.getChild(ASSUMPTIONS);
135: int policy2addAssumptions = TestCase.AT_BOTTOM;
136: try {
137: policy2addAssumptions = Integer
138: .parseInt(eAss
139: .getAttributeValue(POLICY_TO_ADD_ASSUMPTIONS_TO_KB));
140: } catch (Exception x) {
141: throw new ZKBException(
142: "Cannot read policy to add assumptions to kb", x);
143: }
144: List children = eAss.getChildren();
145: ClauseSet[] assumptions = new ClauseSet[children.size()];
146: for (int i = 0; i < children.size(); i++) {
147: Element ea = (Element) children.get(i);
148: Adapter adapter = driver.getAdapter(ea.getName());
149: Object obj = adapter
150: .importObject(ea, driver, ops, lfactory);
151: if (obj == null)
152: throw new ZKBException(
153: "null assumption found in test case");
154: if (!(obj instanceof ClauseSet))
155: throw new ZKBException(
156: "assumptions in test cases must be instances of ClauseSet, but object found is "
157: + obj);
158: assumptions[i] = (ClauseSet) obj;
159: }
160:
161: // expected number of results
162: int expectedNumberOfResults = -1;
163: String value = e.getChild(EXPECTED_NUMBER_OF_RESULTS)
164: .getTextTrim();
165: try {
166: expectedNumberOfResults = Integer.parseInt(value);
167: } catch (Exception x) {
168: throw new ZKBException(
169: "Cannot read expected number of results", x);
170: }
171:
172: // expected outcome for ground queries
173: boolean expectedResult4GroundQueries = true;
174: value = e.getChild(EXPECTED_RESULT).getTextTrim();
175: try {
176: expectedResult4GroundQueries = new Boolean(value)
177: .booleanValue();
178: } catch (Exception x) {
179: throw new ZKBException(
180: "Cannot read expected result for ground queries", x);
181: }
182:
183: // expected variable substitutions for non ground queries
184: Map[] expectedReplacements = null;
185: Element eRes = e.getChild(EXPECTED_REPLACEMENTS);
186: if (eRes != null) {
187: List eResultList = eRes.getChildren(EXPECTED_RESULT2);
188: expectedReplacements = new Map[eResultList.size()];
189: for (int i = 0; i < eResultList.size(); i++) {
190: expectedReplacements[i] = new Hashtable(); // TODO map class is hard coded here , should we store it (as attribute)?
191: Element eResult = (Element) eResultList.get(i);
192: List eReplacements = eResult
193: .getChildren(EXPECTED_REPLACEMENT);
194: for (int j = 0; j < eReplacements.size(); j++) {
195: Element eRe = (Element) eReplacements.get(j);
196: Element eObj = eRe.getChild(EXPECTED_OBJECT);
197: VariableTerm var = (VariableTerm) this .importChild(
198: eRe, VARIABLE_TERM, driver, ops, lfactory);
199: Object expectedObject = getObjectByURI(eObj, ops);
200: expectedReplacements[i].put(var, expectedObject);
201: }
202: }
203: }
204:
205: // build test case
206: TestCase tc = lfactory.createTestCase(query, assumptions,
207: policy2addAssumptions, expectedNumberOfResults,
208: expectedResult4GroundQueries, expectedReplacements);
209: importProperties(e, tc);
210: return tc;
211: }
212:
213: /**
214: * Get the name of the associated tag (element).
215: * @return a string
216: */
217: public String getTagName() {
218: return TESTCASE;
219: }
220:
221: /**
222: * Print the DTD associated with this adapter on a string buffer.
223: * @param out the buffer to print on.
224: */
225: public void printDTD(StringBuffer out) {
226:
227: // warning : properties are declared in the rule adapter, not here
228: out
229: .append("<!ELEMENT testcase (query,assumptions,expected_number_of_results,expected_result,expected_replacements?,properties?)>\n");
230: out
231: .append("<!ELEMENT assumptions (atom|rule|sql_clause_set|custom_clause_set)* >\n");
232: out
233: .append("<!ELEMENT expected_number_of_results (#PCDATA) >\n"); // int
234: out.append("<!ELEMENT expected_result (#PCDATA) >\n"); // boolean
235: out.append("<!ELEMENT expected_replacements (result)* >\n");
236: out.append("<!ELEMENT result (var,expected_object) >\n");
237: out.append("<!ELEMENT expected_object (#PCDATA)>\n");
238: out
239: .append("<!ATTLIST assumptions add2kb_policy (0 | 1) \"0\">\n"); // hard coded - there are only two possible values defined in TestCase
240:
241: }
242:
243: }
|