1 22 23 package org.xquark.mediator.plan; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.mediator.DOMUtils.DOMUtils; 28 import org.xquark.mediator.DOMUtils.EvaluationVisitor; 29 import org.xquark.mediator.DOMUtils.Tuple; 30 import org.xquark.mediator.algebra.Algebra; 31 import org.xquark.mediator.runtime.MediatorException; 32 import org.xquark.xpath.datamodel.TypedNode; 33 import org.xquark.xquery.parser.Variable; 34 import org.xquark.xquery.parser.XQueryException; 35 import org.xquark.xquery.parser.XQueryExpression; 36 37 public class OpNotSource extends OpUn { 38 private static final String RCSRevision = "$Revision: 1.12 $"; 42 private static final String RCSName = "$Name: $"; private boolean onlyVar = false; 45 46 public OpNotSource(ExecutionPlan plan, XQueryExpression expression, Variable var, Operator childexp, boolean isLet) throws MediatorException { 47 super(plan, expression, childexp); 48 init(var); 49 this.isLet(isLet); 50 idsize = childOperator.getIdSize(); 51 if (plan.getIdVars().contains(var)) { 52 idsize++; 53 } 54 } 55 56 65 public OpNotSource(ExecutionPlan plan, Algebra depnode) throws MediatorException { 66 super(plan, depnode.getExpression(), null); 67 if (depnode.getPaths().isEmpty()) 68 init((Variable) depnode.getVariables().get(0)); 69 else { 70 addPaths(depnode.getPaths()); 71 size = paths.size(); 72 } 73 this.isLet(depnode.isLet()); 74 if (depnode.hasIdentifier()) { 75 idsize = 1; 76 } 77 } 78 79 private void init(Variable var) { 80 onlyVar = true; 81 resetPaths(); 82 addPath(var); 83 size = 1; 84 } 85 86 public boolean isOnlyVar() { 87 return onlyVar; 88 } 89 90 94 public void accept(OperatorVisitor visitor) throws MediatorException { 95 visitor.visit(this); 96 } 97 98 104 protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException { 105 return new NotSourceResultSet(this, context, child); 106 } 107 108 112 118 } 119 120 class NotSourceResultSet extends UnResultSet { 121 private static final String RCSRevision = "$Revision: 1.12 $"; 125 private static final String RCSName = "$Name: $"; 128 EvaluationVisitor visitor = null; 129 132 public NotSourceResultSet(OpNotSource operator, DynamicContext context, OperatorRunnable child) throws MediatorException { 133 super(operator, context, child); 134 } 135 136 143 protected void evaluate(boolean non_blocking) throws MediatorException { 144 145 boolean islet = this.operator.isLet(); 146 if (islet) 147 non_blocking = false; 148 Tuple newtuple = null; 149 if (resultset == null) { 150 if (expression != null) { 151 ArrayList nodes = DOMUtils.evalNodes(context, expression, null); 152 if (nodes != null) { 153 if (islet) { 154 if (((OpNotSource) operator).isOnlyVar()) { 155 newtuple = buftuples.newTuple(); 156 for (int i = 0; i < nodes.size(); i++) { 157 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i)); 158 } 159 newtuple.fillIdentifiers(); 160 buftuples.add(newtuple); 161 } else { 162 newtuple = buftuples.newTuple(); 163 for (int i = 0; i < nodes.size(); i++) { 164 ArrayList nodelist = new ArrayList (1); 165 nodelist.add(nodes.get(i)); 166 for (int j = 0; j < paths.size(); j++) { 167 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 168 newtuple.addNodesAtIndex(j, reslist); 169 } 170 } 171 newtuple.fillIdentifiers(); 172 buftuples.add(newtuple); 173 } 174 } else { 175 if (((OpNotSource) operator).isOnlyVar()) { 176 for (int i = 0; i < nodes.size(); i++) { 177 newtuple = buftuples.newTuple(); 178 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i)); 179 newtuple.fillIdentifiers(); 180 buftuples.add(newtuple); 181 } 182 } else { 183 for (int i = 0; i < nodes.size(); i++) { 184 ArrayList nodelist = new ArrayList (1); 185 nodelist.add(nodes.get(i)); 186 newtuple = buftuples.newTuple(); 187 for (int j = 0; j < paths.size(); j++) { 188 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 189 newtuple.addNodesAtIndex(j, reslist); 190 } 191 newtuple.fillIdentifiers(); 192 buftuples.add(newtuple); 193 } 194 } 195 } 196 } 197 } 198 setFinishedWhenEmpty(); 199 } else { 200 while (true) { 201 if (resultset.hasNext()) { 202 Tuple tuple = resultset.next(); 203 if (expression == null) { 204 if (islet) { 205 if (newtuple == null) { 206 newtuple = tuple.getPart(0, operator.getIdSize()); 207 buftuples.add(newtuple); 208 } else 209 newtuple.addNodesAtIndex(0, tuple.getNodesAtIndex(0)); 210 } else { 211 int index = 0; 212 while ((newtuple = tuple.getPart(0, index++, operator.getIdSize())) != null) { 213 buftuples.add(newtuple); 214 } 215 } 216 } else { try { 218 ArrayList tuples = context.getCurrentTuples(); 219 tuples.add(tuple); 220 if (visitor == null) 221 visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager()); 222 visitor.reset(tuples); 223 visitor.setReturnType(EvaluationVisitor.NODE_TYPE); 224 expression.accept(visitor); 225 ArrayList nodes = visitor.getResNodes(); 226 if (nodes != null) { 227 if (islet) { 228 if (((OpNotSource) operator).isOnlyVar()) { 229 if (newtuple == null) { 230 newtuple = buftuples.newTuple(); 231 newtuple.addNodesAtIndex(0, nodes); 232 newtuple.fillIdentifiers(); 233 buftuples.add(newtuple); 234 } else 235 newtuple.addNodesAtIndex(0, nodes); 236 } else { 237 if (newtuple == null) { 238 newtuple = buftuples.newTuple(); 239 for (int i = 0; i < nodes.size(); i++) { 240 ArrayList nodelist = new ArrayList (1); 241 nodelist.add(nodes.get(i)); 242 for (int j = 0; j < paths.size(); j++) { 243 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 244 newtuple.addNodesAtIndex(j, reslist); 245 } 246 } 247 newtuple.fillIdentifiers(); 248 buftuples.add(newtuple); 249 } else { 250 for (int i = 0; i < nodes.size(); i++) { 251 ArrayList nodelist = new ArrayList (1); 252 nodelist.add(nodes.get(i)); 253 for (int j = 0; j < paths.size(); j++) { 254 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 255 newtuple.addNodesAtIndex(j, reslist); 256 } 257 } 258 } 259 } 260 } else { 261 if (((OpNotSource) operator).isOnlyVar()) { 262 for (int i = 0; i < nodes.size(); i++) { 263 newtuple = buftuples.newTuple(); 264 newtuple.addNodeAtIndex(0, (TypedNode) nodes.get(i)); 265 newtuple.fillIdentifiers(); 266 buftuples.add(newtuple); 267 } 268 } else { 269 for (int i = 0; i < nodes.size(); i++) { 270 ArrayList nodelist = new ArrayList (1); 271 nodelist.add(nodes.get(i)); 272 newtuple = buftuples.newTuple(); 273 for (int j = 0; j < paths.size(); j++) { 274 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 275 newtuple.addNodesAtIndex(j, reslist); 276 } 277 newtuple.fillIdentifiers(); 278 buftuples.add(newtuple); 279 } 280 } 281 } 282 } 283 tuples.remove(tuple); 284 } catch (XQueryException e) { 285 throw new MediatorException("AlgNotSource.evaluate: " + e.getMessage(), e); 286 } 287 } 288 if (non_blocking) { 289 if ((buftuples != null) && (!buftuples.isEmpty())) 291 break; 292 } 293 } else { 294 setFinishedWhenEmpty(); 295 resultset.close(); 296 return; 297 } 298 } 299 if (!resultset.hasNext()) { 300 setFinishedWhenEmpty(); 301 resultset.close(); 302 } 303 } 304 } 305 } 306 | Popular Tags |