KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > ModelQueryUtil


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ModelQueryUtil.java,v 1.7 2005/02/21 12:18:57 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.util;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.util.iterator.*;
12
13 import java.util.*;
14
15 /**
16     A utility for using the graph query interface from a Model. Queries may be represented
17     as models, where each statement in the model corresponds to a search for matching
18     statements in the model being queried. Variables are represented as resources
19     with URIs using the ficticious "jqv" protocol.
20 <p>
21     See also <code>QueryMapper</code>.
22     
23     @author kers
24 */

25 public class ModelQueryUtil
26     {
27     private ModelQueryUtil()
28         {}
29     
30     public static ExtendedIterator queryBindingsWith
31         ( final Model model, Model query, Resource [] variables )
32         {
33         Map1 mm = new Map1()
34             { public Object JavaDoc map1( Object JavaDoc x ) { return mappy( model, x ); } };
35         QueryMapper qm = new QueryMapper( query, variables );
36         return
37             qm.getQuery().executeBindings( model.getGraph(), qm.getVariables() )
38             .mapWith( mm )
39             ;
40         }
41
42     public static RDFNode asRDF( Model m, Node n )
43         { return m.asRDFNode( n ); }
44         
45     public static List mappy( Model m, Object JavaDoc x )
46         {
47         List L = (List) x;
48         ArrayList result = new ArrayList( L.size() );
49         for (int i = 0; i < L.size(); i += 1) result.add( asRDF( m, (Node) L.get( i ) ) );
50         return result;
51         }
52
53     }
54
55
56 /*
57     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
58     All rights reserved.
59
60     Redistribution and use in source and binary forms, with or without
61     modification, are permitted provided that the following conditions
62     are met:
63
64     1. Redistributions of source code must retain the above copyright
65        notice, this list of conditions and the following disclaimer.
66
67     2. Redistributions in binary form must reproduce the above copyright
68        notice, this list of conditions and the following disclaimer in the
69        documentation and/or other materials provided with the distribution.
70
71     3. The name of the author may not be used to endorse or promote products
72        derived from this software without specific prior written permission.
73
74     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
75     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
77     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
78     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
79     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
80     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
81     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
82     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
83     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84 */
Popular Tags