1 5 6 7 8 package com.hp.hpl.jena.rdql.parser; 9 10 11 import com.hp.hpl.jena.datatypes.xsd.*; 12 import com.hp.hpl.jena.rdql.*; 13 14 import java.util.* ; 15 16 22 23 public class Q_Query extends SimpleNode 24 { 25 26 public Q_Query(int id) { super(id); } 27 28 public Q_Query(RDQLParser p, int id) { super(p, id); } 29 30 private Query query = null ; 31 32 34 boolean selectAllVars = false ; 35 36 47 50 public void phase2(Query q) 51 { 52 query = q ; 53 try { 54 int numQueryChildren = jjtGetNumChildren() ; 55 58 for ( int j = 0 ; j < numQueryChildren ; j++ ) 59 { 60 Node n = jjtGetChild(j) ; 61 if ( n instanceof Q_PrefixesClause ) 62 { 63 extractPrefixes(q, (Q_PrefixesClause)n) ; 64 } 65 } 66 this.postParse(q) ; 67 68 int i = 0 ; 69 if ( jjtGetChild(i) instanceof Q_SelectClause ) 71 { 72 extractVarList(q, jjtGetChild(i)) ; 73 i++ ; 74 } 75 else 76 throw new RDQL_InternalErrorException("Parser didn't catch absense of select clause") ; 77 78 if ( jjtGetChild(i) instanceof Q_SourceClause ) 80 { 81 int numSources = jjtGetChild(i).jjtGetNumChildren() ; 83 if ( numSources > 1 ) 84 { 85 throw new QueryException("Error: Multiple sources in FROM clause") ; 86 } 87 88 for ( int j = 0 ; j < numSources ; j++ ) 91 { 92 Node n = jjtGetChild(i).jjtGetChild(j).jjtGetChild(0) ; 93 String source = ((Q_URL)n).urlString ; 94 if ( j == 0 ) 96 q.setSourceURL(source) ; 97 } 98 i++ ; 99 } 100 101 103 if ( jjtGetChild(i) instanceof Q_TriplePatternClause ) 104 { 105 extractTriplePatternsFP(q, jjtGetChild(i)) ; 107 i++ ; 108 } 109 else 110 throw new RDQL_InternalErrorException("Parser didn't catch absense of triple patterns") ; 111 112 114 if ( i < numQueryChildren ) 115 { 116 if ( jjtGetChild(i) instanceof Q_ConstraintClause ) 117 { 118 extractConstraints(q, jjtGetChild(i)) ; 119 i++ ; 120 } 121 } 122 } 123 catch (RDQL_InternalErrorException e) { throw e ; } 124 catch (QueryException qEx) { throw qEx; } 125 catch (ClassCastException e) { throw new RDQL_InternalErrorException("Parser generated illegal parse tree: "+e) ; } 126 catch (Exception e) 127 { 128 e.printStackTrace(System.err) ; 129 throw new RDQL_InternalErrorException("Unknown exception: "+e) ; 130 } 131 } 132 133 134 139 140 public String toString() 141 { 142 throw new UnsupportedOperationException ("Q_Query.toString()") ; 143 156 } 157 158 private void extractVarList(Query q, Node node) 159 { 160 int n = node.jjtGetNumChildren() ; 161 selectAllVars = ( n == 0 ) ; 162 163 for ( int i = 0 ; i < n ; i++ ) 164 { 165 Node c = node.jjtGetChild(i) ; 166 if ( ! (c instanceof Q_Var) ) 167 throw new RDQL_InternalErrorException("Internal error: parser created '"+c.getClass().getName()+"' when Q_Var expected") ; 168 Q_Var v = (Q_Var)c ; 169 q.addResultVar(v.varName) ; 170 } 171 } 172 173 175 private void extractTriplePatternsFP(Query q, Node node) 176 { 177 Q_TriplePatternClause tpc = (Q_TriplePatternClause)node ; 178 List patternVars = q.getBoundVars() ; 179 int n = tpc.jjtGetNumChildren() ; 180 for ( int j = 0 ; j < n ; j++ ) 181 { 182 Q_TriplePattern tp = (Q_TriplePattern)tpc.jjtGetChild(j) ; 183 if ( tp.jjtGetNumChildren() != 3 ) 184 throw new RDQL_InternalErrorException("Triple pattern has "+tp.jjtGetNumChildren()+" children") ; 185 186 com.hp.hpl.jena.graph.Node nodeSubj = convertToGraphNode(tp.jjtGetChild(0), q) ; 187 com.hp.hpl.jena.graph.Node nodePred = convertToGraphNode(tp.jjtGetChild(1), q) ; 188 com.hp.hpl.jena.graph.Node nodeObj = convertToGraphNode(tp.jjtGetChild(2), q) ; 189 q.addTriplePattern(nodeSubj, nodePred, nodeObj) ; 190 } 191 192 if ( selectAllVars ) 193 { 194 for ( Iterator iter = patternVars.iterator() ; iter.hasNext() ; ) 195 { 196 String varName = (String )iter.next() ; 197 q.addResultVar(varName) ; 198 } 199 } 200 201 202 } 203 204 207 208 static private com.hp.hpl.jena.graph.Node convertToGraphNode(Node n, Query q) 209 { 210 if ( n instanceof Var ) 211 { 212 String varName = ((Var)n).getVarName() ; 213 q.addBoundVar(varName) ; 214 return com.hp.hpl.jena.graph.Node.createVariable(((Var)n).getVarName()) ; 215 } 216 if ( n instanceof ParsedLiteral) 217 { 218 ParsedLiteral v = (ParsedLiteral)n ; 219 220 if ( v.isNode() ) 221 return v.getNode() ; 222 223 if ( v.isURI() ) 224 return com.hp.hpl.jena.graph.Node.createURI(v.getURI()) ; 225 226 if ( v.isString() ) 227 return com.hp.hpl.jena.graph.Node.createLiteral(v.getString(), null, null) ; 228 if ( v.isBoolean()) 229 return com.hp.hpl.jena.graph.Node.createLiteral(v.asUnquotedString(), null,null) ; 230 231 if ( v.isInt() ) 233 return com.hp.hpl.jena.graph.Node.createLiteral( 234 v.asUnquotedString(), null, XSDDatatype.XSDinteger) ; 235 if ( v.isDouble()) 236 return com.hp.hpl.jena.graph.Node.createLiteral( 237 v.asUnquotedString(), null, XSDDatatype.XSDdouble) ; 238 239 String s = v.getString() ; 240 System.err.println("BUG: "+s) ; 241 242 246 } 247 throw new RDQL_InternalErrorException("convertToGraphNode encountered strange type: "+n.getClass().getName()) ; 248 249 } 250 251 private void extractConstraints(Query q, Node node) 252 { 253 Q_ConstraintClause qcc = (Q_ConstraintClause)node ; 254 int n = qcc.jjtGetNumChildren() ; 255 for ( int j = 0 ; j < n ; j++ ) 256 { 257 Object obj = qcc.jjtGetChild(j) ; 258 if ( ! ( obj instanceof Constraint ) ) 259 throw new RDQL_InternalErrorException("Parse node in AND clause isn't a Constraint") ; 260 q.addConstraint((Constraint)obj) ; 262 } 263 } 264 265 private void extractPrefixes(Query q, Q_PrefixesClause qns) 266 { 267 if ( qns == null ) 268 return ; 269 270 int n = qns.jjtGetNumChildren() ; 271 for ( int j = 0 ; j < n ; j++ ) 272 { 273 Q_PrefixDecl qnsd = (Q_PrefixDecl)qns.jjtGetChild(j) ; 274 for ( int k = 0 ; k < qnsd.jjtGetNumChildren() ; k+=2 ) 276 { 277 Q_Identifier id = (Q_Identifier)qnsd.jjtGetChild(k) ; 278 Q_URI uri = (Q_URI)qnsd.jjtGetChild(k+1) ; 280 query.setPrefix(id.toString(), uri.toString()) ; 281 } 282 } 283 } 284 } 285 286 312 | Popular Tags |