KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: QueryMapper.java,v 1.4 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.graph.query.*;
12
13 /**
14     Utility class for <code>ModelQueryUtil</code> which converts a query represented
15     by a model with jqv: variables into a <code>.graph.query.Query</code> object.
16     
17     @author kers
18 */

19 public class QueryMapper
20     {
21     private Query query;
22     private Node [] variables;
23     private Graph graph;
24     
25     public QueryMapper( Model m, Resource [] variables )
26         {
27         super();
28         this.graph = toQueryGraph( m );
29         this.query = new Query( this.graph );
30         this.variables = toQueryVariables( variables );
31         }
32         
33     public Node [] getVariables()
34         { return variables; }
35         
36     public Query getQuery()
37         { return query; }
38         
39     public Graph getGraph()
40         { return graph; }
41
42     public Graph toQueryGraph( Model m )
43         {
44         StmtIterator st = m.listStatements();
45         Graph result = Factory.createDefaultGraph();
46         while (st.hasNext()) result.add( toQueryTriple( st.nextStatement() ) );
47         return result;
48         }
49     
50     public Triple toQueryTriple( Statement s )
51         {
52         return Triple.create
53             (
54             toQueryNode( s.getSubject() ),
55             toQueryNode( s.getPredicate() ),
56             toQueryNode( s.getObject() )
57             );
58         }
59     
60     public Node [] toQueryVariables( Resource [] vars )
61         {
62         Node [] result = new Node[vars.length];
63         for (int i = 0; i < vars.length; i += 1) result[i] = toQueryNode( vars[i] );
64         return result;
65         }
66     
67     static final String JavaDoc varPrefix = "jqv:";
68     
69     public Node toQueryNode( RDFNode rn )
70         {
71         Node n = rn.asNode();
72         return n.isURI() && n.getURI().startsWith( varPrefix )
73             ? Node.createVariable( n.getURI().substring( varPrefix.length() ) )
74             : n;
75         }
76         
77     }
78
79
80 /*
81     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
82     All rights reserved.
83
84     Redistribution and use in source and binary forms, with or without
85     modification, are permitted provided that the following conditions
86     are met:
87
88     1. Redistributions of source code must retain the above copyright
89        notice, this list of conditions and the following disclaimer.
90
91     2. Redistributions in binary form must reproduce the above copyright
92        notice, this list of conditions and the following disclaimer in the
93        documentation and/or other materials provided with the distribution.
94
95     3. The name of the author may not be used to endorse or promote products
96        derived from this software without specific prior written permission.
97
98     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
99     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
100     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
101     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
102     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
103     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
104     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
105     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
106     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
107     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
108 */
Popular Tags