001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: AbstractTestReifier.java,v 1.33 2008/01/02 12:05:31 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.test;
008:
009: import java.util.Collections;
010:
011: import com.hp.hpl.jena.db.impl.DBReifier;
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.graph.impl.GraphBase;
014: import com.hp.hpl.jena.shared.*;
015: import com.hp.hpl.jena.util.CollectionFactory;
016: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
017: import com.hp.hpl.jena.vocabulary.RDF;
018:
019: /**
020: Abstract base class for reification tests.
021: @author kers
022: */
023: public abstract class AbstractTestReifier extends GraphTestBase {
024: protected static final ReificationStyle Minimal = ReificationStyle.Minimal;
025: protected static final ReificationStyle Standard = ReificationStyle.Standard;
026: protected static final ReificationStyle Convenient = ReificationStyle.Convenient;
027:
028: protected static final Triple ALL = Triple.create("?? ?? ??");
029:
030: public AbstractTestReifier(String name) {
031: super (name);
032: }
033:
034: public abstract Graph getGraph();
035:
036: public abstract Graph getGraph(ReificationStyle style);
037:
038: protected final Graph getGraphWith(String facts) {
039: Graph result = getGraph();
040: graphAdd(result, facts);
041: return result;
042: }
043:
044: /**
045: Answer the empty graph if cond is false, otherwise the graph with the given facts.
046: */
047: protected final Graph graphWithUnless(boolean cond, String facts) {
048: return graphWith(cond ? "" : facts);
049: }
050:
051: protected final Graph graphWithIf(boolean cond, String facts) {
052: return graphWithUnless(!cond, facts);
053: }
054:
055: public void testStyle() {
056: assertSame(Minimal, getGraph(Minimal).getReifier().getStyle());
057: assertSame(Standard, getGraph(Standard).getReifier().getStyle());
058: assertSame(Convenient, getGraph(Convenient).getReifier()
059: .getStyle());
060: }
061:
062: public void testEmptyReifiers() {
063: assertFalse(getGraphWith("x R y").getReifier().findExposed(ALL)
064: .hasNext());
065: assertFalse(getGraphWith("x R y; p S q").getReifier()
066: .findExposed(ALL).hasNext());
067: }
068:
069: public void testSameReifier() {
070: Graph G = getGraph();
071: Reifier R1 = G.getReifier();
072: G.add(triple("x R y"));
073: assertTrue("same reifier", R1 == G.getReifier());
074: }
075:
076: public void testReifierClosed() {
077: Graph g = getGraph();
078: Reifier r = g.getReifier();
079: g.close();
080: }
081:
082: public void testParent() {
083: Graph G = getGraph(), H = getGraph();
084: assertTrue("correct reifier (G)", G == G.getReifier()
085: .getParentGraph());
086: assertTrue("correct reifier (H)", H == H.getReifier()
087: .getParentGraph());
088: }
089:
090: public void testIntercept() {
091: Graph g = getGraph(Convenient);
092: Reifier r = g.getReifier();
093: Node S = node("sub"), O = node("obj");
094: Node RS = node("http://example.org/type");
095: /* */
096: assertFalse("reifier must not intercept quadlet", r
097: .handledAdd(Triple.create(S, RDF.Nodes.type, RS)));
098: assertFalse("reifier must not intercept quadlet", r
099: .handledAdd(Triple.create(S, S, RDF.Nodes.subject)));
100: assertFalse("reifier must not intercept quadlet", r
101: .handledAdd(Triple.create(S, S, RDF.Nodes.type)));
102: /* */
103: assertTrue("reifier must intercept quadlet", r
104: .handledAdd(Triple.create(S, RDF.Nodes.predicate, O)));
105: assertTrue("reifier must intercept quadlet", r
106: .handledAdd(Triple.create(S, RDF.Nodes.type,
107: RDF.Nodes.Statement)));
108: }
109:
110: /**
111: Check that the standard reifier will note, but not hide, reification quads.
112: */
113: public void testStandard() {
114: Graph g = getGraph(Standard);
115: assertFalse(g.getReifier().hasTriple(triple("s p o")));
116: g.add(Triple.create("x rdf:subject s"));
117: assertEquals(1, g.size());
118: g.add(Triple.create("x rdf:predicate p"));
119: assertEquals(2, g.size());
120: g.add(Triple.create("x rdf:object o"));
121: assertEquals(3, g.size());
122: g.add(Triple.create("x rdf:type rdf:Statement"));
123: assertEquals(4, g.size());
124: assertTrue(g.getReifier().hasTriple(triple("s p o")));
125: }
126:
127: /**
128: Test that the Standard reifier will expose implicit quads arising from reifyAs().
129: */
130: public void testStandardExplode() {
131: Graph g = getGraph(Standard);
132: g.getReifier().reifyAs(node("a"), triple("p Q r"));
133: Graph r = Factory.createDefaultGraph(Minimal);
134: graphAdd(r,
135: "a rdf:type rdf:Statement; a rdf:subject p; a rdf:predicate Q; a rdf:object r");
136: assertEquals(4, g.size());
137: assertIsomorphic(r, g);
138: }
139:
140: public void testMinimalExplode() {
141: Graph g = getGraph(Minimal);
142: g.getReifier().reifyAs(node("a"), triple("p Q r"));
143: assertEquals(0, g.size());
144: }
145:
146: public void testReificationTriplesConvenient() {
147: testReificationTriples(Convenient);
148: }
149:
150: public void testReificationTriplesStandard() {
151: testReificationTriples(Standard);
152: }
153:
154: public void testReificationQuadletsMinimal() {
155: testReificationTriples(Minimal);
156: }
157:
158: /**
159: test that a reifier with the given style sees [or not, if it's minimal] the reification quads
160: that are inserted through its graph.
161: */
162: protected void testReificationTriples(ReificationStyle style) {
163: Graph g = getGraph(style);
164: Graph quadlets = getReificationTriples(g.getReifier());
165: String S1 = "SSS rdf:predicate PPP", S2 = "SSS rdf:subject SSS";
166: g.add(triple(S1));
167: assertIsomorphic(graphWithUnless(style == Minimal, S1),
168: quadlets);
169: g.add(triple(S2));
170: assertIsomorphic(graphWithUnless(style == Minimal, S1 + "; "
171: + S2), quadlets);
172: assertEquals("convenient hides quadlets", style == Convenient,
173: g.size() == 0);
174: }
175:
176: /**
177: Ensure that over-specifying a reification means that we don't get a triple
178: back. Goodness knows why this test wasn't in right from the beginning.
179: */
180: public void testOverspecificationSuppressesReification() {
181: Graph g = getGraph(Standard);
182: Reifier r = g.getReifier();
183: graphAdd(g,
184: "x rdf:subject A; x rdf:predicate P; x rdf:object O; x rdf:type rdf:Statement");
185: assertEquals(triple("A P O"), r.getTriple(node("x")));
186: try {
187: graphAdd(g, "x rdf:subject BOOM");
188: assertEquals(null, r.getTriple(node("x")));
189: } catch (AlreadyReifiedException e) {
190: if (r instanceof DBReifier) { /* System.err.println( "! Db reifier must fix over-specification problem" ); */
191: } else
192: throw e;
193: }
194: }
195:
196: public void testReificationSubjectClash() {
197: testReificationClash("x rdf:subject SS");
198: }
199:
200: public void testReificationPredicateClash() {
201: testReificationClash("x rdf:predicate PP");
202: }
203:
204: public void testReificationObjectClash() {
205: testReificationClash("x rdf:object OO");
206: }
207:
208: /**
209: * @param clashingStatement
210: */
211: protected void testReificationClash(String clashingStatement) {
212: Graph g = getGraph(Standard);
213: Triple SPO = Triple.create("S P O");
214: g.getReifier().reifyAs(node("x"), SPO);
215: assertTrue(g.getReifier().hasTriple(SPO));
216: try {
217: graphAdd(g, clashingStatement);
218: assertEquals(null, g.getReifier().getTriple(node("x")));
219: assertFalse(g.getReifier().hasTriple(SPO));
220: } catch (AlreadyReifiedException e) {
221: if (g.getReifier() instanceof DBReifier) { /* System.err.println( "! Db reifier must fix over-specification problem" ); */
222: } else
223: throw e;
224: }
225: }
226:
227: public void testManifestQuadsStandard() {
228: testManifestQuads(Standard);
229: }
230:
231: public void testManifestQuadsConvenient() {
232: testManifestQuads(Convenient);
233: }
234:
235: public void testManifestQuadsMinimal() {
236: testManifestQuads(Minimal);
237: }
238:
239: /**
240: Test that reifying a triple explicitly has some effect on the graph only for Standard
241: reifiers.
242: */
243: public void testManifestQuads(ReificationStyle style) {
244: Graph g = getGraph(style);
245: Reifier r = g.getReifier();
246: r.reifyAs(node("A"), triple("S P O"));
247: String reified = "A rdf:type rdf:Statement; A rdf:subject S; A rdf:predicate P; A rdf:object O";
248: assertIsomorphic(graphWithIf(style == Standard, reified), g);
249: }
250:
251: public void testHiddenVsReificationMinimal() {
252: testHiddenVsReification(Minimal);
253: }
254:
255: public void testHiddenVsStandard() {
256: testHiddenVsReification(Standard);
257: }
258:
259: public void testHiddenVsReificationConvenient() {
260: testHiddenVsReification(Convenient);
261: }
262:
263: public void testHiddenVsReification(ReificationStyle style) {
264: Graph g = getGraph(style);
265: Reifier r = g.getReifier();
266: r.reifyAs(node("A"), triple("S P O"));
267: assertEquals(style == Standard, r.findEither(ALL, false)
268: .hasNext());
269: }
270:
271: public void testRetrieveTriplesByNode() {
272: Graph G = getGraph();
273: Reifier R = G.getReifier();
274: Node N = Node.createAnon(), M = Node.createAnon();
275: R.reifyAs(N, triple("x R y"));
276: assertEquals("gets correct triple", triple("x R y"), R
277: .getTriple(N));
278: R.reifyAs(M, triple("p S q"));
279: assertDiffer("the anon nodes must be distinct", N, M);
280: assertEquals("gets correct triple", triple("p S q"), R
281: .getTriple(M));
282: /* */
283: assertTrue("node is known bound", R.hasTriple(M));
284: assertTrue("node is known bound", R.hasTriple(N));
285: assertFalse("node is known unbound", R.hasTriple(Node
286: .createURI("any:thing")));
287: /* */
288: // Graph GR = R.getReifiedTriples();
289: // assertTrue( "reified triples", getGraphWith( "x R y; p S q" ).isIsomorphicWith(GR) );
290: // assertTrue( "untouched graph", getGraph().isIsomorphicWith(G) );
291: }
292:
293: public void testRetrieveTriplesByTriple() {
294: Graph G = getGraph();
295: Reifier R = G.getReifier();
296: Triple T = triple("x R y"), T2 = triple("y R x");
297: Node N = node("someNode");
298: R.reifyAs(N, T);
299: assertTrue("R must have T", R.hasTriple(T));
300: assertFalse("R must not have T2", R.hasTriple(T2));
301: }
302:
303: public void testReifyAs() {
304: Graph G = getGraph();
305: Reifier R = G.getReifier();
306: Node X = Node.createURI("some:uri");
307: assertEquals("node used", X, R.reifyAs(X, triple("x R y")));
308: assertEquals("retrieves correctly", triple("x R y"), R
309: .getTriple(X));
310: }
311:
312: public void testAllNodes() {
313: Reifier R = getGraph().getReifier();
314: R.reifyAs(node("x"), triple("cows eat grass"));
315: R.reifyAs(node("y"), triple("pigs can fly"));
316: R.reifyAs(node("z"), triple("dogs may bark"));
317: assertEquals("", nodeSet("z y x"), iteratorToSet(R.allNodes()));
318: }
319:
320: public void testRemoveByNode() {
321: Graph G = getGraph();
322: Reifier R = G.getReifier();
323: Node X = node("x"), Y = node("y");
324: R.reifyAs(X, triple("x R a"));
325: R.reifyAs(Y, triple("y R a"));
326: R.remove(X, triple("x R a"));
327: assertFalse("triple X has gone", R.hasTriple(X));
328: assertEquals("triple Y still there", triple("y R a"), R
329: .getTriple(Y));
330: }
331:
332: public void testRemoveFromNothing() {
333: Graph G = getGraph();
334: Reifier R = G.getReifier();
335: G.delete(triple("quint rdf:subject S"));
336: }
337:
338: // public void testRemoveByTriple()
339: // {
340: // Graph G = getGraph();
341: // Reifier R = G.getReifier();
342: // Node X = node( "x" ), Y = node( "y" );
343: // R.reifyAs( X, triple( "x R a" ) );
344: // R.reifyAs( Y, triple( "y R a" ) );
345: // R.remove( triple( "x R a" ) );
346: // assertFalse( "triple X has gone", R.hasTriple( X ) );
347: // assertEquals( "triple Y still there", triple( "y R a" ), R.getTriple( Y ) );
348: // }
349:
350: public void testException() {
351: Graph G = getGraph();
352: Reifier R = G.getReifier();
353: Node X = node("x");
354: R.reifyAs(X, triple("x R y"));
355: R.reifyAs(X, triple("x R y"));
356: try {
357: R.reifyAs(X, triple("x R z"));
358: fail("did not detect already reified node");
359: } catch (AlreadyReifiedException e) {
360: }
361: }
362:
363: public void testKevinCaseA() {
364: Graph G = getGraph(Standard);
365: Node X = node("x"), a = node("a"), b = node("b"), c = node("c");
366: G.add(Triple.create(X, RDF.Nodes.type, RDF.Nodes.Statement));
367: G.getReifier().reifyAs(X, Triple.create(a, b, c));
368: }
369:
370: public void testKevinCaseB() {
371: Graph G = getGraph(Standard);
372: Node X = node("x"), Y = node("y");
373: Node a = node("a"), b = node("b"), c = node("c");
374: G.add(Triple.create(X, RDF.Nodes.subject, Y));
375: try {
376: G.getReifier().reifyAs(X, Triple.create(a, b, c));
377: fail("X already has subject Y: cannot make it a");
378: } catch (CannotReifyException e) {
379: pass();
380: }
381: }
382:
383: /**
384: Test that the hidden triples graph is dynamic, not static.
385: */
386: public void testDynamicHiddenTriples() {
387: Graph g = getGraph(Minimal);
388: Reifier r = g.getReifier();
389: Graph h = getHiddenTriples(r);
390: Graph wanted = graphWith("x rdf:type rdf:Statement"
391: + "; x rdf:subject a" + "; x rdf:predicate B"
392: + "; x rdf:object c");
393: assertTrue(h.isEmpty());
394: r.reifyAs(node("x"), triple("a B c"));
395: assertIsomorphic(wanted, h);
396: }
397:
398: protected Graph getHiddenTriples(final Reifier r) {
399: return new GraphBase() {
400: public ExtendedIterator graphBaseFind(TripleMatch m) {
401: return r.findEither(m, true);
402: }
403: };
404: }
405:
406: public void testQuadRemove() {
407: Graph g = getGraph(Standard);
408: assertEquals(0, g.size());
409:
410: Triple s = Triple.create("x rdf:subject s");
411: Triple p = Triple.create("x rdf:predicate p");
412: Triple o = Triple.create("x rdf:object o");
413: Triple t = Triple.create("x rdf:type rdf:Statement");
414: g.add(s);
415: g.add(p);
416: g.add(o);
417: g.add(t);
418: assertEquals(4, g.size());
419:
420: g.delete(s);
421: g.delete(p);
422: g.delete(o);
423: g.delete(t);
424: assertEquals(0, g.size());
425: }
426:
427: public void testReifierSize() {
428: Graph g = getGraph();
429: Reifier r = g.getReifier();
430: assertEquals(0, r.size());
431: }
432:
433: public void testEmpty() {
434: Graph g = getGraph(Standard);
435: assertTrue(g.isEmpty());
436: graphAdd(g, "x rdf:type rdf:Statement");
437: assertFalse(g.isEmpty());
438: graphAdd(g, "x rdf:subject Deconstruction");
439: assertFalse(g.isEmpty());
440: graphAdd(g, "x rdf:predicate rdfs:subTypeOf");
441: assertFalse(g.isEmpty());
442: graphAdd(g, "x rdf:object LiteraryCriticism");
443: assertFalse(g.isEmpty());
444: }
445:
446: public void testReifierEmptyFind() {
447: Graph g = getGraph(Standard);
448: Reifier r = g.getReifier();
449: assertEquals(CollectionFactory.createHashedSet(),
450: iteratorToSet(r.findExposed(triple("?? ?? ??"))));
451: }
452:
453: public void testReifierFindSubject() {
454: testReifierFind("x rdf:subject S");
455: }
456:
457: public void testReifierFindObject() {
458: testReifierFind("x rdf:object O");
459: }
460:
461: public void testReifierFindPredicate() {
462: testReifierFind("x rdf:predicate P");
463: }
464:
465: public void testReifierFindComplete() {
466: testReifierFind("x rdf:predicate P; x rdf:subject S; x rdf:object O; x rdf:type rdf:Statement");
467: }
468:
469: public void testReifierFindFilter() {
470: Graph g = getGraph(Standard);
471: Reifier r = g.getReifier();
472: graphAdd(g, "s rdf:subject S");
473: assertEquals(tripleSet(""), iteratorToSet(r
474: .findExposed(triple("s otherPredicate S"))));
475: }
476:
477: protected void testReifierFind(String triples) {
478: testReifierFind(triples, "?? ?? ??");
479: }
480:
481: protected void testReifierFind(String triples, String pattern) {
482: Graph g = getGraph(Standard);
483: Reifier r = g.getReifier();
484: graphAdd(g, triples);
485: assertEquals(tripleSet(triples), iteratorToSet(r
486: .findExposed(triple(pattern))));
487: }
488:
489: public void testQuintetBug() {
490: String spec = "rs rdf:type rdf:Statement; foo rdf:value rs; rs rdf:subject X; rs rdf:predicate P; rs rdf:object O1; rs rdf:object O2";
491: Graph g = getGraph(Standard);
492: Reifier r = g.getReifier();
493: try {
494: graphAdd(g, spec);
495: Graph wanted = getGraph(Minimal);
496: graphAdd(wanted, spec);
497: assertIsomorphic(wanted, g);
498: } catch (AlreadyReifiedException e) {
499: if (r instanceof DBReifier) { /* System.err.println( "! Db reifier must fix over-specification problem" ); */
500: } else
501: throw e;
502: }
503: }
504:
505: public void testBulkClearReificationTriples() {
506: Graph g = getGraphWith("x rdf:subject S");
507: g.getBulkUpdateHandler().removeAll();
508: assertEquals("oops: " + g.getClass(), Collections.EMPTY_SET, g
509: .find(Node.ANY, Node.ANY, Node.ANY).toSet());
510: }
511:
512: public void testBulkClearReificationTriples2() {
513: Graph g = getGraphWith("x rdf:subject S; x rdf:predicate P; x rdf:object O; x rdf:type rdf:Statement");
514: g.getBulkUpdateHandler().removeAll();
515: assertEquals(Collections.EMPTY_SET, g.find(Node.ANY, Node.ANY,
516: Node.ANY).toSet());
517: }
518:
519: // public void testKevinCaseC()
520: // {
521: // Graph G = GraphBase.withReification( getGraph() );
522: // Node X = node( "x" ), Y = node( "y" );
523: // Node a = node( "a" ), b = node( "b" ), c = node( "c" );
524: // G.getReifier().reifyAs( X, Triple.create( a, b, c ) );
525: // try
526: // {
527: // G.add( Triple.create( X, Reifier.subject, Y ) );
528: // fail( "X already reifies (a, b, c): cannot give it subject Y" );
529: // }
530: // catch (Reifier.CannotReifyException e)
531: // { /* as requried */ }
532: // }
533:
534: // public void testQuads()
535: // {
536: // Node A = node( "a" ), B = node( "b" );
537: // Graph G = getGraph();
538: // Graph quads = getGraphWith
539: // (
540: // "a " + RDF.type + " " + RDF.Statement
541: // + "; a " + RDF.subject + " x"
542: // + "; a " + RDF.predicate + " R"
543: // + "; a " + RDF.object + " y"
544: // + "; b " + RDF.type + " " + RDF.Statement
545: // + "; b " + RDF.subject + " p"
546: // + "; b " + RDF.predicate + " S"
547: // + "; b " + RDF.object + " q"
548: // );
549: // Reifier R = G.getReifier();
550: // R.reifyAs( A, triple( "x R y") );
551: // R.reifyAs( B, triple( "p S q" ) );
552: // assertEquals( "same", quads, R.getReificationQuads() );
553: // }
554:
555: }
556:
557: /*
558: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
559: All rights reserved.
560:
561: Redistribution and use in source and binary forms, with or without
562: modification, are permitted provided that the following conditions
563: are met:
564:
565: 1. Redistributions of source code must retain the above copyright
566: notice, this list of conditions and the following disclaimer.
567:
568: 2. Redistributions in binary form must reproduce the above copyright
569: notice, this list of conditions and the following disclaimer in the
570: documentation and/or other materials provided with the distribution.
571:
572: 3. The name of the author may not be used to endorse or promote products
573: derived from this software without specific prior written permission.
574:
575: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
576: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
577: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
578: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
579: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
580: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
581: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
582: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
583: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
584: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
585: */
|