1 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 21 22 23 public class QueryResultsMem implements QueryResultsRewindable 24 { 25 static final boolean DEBUG = false; 26 List rows = new ArrayList(); 28 List varNames = null ; 29 int rowNumber = 0 ; 30 Iterator iterator = null ; 31 32 36 37 public QueryResultsMem(QueryResultsMem imrs2) 38 { 39 this(imrs2, false) ; 40 } 41 42 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 rows = imrs2.rows ; 61 reset() ; 62 } 63 64 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 97 public QueryResultsMem(Model model) 98 { 99 buildFromDumpFormat(model); 100 } 101 102 107 108 public QueryResultsMem(String urlStr) 109 throws java.io.FileNotFoundException 110 { 111 Model m = FileManager.get().loadModel(urlStr) ; 112 buildFromDumpFormat(m); 113 } 114 115 116 120 121 public void remove() throws java.lang.UnsupportedOperationException 122 { 123 throw new java.lang.UnsupportedOperationException ( 124 "QueryResultsMem: Attempt to remove an element"); 125 } 126 127 130 public boolean hasNext() { return iterator.hasNext() ; } 131 132 135 136 public Object next() { rowNumber++ ; return iterator.next() ; } 137 138 141 142 public void close() 143 { 144 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 157 public int getRowNumber() { return rowNumber ; } 158 159 161 public int size() { return rows.size() ; } 162 163 165 public List getResultVars() { return varNames ; } 166 167 173 174 public List getAll() { return rows ; } 175 176 178 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 StmtIterator rVarsIter = root.listProperties(ResultSet.resultVariable) ; 191 for ( ; rVarsIter.hasNext() ; ) 192 { 193 String varName = rVarsIter.nextStatement().getString() ; 194 varNames.add(varName) ; 195 } 196 rVarsIter.close() ; 197 int count = 0 ; 199 StmtIterator solnIter = root.listProperties(ResultSet.solution) ; 200 for ( ; solnIter.hasNext() ; ) 201 { 202 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 var = binding.getRequiredProperty(ResultSet.variable).getString() ; 212 RDFNode val = binding.getRequiredProperty(ResultSet.value).getObject() ; 213 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 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 264 265 266 public Model toModel() 267 { 268 Model m = ModelFactory.createDefaultModel() ; 269 asRDF(m) ; 270 return m ; 271 } 272 273 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 289 290 public void list(PrintWriter pw) 291 { 292 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 328 | Popular Tags |