Generated on Thu Mar 6 14:51:42 2008 for Gecode/J by doxygen 1.5.4

BExpr.java

Go to the documentation of this file.
00001 /* -*- indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-11-30 16:59:40 +0100 (Fri, 30 Nov 2007) $ by $Author: zayenz $
00011  *     $Revision: 5528 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 package org.gecode;
00039 
00040 import java.util.ArrayList;
00041 import static org.gecode.BoolOpType.*;
00042 
00058 public class BExpr {
00059     E e;
00060 
00063     public BExpr(BExpr be) {
00064         e = be.e;
00065     }
00066 
00069     public BExpr(BoolVar b) {
00070         e = new BoolE(b);
00071     }
00072 
00075     public BExpr(boolean v) {
00076         e = new BooleanE(v);
00077     }
00078 
00081     public BExpr(Expr e1, IntRelType irt, Expr e2) {
00082         e = new RelE(e1, irt, e2);
00083     }
00084 
00087     public BExpr(Expr e1, IntRelType irt, int d) {
00088         e = new RelE(e1, irt, new Expr(d));
00089     }
00090 
00093     public BExpr(IntVar v1, IntRelType irt, IntVar v2) {
00094         e = new RelE(new Expr(v1), irt, new Expr(v2));
00095     }
00096 
00099     public BExpr(IntVar v, IntRelType irt, int d) {
00100         e = new RelE(new Expr(v), irt, new Expr(d));
00101     }
00102 
00104     //  Methods for handling BExpr's
00106 
00110     public BExpr and(BExpr be) {
00111         if (e instanceof BiE && ((BiE)e).op == Op.AND) {
00112             ((BiE)e).e.add(be.e);
00113         } else {
00114             e = new BiE(e, Op.AND, be.e);
00115         }
00116         return this;
00117     }
00118 
00122     public BExpr or(BExpr be) {
00123         if (e instanceof BiE && ((BiE)e).op == Op.OR) {
00124             ((BiE)e).e.add(be.e);
00125         } else {
00126             e = new BiE(e, Op.OR, be.e);
00127         }
00128         return this;
00129     }
00130 
00134     public BExpr xor(BExpr be) {
00135         e = new BiE(e, Op.XOR, be.e);
00136         return this;
00137     }
00138 
00142     public BExpr imp(BExpr be) {
00143         e = new BiE(e, Op.IMP, be.e);
00144         return this;
00145     }
00146 
00150     public BExpr consequence_of(BExpr be) {
00151         e = new BiE(be.e, Op.IMP, e);
00152         return this;
00153     }
00154 
00155 
00159     public BExpr eqv(BExpr be) {
00160         e = new BiE(e, Op.EQV, be.e);
00161         return this;
00162     }
00163 
00165     //  Methods for handling BoolVars
00167 
00171     public BExpr and(BoolVar b) {
00172         if (e instanceof BiE && ((BiE)e).op == Op.AND) {
00173             ((BiE)e).e.add(new BoolE(b));
00174         } else {
00175             e = new BiE(e, Op.AND, new BoolE(b));
00176         }
00177         return this;
00178     }
00179 
00183     public BExpr or(BoolVar b) {
00184         if (e instanceof BiE && ((BiE)e).op == Op.OR) {
00185             ((BiE)e).e.add(new BoolE(b));
00186         } else {
00187             e = new BiE(e, Op.OR, new BoolE(b));
00188         }
00189         return this;
00190     }
00191 
00195     public BExpr xor(BoolVar b) {
00196         e = new BiE(e, Op.XOR, new BoolE(b));
00197         return this;
00198     }
00199 
00203     public BExpr imp(BoolVar b) {
00204         e = new BiE(e, Op.IMP, new BoolE(b));
00205         return this;
00206     }
00207 
00208 
00212     public BExpr consequence_of(BoolVar b) {
00213         e = new BiE(new BoolE(b), Op.IMP, e);
00214         return this;
00215     }
00216 
00220     public BExpr eqv(BoolVar b) {
00221         e = new BiE(e, Op.EQV, new BoolE(b));
00222         return this;
00223     }
00224 
00226     //  Methods for handling booleans
00228  
00232     public BExpr and(boolean v) {
00233         if (e instanceof BiE && ((BiE)e).op == Op.AND) {
00234             ((BiE)e).e.add(new BooleanE(v));
00235         } else {
00236             e = new BiE(e, Op.AND, new BooleanE(v));
00237         }
00238         return this;
00239     }
00240 
00244     public BExpr or(boolean v) {
00245         if (e instanceof BiE && ((BiE)e).op == Op.OR) {
00246             ((BiE)e).e.add(new BooleanE(v));
00247         } else {
00248             e = new BiE(e, Op.OR, new BooleanE(v));
00249         }
00250         return this;
00251     }
00252 
00256     public BExpr xor(boolean v) {
00257         e = new BiE(e, Op.XOR, new BooleanE(v));
00258         return this;
00259     }
00260 
00264     public BExpr imp(boolean v) {
00265         e = new BiE(e, Op.IMP, new BooleanE(v));
00266         return this;
00267     }
00268 
00269 
00273     public BExpr consequence_of(boolean v) {
00274         e = new BiE(new BooleanE(v), Op.IMP, e);
00275         return this;
00276     }
00277 
00281     public BExpr eqv(boolean v) {
00282         e = new BiE(e, Op.EQV, new BooleanE(v));
00283         return this;
00284     }
00285 
00287     //  Methods for handling relations
00289 
00293     public BExpr and(Expr e1, IntRelType irt, Expr e2) {
00294         if (e instanceof BiE && ((BiE)e).op == Op.AND) {
00295             ((BiE)e).e.add(new RelE(e1, irt, e2));
00296         } else {
00297             e = new BiE(e, Op.AND, new RelE(e1, irt, e2));
00298         }
00299         return this;
00300     }
00301 
00305     public BExpr or(Expr e1, IntRelType irt, Expr e2) {
00306         if (e instanceof BiE && ((BiE)e).op == Op.OR) {
00307             ((BiE)e).e.add(new RelE(e1, irt, e2));
00308         } else {
00309             e = new BiE(e, Op.OR, new RelE(e1, irt, e2));
00310         }
00311         return this;
00312     }
00313 
00317     public BExpr xor(Expr e1, IntRelType irt, Expr e2) {
00318         e = new BiE(e, Op.XOR, new RelE(e1, irt, e2));
00319         return this;
00320     }
00321 
00325     public BExpr imp(Expr e1, IntRelType irt, Expr e2) {
00326         e = new BiE(e, Op.IMP, new RelE(e1, irt, e2));
00327         return this;
00328     }
00329 
00330 
00335     public BExpr consequence_of(Expr e1, IntRelType irt, Expr e2) {
00336         e = new BiE(new RelE(e1, irt, e2), Op.IMP, e);
00337         return this;
00338     }
00339 
00343     public BExpr eqv(Expr e1, IntRelType irt, Expr e2) {
00344         e = new BiE(e, Op.EQV, new RelE(e1, irt, e2));
00345         return this;
00346     }
00347 
00349     //  Convenience methods
00351 
00352     // Variable number of arguments. No arguments is a no-op
00353 
00357     public BExpr and(BExpr... bes) {
00358         for(BExpr be: bes)
00359             and(be);
00360         return this;
00361     }
00362 
00366     public BExpr or(BExpr... bes) {
00367         for(BExpr be: bes)
00368             or(be);
00369         return this;
00370     }
00371 
00375     public BExpr eqv(BExpr... bes) {
00376         for (BExpr be: bes)
00377             eqv(be);
00378         return this;
00379     }
00380 
00384     public BExpr and(BoolVar... bvs) {
00385         for(BoolVar bv: bvs)
00386             and(bv);
00387         return this;
00388     }
00389 
00393     public BExpr or(BoolVar... bvs) {
00394         for(BoolVar bv: bvs)
00395             or(bv);
00396         return this;
00397     }
00398 
00399 
00403     public BExpr eqv(BoolVar... bvs) {
00404         for (BoolVar bv: bvs)
00405             eqv(bv);
00406         return this;
00407     }
00408 
00409     // Second expression integer
00410 
00414     public BExpr and(Expr e1, IntRelType irt, int d) {
00415         return and(e1, irt, new Expr(d));
00416     }
00417 
00421     public BExpr or(Expr e1, IntRelType irt, int d) {
00422         return or(e1, irt, new Expr(d));
00423     }
00424 
00428     public BExpr xor(Expr e1, IntRelType irt, int d) {
00429         return xor(e1, irt, new Expr(d));
00430     }
00431 
00435     public BExpr imp(Expr e1, IntRelType irt, int d) {
00436         return imp(e1, irt, new Expr(d));
00437     }
00438 
00439 
00444     public BExpr consequence_of(Expr e1, IntRelType irt, int d) {
00445         return consequence_of(e1, irt, new Expr(d));
00446     }
00447 
00451     public BExpr eqv(Expr e1, IntRelType irt, int d) {
00452         return eqv(e1, irt, new Expr(d));
00453     }
00454 
00455     // Both expressions IntVar's
00456 
00460     public BExpr and(IntVar v1, IntRelType irt, IntVar v2) {
00461         return and(new Expr(v1), irt, new Expr(v2));
00462     }
00463 
00467     public BExpr or(IntVar v1, IntRelType irt, IntVar v2) {
00468         return or(new Expr(v1), irt, new Expr(v2));
00469     }
00470 
00474     public BExpr xor(IntVar v1, IntRelType irt, IntVar v2) {
00475         return xor(new Expr(v1), irt, new Expr(v2));
00476     }
00477 
00481     public BExpr imp(IntVar v1, IntRelType irt, IntVar v2) {
00482         return imp(new Expr(v1), irt, new Expr(v2));
00483     }
00484 
00485 
00490     public BExpr consequence_of(IntVar v1, IntRelType irt, IntVar v2) {
00491         return consequence_of(new Expr(v1), irt, new Expr(v2));
00492     }
00493 
00497     public BExpr eqv(IntVar v1, IntRelType irt, IntVar v2) {
00498         return eqv(new Expr(v1), irt, new Expr(v2));
00499     }
00500 
00501 
00502     // First expression IntVar, second expression integer
00503 
00507     public BExpr and(IntVar v1, IntRelType irt, int d) {
00508         return and(new Expr(v1), irt, new Expr(d));
00509     }
00510 
00514     public BExpr or(IntVar v1, IntRelType irt, int d) {
00515         return or(new Expr(v1), irt, new Expr(d));
00516     }
00517 
00521     public BExpr xor(IntVar v1, IntRelType irt, int d) {
00522         return xor(new Expr(v1), irt, new Expr(d));
00523     }
00524 
00528     public BExpr imp(IntVar v1, IntRelType irt, int d) {
00529         return imp(new Expr(v1), irt, new Expr(d));
00530     }
00531 
00532 
00537     public BExpr consequence_of(IntVar v1, IntRelType irt, int d) {
00538         return consequence_of(new Expr(v1), irt, new Expr(d));
00539     }
00540 
00544     public BExpr eqv(IntVar v1, IntRelType irt, int d) {
00545         return eqv(new Expr(v1), irt, new Expr(d));
00546     }
00547 
00548 
00550     //  Syntax tree classes
00552     
00553     abstract class E {
00554         abstract BoolVar post(JavaSpace home);
00555         abstract E copy();
00556     }
00557 
00558     enum Op {AND, OR, XOR, IMP, EQV}
00559 
00560     //int indent;
00561     //void print(String s) {
00562     //        for(int i = 0; i < indent; ++i)
00563     //            System.err.print("  ");
00564     //        System.err.println(s);
00565     //}
00566 
00567     class BiE extends E {
00568         Op op;
00569         ArrayList<E> e;
00570         BiE(E e1, Op op, E e2) {
00571             e = new ArrayList<E>(2);
00572             this.op = op;
00573             e.add(e1); e.add(e2);
00574         }
00575         BoolVar post(JavaSpace home) {
00576             BoolVar res = new BoolVar(home);
00577             switch (op) {
00578             case AND:
00579                 //print("Posting and"); ++indent;
00580                 if (e.size() == 2) {
00581                   Gecode.rel(home, e.get(0).post(home), BOT_AND, e.get(1).post(home), res);
00582                 } else {
00583                     VarArray<BoolVar> va = new VarArray<BoolVar>(e.size());
00584                     for (E exp: e) va.add(exp.post(home));
00585                     Gecode.rel(home, BOT_AND, va, res);
00586                 }
00587                 //--indent;
00588                 return res;
00589             case OR:
00590                 //print("Posting or"); ++indent;
00591                 if (e.size() == 2) {
00592                     Gecode.rel(home, e.get(0).post(home), BOT_OR, e.get(1).post(home), res);
00593                 } else {
00594                     VarArray<BoolVar> va = new VarArray<BoolVar>(e.size());
00595                     for (E exp: e) va.add(exp.post(home));
00596                     Gecode.rel(home, BOT_OR, va, res);
00597                 }
00598                 //--indent;
00599                 return res;
00600             case XOR:
00601                 Gecode.rel(home, e.get(0).post(home), BOT_XOR, e.get(1).post(home), res);
00602                 return res;
00603             case IMP:
00604                 Gecode.rel(home, e.get(0).post(home), BOT_IMP, e.get(1).post(home), res);
00605                 return res;
00606             case EQV:
00607                 Gecode.rel(home, e.get(0).post(home), BOT_EQV, e.get(1).post(home), res);
00608                 return res;
00609             }
00610             return res;
00611         }
00612         BiE copy() {
00613             return new BiE(e.get(0).copy(), op, e.get(1).copy());
00614         }
00615     }
00616     class NE extends E {
00617         E e;
00618         NE(E e) {
00619             this.e = e;
00620         }
00621         BoolVar post(JavaSpace home) {
00622             BoolVar res = new BoolVar(home);
00623             Gecode.rel(home, res, IntRelType.IRT_NQ, e.post(home));
00624             return res;
00625         }
00626         NE copy() {
00627             return new NE(e.copy());
00628         }
00629     }
00630     class BoolE extends E {
00631         BoolVar b;
00632         BoolE(BoolVar b) {
00633             this.b = b;
00634         }
00635         BoolVar post(JavaSpace home) {
00636             //print("Posting BoolVar " + b);
00637             return b;
00638         }
00639         BoolE copy() {
00640             return new BoolE(b);
00641         }
00642     }
00643     class BooleanE extends E {
00644         boolean v;
00645         BooleanE(boolean v) {
00646             this.v = v;
00647         }
00648         BoolVar post(JavaSpace home) {
00649             //print("Posting boolean: " + v);
00650             return new BoolVar(home, v);
00651         }
00652         BooleanE copy() {
00653             return new BooleanE(v);
00654         }
00655     }
00656     class RelE extends E {
00657         Expr e1, e2;
00658         IntRelType irt;
00659         RelE(Expr e1, IntRelType irt, Expr e2) {
00660             this.e1 = e1;
00661             this.irt = irt;
00662             this.e2 = e2;
00663         }
00664         BoolVar post(JavaSpace home) {
00665             //print("Posting relation " + e1 + " " + irt + " " + e2);
00666             BoolVar res = new BoolVar(home);
00667             Gecode.post(home, e1, irt, e2, res);
00668             return res;
00669         }
00670         RelE copy() {
00671             return new RelE(e1, irt, e2);
00672         }
00673     }
00674 }