| 1 | package felix.optimizer; |
| 2 | |
| 3 | import java.util.HashSet; |
| 4 | |
| 5 | |
| 6 | import tuffy.mln.Literal; |
| 7 | import tuffy.util.ExceptionMan; |
| 8 | import tuffy.util.StringMan; |
| 9 | |
| 10 | import felix.dstruct.FelixClause; |
| 11 | import felix.dstruct.FelixPredicate; |
| 12 | import felix.dstruct.FelixQuery; |
| 13 | import felix.dstruct.StatOperator; |
| 14 | import felix.dstruct.FelixPredicate.FPProperty; |
| 15 | import felix.dstruct.StatOperator.OPType; |
| 16 | import felix.operator.COREFOperator; |
| 17 | import felix.operator.CRFOperator; |
| 18 | import felix.operator.LROperator; |
| 19 | import felix.operator.TUFFYOperator; |
| 20 | import felix.parser.FelixCommandOptions; |
| 21 | import felix.util.FelixConfig; |
| 22 | import felix.util.FelixStringMan; |
| 23 | import felix.util.FelixUIMan; |
| 24 | |
| 25 | /** |
| 26 | * An object of OperatorSelector will partition the FelixQuery into |
| 27 | * different StatOperators. Current version does not rely on the |
| 28 | * cost model, however, we may need to do that in the future. |
| 29 | * |
| 30 | * @author Ce |
| 31 | * |
| 32 | */ |
| 33 | public class OperatorSelector { |
| 34 | |
| 35 | /** |
| 36 | * Cost model used to partition operators. |
| 37 | */ |
| 38 | CostModel cm; |
| 39 | |
| 40 | /** |
| 41 | * FelixQuery which will be partitioned. |
| 42 | */ |
| 43 | FelixQuery fq; |
| 44 | |
| 45 | /** |
| 46 | * Command line options. |
| 47 | */ |
| 48 | FelixCommandOptions options; |
| 49 | |
| 50 | /** |
| 51 | * The constructor. |
| 52 | * @param _fq |
| 53 | * @param _cm |
| 54 | * @param _options |
| 55 | */ |
| 56 | public OperatorSelector(FelixQuery _fq, CostModel _cm, FelixCommandOptions _options){ |
| 57 | cm = _cm; |
| 58 | fq = _fq; |
| 59 | options = _options; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get partitioned operators. |
| 64 | * @return |
| 65 | */ |
| 66 | public HashSet<StatOperator> getOperators(){ |
| 67 | |
| 68 | HashSet<StatOperator> opts = new HashSet<StatOperator>(); |
| 69 | |
| 70 | FelixUIMan.println(0, 0, ">>> Parsing Operators..."); |
| 71 | |
| 72 | int ctCoref = 0; |
| 73 | int ctMLN = 0; |
| 74 | int ctCRF = 0; |
| 75 | int ctLR = 0; |
| 76 | |
| 77 | HashSet<FelixPredicate> processedPredicates = new HashSet<FelixPredicate>(); |
| 78 | HashSet<FelixPredicate> openPredicates = fq.getAllOpenPred(); |
| 79 | |
| 80 | HashSet<FelixClause> consideredClauses = new HashSet<FelixClause>(); |
| 81 | |
| 82 | if(FelixConfig.allRuleAsMLN == false){ |
| 83 | |
| 84 | for(FelixPredicate fp : openPredicates){ |
| 85 | |
| 86 | // pick coref rules |
| 87 | if( options.noCOREF == false && |
| 88 | fp.hasProperty(FPProperty.SYMM) && |
| 89 | fp.hasProperty(FPProperty.REFLEX) && |
| 90 | fp.hasProperty(FPProperty.TRANS) && |
| 91 | //!fp.hasProperty(FPProperty.EMBED_WEIGHT_RULE) && |
| 92 | (fp.mustbe == null || fp.mustbe.equals(OPType.COREF)) ){ |
| 93 | |
| 94 | FelixUIMan.println(0, 0, " Add coref operator for {" + fp.getName() + "}"); |
| 95 | COREFOperator op = new COREFOperator(fq, FelixStringMan.toHashSet(fp), options); |
| 96 | opts.add(op); |
| 97 | |
| 98 | for(FelixClause fc : fp.getRelevantClauses()){ |
| 99 | |
| 100 | |
| 101 | if(fp.getPropertyClauses(FPProperty.CHAIN_RECUR).contains(fc)){ |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if(fp.getPropertyClauses(FPProperty.KEY_CONSTRAINT).contains(fc)){ |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | if(fp.getPropertyClauses(FPProperty.OTHER_RECUR).contains(fc)){ |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | if(fp.getPropertyClauses(FPProperty.OTHER_RECUR_WITHOTHER_OPENPRED).contains(fc)){ |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | // pure-evidence op assumption. |
| 119 | // we need a better optimizer and scheduler for |
| 120 | // other non-trival cases. |
| 121 | boolean hasOtherOpen = false; |
| 122 | for(Literal l : fc.getRegLiterals()){ |
| 123 | if(l.getPred().isClosedWorld() == false && |
| 124 | !l.getPred().getName().equals(fp.getName())){ |
| 125 | hasOtherOpen = true; |
| 126 | } |
| 127 | } |
| 128 | if(hasOtherOpen){ |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | op.registerRelevantClause(fc); |
| 133 | consideredClauses.add(fc); |
| 134 | } |
| 135 | op.sealDefinition(); |
| 136 | |
| 137 | processedPredicates.add(fp); |
| 138 | ctCoref ++; |
| 139 | } |
| 140 | |
| 141 | // pick CRF and LR rules |
| 142 | else if(fp.hasProperty(FPProperty.KEY_CONSTRAINT)){ |
| 143 | |
| 144 | //crf |
| 145 | if( options.noCRF == false && |
| 146 | //TODO: CHECK CONSISTENT CHAIN RULE |
| 147 | fp.getPropertyClauses(FPProperty.CHAIN_RECUR).size() >= 1 && |
| 148 | fp.hasProperty(FPProperty.OTHER_RECUR) == false && |
| 149 | (fp.mustbe == null || fp.mustbe.equals(OPType.CRF)) ){ |
| 150 | |
| 151 | FelixUIMan.println(0, 0, " Add crf operator for {" + fp.getName() + "}"); |
| 152 | CRFOperator op = new CRFOperator(fq, FelixStringMan.toHashSet(fp), options); |
| 153 | opts.add(op); |
| 154 | |
| 155 | for(FelixClause fc : fp.getRelevantClauses()){ |
| 156 | |
| 157 | if(fp.getPropertyClauses(FPProperty.KEY_CONSTRAINT).contains(fc) || |
| 158 | fp.getPropertyClauses(FPProperty.CHAIN_RECUR).contains(fc) || |
| 159 | fp.getPropertyClauses(FPProperty.NON_RECUR).contains(fc)){ |
| 160 | |
| 161 | // pure-evidence op assumption. |
| 162 | // we need a better optimizer and scheduler for |
| 163 | // other non-trival cases. |
| 164 | boolean hasOtherOpen = false; |
| 165 | for(Literal l : fc.getRegLiterals()){ |
| 166 | if(l.getPred().isClosedWorld() == false && |
| 167 | !l.getPred().getName().equals(fp.getName())){ |
| 168 | hasOtherOpen = true; |
| 169 | } |
| 170 | } |
| 171 | if(hasOtherOpen){ |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | op.registerRelevantClause(fc); |
| 176 | consideredClauses.add(fc); |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | op.sealDefinition(); |
| 181 | |
| 182 | processedPredicates.add(fp); |
| 183 | ctCRF ++; |
| 184 | } |
| 185 | //lr |
| 186 | else if(options.noLR == false && |
| 187 | fp.hasProperty(FPProperty.CHAIN_RECUR) == false && |
| 188 | fp.hasProperty(FPProperty.OTHER_RECUR) == false && |
| 189 | (fp.mustbe == null || fp.mustbe.equals(OPType.LR)) ){ |
| 190 | |
| 191 | FelixUIMan.println(0, 0, " Add lr operator for {" + fp.getName() + "}"); |
| 192 | LROperator op = new LROperator(fq, FelixStringMan.toHashSet(fp), options); |
| 193 | opts.add(op); |
| 194 | |
| 195 | for(FelixClause fc : fp.getRelevantClauses()){ |
| 196 | |
| 197 | |
| 198 | if(fp.getPropertyClauses(FPProperty.KEY_CONSTRAINT).contains(fc) || |
| 199 | fp.getPropertyClauses(FPProperty.NON_RECUR).contains(fc)){ |
| 200 | |
| 201 | // pure-evidence op assumption. |
| 202 | // we need a better optimizer and scheduler for |
| 203 | // other non-trival cases. |
| 204 | boolean hasOtherOpen = false; |
| 205 | for(Literal l : fc.getRegLiterals()){ |
| 206 | if(l.getPred().isClosedWorld() == false && |
| 207 | !l.getPred().getName().equals(fp.getName())){ |
| 208 | hasOtherOpen = true; |
| 209 | } |
| 210 | } |
| 211 | if(hasOtherOpen){ |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | op.registerRelevantClause(fc); |
| 216 | consideredClauses.add(fc); |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | op.sealDefinition(); |
| 221 | |
| 222 | processedPredicates.add(fp); |
| 223 | ctLR ++; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | HashSet<FelixPredicate> alsoInput = new HashSet<FelixPredicate>(); |
| 230 | // other predicates as MLN |
| 231 | HashSet<FelixPredicate> mlnPredicates = new HashSet<FelixPredicate>(); |
| 232 | |
| 233 | HashSet<String> openPredicatesInLastMLN = new HashSet<String>(); |
| 234 | |
| 235 | for(FelixPredicate fp : openPredicates){ |
| 236 | |
| 237 | // do not include predicate as output of other operator |
| 238 | if(processedPredicates.contains(fp)){ |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | openPredicatesInLastMLN.add(fp.getName()); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | for(FelixPredicate fp : openPredicates){ |
| 247 | |
| 248 | // do not include predicate as output of other operator |
| 249 | if(processedPredicates.contains(fp)){ |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if(fp.mustbe != null && !fp.mustbe.equals(OPType.TUFFY)){ |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | processedPredicates.add(fp); |
| 258 | |
| 259 | // do not include predicate w/o any relevant clauses |
| 260 | if(fp.getRelevantClauses().size() == 0){ |
| 261 | continue; |
| 262 | } |
| 263 | |
| 264 | ctMLN ++; |
| 265 | mlnPredicates.add(fp); |
| 266 | |
| 267 | } |
| 268 | if(mlnPredicates.size() != 0 || fq.getAllClause().size() != consideredClauses.size()){ |
| 269 | TUFFYOperator op = new TUFFYOperator(fq, mlnPredicates, options); |
| 270 | op.inputPredicates.addAll(alsoInput); |
| 271 | op.setPrecedence(2); |
| 272 | FelixUIMan.println(0, 0, " Add mln operator for {" + |
| 273 | StringMan.join(",", FelixStringMan.colToStringArray(mlnPredicates))+"}"); |
| 274 | opts.add(op); |
| 275 | |
| 276 | for(FelixClause fc : fq.getAllClause()){ |
| 277 | if(consideredClauses.contains(fc)){ |
| 278 | continue; |
| 279 | } |
| 280 | op.registerRelevantClause(fc); |
| 281 | } |
| 282 | |
| 283 | op.sealDefinition(); |
| 284 | |
| 285 | } |
| 286 | |
| 287 | // if there are remanining predicates, this program is not decomposable. |
| 288 | if(processedPredicates.size() != openPredicates.size()){ |
| 289 | ExceptionMan.die("The input program is not decomposable. In current version of Felix, please" + |
| 290 | " check whether you use P_map correctly. This predicate can only be associated" + |
| 291 | " with Coref predicate P. Please change its name if you use it as a regular predicate"); |
| 292 | } |
| 293 | |
| 294 | FelixUIMan.println(2, 0, "\t # Coref Operators: " + ctCoref + "\n" + |
| 295 | "\t # MLN Operators: " + ctMLN + "\n" + |
| 296 | "\t # LR Operators: " + ctLR + "\n" + |
| 297 | "\t # CRF Operators: " + ctCRF + "\n"); |
| 298 | return opts; |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |