1 22 23 package org.xquark.mediator.plan; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.mediator.DOMUtils.EvaluationVisitor; 28 import org.xquark.mediator.DOMUtils.Tuple; 29 import org.xquark.mediator.runtime.MediatorException; 30 import org.xquark.xquery.parser.XQueryException; 31 import org.xquark.xquery.parser.XQueryExpression; 32 33 public class OpFunc extends OpUn { 34 private static final String RCSRevision = "$Revision: 1.7 $"; 38 private static final String RCSName = "$Name: $"; 43 public OpFunc(ExecutionPlan plan, XQueryExpression expression, Operator childexp) throws MediatorException { 44 super(plan, expression, childexp); 45 size = 1; 46 idsize = 0; 47 } 48 49 53 public void accept(OperatorVisitor visitor) throws MediatorException { 54 visitor.visit(this); 55 } 56 57 63 protected ResultSet getResultSet(DynamicContext context, OperatorRunnable child) throws MediatorException { 64 return new FuncResultSet(this, context, child); 65 } 66 67 76 78 84 } 85 86 class FuncResultSet extends UnResultSet { 87 private static final String RCSRevision = "$Revision: 1.7 $"; 91 private static final String RCSName = "$Name: $"; EvaluationVisitor visitor = null; 94 public FuncResultSet(OpFunc operator, DynamicContext context, OperatorRunnable child) throws MediatorException { 98 super(operator, context, child); 99 visitor = new EvaluationVisitor(operator.getPlan().getSchemaManager()); 100 } 101 102 113 protected void evaluate(boolean non_blocking) throws MediatorException { 114 118 Tuple newtuple = null; 120 if (resultset == null) { 121 try { 122 ArrayList tuples = context.getCurrentTuples(); 123 visitor = new EvaluationVisitor(this.operator.getPlan().getSchemaManager()); 125 visitor.reset(tuples); 126 visitor.setReturnType(EvaluationVisitor.NODE_TYPE); 127 if (expression != null) { 128 expression.accept(visitor); 129 ArrayList resnodes = visitor.getResNodes(); 130 if (resnodes != null && !resnodes.isEmpty()) { 131 newtuple = buftuples.newTuple(); 132 newtuple.addNodesAtIndex(0,resnodes); 133 buftuples.add(newtuple); 134 } 135 } 136 } catch (XQueryException e) { 138 throw new MediatorException("AlgNotSource.evaluate: " + e.getMessage(), e); 139 } 140 setFinishedWhenEmpty(); 141 } 142 else { 143 if (!resultset.hasNext()) { 144 resultset.close(); 145 setFinishedWhenEmpty(); 146 return; 147 } 148 while (true) { 149 try { 150 Tuple tuple = resultset.next(); 151 tuple.eraseIdSize(); 152 visitor.reset(tuple); 153 visitor.setReturnType(EvaluationVisitor.NODE_TYPE); 154 this.getOperator().getExpression().accept(visitor); 155 ArrayList resnodes = visitor.getResNodes(); 156 if (resnodes != null && !resnodes.isEmpty()) { 157 newtuple = buftuples.newTuple(); 158 newtuple.addNodesAtIndex(0,resnodes); 159 buftuples.add(newtuple); 160 if (non_blocking) 161 break; 162 } 163 } 164 catch (XQueryException e) { 165 throw new MediatorException("Could not evaluate function", e); 166 } 167 } 168 if (!resultset.hasNext()) { 169 resultset.close(); 170 setFinishedWhenEmpty(); 171 } 172 } 173 } 174 } 175 | Popular Tags |