KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.* ;
10
11 import com.hp.hpl.jena.rdf.model.* ;
12 import com.hp.hpl.jena.vocabulary.* ;
13 import com.hp.hpl.jena.shared.*;
14
15 import com.hp.hpl.jena.util.FileManager;
16
17 /**
18  * @author Andy Seaborne
19  * @version $Id: QueryResultsMem.java,v 1.15 2005/02/21 12:15:25 andy_seaborne Exp $
20  */

21
22
23 public class QueryResultsMem implements QueryResultsRewindable
24 {
25     static final boolean DEBUG = false;
26     // The result set in memory
27
List rows = new ArrayList();
28     List varNames = null ;
29     int rowNumber = 0 ;
30     Iterator iterator = null ;
31
32     /** Create an in-memory result set from another one
33      *
34      * @param imrs2 The other QueryResultsMem object
35      */

36
37     public QueryResultsMem(QueryResultsMem imrs2)
38     {
39         this(imrs2, false) ;
40     }
41
42     /** Create an in-memory result set from another one
43      *
44      * @param imrs2 The other QueryResultsMem object
45      * @param takeCopy Should we copy the rows?
46      */

47
48     public QueryResultsMem(QueryResultsMem imrs2, boolean takeCopy)
49     {
50         varNames = imrs2.varNames;
51         if ( takeCopy )
52         {
53             for (Iterator iter = imrs2.rows.iterator(); iter.hasNext();)
54             {
55                 rows.add((ResultBinding) iter.next());
56             }
57         }
58         else
59             // Share results (not the iterator).
60
rows = imrs2.rows ;
61         reset() ;
62     }
63
64     /** Create an in-memory result set from any QueryResults object.
65      * If the QueryResults is an in-memory one already, then no
66      * copying is done - the necessary internal datastructures
67      * are shared. This operation destroys (uses up) a QueryResults
68      * object that is not an in memory one.
69      */

70
71     public QueryResultsMem(QueryResults qr)
72     {
73         if (qr instanceof QueryResultsMem)
74         {
75             QueryResultsMem qrm = (QueryResultsMem) qr;
76             this.rows = qrm.rows;
77             this.varNames = qrm.varNames;
78         }
79         else
80         {
81             varNames = qr.getResultVars();
82             while (qr.hasNext())
83             {
84                 ResultBinding rb = (ResultBinding) qr.next();
85                 rows.add(rb);
86             }
87             qr.close();
88         }
89         reset();
90     }
91
92     /** Prcoess a result set encoded in RDF according to
93      * <code>http://jena.hpl.hp.com/2003/03/result-set#</code>
94      *
95      * @param model
96      */

97     public QueryResultsMem(Model model)
98     {
99         buildFromDumpFormat(model);
100     }
101
102     /** Read in a result set encoded in RDF according to
103      * <code>http://jena.hpl.hp.com/2003/03/result-set#</code>
104      *
105      * @param urlStr URL string
106      */

107
108     public QueryResultsMem(String JavaDoc urlStr)
109         throws java.io.FileNotFoundException JavaDoc
110     {
111         Model m = FileManager.get().loadModel(urlStr) ;
112         buildFromDumpFormat(m);
113     }
114
115
116    // -------- QueryResults interface ------------------------------
117
/**
118      * @throws UnsupportedOperationException Always thrown.
119      */

120
121     public void remove() throws java.lang.UnsupportedOperationException JavaDoc
122     {
123         throw new java.lang.UnsupportedOperationException JavaDoc(
124             "QueryResultsMem: Attempt to remove an element");
125     }
126
127     /**
128      * Is there another possibility?
129      */

130     public boolean hasNext() { return iterator.hasNext() ; }
131
132     /** Moves onto the next result possibility.
133      * The returned object should be of class ResultBindingImpl
134      */

135
136     public Object JavaDoc next() { rowNumber++ ; return iterator.next() ; }
137
138     /** Close the results set.
139      * Should be called on all QueryResults objects
140      */

141
142     public void close()
143     {
144         // Can be rewound - do do throw away.
145
//rows = null ;
146
iterator = null ;
147         varNames = null ;
148         return ;
149     }
150
151     public void rewind( ) { reset() ; }
152
153     public void reset() { iterator = rows.iterator() ; rowNumber = 0 ; }
154
155     /** Return the "row" number for the current iterator item
156      */

157     public int getRowNumber() { return rowNumber ; }
158
159     /** Return the number of rows
160      */

161     public int size() { return rows.size() ; }
162
163     /** Get the variable names for the projection
164      */

165     public List getResultVars() { return varNames ; }
166
167     /** Convenience function to consume a query.
168      * Returns a list of {@link ResultBindingImpl}s.
169      *
170      * @return List
171      * @deprecated Old QueryResults operation
172      */

173
174     public List getAll() { return rows ; }
175
176     // -------- End QueryResults interface ------------------------------
177

178     // Convert from RDF model to in-memory result set
179

180     private void buildFromDumpFormat(Model resultsModel)
181     {
182         varNames = new ArrayList() ;
183         StmtIterator sIter = resultsModel.listStatements(null, RDF.type, ResultSet.ResultSet) ;
184         for ( ; sIter.hasNext() ;)
185         {
186             Statement s = sIter.nextStatement() ;
187             Resource root = s.getSubject() ;
188
189             // Variables
190
StmtIterator rVarsIter = root.listProperties(ResultSet.resultVariable) ;
191             for ( ; rVarsIter.hasNext() ; )
192             {
193                 String JavaDoc varName = rVarsIter.nextStatement().getString() ;
194                 varNames.add(varName) ;
195             }
196             rVarsIter.close() ;
197             // Now the results themselves
198
int count = 0 ;
199             StmtIterator solnIter = root.listProperties(ResultSet.solution) ;
200             for ( ; solnIter.hasNext() ; )
201             {
202                 // foreach row
203
ResultBindingImpl rb = new ResultBindingImpl() ;
204                 count++ ;
205
206                 Resource soln = solnIter.nextStatement().getResource() ;
207                 StmtIterator bindingIter = soln.listProperties(ResultSet.binding) ;
208                 for ( ; bindingIter.hasNext() ; )
209                 {
210                     Resource binding = bindingIter.nextStatement().getResource() ;
211                     String JavaDoc var = binding.getRequiredProperty(ResultSet.variable).getString() ;
212                     RDFNode val = binding.getRequiredProperty(ResultSet.value).getObject() ;
213                     // We include the value even if it is the marker term "rs:undefined"
214
//if ( val.equals(ResultSet.undefined))
215
// continue ;
216
// The QueryResultFormatter code equates null (not found) with
217
// rs:undefined. When Jena JUnit testing, it does not matter if the
218
// recorded result has the term absent or explicitly undefined.
219

220                     rb.add(var, val) ;
221                 }
222                 bindingIter.close() ;
223                 rows.add(rb) ;
224             }
225             solnIter.close() ;
226
227             if ( root.hasProperty(ResultSet.size))
228             {
229                 try {
230                     int size = root.getRequiredProperty(ResultSet.size).getInt() ;
231                     if ( size != count )
232                         System.err.println("Warning: Declared size = "+size+" : Count = "+count) ;
233                 } catch (JenaException rdfEx) {}
234             }
235             sIter.close() ;
236         }
237
238         reset() ;
239     }
240
241     /** Are two result sets the same (isomorphic)?
242      *
243      * @param irs1
244      * @param irs2
245      * @return boolean
246      */

247
248     static public boolean equivalent(
249         QueryResultsMem irs1,
250         QueryResultsMem irs2)
251     {
252         QueryResultsFormatter fmt1 = new QueryResultsFormatter(irs1) ;
253         Model model1 = fmt1.toModel() ;
254
255         QueryResultsFormatter fmt2 = new QueryResultsFormatter(irs2) ;
256         Model model2 = fmt2.toModel() ;
257
258         return model1.isIsomorphicWith(model2) ;
259     }
260
261     /** Encode the result set as RDF.
262      * @return Model Model contains the results
263      */

264
265
266     public Model toModel()
267     {
268         Model m = ModelFactory.createDefaultModel() ;
269         asRDF(m) ;
270         return m ;
271     }
272
273     /** Encode the result set as RDF in the model provided.
274       *
275       * @param model The place where to put the RDF.
276       * @return Resource The resource for the result set.
277       */

278
279     public Resource asRDF(Model model)
280     {
281         QueryResultsFormatter fmt = new QueryResultsFormatter(this) ;
282         Resource r = fmt.asRDF(model) ;
283         fmt.close() ;
284         return r ;
285     }
286
287     /** Print out the result set in dump format. Easeier to read than computed N3
288      */

289
290     public void list(PrintWriter pw)
291     {
292         // Duplicate so we can reset the iterator.
293
QueryResultsMem qrm = new QueryResultsMem(this) ;
294         QueryResultsFormatter fmt = new QueryResultsFormatter(qrm) ;
295         fmt.dump(pw, false) ;
296         qrm.close() ;
297     }
298
299 }
300
301
302 /*
303  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
304  * All rights reserved.
305  *
306  * Redistribution and use in source and binary forms, with or without
307  * modification, are permitted provided that the following conditions
308  * are met:
309  * 1. Redistributions of source code must retain the above copyright
310  * notice, this list of conditions and the following disclaimer.
311  * 2. Redistributions in binary form must reproduce the above copyright
312  * notice, this list of conditions and the following disclaimer in the
313  * documentation and/or other materials provided with the distribution.
314  * 3. The name of the author may not be used to endorse or promote products
315  * derived from this software without specific prior written permission.
316  *
317  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
318  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
319  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
320  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
321  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
322  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
323  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
324  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
325  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
326  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327  */

328
Popular Tags