001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 06-Jun-2003
009: * Filename $RCSfile: TestResourceUtils.java,v $
010: * Revision $Revision: 1.11 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:08:44 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.util.iterator.test;
019:
020: // Imports
021: ///////////////
022: import com.hp.hpl.jena.rdf.model.*;
023: import com.hp.hpl.jena.vocabulary.RDFS;
024: import com.hp.hpl.jena.util.*;
025:
026: import junit.framework.*;
027:
028: import java.util.*;
029:
030: /**
031: * <p>
032: * Unit tests on resource utilities
033: * </p>
034: *
035: * @author Ian Dickinson, HP Labs
036: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
037: * @version CVS $Id: TestResourceUtils.java,v 1.11 2008/01/02 12:08:44 andy_seaborne Exp $
038: */
039: public class TestResourceUtils extends TestCase {
040: // Constants
041: //////////////////////////////////
042:
043: public static final String NS = "http://jena.hp.com/test#";
044:
045: // Static variables
046: //////////////////////////////////
047:
048: // Instance variables
049: //////////////////////////////////
050:
051: // Constructors
052: //////////////////////////////////
053:
054: public TestResourceUtils(String name) {
055: super (name);
056: }
057:
058: // External signature methods
059: //////////////////////////////////
060:
061: public void testMaximalLowerElements() {
062: Model m = ModelFactory.createDefaultModel();
063:
064: Resource a = m.createResource(NS + "a");
065: Resource b = m.createResource(NS + "b");
066: Resource c = m.createResource(NS + "c");
067: Resource d = m.createResource(NS + "d");
068:
069: b.addProperty(RDFS.subClassOf, a);
070: c.addProperty(RDFS.subClassOf, a);
071: d.addProperty(RDFS.subClassOf, c);
072: d.addProperty(RDFS.subClassOf, a);
073:
074: List abcd = Arrays.asList(new Object[] { a, b, c, d });
075: List bcd = Arrays.asList(new Object[] { b, c, d });
076: List cd = Arrays.asList(new Object[] { c, d });
077:
078: assertEquals("Wrong number of remaining resources", 1,
079: ResourceUtils.maximalLowerElements(abcd,
080: RDFS.subClassOf, true).size());
081: assertEquals("Result should be a", a, ResourceUtils
082: .maximalLowerElements(abcd, RDFS.subClassOf, true)
083: .iterator().next());
084: assertEquals("Wrong number of remaining resources", 2,
085: ResourceUtils.maximalLowerElements(bcd,
086: RDFS.subClassOf, true).size());
087: assertEquals("Wrong number of remaining resources", 1,
088: ResourceUtils.maximalLowerElements(cd, RDFS.subClassOf,
089: true).size());
090: assertEquals("Result should be a", c, ResourceUtils
091: .maximalLowerElements(cd, RDFS.subClassOf, true)
092: .iterator().next());
093: }
094:
095: public void testRenameResource() {
096: Model m = ModelFactory.createDefaultModel();
097:
098: Resource a = m.createResource(NS + "a");
099: Resource b = m.createResource(NS + "b");
100: Resource c = m.createResource(NS + "c");
101: Resource d = m.createResource(NS + "d");
102:
103: Property p = m.createProperty(NS, "p");
104: Property q = m.createProperty(NS, "q");
105:
106: a.addProperty(p, b);
107: a.addProperty(q, c);
108: d.addProperty(p, a);
109: d.addProperty(p, b);
110:
111: // now rename a to e
112: Resource e = ResourceUtils.renameResource(a, NS + "e");
113:
114: assertTrue("should be no properties of a", !a.listProperties()
115: .hasNext());
116: assertEquals("uri of a", NS + "a", a.getURI());
117: assertEquals("uri of e", NS + "e", e.getURI());
118:
119: assertTrue("d should not have p a", !d.hasProperty(p, a));
120: assertTrue("d should have p e", d.hasProperty(p, e));
121:
122: assertTrue("e should have p b", e.hasProperty(p, b));
123: assertTrue("e should have q c", e.hasProperty(q, c));
124:
125: assertTrue("d p b should be unchanged", d.hasProperty(p, b));
126:
127: // now rename e to anon
128: Resource anon = ResourceUtils.renameResource(e, null);
129:
130: assertTrue("should be no properties of e", !e.listProperties()
131: .hasNext());
132: assertEquals("uri of e", NS + "e", e.getURI());
133: assertTrue("anon", anon.isAnon());
134:
135: assertTrue("d should not have p e", !d.hasProperty(p, e));
136: assertTrue("d should have p anon", d.hasProperty(p, anon));
137:
138: assertTrue("anon should have p b", anon.hasProperty(p, b));
139: assertTrue("anon should have q c", anon.hasProperty(q, c));
140:
141: assertTrue("d p b should be unchanged", d.hasProperty(p, b));
142:
143: // reflexive case
144: Resource f = m.createResource(NS + "f");
145: f.addProperty(p, f);
146:
147: Resource f1 = ResourceUtils.renameResource(f, NS + "f1");
148: assertFalse("Should be no f statements", m.listStatements(f,
149: null, (RDFNode) null).hasNext());
150: assertTrue("f1 has p f1", f1.hasProperty(p, f1));
151: }
152:
153: public void testReachableGraphClosure() {
154: Model m0 = ModelFactory.createDefaultModel();
155: Resource a = m0.createResource("a");
156: Resource b = m0.createResource("b");
157: Resource c = m0.createResource("c");
158: Resource d = m0.createResource("d");
159: Property p = m0.createProperty("p");
160:
161: m0.add(a, p, b);
162: m0.add(a, p, c);
163: m0.add(b, p, b); // unit loop
164: m0.add(b, p, a); // loop
165: m0.add(d, p, a); // not reachable from a
166:
167: Model m1 = ModelFactory.createDefaultModel();
168: m1.add(a, p, b);
169: m1.add(a, p, c);
170: m1.add(b, p, b);
171: m1.add(b, p, a);
172:
173: assertTrue(
174: "m1 should be isomorphic with the reachable sub-graph from a",
175: m1.isIsomorphicWith(ResourceUtils.reachableClosure(a)));
176: }
177:
178: public void testRemoveEquiv() {
179: Model m = ModelFactory.createDefaultModel();
180:
181: Resource a = m.createResource(NS + "a");
182: Resource b = m.createResource(NS + "b");
183: Resource c = m.createResource(NS + "c");
184: Resource d = m.createResource(NS + "d");
185: Resource e = m.createResource(NS + "e");
186:
187: b.addProperty(RDFS.subClassOf, a);
188: a.addProperty(RDFS.subClassOf, b); // a,b are equivalent
189: d.addProperty(RDFS.subClassOf, e);
190: e.addProperty(RDFS.subClassOf, d); // d,e are equivalent
191:
192: // reflexive relations - would be inferred by inf engine
193: a.addProperty(RDFS.subClassOf, a);
194: b.addProperty(RDFS.subClassOf, b);
195: c.addProperty(RDFS.subClassOf, c);
196: d.addProperty(RDFS.subClassOf, d);
197: e.addProperty(RDFS.subClassOf, e);
198:
199: List abcde = Arrays.asList(new Object[] { a, b, c, d, e });
200: List ab = Arrays.asList(new Object[] { a, b });
201: List cde = Arrays.asList(new Object[] { c, d, e });
202: List abde = Arrays.asList(new Object[] { a, b, d, e });
203: List de = Arrays.asList(new Object[] { d, e });
204:
205: List in = new ArrayList();
206: in.addAll(abcde);
207: List out = null;
208: assertTrue(in.equals(abcde));
209: assertFalse(in.equals(cde));
210: assertNull(out);
211:
212: out = ResourceUtils.removeEquiv(in, RDFS.subClassOf, a);
213:
214: assertFalse(in.equals(abcde));
215: assertTrue(in.equals(cde));
216: assertNotNull(out);
217: assertEquals(out, ab);
218:
219: out = ResourceUtils.removeEquiv(in, RDFS.subClassOf, e);
220:
221: assertFalse(in.equals(abcde));
222: assertTrue(in.equals(Collections.singletonList(c)));
223: assertNotNull(out);
224: assertEquals(out, de);
225: }
226:
227: public void testPartition() {
228: Model m = ModelFactory.createDefaultModel();
229:
230: Resource a = m.createResource(NS + "a");
231: Resource b = m.createResource(NS + "b");
232: Resource c = m.createResource(NS + "c");
233: Resource d = m.createResource(NS + "d");
234: Resource e = m.createResource(NS + "e");
235:
236: b.addProperty(RDFS.subClassOf, a);
237: a.addProperty(RDFS.subClassOf, b); // a,b are equivalent
238: d.addProperty(RDFS.subClassOf, e);
239: e.addProperty(RDFS.subClassOf, d); // d,e are equivalent
240:
241: // reflexive relations - would be inferred by inf engine
242: a.addProperty(RDFS.subClassOf, a);
243: b.addProperty(RDFS.subClassOf, b);
244: c.addProperty(RDFS.subClassOf, c);
245: d.addProperty(RDFS.subClassOf, d);
246: e.addProperty(RDFS.subClassOf, e);
247:
248: List abcde = Arrays.asList(new Object[] { a, b, c, d, e });
249: List ab = Arrays.asList(new Object[] { b, a });
250: List cc = Arrays.asList(new Object[] { c });
251: List de = Arrays.asList(new Object[] { e, d });
252:
253: List partition = ResourceUtils
254: .partition(abcde, RDFS.subClassOf);
255: assertEquals("Should be 3 partitions", 3, partition.size());
256: assertEquals("First parition should be (a,b)", ab, partition
257: .get(0));
258: assertEquals("First parition should be (c)", cc, partition
259: .get(1));
260: assertEquals("First parition should be (d,e)", de, partition
261: .get(2));
262: }
263:
264: // Internal implementation methods
265: //////////////////////////////////
266:
267: //==============================================================================
268: // Inner class definitions
269: //==============================================================================
270:
271: }
272:
273: /*
274: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
275: All rights reserved.
276:
277: Redistribution and use in source and binary forms, with or without
278: modification, are permitted provided that the following conditions
279: are met:
280:
281: 1. Redistributions of source code must retain the above copyright
282: notice, this list of conditions and the following disclaimer.
283:
284: 2. Redistributions in binary form must reproduce the above copyright
285: notice, this list of conditions and the following disclaimer in the
286: documentation and/or other materials provided with the distribution.
287:
288: 3. The name of the author may not be used to endorse or promote products
289: derived from this software without specific prior written permission.
290:
291: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
292: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
293: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
294: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
295: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
296: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
297: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
298: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
299: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
300: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301: */
|