1 23 package org.objectweb.medor.optim.lib; 24 25 import org.objectweb.jorm.type.api.PType; 26 import org.objectweb.medor.api.MedorException; 27 import org.objectweb.medor.expression.api.BinaryLogicalOperator; 28 import org.objectweb.medor.expression.api.BinaryOperator; 29 import org.objectweb.medor.expression.api.Comparator; 30 import org.objectweb.medor.expression.api.Expression; 31 import org.objectweb.medor.expression.lib.And; 32 import org.objectweb.medor.expression.lib.ConditionalAnd; 33 import org.objectweb.medor.expression.lib.ConditionalOr; 34 import org.objectweb.medor.expression.lib.Equal; 35 import org.objectweb.medor.expression.lib.Greater; 36 import org.objectweb.medor.expression.lib.GreaterEqual; 37 import org.objectweb.medor.expression.lib.Lower; 38 import org.objectweb.medor.expression.lib.LowerEqual; 39 import org.objectweb.medor.expression.lib.Not; 40 import org.objectweb.medor.expression.lib.NotEqual; 41 import org.objectweb.medor.expression.lib.Or; 42 import org.objectweb.medor.filter.lib.IsNull; 43 import org.objectweb.medor.query.api.FilteredQueryTree; 44 import org.objectweb.medor.query.api.QueryNode; 45 import org.objectweb.medor.query.api.QueryTree; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 48 61 62 public class PushNotInExpressionRule extends BasicRule { 63 64 public PushNotInExpressionRule() { 65 super("PushNotInExpressionRule"); 66 } 67 68 79 public QueryTree rewrite(QueryTree qt, QueryNode _parent) 80 throws MedorException { 81 if (qt instanceof FilteredQueryTree) { 82 ModifiedExpression me = pushNotInExpression( 83 ((FilteredQueryTree) qt).getQueryFilter(), false); 84 if (me.isModified) 85 ((FilteredQueryTree) qt).setQueryFilter(me.e); 86 } 87 if (qt instanceof QueryNode) { 88 QueryTree[] children = ((QueryNode) qt).getChildren(); 89 for (int i = 0; i < children.length; i++) { 90 rewrite(children[i], (QueryNode) qt); 91 } 92 } 93 return qt; 94 } 95 96 104 public ModifiedExpression pushNotInExpression( 105 Expression e, boolean hasANot) throws MedorException { 106 log.log(BasicLevel.DEBUG, "pushNotInExpression: (" + hasANot + ", exp:" + e + ")"); 107 ModifiedExpression me = new ModifiedExpression(e, false); 108 if (hasANot && e instanceof Comparator) { 109 Expression l = ((BinaryOperator) e).getExpression(0); 110 Expression r = ((BinaryOperator) e).getExpression(1); 111 me.isModified = true; 112 if (e instanceof Equal) 113 me.e = new NotEqual(l, r); 114 else if (e instanceof NotEqual) 115 me.e = new Equal(l, r); 116 else if (e instanceof Lower) 117 me.e = new GreaterEqual(l, r); 118 else if (e instanceof GreaterEqual) 119 me.e = new Lower(l, r); 120 else if (e instanceof LowerEqual) 121 me.e = new Greater(l, r); 122 else if (e instanceof Greater) 123 me.e = new LowerEqual(l, r); 124 else 125 throw new MedorException( 126 "Comparator unmanaged by this method: " + e); 127 } 128 else if (e instanceof Not) 129 return pushNotInExpression(((Not) e).getExpression(), !hasANot); 130 else if (e instanceof IsNull) { 131 if (hasANot) { 132 ((IsNull) me.e).setNot(!((IsNull) me.e).getNot()); 133 me.isModified = true; 134 } 135 } else if (e instanceof BinaryLogicalOperator) { 136 ModifiedExpression lme = pushNotInExpression( 137 ((BinaryOperator) e).getExpression(0), hasANot); 138 ModifiedExpression rme = pushNotInExpression( 139 ((BinaryOperator) e).getExpression(1), hasANot); 140 if (hasANot) { 141 me.isModified = true; 142 if (e instanceof And) 143 me.e = new Or(lme.e, rme.e); 144 else if (e instanceof ConditionalAnd) 145 me.e = new ConditionalOr(lme.e, rme.e); 146 else if (e instanceof Or) 147 me.e = new And(lme.e, rme.e); 148 else if (e instanceof ConditionalOr) 149 me.e = new ConditionalAnd(lme.e, rme.e); 150 else 151 throw new MedorException( 152 "BinaryLogicalOperator unmanaged by this method: " + e); 153 } 154 else { 155 if (lme.isModified) 156 ((BinaryOperator) e).setExpression(0, lme.e); 157 if (rme.isModified) 158 ((BinaryOperator) e).setExpression(1, rme.e); 159 me.isModified = lme.isModified || rme.isModified; 160 } 161 } 162 else if (hasANot && e.getType().getTypeCode() == PType.TYPECODE_BOOLEAN) { 163 me.e = new Not(e); 164 me.isModified = true; 165 } 166 return me; 167 } 168 } 169 | Popular Tags |