| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MetaSetFilter |
|
| 10.4;10.4 |
| 1 | /****************************************************************************** | |
| 2 | * MetaSetFilter.java - Process a MetaSet and return a List of Pictures | |
| 3 | * | |
| 4 | * PicMan - The BuckoSoft Picture Manager in Java | |
| 5 | * Copyright(c) 2006 - Dick Balaska | |
| 6 | * | |
| 7 | */ | |
| 8 | package com.buckosoft.PicMan.db.logic; | |
| 9 | ||
| 10 | import java.util.HashMap; | |
| 11 | import java.util.Iterator; | |
| 12 | import java.util.LinkedHashSet; | |
| 13 | import java.util.LinkedList; | |
| 14 | import java.util.List; | |
| 15 | ||
| 16 | import org.apache.commons.logging.Log; | |
| 17 | import org.apache.commons.logging.LogFactory; | |
| 18 | ||
| 19 | import com.buckosoft.PicMan.db.DatabaseFacade; | |
| 20 | import com.buckosoft.PicMan.domain.Filter; | |
| 21 | import com.buckosoft.PicMan.domain.MetaSet; | |
| 22 | import com.buckosoft.PicMan.domain.MetaSetRule; | |
| 23 | import com.buckosoft.PicMan.domain.Set; | |
| 24 | ||
| 25 | /** Processing for MetaSets | |
| 26 | * @author Dick Balaska | |
| 27 | * @since 2006/07/06 | |
| 28 | * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/db/logic/MetaSetFilter.java">MetaSetFilter.java</a> | |
| 29 | */ | |
| 30 | 0 | public class MetaSetFilter { |
| 31 | private static final boolean DEBUG = false; | |
| 32 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 33 | ||
| 34 | private DatabaseFacade dbf; | |
| 35 | ||
| 36 | /** Set the reference to the Database | |
| 37 | * @param dbf A reference to the database | |
| 38 | */ | |
| 39 | public void setDatabase(DatabaseFacade dbf) { | |
| 40 | 0 | this.dbf = dbf; |
| 41 | 0 | } |
| 42 | ||
| 43 | /** Get a List of picNames for this MetaSet set and size | |
| 44 | * @param set the set | |
| 45 | * @param size the size | |
| 46 | * @param rateOp An index into the MetaSet rateOps table. i.e. NONE, =, !=, <, >. | |
| 47 | * @param rateVal The value to apply with the rateOp against this set | |
| 48 | * @return A list of picNames | |
| 49 | */ | |
| 50 | public List<String> getListOfPicNamesInMetaSet(Set set, int size, int rateOp, int rateVal) { | |
| 51 | 0 | MetaSet mset = dbf.getMetaSet(set.getSid()); |
| 52 | 0 | return(getListOfPicNames(mset, size, rateOp, rateVal)); |
| 53 | } | |
| 54 | ||
| 55 | public List<String> getListOfPicNames(MetaSet mset, int size, int rateOp, int rateValue) { | |
| 56 | 0 | LinkedHashSet<String> workingHash = new LinkedHashSet<String>(); |
| 57 | 0 | List<MetaSetRule> metaSetRules = mset.getRules(); |
| 58 | 0 | int index = -1; |
| 59 | 0 | int currentOperator = MetaSet.NONE; |
| 60 | if (DEBUG) | |
| 61 | logger.info("******************getList:" + mset.getSid() + "-" + size); | |
| 62 | 0 | while (++index < metaSetRules.size()) { |
| 63 | 0 | MetaSetRule rule = (MetaSetRule)metaSetRules.get(index); |
| 64 | if (DEBUG) | |
| 65 | logger.info("type:" + rule.getType() + " value:" + rule.getValue() + "(" + rule.getOperator()+ ")"); | |
| 66 | 0 | if (rule.getType() == MetaSet.NAME || rule.getType() == MetaSet.FUNCTION) { |
| 67 | 0 | List<String> list = null; |
| 68 | 0 | if (rule.getType() == MetaSet.NAME) |
| 69 | 0 | list = dbf.getPicNamesBySet(rule.getValue(), size, rule.getRateOp(), rule.getRateVal()); |
| 70 | else | |
| 71 | 0 | list = dbf.getPicNamesByFunc(rule.getRateVal(), size, rule.getRateOp(), rule.getValue()); |
| 72 | if (DEBUG) | |
| 73 | logger.info("Got " + list.size() + " pics for " + rule.getValue()); | |
| 74 | 0 | if (currentOperator == MetaSet.NONE || currentOperator == MetaSet.OR) |
| 75 | 0 | workingHash.addAll(list); |
| 76 | 0 | else if (currentOperator == MetaSet.AND) |
| 77 | 0 | workingHash.retainAll(list); |
| 78 | 0 | else if (currentOperator == MetaSet.NOT) { |
| 79 | 0 | Iterator<String> iter = list.iterator(); |
| 80 | 0 | while (iter.hasNext()) { |
| 81 | 0 | workingHash.remove(iter.next()); |
| 82 | } | |
| 83 | } | |
| 84 | 0 | currentOperator = MetaSet.NONE; |
| 85 | 0 | } else if (rule.getType() == MetaSet.OPERATOR) { |
| 86 | 0 | currentOperator = rule.getOperator(); |
| 87 | } else { | |
| 88 | 0 | throw new RuntimeException("MetaSet: " + mset.getSid() + " Rule:" + index |
| 89 | 0 | + " Unknown type " + rule.getType()); |
| 90 | } | |
| 91 | 0 | } |
| 92 | Iterator<String> iter; | |
| 93 | 0 | switch (rateOp) { |
| 94 | case MetaSet.NONE: | |
| 95 | 0 | break; |
| 96 | case MetaSet.EQ: | |
| 97 | 0 | iter = workingHash.iterator(); |
| 98 | 0 | while (iter.hasNext()) { |
| 99 | 0 | double d = getPicRate(mset, dbf.getFilter(iter.next()), size); |
| 100 | 0 | if (d != rateValue) |
| 101 | 0 | iter.remove(); |
| 102 | 0 | } |
| 103 | break; | |
| 104 | } | |
| 105 | 0 | return(new LinkedList<String>(workingHash)); |
| 106 | } | |
| 107 | ||
| 108 | /** See if this MetaSet intersects with this filter | |
| 109 | * @param mset The MetaSet to check | |
| 110 | * @param f The Filter to check | |
| 111 | * @return an array of booleans, one for each size | |
| 112 | */ | |
| 113 | public boolean[] isFilterInSet(MetaSet mset, Filter f) { | |
| 114 | 0 | int[] sizes = dbf.getSizeArray(); |
| 115 | int sz; | |
| 116 | int filterValue; | |
| 117 | 0 | boolean[] ret = new boolean[sizes.length]; |
| 118 | 0 | for (sz=0; sz<ret.length; sz++) |
| 119 | 0 | ret[sz] = false; |
| 120 | 0 | for (sz=0; sz<sizes.length; sz++) { |
| 121 | 0 | int size = sizes[sz]; |
| 122 | 0 | Iterator<MetaSetRule> iter = mset.getRules().iterator(); |
| 123 | 0 | int currentOperator = MetaSet.NONE; |
| 124 | 0 | while (iter.hasNext()) { |
| 125 | 0 | MetaSetRule rule = iter.next(); |
| 126 | 0 | if (rule.getType() == MetaSet.NAME) { |
| 127 | boolean filterBool; | |
| 128 | 0 | filterValue = f.getFilter(rule.getValue(), size); |
| 129 | 0 | switch (rule.getRateOp()) { |
| 130 | case MetaSet.EQ: | |
| 131 | 0 | filterBool = filterValue == rule.getRateVal(); |
| 132 | 0 | break; |
| 133 | case MetaSet.LT: | |
| 134 | 0 | filterBool = filterValue < rule.getRateVal(); |
| 135 | 0 | break; |
| 136 | case MetaSet.GT: | |
| 137 | 0 | filterBool = filterValue > rule.getRateVal(); |
| 138 | 0 | break; |
| 139 | default: // case MetaSet.NONE | |
| 140 | 0 | filterBool = filterValue > 0 ? true : false; |
| 141 | break; | |
| 142 | } | |
| 143 | 0 | switch (currentOperator) { |
| 144 | case MetaSet.NONE: | |
| 145 | case MetaSet.OR: | |
| 146 | 0 | ret[sz] = ret[sz] | filterBool; |
| 147 | 0 | break; |
| 148 | case MetaSet.AND: | |
| 149 | 0 | ret[sz] = ret[sz] & filterBool; |
| 150 | 0 | break; |
| 151 | case MetaSet.NOT: | |
| 152 | 0 | ret[sz] = ret[sz] & !filterBool; |
| 153 | break; | |
| 154 | } | |
| 155 | 0 | currentOperator = MetaSet.NONE; |
| 156 | 0 | } else if (rule.getType() == MetaSet.OPERATOR) { |
| 157 | 0 | currentOperator = rule.getOperator(); |
| 158 | 0 | } else if (rule.getType() == MetaSet.FUNCTION) { |
| 159 | // XXX: Add tests for FUNCTIONS | |
| 160 | } else { | |
| 161 | 0 | throw new RuntimeException("MetaSet: " + dbf.getSet(mset.getSid()).getName() |
| 162 | 0 | + " Unknown type " + rule.getType()); |
| 163 | } | |
| 164 | 0 | } |
| 165 | } | |
| 166 | 0 | return(ret); |
| 167 | } | |
| 168 | ||
| 169 | public double getPicRate(MetaSet mset, Filter f, int size) { | |
| 170 | 0 | double filterValue = 0.0; |
| 171 | 0 | int count = 0; |
| 172 | 0 | double sum = 0.0; |
| 173 | 0 | Iterator<MetaSetRule> iter = mset.getRules().iterator(); |
| 174 | // int currentOperator = MetaSet.NONE; | |
| 175 | 0 | while (iter.hasNext()) { |
| 176 | 0 | MetaSetRule rule = iter.next(); |
| 177 | 0 | if (rule.getType() == MetaSet.NAME) { |
| 178 | 0 | boolean filterBool = true; |
| 179 | 0 | Set s = dbf.getSet(rule.getValue()); |
| 180 | 0 | if (s.isMetaSet()) |
| 181 | 0 | filterValue = getPicRate(dbf.getMetaSet(s.getSid()), f, size); |
| 182 | 0 | else if (s.isNanoSet()) { |
| 183 | 0 | HashMap<String,Integer> filters = f.getFilters(); |
| 184 | 0 | if (filters != null && filters.get(s.getName()) != null) |
| 185 | 0 | filterValue = filters.get(s.getName()) > 0 ? 9 : 0; |
| 186 | else | |
| 187 | 0 | filterValue = 0.0; |
| 188 | 0 | } else |
| 189 | 0 | filterValue = f.getFilter(rule.getValue(), size); |
| 190 | 0 | switch (rule.getRateOp()) { |
| 191 | case MetaSet.EQ: | |
| 192 | 0 | filterBool = filterValue == rule.getRateVal(); |
| 193 | 0 | break; |
| 194 | case MetaSet.LT: | |
| 195 | 0 | filterBool = filterValue < rule.getRateVal(); |
| 196 | 0 | break; |
| 197 | case MetaSet.GT: | |
| 198 | 0 | filterBool = filterValue > rule.getRateVal(); |
| 199 | 0 | break; |
| 200 | default: // case MetaSet.NONE | |
| 201 | 0 | filterBool = filterValue > 0 ? true : false; |
| 202 | break; | |
| 203 | } | |
| 204 | 0 | if (filterBool && filterValue != 0) { |
| 205 | 0 | sum += filterValue; |
| 206 | 0 | count++; |
| 207 | } | |
| 208 | /* switch (currentOperator) { | |
| 209 | case MetaSet.NONE: | |
| 210 | case MetaSet.OR: | |
| 211 | ret[sz] = ret[sz] | filterBool; | |
| 212 | break; | |
| 213 | case MetaSet.AND: | |
| 214 | ret[sz] = ret[sz] & filterBool; | |
| 215 | break; | |
| 216 | case MetaSet.NOT: | |
| 217 | ret[sz] = ret[sz] & !filterBool; | |
| 218 | break; | |
| 219 | } | |
| 220 | currentOperator = MetaSet.NONE; | |
| 221 | } else if (rule.getType() == MetaSet.OPERATOR) { | |
| 222 | currentOperator = rule.getOperator(); | |
| 223 | } else if (rule.getType() == MetaSet.FUNCTION) { | |
| 224 | // XXX: Add tests for FUNCTIONS | |
| 225 | } else { | |
| 226 | throw new RuntimeException("MetaSet: " + dbf.getSet(mset.getSid()).getName() | |
| 227 | + " Unknown type " + rule.getType()); | |
| 228 | */ } | |
| 229 | 0 | } |
| 230 | 0 | return(count == 0 ? 0 : sum / (double)count); |
| 231 | } | |
| 232 | ||
| 233 | } |