001: /*****************************************************************************
002: * Source code metadata
003: *
004: * Original author ijd
005: * Package Jena2
006: * Created 4 Dec 2007
007: * File TestOntTools.java
008: *
009: * (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
010: * (see footer for full conditions)
011: *****************************************************************************/package com.hp.hpl.jena.ontology.impl.test;
012:
013: // Imports
014: ///////////////
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import junit.framework.TestCase;
019:
020: import com.hp.hpl.jena.ontology.*;
021: import com.hp.hpl.jena.rdf.model.*;
022: import com.hp.hpl.jena.util.iterator.Filter;
023: import com.hp.hpl.jena.vocabulary.OWL;
024:
025: /**
026: * <p>
027: * Unit tests for experimental ontology tools class.
028: * </p>
029: *
030: * @author Ian Dickinson, HP Labs (<a href="mailto:ian.dickinson@hp.com">email</a>)
031: */
032: public class TestOntTools extends TestCase {
033: // Constants
034: //////////////////////////////////
035:
036: String NS = "http://example.com/test#";
037:
038: // Static variables
039: //////////////////////////////////
040:
041: // Instance variables
042: //////////////////////////////////
043:
044: OntModel m_model;
045:
046: OntClass m_a;
047: OntClass m_b;
048: OntClass m_c;
049: OntClass m_d;
050: OntClass m_e;
051: OntClass m_f;
052: OntClass m_g;
053: OntClass m_top;
054:
055: // Constructors
056: //////////////////////////////////
057:
058: // External signature methods
059: //////////////////////////////////
060:
061: /**
062: * @throws java.lang.Exception
063: * @see junit.framework.TestCase#setUp()
064: */
065: protected void setUp() throws Exception {
066: m_model = ModelFactory
067: .createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
068: m_a = m_model.createClass(NS + "A");
069: m_b = m_model.createClass(NS + "B");
070: m_c = m_model.createClass(NS + "C");
071: m_d = m_model.createClass(NS + "D");
072: m_e = m_model.createClass(NS + "E");
073: m_f = m_model.createClass(NS + "F");
074: m_g = m_model.createClass(NS + "G");
075: m_top = m_model.createClass(OWL.Thing.getURI());
076: }
077:
078: /**
079: * Test method for <code>com.hp.hpl.jena.ontology.OntTools#indexLCA</code>
080: */
081: public void testIndexLCA0() {
082: m_a.addSubClass(m_b);
083: m_a.addSubClass(m_c);
084:
085: assertEquals(m_a, OntTools.getLCA(m_model, m_b, m_c));
086: }
087:
088: public void testIndexLCA1() {
089: m_a.addSubClass(m_b);
090: m_a.addSubClass(m_c);
091:
092: assertEquals(m_a, OntTools.getLCA(m_model, m_c, m_b));
093: }
094:
095: public void testIndexLCA2() {
096: m_a.addSubClass(m_b);
097: m_a.addSubClass(m_c);
098:
099: assertEquals(m_a, OntTools.getLCA(m_model, m_a, m_c));
100: }
101:
102: public void testIndexLCA3() {
103: m_a.addSubClass(m_b);
104: m_a.addSubClass(m_c);
105:
106: assertEquals(m_a, OntTools.getLCA(m_model, m_b, m_a));
107: }
108:
109: public void testIndexLCA4() {
110: m_a.addSubClass(m_b);
111: m_a.addSubClass(m_c);
112: m_b.addSubClass(m_d);
113:
114: assertEquals(m_a, OntTools.getLCA(m_model, m_d, m_c));
115: }
116:
117: public void testIndexLCA5() {
118: m_a.addSubClass(m_b);
119: m_a.addSubClass(m_c);
120: m_b.addSubClass(m_d);
121:
122: assertEquals(m_a, OntTools.getLCA(m_model, m_c, m_d));
123: }
124:
125: public void testIndexLCA6() {
126: m_a.addSubClass(m_b);
127: m_a.addSubClass(m_c);
128: m_b.addSubClass(m_d);
129: m_c.addSubClass(m_e);
130:
131: assertEquals(m_a, OntTools.getLCA(m_model, m_d, m_e));
132: }
133:
134: public void testIndexLCA7() {
135: m_a.addSubClass(m_b);
136: m_a.addSubClass(m_c);
137: m_b.addSubClass(m_d);
138: m_c.addSubClass(m_e);
139:
140: assertEquals(m_a, OntTools.getLCA(m_model, m_e, m_d));
141: }
142:
143: public void testIndexLCA8() {
144: m_a.addSubClass(m_b);
145: m_a.addSubClass(m_c);
146: m_b.addSubClass(m_d);
147: m_d.addSubClass(m_e);
148:
149: assertEquals(m_a, OntTools.getLCA(m_model, m_c, m_e));
150: }
151:
152: public void testIndexLCA9() {
153: m_a.addSubClass(m_b);
154: m_a.addSubClass(m_c);
155: m_b.addSubClass(m_d);
156: m_d.addSubClass(m_e);
157:
158: assertEquals(m_a, OntTools.getLCA(m_model, m_b, m_c));
159: }
160:
161: public void testIndexLCA10() {
162: m_a.addSubClass(m_b);
163: m_a.addSubClass(m_c);
164: m_a.addSubClass(m_d);
165: m_c.addSubClass(m_e);
166: m_d.addSubClass(m_f);
167:
168: assertEquals(m_a, OntTools.getLCA(m_model, m_b, m_e));
169: }
170:
171: public void testIndexLCA11() {
172: m_a.addSubClass(m_b);
173: m_a.addSubClass(m_c);
174: m_a.addSubClass(m_d);
175: m_c.addSubClass(m_e);
176: m_d.addSubClass(m_f);
177:
178: assertEquals(m_a, OntTools.getLCA(m_model, m_b, m_f));
179: }
180:
181: public void testIndexLCA12() {
182: m_a.addSubClass(m_b);
183: m_a.addSubClass(m_c);
184: m_a.addSubClass(m_d);
185: m_d.addSubClass(m_e);
186: m_d.addSubClass(m_f);
187:
188: assertEquals(m_d, OntTools.getLCA(m_model, m_f, m_e));
189: }
190:
191: public void testIndexLCA13() {
192: m_a.addSubClass(m_b);
193: m_a.addSubClass(m_c);
194: m_a.addSubClass(m_d);
195: m_c.addSubClass(m_e);
196: m_d.addSubClass(m_e);
197: m_d.addSubClass(m_f);
198:
199: assertEquals(m_d, OntTools.getLCA(m_model, m_f, m_e));
200: }
201:
202: /** Disconnected trees */
203: public void testIndexLCA14() {
204: m_a.addSubClass(m_b);
205: m_a.addSubClass(m_c);
206:
207: assertEquals(OWL.Thing, OntTools.getLCA(m_model, m_b, m_e));
208: assertEquals(OWL.Thing, OntTools.getLCA(m_model, m_c, m_e));
209: assertEquals(OWL.Thing, OntTools.getLCA(m_model, m_a, m_e));
210: }
211:
212: /** Shortest path tests */
213:
214: public void testShortestPath0() {
215: Property p = m_model.createProperty(NS + "p");
216: m_a.addProperty(p, m_b);
217:
218: testPath(OntTools.findShortestPath(m_model, m_a, m_b,
219: Filter.any), new Property[] { p });
220: }
221:
222: public void testShortestPath1() {
223: Property p = m_model.createProperty(NS + "p");
224: m_a.addProperty(p, m_b);
225: m_b.addProperty(p, m_c);
226:
227: testPath(OntTools.findShortestPath(m_model, m_a, m_c,
228: Filter.any), new Property[] { p, p });
229: }
230:
231: public void testShortestPath2() {
232: Property p = m_model.createProperty(NS + "p");
233: // a - b - c
234: m_a.addProperty(p, m_b);
235: m_b.addProperty(p, m_c);
236:
237: // a - d - e - f
238: m_a.addProperty(p, m_d);
239: m_d.addProperty(p, m_e);
240: m_e.addProperty(p, m_f);
241:
242: testPath(OntTools.findShortestPath(m_model, m_a, m_c,
243: Filter.any), new Property[] { p, p });
244: testPath(OntTools.findShortestPath(m_model, m_a, m_f,
245: Filter.any), new Property[] { p, p, p });
246: }
247:
248: public void testShortestPath3() {
249: Property p = m_model.createProperty(NS + "p");
250: // a - b - c
251: m_a.addProperty(p, m_b);
252: m_b.addProperty(p, m_c);
253:
254: // a - d - e - f
255: m_a.addProperty(p, m_d);
256: m_d.addProperty(p, m_e);
257: m_e.addProperty(p, m_f);
258:
259: testPath(OntTools.findShortestPath(m_model, m_a, m_c,
260: new OntTools.PredicatesFilter(p)), new Property[] { p,
261: p });
262: testPath(OntTools.findShortestPath(m_model, m_a, m_f,
263: new OntTools.PredicatesFilter(p)), new Property[] { p,
264: p, p });
265: }
266:
267: public void testShortestPath4() {
268: Property p = m_model.createProperty(NS + "p");
269: Property q = m_model.createProperty(NS + "q");
270:
271: // a - b - c by q
272: m_a.addProperty(q, m_b);
273: m_b.addProperty(q, m_c);
274:
275: // a - d - e - f by p
276: m_a.addProperty(p, m_d);
277: m_d.addProperty(p, m_e);
278: m_e.addProperty(p, m_f);
279:
280: assertNull(OntTools.findShortestPath(m_model, m_a, m_c,
281: new OntTools.PredicatesFilter(p)));
282: testPath(OntTools.findShortestPath(m_model, m_a, m_f,
283: new OntTools.PredicatesFilter(p)), new Property[] { p,
284: p, p });
285: }
286:
287: /** Reflexive loop is allowed */
288: public void testShortestPath5() {
289: Property p = m_model.createProperty(NS + "p");
290: m_a.addProperty(p, m_a);
291:
292: testPath(OntTools.findShortestPath(m_model, m_a, m_a,
293: Filter.any), new Property[] { p });
294: }
295:
296: public void testShortestPath6() {
297: Property p = m_model.createProperty(NS + "p");
298: Property q = m_model.createProperty(NS + "q");
299:
300: // a - b - a by q
301: // tests loop detection
302: m_a.addProperty(q, m_b);
303: m_b.addProperty(q, m_a);
304:
305: assertNull(OntTools.findShortestPath(m_model, m_a, m_c,
306: new OntTools.PredicatesFilter(new Property[] { p, q })));
307: }
308:
309: public void testShortestPath7() {
310: Property p = m_model.createProperty(NS + "p");
311: Property q = m_model.createProperty(NS + "q");
312:
313: // a - d - e - f by p and q
314: m_a.addProperty(p, m_d);
315: m_d.addProperty(q, m_e);
316: m_d.addProperty(q, m_b);
317: m_e.addProperty(p, m_f);
318:
319: testPath(
320: OntTools.findShortestPath(m_model, m_a, m_f,
321: new OntTools.PredicatesFilter(new Property[] {
322: p, q })), new Property[] { p, q, p });
323: }
324:
325: /** Find a literal target */
326: public void testShortestPath8() {
327: Property p = m_model.createProperty(NS + "p");
328: Property q = m_model.createProperty(NS + "q");
329:
330: // a - d - e - f by p and q
331: m_a.addProperty(p, m_d);
332: m_d.addProperty(q, m_e);
333: m_d.addProperty(q, "bluff");
334: m_d.addProperty(q, m_b);
335: m_e.addProperty(p, m_f);
336: m_f.addProperty(q, "arnie");
337:
338: testPath(
339: OntTools.findShortestPath(m_model, m_a, ResourceFactory
340: .createPlainLiteral("arnie"),
341: new OntTools.PredicatesFilter(new Property[] {
342: p, q })), new Property[] { p, q, p, q });
343: }
344:
345: /** Tests on {@link OntTools#namedHierarchyRoots(OntModel)} */
346:
347: public void testNamedHierarchyRoots0() {
348: m_a.addSubClass(m_b);
349: m_b.addSubClass(m_c);
350: m_c.addSubClass(m_d);
351: m_e.addSubClass(m_e);
352: m_e.addSubClass(m_f);
353:
354: List nhr = OntTools.namedHierarchyRoots(m_model);
355: assertEquals(3, nhr.size());
356: assertTrue(nhr.contains(m_a));
357: assertTrue(nhr.contains(m_e));
358: assertTrue(nhr.contains(m_g));
359: }
360:
361: public void testNamedHierarchyRoots1() {
362: m_a.addSubClass(m_b);
363: m_b.addSubClass(m_c);
364: m_c.addSubClass(m_d);
365: m_e.addSubClass(m_e);
366: m_e.addSubClass(m_f);
367:
368: OntClass anon0 = m_model.createClass();
369: anon0.addSubClass(m_a);
370: anon0.addSubClass(m_e);
371:
372: List nhr = OntTools.namedHierarchyRoots(m_model);
373: assertEquals(3, nhr.size());
374: assertTrue(nhr.contains(m_a));
375: assertTrue(nhr.contains(m_e));
376: assertTrue(nhr.contains(m_g));
377: }
378:
379: public void testNamedHierarchyRoots2() {
380: OntClass anon0 = m_model.createClass();
381: OntClass anon1 = m_model.createClass();
382: anon0.addSubClass(m_a);
383: anon0.addSubClass(m_e);
384: anon0.addSubClass(anon1);
385: anon1.addSubClass(m_g);
386:
387: m_a.addSubClass(m_b);
388: m_b.addSubClass(m_c);
389: m_c.addSubClass(m_d);
390: m_e.addSubClass(m_e);
391: m_e.addSubClass(m_f);
392:
393: List nhr = OntTools.namedHierarchyRoots(m_model);
394: assertEquals(3, nhr.size());
395: assertTrue(nhr.contains(m_a));
396: assertTrue(nhr.contains(m_e));
397: assertTrue(nhr.contains(m_g));
398: }
399:
400: // Internal implementation methods
401: //////////////////////////////////
402:
403: private void testPath(OntTools.Path path, Property[] expected) {
404: assertEquals(expected.length, path.size());
405:
406: int i = 0;
407: Iterator j = path.iterator();
408: while (j.hasNext()) {
409: assertEquals("path position: " + i, expected[i],
410: ((Statement) j.next()).getPredicate());
411: i++;
412: }
413: }
414:
415: //==============================================================================
416: // Inner class definitions
417: //==============================================================================
418:
419: }
420:
421: /*
422: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
423: All rights reserved.
424:
425: Redistribution and use in source and binary forms, with or without
426: modification, are permitted provided that the following conditions
427: are met:
428:
429: 1. Redistributions of source code must retain the above copyright
430: notice, this list of conditions and the following disclaimer.
431:
432: 2. Redistributions in binary form must reproduce the above copyright
433: notice, this list of conditions and the following disclaimer in the
434: documentation and/or other materials provided with the distribution.
435:
436: 3. The name of the author may not be used to endorse or promote products
437: derived from this software without specific prior written permission.
438:
439: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
440: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
441: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
442: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
443: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
444: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
445: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
446: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
447: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
448: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
449: */
|