1 6 7 package com.hp.hpl.jena.db.impl; 8 9 13 14 import com.hp.hpl.jena.graph.*; 15 import com.hp.hpl.jena.graph.query.*; 16 import com.hp.hpl.jena.db.*; 17 import com.hp.hpl.jena.shared.JenaException; 18 19 import java.util.*; 20 21 public class DBQueryHandler extends SimpleQueryHandler { 22 23 private GraphRDB graph; 24 boolean queryOnlyStmt; boolean queryOnlyReif; boolean queryFullReif; private boolean doFastpath; private boolean doImplicitJoin; 31 32 public DBQueryHandler ( GraphRDB graph ) { 33 super(graph); 34 this.graph = graph; 35 if ( graph.reificationBehavior() == GraphRDB.OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING ) { 36 queryFullReif = queryOnlyReif = queryOnlyStmt = false; 37 } else { 38 queryFullReif = queryOnlyReif = false; 39 queryOnlyStmt = true; 40 } 41 doFastpath = true; 42 } 43 44 public void setDoFastpath ( boolean val ) { doFastpath = val; } 45 public boolean getDoFastpath () { return doFastpath; } 46 public void setDoImplicitJoin ( boolean val ) { doImplicitJoin = val; } 47 48 public Stage patternStage( 49 Mapping varMap, 50 ExpressionSet constraints, 51 Triple[] ptn) { 52 final Stage[] stages = new Stage[ptn.length]; 53 int stageCnt = 0; 54 final Integer numStages; 55 DBPattern[] source = new DBPattern[ptn.length]; 56 57 int i; 59 Triple pat; 60 List patternsToDo = new ArrayList(); 61 for(i=0;i<ptn.length;i++) patternsToDo.add(new Integer (i)); 62 DBPattern src; 63 int reifBehavior = graph.reificationBehavior(); 64 65 if ( ((patternsToDo.size() == 1) && !constraints.isComplex()) || 66 (doFastpath == false) ) { 67 for(i=0;i<patternsToDo.size();i++) 69 stages[stageCnt++] = 70 super.patternStage( varMap, constraints, new Triple[] { ptn[i] }); 71 } else { 72 for (i = 0; i < ptn.length; i++) { 73 pat = ptn[i]; 74 src = new DBPattern(pat, varMap); 75 Iterator it = graph.getSpecializedGraphs(); 76 while (it.hasNext()) { 78 SpecializedGraph sg = (SpecializedGraph) it.next(); 79 char sub = sg.subsumes(pat,reifBehavior); 80 if (sub == SpecializedGraph.noTriplesForPattern) 81 continue; 82 src.sourceAdd(sg, sub); 83 if (sub == SpecializedGraph.allTriplesForPattern) { 84 break; 85 } 86 } 87 92 source[i] = src; 93 } 94 95 int minCost, minConnCost; 96 int cost; 97 DBPattern minSrc, unstaged = null; 98 boolean isConnected = false; 99 while (patternsToDo.size() > 0) { 103 minCost = minConnCost = DBPattern.costMax; 104 isConnected = false; 105 minSrc = null; 106 int minIx = -1; 107 for (i = 0; i < patternsToDo.size(); i++) { 108 unstaged = source[((Integer )patternsToDo.get(i)).intValue()]; 109 cost = unstaged.cost(varMap); 110 if (unstaged.isConnected) { 111 if (cost < minConnCost) { 112 minSrc = unstaged; 113 minConnCost = cost; 114 isConnected = true; 115 minIx = i; 116 } 117 } else if ((cost < minCost) && !isConnected) { 118 minCost = cost; 119 minSrc = unstaged; 120 minIx = i; 121 } 122 } 123 if ( minSrc == null ) { 124 src = unstaged; minIx = i-1; 125 } else { 126 src = minSrc; 127 } 128 src.isStaged = true; 129 patternsToDo.remove(minIx); 130 131 List varList = new ArrayList(); ExpressionSet evalCons = new ExpressionSet(); List qryPat = new ArrayList(); qryPat.add(src); 136 boolean doQuery = false; 137 boolean didJoin = false; 138 boolean foundJoin; 139 if (src.isSingleSource()) { 141 src.addFreeVars(varList); 143 do { 144 foundJoin = false; 145 for (i = 0; i < patternsToDo.size(); i++) { 146 unstaged = source[((Integer )patternsToDo.get(i)).intValue()]; 147 if (unstaged.joinsWith(src,varList,queryOnlyStmt,queryOnlyReif,doImplicitJoin)) { 148 qryPat.add(unstaged); 149 patternsToDo.remove(i); 150 unstaged.addFreeVars(varList); 151 unstaged.isStaged = true; 152 foundJoin = didJoin = true; 153 } 154 } 155 } while ( foundJoin && (patternsToDo.size() > 0)); 156 if ( didJoin ) 161 doQuery = true; 162 else { 163 for(i=0;i<varList.size();i++) { 164 VarDesc vx = (VarDesc) varList.get(i); 165 168 if ( (vx.isArgVar == false) && 169 findConstraints(constraints,evalCons,vx) ) 170 doQuery = true; 171 172 } 173 } 174 if ( doQuery ) { 175 for(i=0;i<varList.size();i++) { 177 VarDesc vx = (VarDesc) varList.get(i); 178 if ( vx.isArgVar == false ) 179 vx.bindToVarMap(varMap); 180 } 181 } 182 183 } else if ( !src.hasSource() ) 184 doQuery = true; 185 if ( doQuery ) { 187 stages[stageCnt] = 188 new DBQueryStage(graph,src.hasSource() ? src.singleSource() : null ,varList,qryPat, evalCons); 189 } else { 190 stages[stageCnt] = 191 super.patternStage(varMap,constraints, new Triple[]{src.pattern}); 192 } 193 stageCnt++; 194 } 195 } 196 numStages = new Integer (stageCnt); 197 198 return new Stage() { 199 public Stage connectFrom(Stage s) { 200 for (int i = 0; i < numStages.intValue(); i += 1) { 201 stages[i].connectFrom(s); 202 s = stages[i]; 203 } 204 return super.connectFrom(s); 205 } 206 public Pipe deliver(Pipe L) { 207 return stages[numStages.intValue() - 1].deliver(L); 208 } 209 }; 210 } 211 212 214 public void setQueryOnlyAsserted ( boolean opt ) { 215 if ( (opt == true) && (queryOnlyReif==true) ) 216 throw new JenaException("QueryOnlyAsserted and QueryOnlyReif cannot both be true"); 217 queryOnlyStmt = opt; 218 } 219 220 public boolean getQueryOnlyAsserted() { 221 return queryOnlyStmt; 222 } 223 224 public void setQueryOnlyReified ( boolean opt ) { 225 if ( graph.reificationBehavior() != GraphRDB.OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING ) 226 throw new JenaException("Reified statements cannot be queried for this model's reification style"); 227 if ( (opt == true) && (queryOnlyReif==true) ) 228 throw new JenaException("QueryOnlyAsserted and QueryOnlyReif cannot both be true"); 229 queryOnlyReif = true; 230 throw new JenaException("QueryOnlyReified is not yet supported"); 231 } 232 233 public boolean getQueryOnlyReified() { 234 return queryOnlyReif; 235 } 236 237 public void setQueryFullReified ( boolean opt ) { 238 if ( graph.reificationBehavior() != GraphRDB.OPTIMIZE_ALL_REIFICATIONS_AND_HIDE_NOTHING ) 239 throw new JenaException("Reified statements cannot be queried for this model's reification style"); 240 queryFullReif = true; 241 } 242 243 public boolean getQueryFullReified() { 244 return queryFullReif; 245 } 246 247 248 private boolean findConstraints ( ExpressionSet constraints, ExpressionSet evalCons, VarDesc vx ) { 249 boolean res = false; 250 Iterator it = constraints.iterator(); 251 Expression e; 252 while (it.hasNext()) { 253 e = (Expression) it.next(); 254 if (e.isApply() && e.argCount() == 2) { 255 Expression l = e.getArg(0); 256 if ( l.isVariable() && vx.var.getName().equals(l.getName()) ) { 257 String f = e.getFun(); 258 if ( f.equals(ExpressionFunctionURIs.J_startsWith) || 259 f.equals(ExpressionFunctionURIs.J_startsWithInsensitive) || 260 f.equals(ExpressionFunctionURIs.J_contains) || 261 f.equals(ExpressionFunctionURIs.J_containsInsensitive) || 262 f.equals(ExpressionFunctionURIs.J_EndsWith) || 263 f.equals(ExpressionFunctionURIs.J_endsWithInsensitive) ) { 264 evalCons.add(e); 265 res = true; 270 } 271 } 272 } 273 } 274 return res; 275 } 276 } 277 278 307 | Popular Tags |