KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > QueryResultsStream


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

5
6 package com.hp.hpl.jena.rdql;
7
8 import java.util.*;
9
10 /** The main QueryResults implementation for returning results from queries.
11  * This version is "use once" - you can not reset the result set because
12  * the resulys of the query are not remembered so as not to consume potentially
13  * large amounts of memory.
14  *
15  * @see Query
16  * @see QueryEngine
17  * @see ResultBindingImpl
18  * @see QueryResultsStream
19  *
20  * @author Andy Seaborne
21  * @version $Id: QueryResultsStream.java,v 1.8 2005/02/21 12:15:25 andy_seaborne Exp $
22  */

23
24 public class QueryResultsStream implements QueryResults
25 {
26     Iterator queryExecutionIter ;
27     QueryExecution queryExecution ;
28     List resultVars ;
29     ResultBinding currentEnv ;
30     int rowNumber ;
31     volatile boolean finished = false ;
32
33     
34     public QueryResultsStream(Query query, QueryExecution qe, Iterator iter)
35     {
36         queryExecutionIter = iter ;
37         queryExecution = qe ;
38         // Maybe we should take a copy.
39
resultVars = query.getResultVars() ;
40         currentEnv = null ;
41         rowNumber = 0 ;
42     }
43
44     /**
45      * @throws UnsupportedOperationException Always thrown.
46      */

47
48     public void remove() throws java.lang.UnsupportedOperationException JavaDoc
49     {
50         throw new java.lang.UnsupportedOperationException JavaDoc("com.hp.hpl.jena.rdf.query.QueryResults.remove") ;
51     }
52
53     /**
54      * Is there another possibility?
55      */

56     public boolean hasNext()
57     {
58         return queryExecutionIter.hasNext() ;
59     }
60
61     /** Moves onto the next result possibility.
62      * The returned object is actual the binding for this
63      * result; it is possible to access the bound variables
64      * for the current possibility through the additional variable
65      * accessor opertations.
66      */

67     public Object JavaDoc next()
68     {
69         currentEnv = (ResultBinding)queryExecutionIter.next() ;
70         if ( currentEnv != null )
71             rowNumber++ ;
72         return currentEnv ;
73     }
74
75     /** Close the results iterator and stop query evaluation as soon as convenient.
76      */

77
78     public void close()
79     {
80         if ( ! finished )
81         {
82             queryExecution.abort() ;
83             finished = true ;
84         }
85         else
86             queryExecution.close() ;
87     }
88
89     /** Access a binding (a mapping from variable name to value). RDF does not explicitly type values so we only provide a string
90      * form and leave it to the application context to interpret as an integer, date etc.
91      */

92
93     public String JavaDoc getBinding(String JavaDoc name)
94     {
95         // Coudl mask with the result variables list. But we don't.
96
return getBindingWorker(name, false) ;
97     }
98
99     /** Return the "row number" - a count of the number of possibilities returned so far.
100      * Remains valid (as the total number of possibilities) after the iterator ends.
101      */

102
103     public int getRowNumber()
104     {
105         return rowNumber ;
106     }
107
108     /** Get the variable names for the projection
109      */

110
111     public List getResultVars() { return resultVars ; }
112
113     /** Convenience function to consume a query.
114      * Returns a list of {@link ResultBindingImpl}s.
115      *
116      * @return List
117      * @deprecated QueryResultsStream do not have all the results at once - {@link QueryResultsMem}
118      */

119
120     public List getAll()
121     {
122         List all = new ArrayList() ;
123         while(this.hasNext())
124         {
125             all.add(next());
126         }
127         close() ;
128         return all ;
129     }
130
131     private String JavaDoc getBindingWorker(String JavaDoc name, boolean projectResultVars)
132     {
133         if ( ! projectResultVars || resultVars.contains(name) )
134         {
135             Object JavaDoc v = currentEnv.get(name) ;
136             if ( v == null ) return null ;
137             return v.toString() ;
138         }
139         return null ;
140     }
141 }
142
143 /*
144  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
145  * All rights reserved.
146  *
147  * Redistribution and use in source and binary forms, with or without
148  * modification, are permitted provided that the following conditions
149  * are met:
150  * 1. Redistributions of source code must retain the above copyright
151  * notice, this list of conditions and the following disclaimer.
152  * 2. Redistributions in binary form must reproduce the above copyright
153  * notice, this list of conditions and the following disclaimer in the
154  * documentation and/or other materials provided with the distribution.
155  * 3. The name of the author may not be used to endorse or promote products
156  * derived from this software without specific prior written permission.
157  *
158  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168  */

169
Popular Tags