001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package test.org.mandarax.rdf;
020:
021: import org.mandarax.rdf.lib.*;
022: import java.net.URL;
023: import org.mandarax.reference.AdvancedKnowledgeBase;
024: import org.mandarax.reference.ResolutionInferenceEngine2;
025: import org.mandarax.kernel.*;
026: import java.util.*;
027: import org.mandarax.rdf.*;
028:
029: /**
030: * Test case for RDF clause sets with RDF containers.
031: * @see org.mandarax.rdf.RDFClauseSet org.mandarax.rdf.RDFContainer
032: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A> <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
033: * @version 1.1 <01 August 2004>
034: * @since 0.1
035: */
036:
037: public class RDFContainerTestCase extends AbstractRDFTestCase {
038:
039: public static final int ALT = 0;
040: public static final int SEQ = 1;
041: public static final int BAG = 2;
042:
043: /**
044: * Utility method: build kb, query and issue query, return result set.
045: * @param example the example (a relative file name)
046: * @return a result set
047: */
048: private ResultSet queryTestKB(String example) throws Exception {
049: // add RDFClauset into Knowledgebase
050: KnowledgeBase kb = new AdvancedKnowledgeBase();
051: URL url = this .getClass().getResource(TEST_DATA_ROOT + example);
052: RDFClauseSet clauseSet = new RDFClauseSet(url);
053: // this line is ok for testing, but should not be used for
054: // with large amounts real world
055: clauseSet.setPredicates(RDFUtils.findPredicates(url));
056: kb.add(clauseSet);
057:
058: // query
059: VariableTerm container = lfactory.createVariableTerm(
060: "container", Object.class);
061: VariableTerm element = lfactory.createVariableTerm("element",
062: Object.class);
063: Query query = lfactory.createQuery(lfactory.createFact(
064: RDFLib.CONTAINS, new Term[] { container, element }),
065: "query");
066: InferenceEngine ie = new ResolutionInferenceEngine2();
067: ResultSet rs = ie.query(query, kb, InferenceEngine.ALL,
068: InferenceEngine.BUBBLE_EXCEPTIONS);
069: return rs;
070: }
071:
072: /**
073: * Counts the expected results.
074: * @throws an exception (indicating that the test case has failed)
075: */
076: public void test1a() throws Exception {
077: this ._testContainsFacts("chap0401.rdf", 6);
078: }
079:
080: /**
081: * Checks the container type.
082: * @throws an exception (indicating that the test case has failed)
083: */
084: public void test1b() throws Exception {
085: this ._testKindOfContainer("chap0401.rdf", BAG);
086: }
087:
088: /**
089: * Checks the size of the container object.
090: * @throws an exception (indicating that the test case has failed)
091: */
092: public void test1c() throws Exception {
093: this ._testContainerSize("chap0401.rdf", 6);
094: }
095:
096: /**
097: * Checks whether the container object in the various results is the same (equal).
098: * @throws an exception (indicating that the test case has failed)
099: */
100: public void test1d() throws Exception {
101: this ._testNumberOfEqualsContainers("chap0401.rdf", 1);
102: }
103:
104: /**
105: * Checks whether the container object in the various results is the same (==).
106: * @throws an exception (indicating that the test case has failed)
107: */
108: public void test1e() throws Exception {
109: this ._testNumberOfContainerInstances("chap0401.rdf", 1);
110: }
111:
112: /**
113: * Checks whether the Collection.contains relationships are consistent with the facts in the kb.
114: * @throws an exception (indicating that the test case has failed)
115: */
116: public void test1f() throws Exception {
117: this ._testContainerContains("chap0401.rdf");
118: }
119:
120: /**
121: * Counts the expected results.
122: * @throws an exception (indicating that the test case has failed)
123: */
124: public void test2a() throws Exception {
125: this ._testContainsFacts("chap0402.rdf", 3);
126: }
127:
128: /**
129: * Checks the container type.
130: * @throws an exception (indicating that the test case has failed)
131: */
132: public void test2b() throws Exception {
133: this ._testKindOfContainer("chap0402.rdf", SEQ);
134: }
135:
136: /**
137: * Checks the size of the container object.
138: * @throws an exception (indicating that the test case has failed)
139: */
140: public void test2c() throws Exception {
141: this ._testContainerSize("chap0402.rdf", 3);
142: }
143:
144: /**
145: * Checks whether the container object in the various results is the same (equal).
146: * @throws an exception (indicating that the test case has failed)
147: */
148: public void test2d() throws Exception {
149: this ._testNumberOfEqualsContainers("chap0402.rdf", 1);
150: }
151:
152: /**
153: * Checks whether the container object in the various results is the same (==).
154: * @throws an exception (indicating that the test case has failed)
155: */
156: public void test2e() throws Exception {
157: this ._testNumberOfContainerInstances("chap0402.rdf", 1);
158: }
159:
160: /**
161: * Checks whether the Collection.contains relationships are consistent with the facts in the kb.
162: * @throws an exception (indicating that the test case has failed)
163: */
164: public void test2f() throws Exception {
165: this ._testContainerContains("chap0402.rdf");
166: }
167:
168: /**
169: * Checks the expected order of resources in the container.
170: * @throws an exception (indicating that the test case has failed)
171: */
172: public void test2g() throws Exception {
173: this ._testOrder("chap0402.rdf", new String[] {
174: "http://burningbird.net/articles.htm",
175: "http://burningbird.net/dynatech.htm",
176: "http://burningbird.net/interact.htm" });
177: }
178:
179: /**
180: * Counts the expected results.
181: * @throws an exception (indicating that the test case has failed)
182: */
183: public void test5a() throws Exception {
184: this ._testContainsFacts("chap0405.rdf", 3);
185: }
186:
187: /**
188: * Checks the container type.
189: * @throws an exception (indicating that the test case has failed)
190: */
191: public void test5b() throws Exception {
192: this ._testKindOfContainer("chap0405.rdf", SEQ);
193: }
194:
195: /**
196: * Checks the size of the container object.
197: * @throws an exception (indicating that the test case has failed)
198: */
199: public void test5c() throws Exception {
200: this ._testContainerSize("chap0405.rdf", 3);
201: }
202:
203: /**
204: * Checks whether the container object in the various results is the same (equal).
205: * @throws an exception (indicating that the test case has failed)
206: */
207: public void test5d() throws Exception {
208: this ._testNumberOfEqualsContainers("chap0405.rdf", 1);
209: }
210:
211: /**
212: * Checks whether the container object in the various results is the same (==).
213: * @throws an exception (indicating that the test case has failed)
214: */
215: public void test5e() throws Exception {
216: this ._testNumberOfContainerInstances("chap0405.rdf", 1);
217: }
218:
219: /**
220: * Checks whether the Collection.contains relationships are consistent with the facts in the kb.
221: * @throws an exception (indicating that the test case has failed)
222: */
223: public void test5f() throws Exception {
224: this ._testContainerContains("chap0405.rdf");
225: }
226:
227: /**
228: * Checks the expected order of resources in the container.
229: * @throws an exception (indicating that the test case has failed)
230: */
231: public void test5g() throws Exception {
232: this ._testOrder("chap0405.rdf", new String[] {
233: "http://burningbird.net/articles.htm",
234: "http://burningbird.net/dynatech.htm",
235: "http://burningbird.net/interact.htm" });
236: }
237:
238: /**
239: * Generic test case counting the number of expected container instances (==).
240: * @throws an exception (indicating that the test case has failed)
241: */
242: private void _testNumberOfContainerInstances(String testData,
243: int expectedNumberOfContainers) throws Exception {
244: ResultSet rs = queryTestKB(testData);
245: IdentityHashMap map = new IdentityHashMap();
246: while (rs.next()) {
247: RDFContainer container = (RDFContainer) rs.getResult(
248: Object.class, "container");
249: map.put(container, container);
250: }
251: rs.close();
252: assertTrue(map.size() == expectedNumberOfContainers);
253: }
254:
255: /**
256: * Generic test case counting the number of expected container instances (equals).
257: * @throws an exception (indicating that the test case has failed)
258: */
259: private void _testNumberOfEqualsContainers(String testData,
260: int expectedNumberOfContainers) throws Exception {
261: ResultSet rs = queryTestKB(testData);
262: Set set = new HashSet();
263: while (rs.next()) {
264: RDFContainer container = (RDFContainer) rs.getResult(
265: Object.class, "container");
266: set.add(container);
267: }
268: rs.close();
269: assertTrue(set.size() == expectedNumberOfContainers);
270: }
271:
272: /**
273: * Generic test case testing for a certain kind of container (see constants).
274: * @throws an exception (indicating that the test case has failed)
275: */
276: private void _testKindOfContainer(String testData,
277: int expectedContainerType) throws Exception {
278: ResultSet rs = queryTestKB(testData);
279: rs.next();
280: RDFContainer container = (RDFContainer) rs.getResult(
281: Object.class, "container");
282: rs.close();
283: if (expectedContainerType == SEQ)
284: assertTrue(container.isSeq());
285: else if (expectedContainerType == BAG)
286: assertTrue(container.isBag());
287: else if (expectedContainerType == ALT)
288: assertTrue(container.isAlt());
289: else
290: assertTrue(false);
291: }
292:
293: /**
294: * Generic test case to test the size of elements in the container.
295: * @throws an exception (indicating that the test case has failed)
296: */
297: private void _testContainerSize(String testData, int expectedSize)
298: throws Exception {
299: ResultSet rs = queryTestKB(testData);
300: rs.next();
301: RDFContainer container = (RDFContainer) rs.getResult(
302: Object.class, "container");
303: rs.close();
304: assertTrue(container.size() == expectedSize);
305: }
306:
307: /**
308: * Generic test case to count the contains records in a document.
309: * @throws an exception (indicating that the test case has failed)
310: */
311: private void _testContainsFacts(String testData, int expectedSize)
312: throws Exception {
313: ResultSet rs = queryTestKB(testData);
314: int counter = 0;
315: while (rs.next())
316: counter++;
317: rs.close();
318: assertTrue(counter == expectedSize);
319: }
320:
321: /**
322: * Generic test case to test the container contains method.
323: * @throws an exception (indicating that the test case has failed)
324: */
325: private void _testContainerContains(String testData)
326: throws Exception {
327: ResultSet rs = queryTestKB(testData);
328: while (rs.next()) {
329: RDFContainer container = (RDFContainer) rs.getResult(
330: Object.class, "container");
331: Object element = rs.getResult(Object.class, "element");
332: for (Iterator iter = container.iterator(); iter.hasNext();) {
333: Object next = iter.next();
334: // System.out.println(next);
335: }
336: if (!container.contains(element))
337: assertTrue(false);
338: }
339: rs.close();
340: assertTrue(true);
341: }
342:
343: /**
344: * Generic test case to test the order of resources in the container.
345: * @throws an exception (indicating that the test case has failed)
346: */
347: private void _testOrder(String testData, String[] expectedResources)
348: throws Exception {
349: ResultSet rs = queryTestKB(testData);
350: rs.next();
351: RDFContainer container = (RDFContainer) rs.getResult(
352: Object.class, "container");
353: // list items
354: List list = new ArrayList();
355: for (Iterator iter = container.iterator(); iter.hasNext();)
356: list.add(iter.next().toString());
357: List expected = new ArrayList();
358: for (int i = 0; i < expectedResources.length; i++)
359: expected.add(expectedResources[i]);
360: rs.close();
361: assertTrue(list.equals(expected));
362: }
363: }
|