KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > GraphTestBase


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]ispo
4   $Id: GraphTestBase.java,v 1.26 2005/04/12 13:33:24 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.graph.test;
8
9 /**
10     An extension of JenaTestBase (which see) with Graph-specific methods.
11     @author kers
12 */

13
14 import com.hp.hpl.jena.util.CollectionFactory;
15 import com.hp.hpl.jena.util.IteratorCollection;
16 import com.hp.hpl.jena.util.iterator.*;
17 import com.hp.hpl.jena.graph.*;
18 import com.hp.hpl.jena.graph.impl.GraphBase;
19 import com.hp.hpl.jena.mem.*;
20 import com.hp.hpl.jena.rdf.model.*;
21 import com.hp.hpl.jena.shared.*;
22 import com.hp.hpl.jena.test.*;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.util.*;
26
27 public class GraphTestBase extends JenaTestBase
28     {
29     public GraphTestBase( String JavaDoc name )
30         { super( name ); }
31         
32     public static Node node( String JavaDoc x )
33         { return Node.create( x ); }
34         
35     public static Model modelFor( Graph g )
36         { return ModelFactory.createModelForGraph( g ); }
37         
38     public static Set iteratorToSet( Iterator L )
39         { return IteratorCollection.iteratorToSet( L ); }
40
41     public static List iteratorToList( Iterator it )
42         { return IteratorCollection.iteratorToList( it ); }
43                 
44     public Set nodeSet( String JavaDoc nodes )
45         {
46         Set result = CollectionFactory.createHashedSet();
47         StringTokenizer st = new StringTokenizer( nodes );
48         while (st.hasMoreTokens()) result.add( node( st.nextToken() ) );
49         return result;
50         }
51         
52     public Set arrayToSet( Object JavaDoc [] elements )
53         { return CollectionFactory.createHashedSet( Arrays.asList( elements ) ); }
54                 
55     public static Triple triple( String JavaDoc fact )
56         { return Triple.create( fact ); }
57     
58     public static Triple triple( PrefixMapping pm, String JavaDoc fact )
59         { return Triple.create( pm, fact ); }
60         
61     public static Triple [] tripleArray( String JavaDoc facts )
62         {
63         ArrayList al = new ArrayList();
64         StringTokenizer semis = new StringTokenizer( facts, ";" );
65         while (semis.hasMoreTokens()) al.add( triple( PrefixMapping.Extended, semis.nextToken() ) );
66         return (Triple []) al.toArray( new Triple [al.size()] );
67         }
68     
69     public static Set tripleSet( String JavaDoc facts )
70         {
71         Set result = new HashSet();
72         StringTokenizer semis = new StringTokenizer( facts, ";" );
73         while (semis.hasMoreTokens()) result.add( triple( semis.nextToken() ) );
74         return result;
75         }
76         
77     public static Node [] nodes( String JavaDoc items )
78         {
79         ArrayList nl = new ArrayList();
80         StringTokenizer nodes = new StringTokenizer( items );
81         while (nodes.hasMoreTokens()) nl.add( node( nodes.nextToken() ) );
82         return (Node []) nl.toArray( new Node [nl.size()] );
83         }
84         
85     public static Graph graphAdd( Graph g, String JavaDoc s )
86         {
87         StringTokenizer semis = new StringTokenizer( s, ";" );
88         while (semis.hasMoreTokens()) g.add( triple( PrefixMapping.Extended, semis.nextToken() ) );
89         return g;
90         }
91         
92     public static Graph newGraph()
93         {
94         Graph result = new GraphMem();
95         result.getPrefixMapping().setNsPrefixes( PrefixMapping.Extended );
96         return result;
97         }
98     
99     public static Graph graphWith( String JavaDoc s )
100         {
101         return graphAdd( newGraph(), s );
102         }
103         
104     public static void assertEqualsTemplate( String JavaDoc title, Graph g, String JavaDoc template )
105         {
106         // assertTrue( title, g.isIsomorphicWith( graphWith( template ) ) );
107
assertIsomorphic( title, graphWith( template ), g );
108         }
109                 
110     public static void assertIsomorphic( String JavaDoc title, Graph expected, Graph got )
111         {
112         if (!expected.isIsomorphicWith( got ))
113             {
114             Map map = CollectionFactory.createHashedMap();
115             fail( title + ": wanted " + nice( expected, map ) + "\nbut got " + nice( got, map ) );
116             }
117         }
118     
119     public static String JavaDoc nice( Graph g, Map bnodes )
120         {
121         StringBuffer JavaDoc b = new StringBuffer JavaDoc( g.size() * 100 );
122         ExtendedIterator it = GraphUtil.findAll( g );
123         while (it.hasNext()) niceTriple( b, bnodes, (Triple) it.next() );
124         return b.toString();
125         }
126     
127     protected static void niceTriple( StringBuffer JavaDoc b, Map bnodes, Triple t )
128         {
129         b.append( "\n " );
130         appendNode( b, bnodes, t.getSubject() );
131         appendNode( b, bnodes, t.getPredicate() );
132         appendNode( b, bnodes, t.getObject() );
133         }
134
135     static int bnc = 1000;
136     
137     protected static void appendNode( StringBuffer JavaDoc b, Map bnodes, Node n )
138         {
139         b.append( ' ' );
140         if (n.isBlank())
141             {
142             Object JavaDoc already = bnodes.get( n );
143             if (already == null) bnodes.put( n, already = "_b" + bnc++ );
144             b.append( already );
145             }
146         else
147             b.append( nice( n ) );
148         }
149     
150     protected static String JavaDoc nice( Node n )
151         { return n.toString( PrefixMapping.Extended, true ); }
152             
153     public static void assertIsomorphic( Graph expected, Graph got )
154         { assertIsomorphic( "graphs must be isomorphic", expected, got ); }
155
156     public static void assertContains( String JavaDoc name, String JavaDoc s, Graph g )
157         {
158         assertTrue( name + " must contain " + s, g.contains( triple( s ) ) );
159         }
160         
161     public static void assertContainsAll( String JavaDoc name, Graph g, String JavaDoc s )
162         {
163         StringTokenizer semis = new StringTokenizer( s, ";" );
164         while (semis.hasMoreTokens()) assertContains( name, semis.nextToken(), g );
165         }
166         
167     public void assertOmits( String JavaDoc name, Graph g, String JavaDoc s )
168         {
169         assertFalse( name + " must not contain " + s, g.contains( triple( s ) ) );
170         }
171         
172     public void assertOmitsAll( String JavaDoc name, Graph g, String JavaDoc s )
173         {
174         StringTokenizer semis = new StringTokenizer( s, ";" );
175         while (semis.hasMoreTokens()) assertOmits( name, g, semis.nextToken() );
176         }
177         
178     public static boolean contains( Graph g, String JavaDoc fact )
179         {
180         return g.contains( triple( fact ) );
181         }
182       
183     public void testContains( Graph g, Triple [] triples )
184         {
185         for (int i = 0; i < triples.length; i += 1)
186             assertTrue( "contains " + triples[i], g.contains( triples[i] ) );
187         }
188
189     public void testContains( Graph g, List triples )
190         {
191         for (int i = 0; i < triples.size(); i += 1)
192              assertTrue( g.contains( (Triple) triples.get(i) ) );
193         }
194
195     public void testContains( Graph g, Iterator it )
196         { while (it.hasNext()) assertTrue( g.contains( (Triple) it.next() ) ); }
197
198     public void testContains( Graph g, Graph other )
199         { testContains( g, GraphUtil.findAll( other ) ); }
200     
201     public void testOmits( Graph g, Triple [] triples )
202         { for (int i = 0; i < triples.length; i += 1) assertFalse( "", g.contains( triples[i] ) ); }
203
204     public void testOmits( Graph g, List triples )
205         {
206         for (int i = 0; i < triples.size(); i += 1)
207              assertFalse( "", g.contains( (Triple) triples.get(i) ) );
208         }
209
210     public void testOmits( Graph g, Iterator it )
211         { while (it.hasNext()) assertFalse( "", g.contains( (Triple) it.next() ) ); }
212
213     public void testOmits( Graph g, Graph other )
214         { testOmits( g, GraphUtil.findAll( other ) ); }
215     
216     public static void show( String JavaDoc title, Graph g )
217         {
218         ClosableIterator it = GraphUtil.findAll( g );
219         System.out.println( title );
220         while (it.hasNext())
221             {
222             Triple t = (Triple) it.next();
223             System.out.println( " " + t.getSubject() + " @" + t.getPredicate() + " " + t.getObject() );
224             }
225         }
226
227     /**
228         Answer an instance of <code>graphClass</code>. If <code>graphClass</code> has
229         a constructor that takes a <code>ReificationStyle</code> argument, then that
230         constructor is run on <code>style</code> to get the instance. Otherwise, if it has a #
231         constructor that takes an argument of <code>wrap</code>'s class before the
232         <code>ReificationStyle</code>, that constructor is used; this allows non-static inner
233         classes to be used for <code>graphClass</code>, with <code>wrap</code> being
234         the outer class instance. If no suitable constructor exists, a JenaException is thrown.
235         
236         @param wrap the outer class instance if graphClass is an inner class
237         @param graphClass a class implementing Graph
238         @param style the reification style to use
239         @return an instance of graphClass with the given style
240         @throws RuntimeException or JenaException if construction fails
241      */

242     public static Graph getGraph( Object JavaDoc wrap, Class JavaDoc graphClass, ReificationStyle style )
243         {
244         try
245             {
246             Constructor JavaDoc cons = getConstructor( graphClass, new Class JavaDoc[] {ReificationStyle.class} );
247             if (cons != null) return (Graph) cons.newInstance( new Object JavaDoc[] { style } );
248             Constructor JavaDoc cons2 = getConstructor( graphClass, new Class JavaDoc [] {wrap.getClass(), ReificationStyle.class} );
249             if (cons2 != null) return (Graph) cons2.newInstance( new Object JavaDoc[] { wrap, style } );
250             throw new JenaException( "no suitable graph constructor found for " + graphClass );
251             }
252         catch (RuntimeException JavaDoc e)
253             { throw e; }
254         catch (Exception JavaDoc e)
255             { throw new JenaException( e ); }
256         }
257
258     protected static Graph getReificationTriples( final Reifier r )
259         {
260         return new GraphBase()
261             {
262             public ExtendedIterator graphBaseFind( TripleMatch m ) { return r.find( m ); }
263             };
264         }
265
266         
267     }
268
269 /*
270     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
271     All rights reserved.
272
273     Redistribution and use in source and binary forms, with or without
274     modification, are permitted provided that the following conditions
275     are met:
276
277     1. Redistributions of source code must retain the above copyright
278        notice, this list of conditions and the following disclaimer.
279
280     2. Redistributions in binary form must reproduce the above copyright
281        notice, this list of conditions and the following disclaimer in the
282        documentation and/or other materials provided with the distribution.
283
284     3. The name of the author may not be used to endorse or promote products
285        derived from this software without specific prior written permission.
286
287     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
288     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
289     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
290     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
291     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
292     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
293     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
294     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
295     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
296     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297 */

298
Popular Tags