KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > processors > QueryProcessorRDQL


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server.processors;
7
8
9 import org.joseki.util.* ;
10 import org.joseki.vocabulary.*;
11 import org.joseki.server.* ;
12
13 import com.hp.hpl.jena.rdf.model.* ;
14
15 import com.hp.hpl.jena.rdql.* ;
16
17 import org.apache.commons.logging.* ;
18 import java.util.* ;
19
20 /** Query processor that executes an RDQL query on a model
21  * and returns the smallest subgraph that gives the same query results.
22  * The client may then recreate the variable bindings if it so wishes.
23  *
24  * @see com.hp.hpl.jena.joseki.HttpQuery
25  * @see com.hp.hpl.jena.joseki.QueryEngineHTTP
26  *
27  * @author Andy Seaborne
28  * @version $Id: QueryProcessorRDQL.java,v 1.10 2004/04/30 14:13:11 andy_seaborne Exp $
29  */

30
31 public class QueryProcessorRDQL extends QueryProcessorCom implements QueryProcessor
32 {
33     static Log logger = LogFactory.getLog(QueryProcessorRDQL.class.getName()) ;
34     
35     public QueryProcessorRDQL()
36     {
37         super() ;
38     }
39
40     public String JavaDoc getInterfaceURI() { return JosekiVocab.queryOperationRDQL ; }
41         
42     public Model execQuery(ModelSource src, String JavaDoc queryString, Request request)
43         throws RDFException, QueryExecutionException
44     {
45         if (!(src instanceof ModelSourceJena))
46             throw new QueryExecutionException(
47                 ExecutionError.rcOperationNotSupported,
48                 "Wrong implementation - this RDQL processor works with Jena models");
49         Model model = ((ModelSourceJena)src).getModel() ;
50         QueryResults results = null ;
51         
52         try {
53             if ( queryString == null || queryString.equals("") )
54             {
55                 logger.debug("Query: No query string provided") ;
56             }
57             else
58             {
59                 String JavaDoc tmp = queryString ;
60                 tmp = tmp.replace('\n', ' ') ;
61                 tmp = tmp.replace('\r', ' ') ;
62                 logger.debug("Query: "+tmp) ;
63             }
64             
65             if (queryString == null)
66                 throw new QueryExecutionException(
67                     ExecutionError.rcQueryExecutionFailure,
68                     "No query supplied");
69             
70             Query query = null ;
71             try {
72                 query = new Query(queryString) ;
73             } catch (Throwable JavaDoc thrown)
74             {
75                 logger.info("Query parse error: request failed") ;
76                 throw new QueryExecutionException(ExecutionError.rcQueryParseFailure, "Parse error") ;
77             }
78             
79             query.setSource(model) ;
80
81             // Should choose a better execution engine that specialises in building a single subgraph
82
QueryExecution qe = new QueryEngine(query) ;
83             
84             results = qe.exec();
85             String JavaDoc format = request.getParam("format");
86             // EXPERIMENTAL/undocumented
87
String JavaDoc closure$ = request.getParam("closure");
88             boolean closure = (closure$ != null && closure$.equalsIgnoreCase("true"));
89
90             Model resultModel = ModelFactory.createDefaultModel();
91
92             // EXPERIMENTAL/undocumented : closure over the result
93
// variables
94
if (closure)
95             {
96                 doClosure(query, results, resultModel);
97                 return resultModel;
98             }
99
100             if (format == null)
101             {
102                 for (; results.hasNext();)
103                 {
104                     ResultBinding rb = (ResultBinding)results.next();
105                     rb.mergeTriples(resultModel);
106                 }
107                 resultModel.setNsPrefixes(model);
108                 Map m = src.getPrefixes();
109                 if (m != null)
110                     resultModel.setNsPrefixes(src.getPrefixes());
111                 
112                 return resultModel;
113             }
114
115             if (format.equalsIgnoreCase("BV") || format.equals("http://jena.hpl.hp.com/2003/07/query/BV"))
116             {
117                 QueryResultsFormatter qfmt = new QueryResultsFormatter(results);
118                 qfmt.asRDF(resultModel);
119                 qfmt.close() ;
120                 resultModel.setNsPrefix("rs", "http://jena.hpl.hp.com/2003/03/result-set#");
121                 return resultModel;
122             }
123
124             throw new QueryExecutionException(
125                 ExecutionError.rcOperationNotSupported,
126                 "Unknown results format requested: " + format);
127         }
128         catch (QueryException qEx)
129         {
130             logger.info("Query execution error: "+qEx) ;
131             throw new QueryExecutionException(ExecutionError.rcQueryExecutionFailure, null) ;
132         }
133         finally
134         {
135             if ( results != null )
136                 results.close() ;
137         }
138     }
139     
140     public Model execQuery(ModelSource aModel, Model queryModel, Request request)
141         throws RDFException, QueryExecutionException
142     {
143         Model resultModel = null ;
144         NodeIterator nIter = queryModel.listObjectsOfProperty(JosekiVocab.queryScript) ;
145         for ( ; nIter.hasNext() ; )
146         {
147             RDFNode n = nIter.nextNode() ;
148             if ( n instanceof Literal )
149             {
150                 String JavaDoc queryString = ((Literal)n).getString() ;
151                 Model m = execQuery(aModel, queryString, request) ;
152                 if ( resultModel == null )
153                     resultModel = m ;
154                 else
155                     resultModel.add(m) ;
156             }
157         }
158         return resultModel ;
159     }
160     
161     private void doClosure(Query query, QueryResults results, Model resultModel)
162     {
163         for (; results.hasNext() ; )
164         {
165             ResultBinding rb = (ResultBinding)results.next() ;
166             for ( Iterator iter = query.getResultVars().iterator() ; iter.hasNext() ; )
167             {
168                 String JavaDoc var = (String JavaDoc)iter.next() ;
169                 Object JavaDoc x = rb.get(var) ;
170                 if ( ! ( x instanceof Resource ) )
171                 {
172                     logger.warn("Non-resource in query closure: "+x) ;
173                     continue ;
174                 }
175                 Closure.closure((Resource)x, false, resultModel) ;
176             }
177         }
178     }
179     
180 }
181
182 /*
183  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
184  * All rights reserved.
185  *
186  * Redistribution and use in source and binary forms, with or without
187  * modification, are permitted provided that the following conditions
188  * are met:
189  * 1. Redistributions of source code must retain the above copyright
190  * notice, this list of conditions and the following disclaimer.
191  * 2. Redistributions in binary form must reproduce the above copyright
192  * notice, this list of conditions and the following disclaimer in the
193  * documentation and/or other materials provided with the distribution.
194  * 3. The name of the author may not be used to endorse or promote products
195  * derived from this software without specific prior written permission.
196  *
197  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
204  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
205  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
206  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
207  */

208
Popular Tags