1 5 6 package com.hp.hpl.jena.rdql; 7 8 import java.io.* ; 9 import java.util.* ; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.graph.impl.*; 15 16 import com.hp.hpl.jena.rdf.model.* ; 17 import com.hp.hpl.jena.rdql.parser.Q_Query; 18 import com.hp.hpl.jena.rdql.parser.RDQLParser; 19 20 22 36 37 public class Query 38 { 39 static Log logger = LogFactory.getLog(Query.class) ; 40 41 protected List resultVars = new ArrayList() ; protected List patternVars = new ArrayList() ; protected List triplePatterns = new ArrayList() ; protected List constraints = new ArrayList() ; 48 protected Map prefixMap = new HashMap() ; 49 protected static Map defaultPrefixMap = new HashMap() ; 50 static 51 { 52 defaultPrefixMap.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") ; 53 defaultPrefixMap.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#") ; 54 defaultPrefixMap.put("xsd" , "http://www.w3.org/2001/XMLSchema#") ; 55 defaultPrefixMap.put("owl" , "http://www.w3.org/2002/07/owl#") ; 56 } 57 58 String sourceURL = null ; 61 Model source = null ; 62 63 public long parseTime = -1 ; 65 public long buildTime = -1 ; 66 public long loadTime = -1 ; 67 public long executeTime = -1 ; 68 69 76 77 public Query(String s) 78 { 79 this() ; 80 Q_Query query = null ; 81 try { 82 parseTime = 0; 84 long startTime = 0; 85 87 Reader in = new StringReader(s) ; 89 90 startTime = System.currentTimeMillis(); 91 RDQLParser parser = new RDQLParser(in) ; 92 parser.CompilationUnit(); 93 parseTime = System.currentTimeMillis() - startTime; 94 95 logger.debug("Query parse time: "+parseTime) ; 96 97 query = (Q_Query)parser.top() ; 98 query.phase2(this) ; 100 101 buildTime = System.currentTimeMillis() - parseTime - startTime ; 102 logger.debug("Query parse and build time: "+buildTime) ; 103 } 104 catch (QueryException qEx) { throw qEx ; } 105 catch (Exception e) 106 { 107 throw new QueryException("Parse error: "+e) ; 109 } 110 } 111 112 115 116 public Query() 117 { 118 } 119 120 125 public static QueryResults exec(String queryString) 126 { 127 Query q = new Query(queryString) ; 128 QueryExecution qe = new QueryEngine(q) ; 129 qe.init() ; 130 return qe.exec() ; 131 } 132 133 139 public static QueryResults exec(String queryString, Model model) 140 { 141 Query q = new Query(queryString) ; 142 if ( model != null ) 143 q.setSource(model); 144 QueryExecution qe = new QueryEngine(q) ; 145 qe.init() ; 146 return qe.exec() ; 147 } 148 149 150 157 public static QueryResults exec(String queryString, String dataURL) 158 { 159 Query q = new Query(queryString) ; 160 q.setSourceURL(dataURL) ; 161 QueryExecution qe = new QueryEngine(q) ; 162 qe.init() ; 163 return qe.exec() ; 164 } 165 166 167 170 171 public void setSource(Model m) { source = m ; } 172 173 public Model getSource() { return source ; } 174 175 178 public void setSourceURL(String s) { sourceURL = s ; } 179 public String getSourceURL() { return sourceURL ; } 180 181 182 public List getResultVars() { return resultVars ; } 183 184 185 public void addResultVar(String varName) 186 { 187 if ( !resultVars.contains(varName) ) 188 resultVars.add(varName); 189 } 190 191 192 public List getBoundVars() { return patternVars ; } 193 194 195 public void addBoundVar(String varName) 196 { 197 if ( !patternVars.contains(varName) ) 198 patternVars.add(varName); 199 } 200 201 202 public void addConstraint(Constraint c) { constraints.add(c) ; } 203 204 public List getConstraints() { return constraints ; } 205 206 207 public void addTriplePattern(Triple t) { triplePatterns.add(t) ; } 208 public void addTriplePattern(Node s, Node p, Node o) 209 { 210 Triple t = new Triple(s, p, o) ; 211 triplePatterns.add(t) ; 212 } 213 214 215 public List getTriplePatterns() { return triplePatterns ; } 216 217 218 public void setPrefix(String prefix, String expansion) 219 { 220 prefixMap.put(prefix, expansion) ; 221 } 222 223 224 public String getPrefix(String prefix) 225 { 226 String s = null ; 227 228 if ( prefixMap != null ) 229 s = (String )prefixMap.get(prefix) ; 230 231 if ( s == null ) 232 s = (String )defaultPrefixMap.get(prefix) ; 233 234 return s ; 235 } 236 237 238 public Map getPrefixMap() { return prefixMap ; } 239 240 241 public Map getDefaultPrefixMap() { return defaultPrefixMap ; } 242 243 244 245 public String toString() 247 { 248 StringBuffer sb = new StringBuffer () ; 249 250 sb.append("SELECT ") ; 251 if ( resultVars.size() == 0 ) 252 sb.append("*") ; 253 else 254 { 255 boolean first = true ; 256 for ( Iterator iter = resultVars.iterator() ; iter.hasNext() ; ) 257 { 258 String var = (String )iter.next() ; 259 if ( ! first ) 260 sb.append(", ") ; 261 sb.append("?") ; 262 sb.append(var) ; 263 first = false ; 264 } 265 } 266 sb.append("\n") ; 267 268 270 if ( triplePatterns.size() > 0 ) 272 { 273 sb.append("WHERE ") ; 274 boolean first = true ; 275 for ( Iterator iter = triplePatterns.iterator() ; iter.hasNext() ; ) 276 { 277 Triple tp = (Triple)iter.next() ; 278 if ( ! first ) 279 { 280 sb.append(", \n") ; 281 sb.append(" ") ; 282 283 } 284 sb.append(triplePatternToString(tp)) ; 285 first = false ; 286 } 287 sb.append("\n") ; 288 } 289 290 if ( constraints.size() > 0 ) 292 { 293 for ( Iterator iter = constraints.iterator() ; iter.hasNext() ; ) 294 { 295 Constraint c = (Constraint)iter.next() ; 296 sb.append("AND ") ; 297 sb.append(c.toString()) ; 298 sb.append("\n") ; 299 } 300 } 301 302 if ( prefixMap.size() > 0 ) 303 { 304 sb.append("USING\n") ; 305 boolean first = true ; 306 for ( Iterator iter = prefixMap.keySet().iterator() ; iter.hasNext() ; ) 307 { 308 if ( ! first ) 309 sb.append(" ,\n") ; 310 String k = (String )iter.next() ; 311 String v = (String )prefixMap.get(k) ; 312 sb.append(" "+k+" FOR <"+v+">") ; 313 first = false ; 314 } 315 sb.append("\n") ; 316 } 317 318 return sb.toString() ; 319 } 320 321 private String triplePatternToString(Triple tp) 322 { 323 StringBuffer sb = new StringBuffer () ; 324 sb.append("( ") ; 325 sb.append(slotToString(tp.getSubject())) ; 326 sb.append(", ") ; 327 sb.append(slotToString(tp.getPredicate())) ; 328 sb.append(", ") ; 329 sb.append(slotToString(tp.getObject())) ; 330 sb.append(" )") ; 331 return sb.toString() ; 332 } 333 334 private String slotToString(Node n) 335 { 336 if ( n instanceof Node_Variable) 337 return n.toString() ; 338 if ( n instanceof Node_URI) 339 { 340 String s = applyPrefix(n.getURI()) ; 341 if ( s == null ) 342 return "<"+n.getURI()+">" ; 343 return s ; 344 } 345 346 347 if ( n instanceof Node_Literal) 348 { 349 LiteralLabel lit = ((Node_Literal)n).getLiteral() ; 350 StringBuffer sb = new StringBuffer () ; 351 sb.append('"') ; 352 sb.append(lit.getLexicalForm()) ; 353 sb.append('"') ; 354 if ( lit.language() != null && lit.language().length() > 0 ) 355 { 356 sb.append("@") ; 357 sb.append(lit.language()) ; 358 } 359 if ( lit.getDatatypeURI() != null ) 360 { 361 sb.append("^^<") ; 362 sb.append(lit.getDatatypeURI()) ; 363 sb.append(">") ; 364 } 365 366 return sb.toString() ; 367 } 368 if ( n instanceof Node_ANY ) 371 return "any:"+n ; 373 return "unknown:"+n ; 374 } 375 376 private String applyPrefix(String uri) 377 { 378 String s = applyPrefix(uri, prefixMap, null) ; 379 if ( s == null ) 380 s = applyPrefix(uri, defaultPrefixMap, prefixMap) ; 381 return s ; 382 } 383 384 private String applyPrefix(String uri, Map pm, Map exclude) 385 { 386 Iterator it = pm.entrySet().iterator(); 387 for ( ; it.hasNext() ; ) 388 { 389 Map.Entry e = (Map.Entry) it.next(); 390 String ss = (String ) e.getValue(); 391 if ( uri.startsWith( ss ) ) 392 { 393 if ( exclude != null ) 394 { 395 if ( exclude.containsKey(e.getKey()) && 397 ! exclude.get(e.getKey()).equals(e.getValue()) ) 398 continue ; 399 } 400 return e.getKey()+":"+uri.substring(ss.length() ); 401 } 402 } 403 return null ; 404 } 405 } 406 407 433 | Popular Tags |