1 23 24 package org.xquark.mediator.decomposer; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.xquark.xquery.normalize.*; 31 import org.xquark.xquery.parser.*; 32 33 public class AtomizeWhereVisitor extends DefaultParserVisitor { 34 private static final String RCSRevision = "$Revision: 1.5 $"; 38 private static final String RCSName = "$Name: $"; 39 private HashMap varSimpleConditionsMap = null; private HashMap varJoinConditionsMap = null; private HashMap varComplexConditionsMap = null; private HashMap varAllConditionsMap = null; private HashMap varAssociatedVariablesMap = null; private HashMap condAssociatedVariablesMap = null; private ArrayList allSimpleConditionsList = null; 56 private ArrayList allJoinConditionsList = null; 58 private ArrayList allComplexConditionsList = null; 60 private ArrayList allNoVariablesConditionsList = null; 62 private ArrayList allConditionsList = null; 64 private GetBoundVariablesVisitor gbv_visitor = null; 66 67 public AtomizeWhereVisitor() { 71 gbv_visitor = new GetBoundVariablesVisitor(true, false); 72 varSimpleConditionsMap = new HashMap (); 73 varJoinConditionsMap = new HashMap (); 74 varComplexConditionsMap = new HashMap (); 75 varAllConditionsMap = new HashMap (); 76 varAssociatedVariablesMap = new HashMap (); 77 condAssociatedVariablesMap = new HashMap (); 78 allSimpleConditionsList = new ArrayList (); 79 allJoinConditionsList = new ArrayList (); 80 allComplexConditionsList = new ArrayList (); 81 allNoVariablesConditionsList = new ArrayList (); 82 allConditionsList = new ArrayList (); 83 } 84 85 public HashMap getVarSimpleConditionsMap() { 86 return varSimpleConditionsMap; 87 } 88 public HashMap getVarJoinConditionsMap() { 89 return varJoinConditionsMap; 90 } 91 public HashMap getVarComplexConditionsMap() { 92 return varComplexConditionsMap; 93 } 94 public HashMap getVarAllConditionsMap() { 95 return varAllConditionsMap; 96 } 97 public HashMap getVarAssociatedVariablesMap() { 98 return varAssociatedVariablesMap; 99 } 100 public HashMap getCondAssociatedVariablesMap() { 101 return condAssociatedVariablesMap; 102 } 103 public ArrayList getAllSimpleConditionsList() { 104 return allSimpleConditionsList; 105 } 106 public ArrayList getAllJoinConditionsList() { 107 return allJoinConditionsList; 108 } 109 public ArrayList getAllComplexConditionsList() { 110 return allComplexConditionsList; 111 } 112 public ArrayList getAllNoVariablesConditionsList() { 113 return allNoVariablesConditionsList; 114 } 115 public ArrayList getAllConditionsList() { 116 return allConditionsList; 117 } 118 119 134 public void visit(BinOpANDExpression arg) throws XQueryException { 135 arg.getExpression1().accept(this); 136 arg.getExpression2().accept(this); 137 } 138 139 public void visit(ListOpCompExpression arg) throws XQueryException { 140 allConditionsList.add(arg); 141 gbv_visitor.reset(); 142 arg.accept(gbv_visitor); 143 ArrayList vars = gbv_visitor.getAllVariables(); 144 fillVarAssociatedMap(vars); 145 condAssociatedVariablesMap.put(arg,vars); 146 if (vars.isEmpty()) { 147 allNoVariablesConditionsList.add(arg); 148 } else if (vars.size() == 1) { 149 allSimpleConditionsList.add(arg); 150 Variable var0 = (Variable) vars.get(0); 151 addExprToVarMap(var0,arg,varAllConditionsMap); 152 addExprToVarMap(var0,arg,varSimpleConditionsMap); 153 } else if (vars.size() == 2) { 154 gbv_visitor.reset(); 155 arg.getExpression1().accept(gbv_visitor); 156 ArrayList vars1 = gbv_visitor.getAllVariables(); 157 if (vars1.size() != 1) { 158 addExprToVarsMap(vars,arg,varAllConditionsMap); 159 allComplexConditionsList.add(arg); 160 } else { gbv_visitor.reset(); 163 arg.getExpression2().accept(gbv_visitor); 164 ArrayList vars2 = gbv_visitor.getAllVariables(); 165 if (vars2.size() != 1) { 166 addExprToVarsMap(vars,arg,varAllConditionsMap); 167 allComplexConditionsList.add(arg); 168 } else { allJoinConditionsList.add(arg); 171 Map tmpmap = (Map )varJoinConditionsMap.get(vars1.get(0)); 172 if (tmpmap == null) { 173 tmpmap = new HashMap (); 174 varJoinConditionsMap.put(vars1.get(0),tmpmap); 175 } 176 ArrayList tmplist = (ArrayList ) tmpmap.get(vars2.get(0)); 177 if (tmplist == null) { 178 tmplist = new ArrayList (1); 179 tmpmap.put(vars2.get(0), tmplist); 180 } 181 tmplist.add(arg); 182 tmpmap = (Map )varJoinConditionsMap.get(vars2.get(0)); 183 if (tmpmap == null) { 184 tmpmap = new HashMap (); 185 varJoinConditionsMap.put(vars2.get(0),tmpmap); 186 } 187 tmplist = (ArrayList ) tmpmap.get(vars1.get(0)); 188 if (tmplist == null) { 189 tmplist = new ArrayList (1); 190 tmpmap.put(vars1.get(0), tmplist); 191 } 192 tmplist.add(arg); 193 } 194 } 195 } else { for (int i = 0; i < vars.size(); i++) { 197 Variable var0 = (Variable) vars.get(0); 198 ArrayList tmplist = (ArrayList ) varAllConditionsMap.get(var0); 199 if (tmplist == null) { 200 tmplist = new ArrayList (1); 201 varAllConditionsMap.put(var0, tmplist); 202 } 203 tmplist.add(arg); 204 } 205 allComplexConditionsList.add(arg); 206 } 208 209 } 258 259 public void visit(XQueryExpression arg) throws XQueryException { 260 allConditionsList.add(arg); 261 gbv_visitor.reset(); 262 arg.accept(gbv_visitor); 263 ArrayList vars = gbv_visitor.getAllVariables(); 264 fillVarAssociatedMap(vars); 265 condAssociatedVariablesMap.put(arg,vars); 266 if (vars.isEmpty()) { 267 allNoVariablesConditionsList.add(arg); 268 } else if (vars.size() == 1) { 269 allSimpleConditionsList.add(arg); 270 Variable var0 = (Variable) vars.get(0); 271 addExprToVarMap(var0,arg,varAllConditionsMap); 272 addExprToVarMap(var0,arg,varSimpleConditionsMap); 273 } else { allComplexConditionsList.add(arg); 275 addExprToVarsMap(vars,arg,varAllConditionsMap); 276 addExprToVarsMap(vars,arg,varComplexConditionsMap); 277 } 278 } 279 280 private void addExprToVarMap(Variable var, XQueryExpression expr, Map map) { 281 ArrayList tmplist = (ArrayList ) map.get(var); 282 if (tmplist == null) { 283 tmplist = new ArrayList (1); 284 map.put(var, tmplist); 285 } 286 tmplist.add(expr); 287 } 288 private void addExprToVarsMap(ArrayList vars, XQueryExpression expr, Map map) { 289 for (int i = 0; i < vars.size(); i++) { 290 addExprToVarMap((Variable) vars.get(i), expr,map); 291 } 292 } 293 294 private void fillVarAssociatedMap(ArrayList vars) { 295 if (vars.size() < 2) return; 296 for (int i = 0; i < vars.size(); i++) { 297 ArrayList tmplist = (ArrayList ) varAssociatedVariablesMap.get(vars.get(i)); 298 if (tmplist == null) { 299 tmplist = (ArrayList )vars.clone(); 300 tmplist.remove(i); 301 varAssociatedVariablesMap.put(vars.get(i), tmplist); 302 } 303 else { 304 for (int j = 0; j < vars.size(); j++) { 305 if (j == i) continue; 306 if (!tmplist.contains(vars.get(j))) 307 tmplist.add(vars.get(j)); 308 } 309 } 310 } 311 } 312 313 } 314 | Popular Tags |