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.Value; 34 import org.xquark.xquery.parser.Variable; 35 import org.xquark.xquery.parser.XQueryExpression; 36 37 public class OpValue 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 OpValue(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)).getStringValue().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 ValueResultSet(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 ValueResultSet extends ZeroResultSet { 172 private static final String RCSRevision = "$Revision: 1.9 $"; 176 private static final String RCSName = "$Name: $"; 177 183 public ValueResultSet(OpValue operator, DynamicContext context) throws MediatorException { 184 super(operator, context); 185 } 186 187 198 199 201 protected void evaluate(boolean non_blocking) throws MediatorException { 202 XQueryExpression condition = ((OpValue) operator).getcondition(); 204 Tuple tuple; 205 if (expression instanceof Value) { 206 if (condition != null) { 207 if (!DOMUtils.evalCondition(context, condition, null)) { 208 setFinishedWhenEmpty(); 209 return; 210 } 211 } 212 tuple = new Tuple(1, this.operator.getIdSize()); 213 tuple.addNodeAtIndex(0, DOMUtils.makeTextNode((Value) expression, this.operator.getPlan().getSchemaManager())); 214 if (this.operator.getIdSize() > 0) 215 tuple.fillIdentifiers(); 216 buftuples.add(tuple); 217 } else { 218 ArrayList resnodes = DOMUtils.evalNodes(context, expression, null); 219 if (resnodes != null && !resnodes.isEmpty()) { 220 if (operator.isLet()) { 221 tuple = new Tuple(operator.getSize(), operator.getIdSize()); 222 if (((OpValue) operator).isOnlyVar()) { 223 tuple.addNodesAtIndex(0, resnodes); 224 tuple.setParent(buftuples); 225 if (this.operator.getIdSize() > 0) 226 tuple.fillIdentifiers(); 227 if (condition == null || DOMUtils.evalCondition(context, condition, tuple)) 228 buftuples.add(tuple); 229 } else { 230 boolean doAdd = false; 231 for (int i = 0; i < resnodes.size(); i++) { 232 ArrayList nodelist = new ArrayList (1); 233 nodelist.add(resnodes.get(i)); 234 for (int j = 0; j < paths.size(); j++) { 236 if (paths.get(j).toString().equals(expression.getStringValue())) { 237 tuple.addNodeAtIndex(j, (TypedNode) resnodes.get(i)); 238 doAdd = true; 239 } 240 else { 241 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 242 if (reslist != null) { 243 tuple.addNodesAtIndex(j, reslist); 244 doAdd = true; 245 } 246 } 247 } 248 } 249 if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) { 250 tuple.fillIdentifiers(); 251 buftuples.add(tuple); 252 } 253 } 254 255 } else { 256 if (((OpValue) operator).isOnlyVar()) { 257 for (int i = 0; i < resnodes.size(); i++) { 258 tuple = new Tuple(1, this.operator.getIdSize()); 259 tuple.addNodeAtIndex(0, (TypedNode) resnodes.get(i)); 260 tuple.setParent(buftuples); 261 if (this.operator.getIdSize() > 0) 262 tuple.fillIdentifiers(); 263 if (condition == null || DOMUtils.evalCondition(context, condition, tuple)) 264 buftuples.add(tuple); 265 } 266 } else { 267 for (int i = 0; i < resnodes.size(); i++) { 268 ArrayList nodelist = new ArrayList (1); 269 nodelist.add(resnodes.get(i)); 270 tuple = buftuples.newTuple(); 271 boolean doAdd = false; 272 for (int j = 0; j < paths.size(); j++) { 274 if (paths.get(j).toString().equals(expression.getStringValue())) { 275 tuple.addNodeAtIndex(j, (TypedNode) resnodes.get(i)); 276 doAdd = true; 277 } 278 else { 279 ArrayList reslist = DOMUtils.getSteps(nodelist, (XQueryExpression) paths.get(j)); 280 if (reslist != null) { 281 tuple.addNodesAtIndex(j, reslist); 282 doAdd = true; 283 } 284 } 285 } 286 if (doAdd && (condition == null || DOMUtils.evalCondition(context, condition, tuple))) { 287 tuple.fillIdentifiers(); 288 buftuples.add(tuple); 289 } 290 } 291 } 292 } 293 } else 294 throw new MediatorException("OpValue : Could not evaluate expression : " + expression); 295 } 296 297 setFinishedWhenEmpty(); 298 } 299 } 300 | Popular Tags |