1 18 package org.objectweb.medor.optim.lib; 19 20 import org.objectweb.medor.query.api.QueryTree; 21 import org.objectweb.medor.query.api.FilteredQueryTree; 22 import org.objectweb.medor.query.api.QueryNode; 23 import org.objectweb.medor.query.api.PropagatedField; 24 import org.objectweb.medor.query.api.CalculatedField; 25 import org.objectweb.medor.api.Field; 26 import org.objectweb.medor.expression.api.Expression; 27 import org.objectweb.medor.expression.api.Operator; 28 import org.objectweb.medor.filter.api.FieldOperand; 29 30 36 public class QueryNodeReplacer { 37 38 47 public QueryTree replaceNode(QueryTree old, QueryTree neo, QueryTree root) { 48 if (root == old) { 49 return neo; 50 } 51 replaceNode(old.getTupleStructure().getFields(), 52 neo.getTupleStructure().getFields(), 53 root); 54 return root; 55 } 56 57 65 private void replaceNode(Field[] oldFields, Field[] neoFields, QueryTree current) { 66 if (current instanceof FilteredQueryTree) { 67 replaceFields(oldFields, neoFields, 68 ((FilteredQueryTree) current).getQueryFilter()); 69 ((FilteredQueryTree) current).setQueryFilter( 71 ((FilteredQueryTree) current).getQueryFilter()); 72 } 73 if (current instanceof QueryNode) { 74 QueryNode qn = (QueryNode) current; 75 Field[] currentFields = qn.getTupleStructure().getFields(); 76 for (int i = 0; i < currentFields.length; i++) { 77 if (currentFields[i] instanceof PropagatedField) { 78 PropagatedField pf = (PropagatedField) currentFields[i]; 79 Field[] previous = pf.getPreviousFields(); 80 boolean found = false; 81 for (int j = 0; j < previous.length; j++) { 83 for (int k = 0; k < oldFields.length; k++) { 84 if (previous[j] == oldFields[k]) { 85 found = true; 86 previous[j] = neoFields[k]; 88 break; 89 } 90 } 91 } 92 if (found) { 93 pf.replacePreviousField(previous); 94 } 95 } else if (currentFields[i] instanceof CalculatedField) { 96 CalculatedField cf = (CalculatedField) currentFields[i]; 97 replaceFields(oldFields, neoFields, cf.getExpression()); 98 } 99 } 100 QueryTree[] children = qn.getChildren(); 101 for (int i = 0; i < children.length; i++) { 102 replaceNode(oldFields, neoFields, children[i]); 103 } 104 } 105 106 } 107 108 115 public void replaceFields(Field[] oldFields, Field[] neoFields, Expression exp) { 116 if (exp instanceof Operator) { 117 Operator op = (Operator) exp; 118 for (int i = 0; i < op.getOperandNumber(); i++) { 119 replaceFields(oldFields, neoFields, op.getExpression(i)); 120 } 121 } else if (exp instanceof FieldOperand) { 122 FieldOperand fo = (FieldOperand) exp; 123 Field f = fo.getField(); 124 for (int i = 0; i < oldFields.length; i++) { 125 if (f == oldFields[i]) { 126 fo.setField(neoFields[i]); 127 break; } 129 } 130 } 131 } 132 133 } 134 | Popular Tags |