1 23 package org.objectweb.medor.query.lib; 24 25 import java.util.List ; 26 27 import org.objectweb.medor.api.MedorException; 28 import org.objectweb.medor.expression.api.Expression; 29 import org.objectweb.medor.expression.api.MalformedExpressionException; 30 import org.objectweb.medor.expression.lib.And; 31 import org.objectweb.medor.filter.api.FieldOperand; 32 import org.objectweb.medor.filter.lib.BasicFieldOperand; 33 import org.objectweb.medor.filter.lib.MemberOf; 34 import org.objectweb.medor.lib.Log; 35 import org.objectweb.medor.query.api.PropagatedField; 36 import org.objectweb.medor.query.api.QueryLeaf; 37 import org.objectweb.medor.query.api.QueryNode; 38 import org.objectweb.medor.query.api.QueryTree; 39 import org.objectweb.medor.query.api.QueryTreeField; 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.util.monolog.api.Logger; 42 43 46 public class MemberOfHelper { 47 48 public static Logger logger = Log.getLoggerFactory() 49 .getLogger("org.objectweb.medor.query.lib.MemberOfHelper"); 50 51 73 public static void addMemberOf(QueryNode qn, List mofLeft, List mofRight) 74 throws MedorException, MalformedExpressionException { 75 int size = mofLeft.size(); 76 Expression left, right; 77 for (int i = 0; i < size; i++) { 78 logger.log(BasicLevel.DEBUG, "Handling field number " +i); 79 left = (Expression) mofLeft.get(i); 80 right = (Expression) mofRight.get(i); 81 if (left instanceof FieldOperand) { 82 QueryTreeField leftField = (QueryTreeField) ((FieldOperand) left) 83 .getField(); 84 QueryTreeField rightField = (QueryTreeField) ((FieldOperand) right) 85 .getField(); 86 qn = pushDown(leftField, rightField, mofLeft, mofRight, qn, i); 87 } 88 } 89 MemberOf mof = new MemberOf(mofLeft, mofRight); 90 91 if (qn.getQueryFilter() != null) { 92 qn.setQueryFilter(new And(qn.getQueryFilter(), mof)); 93 } else { 94 qn.setQueryFilter(mof); 95 } 96 } 97 98 111 private static QueryNode pushDown(QueryTreeField leftField, 112 QueryTreeField rightField, 113 List mofLeft, 114 List mofRight, 115 QueryNode qn, 116 int i) throws MalformedExpressionException, MedorException { 117 logger.log(BasicLevel.DEBUG, "Entering pushDown for fields " + 118 leftField.getName() + " and " + rightField.getName()); 119 QueryTree qt1 = leftField.getQueryTree(); 120 QueryTree qt2 = rightField.getQueryTree(); 121 if (!(qt1 instanceof QueryLeaf) && (qt1 == qt2)) { 122 logger.log(BasicLevel.DEBUG, "Fields on the same tree " + qt1); 123 if ((rightField instanceof PropagatedField) 125 && (leftField instanceof PropagatedField)) { 126 QueryTreeField leftFDown = (QueryTreeField) ((PropagatedField) leftField) 127 .getPreviousFields()[0]; 128 QueryTreeField rightFDown = (QueryTreeField) ((PropagatedField) rightField) 129 .getPreviousFields()[0]; 130 QueryTree qtLDown = leftFDown.getQueryTree(); 131 if (qtLDown != rightFDown.getQueryTree()) { 132 logger.log(BasicLevel.DEBUG, "Propagated from fields on different trees: stop recursion"); 134 mofLeft.set(i, 135 new BasicFieldOperand(((PropagatedField) leftField) 136 .getPreviousFields()[0])); 137 mofRight.set(i, 138 new BasicFieldOperand( 139 ((PropagatedField) rightField) 140 .getPreviousFields()[0])); 141 ((QueryNode) qt2).removeField(rightField.getName()); 143 return (QueryNode) qt1; 145 } else { 146 logger.log(BasicLevel.DEBUG, "Propagated from fields on the same tree: go on with recursion"); 147 QueryNode qtPushed = pushDown(leftFDown, rightFDown, mofLeft, mofRight, 149 (QueryNode) qtLDown, i); 150 ((QueryNode) qt2).removeField(rightField.getName()); 152 return qtPushed; 153 } 154 } else { 155 throw new MalformedExpressionException( 156 "The helper cannot deal with two fields on the same query node which are not propagated fields"); 157 } 158 } else { 159 return qn; 160 } 161 } 162 163 } | Popular Tags |