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.Tuple; 29 import org.xquark.mediator.algebra.Algebra; 30 import org.xquark.mediator.decomposer.Utils; 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 OpEval extends ZeroOp { 38 private static final String RCSRevision = "$Revision: 1.9 $"; 42 private static final String RCSName = "$Name: $"; 43 44 private XQueryExpression condition = null; 45 private boolean onlyVar = false; 46 52 public OpEval(ExecutionPlan plan, Algebra algebra, XQueryExpression condition, boolean hasIdentifier) throws MediatorException { 53 super(plan, algebra.getExpression()); 54 this.condition = condition; 55 60 Variable var = (Variable) algebra.getVariables().get(0); 61 if (algebra.getPaths().isEmpty()) { 62 addPath(var); 63 size = 1; 64 onlyVar = true; 65 } else { 66 ArrayList tmpPaths = algebra.getPaths(); 67 addPaths(tmpPaths); 68 size = tmpPaths.size(); 69 if (tmpPaths.size() == 1 && ((XQueryExpression)tmpPaths.get(0)).toString().equals(var.getStringValue())) 70 onlyVar = true; 71 } 72 idsize = 0; 73 if (plan.getIdVars().contains(var) || hasIdentifier) 74 idsize++; 75 this.isLet(algebra.isLet()); 76 } 77 78 89 public XQueryExpression getcondition() { 90 return this.condition; 91 } 92 93 public Operator addCondition(ExecutionPlan plan, XQueryExpression expr) throws MediatorException { 94 if (condition == null) { 95 this.condition = expr; 96 return this; 97 } else 98 return new OpRestrict(plan, expr, this); 99 } 100 101 public boolean isOnlyVar() { 102 return onlyVar; 103 } 104 105 109 public void accept(OperatorVisitor visitor) throws MediatorException { 110 visitor.visit(this); 111 } 112 113 119 protected ResultSet getResultSet(DynamicContext context) throws MediatorException { 120 return new EvalResultSet(this, context); 121 } 122 123 129 public ArrayList getSources() { 130 if (sources == null) { 131 sources = new ArrayList (); 132 } 133 return sources; 134 } 135 136 142 public String toCompleteString(int indent) { 143 StringBuffer buf = new StringBuffer (); 144 buf.append(Utils.makeIndent(indent) + "<" + getClass().getName() + " isLet=\"" + isLet() + "\">\n"); 145 buf.append(Utils.makeIndent(indent + 1) + "<Expression>\n"); 146 buf.append(Utils.makeIndent(indent + 2) + expression + "\n"); 147 buf.append(Utils.makeIndent(indent + 1) + "</Expression>\n"); 148 buf.append(Utils.makeIndent(indent + 1) + "<condition>\n"); 149 buf.append(Utils.makeIndent(indent + 2) + condition + "\n"); 150 buf.append(Utils.makeIndent(indent + 1) + "</condition>\n"); 151 if (paths != null) { 159 buf.append(Utils.makeIndent(indent + 1) + "<Paths>\n"); 160 for (int i = 0; i < paths.size(); i++) { 161 buf.append(Utils.makeIndent(indent + 2) + "<Path>" + paths.get(i) + "</Path>\n"); 162 } 163 buf.append(Utils.makeIndent(indent + 1) + "</Paths>\n"); 164 } 165 buf.append(Utils.makeIndent(indent) + "</" + getClass().getName() + ">\n"); 166 return buf.toString(); 167 } 168 169 } 170 171 class EvalResultSet extends ZeroResultSet { 172 private static final String RCSRevision = "$Revision: 1.9 $"; 176 private static final String RCSName = "$Name: $"; 177 183 public EvalResultSet(OpEval operator, DynamicContext context) throws MediatorException { 184 super(operator, context); 185 } 186 187 198 199 protected void evaluate(boolean non_blocking) throws MediatorException { 200 XQueryExpression condition = ((OpEval) operator).getcondition(); 201 Tuple tuple; 202 ArrayList resnodes = DOMUtils.evalNodes(context, expression, null); 203 if (resnodes != null && !resnodes.isEmpty()) { 204 if (operator.isLet()) { 205 tuple = new Tuple(operator.getSize(), operator.getIdSize()); 206 if (((OpEval) operator).isOnlyVar()) { 207 tuple.addNodesAtIndex(0, resnodes); 208 tuple.setParent(buftuples); 209 if (this.operator.getIdSize() > 0) 210 tuple.fillIdentifiers(); 211 if (condition == null || DOMUtils.evalCondition(context, condition, tuple)) 212 buftuples.add(tuple); 213 } else { 214 boolean doAdd = false; 215 for (int j = 0; j < paths.size(); j++) { 216 String pathStrj = paths.get(j).toString(); 217 if (pathStrj.equals(expression.toString())) { 219 tuple.addNodesAtIndex(j, resnodes); 220 doAdd = true; 221 } else { 222 for (int i = 0; i < resnodes.size(); i++) { 223 ArrayList reslist = DOMUtils.getSteps((TypedNode) resnodes.get(i), (XQueryExpression) paths.get(j)); 224 if (reslist != null) { 225 tuple.addNodesAtIndex(j, reslist); 226 doAdd = true; 227 } 228 } 229 } 230 } 231 if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) { 232 tuple.fillIdentifiers(); 233 buftuples.add(tuple); 234 } 235 } 236 237 } else { 238 if (((OpEval) operator).isOnlyVar()) { 239 for (int i = 0; i < resnodes.size(); i++) { 240 tuple = new Tuple(1, this.operator.getIdSize()); 241 tuple.addNodeAtIndex(0, (TypedNode) resnodes.get(i)); 242 tuple.setParent(buftuples); 243 if (this.operator.getIdSize() > 0) 244 tuple.fillIdentifiers(); 245 if (condition == null || DOMUtils.evalCondition(context, condition, tuple)) 246 buftuples.add(tuple); 247 } 248 } else { 249 for (int i = 0; i < resnodes.size(); i++) { 250 ArrayList nodelist = new ArrayList (1); 251 nodelist.add(resnodes.get(i)); 252 tuple = buftuples.newTuple(); 253 boolean doAdd = false; 254 255 for (int j = 0; j < paths.size(); j++) { 256 String pathStrj = paths.get(j).toString(); 257 if (pathStrj.equals(expression.toString())) { 259 tuple.addNodesAtIndex(j, resnodes); 260 doAdd = true; 261 } else { 262 ArrayList reslist = null; 263 try { 264 reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 265 } catch (XQueryException xqe) { 266 throw new MediatorException(xqe.getMessage(), xqe); 267 } 268 if (reslist != null) { 269 tuple.addNodesAtIndex(j, reslist); 270 doAdd = true; 271 } 272 } 273 } 274 if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) { 275 tuple.fillIdentifiers(); 276 buftuples.add(tuple); 277 } 278 } 279 } 280 } 281 } else 282 throw new MediatorException("OpValue : Could not evaluate expression : " + expression); 283 284 setFinishedWhenEmpty(); 285 } 286 } 287 | Popular Tags |