View Javadoc

1   // Replace.java, created Jul 12, 2004 1:34:38 AM by joewhaley
2   // Copyright (C) 2004 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package net.sf.bddbddb.ir.lowlevel;
5   
6   import java.util.Collections;
7   import java.util.Iterator;
8   import java.util.List;
9   import jwutil.util.Assert;
10  import net.sf.bddbddb.Attribute;
11  import net.sf.bddbddb.BDDRelation;
12  import net.sf.bddbddb.Relation;
13  import net.sf.bddbddb.ir.Operation;
14  import net.sf.javabdd.BDDDomain;
15  import net.sf.javabdd.BDDFactory;
16  import net.sf.javabdd.BDDPairing;
17  
18  /***
19   * Replace
20   * 
21   * @author John Whaley
22   * @version $Id: Replace.java 445 2005-02-21 02:32:50Z cs343 $
23   */
24  public class Replace extends LowLevelOperation {
25      BDDRelation r0, r1;
26      BDDPairing pairing;
27      String pairingString;
28  
29      /***
30       * @param r0
31       * @param r1
32       */
33      public Replace(BDDRelation r0, BDDRelation r1) {
34          this.r0 = r0;
35          this.r1 = r1;
36      }
37  
38      /***
39       * 
40       */
41      public BDDPairing setPairing() {
42          this.pairing = makePairing(r0.getBDD().getFactory());
43          this.pairingString = Operation.getRenames(r1, r0);
44          return this.pairing;
45      }
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see net.sf.bddbddb.ir.lowlevel.LowLevelOperation#visit(net.sf.bddbddb.ir.lowlevel.LowLevelOperationVisitor)
51       */
52      public Object visit(LowLevelOperationVisitor i) {
53          return i.visit(this);
54      }
55  
56      /*
57       * (non-Javadoc)
58       * 
59       * @see java.lang.Object#toString()
60       */
61      public String toString() {
62          return (TRACE_VERBOSE ? r0.verboseToString() : r0.toString()) + " = " + getExpressionString();
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see net.sf.bddbddb.ir.Operation#getRelationDest()
69       */
70      public Relation getRelationDest() {
71          return r0;
72      }
73  
74      /*
75       * 
76       * (non-Javadoc)
77       * @see net.sf.bddbddb.ir.Operation#setRelationDest(net.sf.bddbddb.Relation)
78       */
79      public void setRelationDest(Relation r0) {
80          this.r0 = (BDDRelation) r0;
81      }
82  
83      /***
84       * @return  the source relation
85       */
86      public BDDRelation getSrc() {
87          return r1;
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see net.sf.bddbddb.ir.Operation#getSrcs()
94       */
95      public List getSrcs() {
96          return Collections.singletonList(r1);
97      }
98  
99      /*
100      * (non-Javadoc)
101      * 
102      * @see net.sf.bddbddb.ir.Operation#replaceSrc(net.sf.bddbddb.Relation,
103      *      net.sf.bddbddb.Relation)
104      */
105     public void replaceSrc(Relation r_old, Relation r_new) {
106         if (r1 == r_old) {
107             BDDRelation r1_b = (BDDRelation) r1;
108             BDDRelation r_new_b = (BDDRelation) r_new;
109             /* Assert only throws when each relation has
110                the same domains but are in a different order
111             List oldDomains = r1_b.getBDDDomains();
112             List newDomains = r_new_b.getBDDDomains();
113             Assert._assert(oldDomains.equals(newDomains), "old domains: " + oldDomains + " new domains: " + newDomains);
114             */
115             r1 = (BDDRelation) r_new;
116         }
117     }
118 
119     /*
120      * (non-Javadoc)
121      * 
122      * @see net.sf.bddbddb.ir.Operation#getExpressionString()
123      */
124     public String getExpressionString() {
125         return "replace(" + (TRACE_VERBOSE ? r1.verboseToString() : r1.toString()) + pairingString + ")";
126     }
127 
128     /***
129      * @return  the pairing
130      */
131     public BDDPairing getPairing() {
132         Assert._assert(pairingString != null,this.toString());
133         return pairing;
134     }
135 
136     BDDPairing makePairing(BDDFactory factory) {
137         boolean any = false;
138         BDDPairing pair = factory.makePair();
139         for (Iterator i = r1.getAttributes().iterator(); i.hasNext();) {
140             Attribute a = (Attribute) i.next();
141             BDDDomain d1 = r1.getBDDDomain(a);
142             BDDDomain d2 = r0.getBDDDomain(a);
143             if (d2 == null || d1 == d2) continue;
144             any = true;
145             pair.set(d1, d2);
146         }
147         if (any) return pairing = pair;
148         else return pairing = null;
149     }
150 
151     public Operation copy() {
152        Replace r = new Replace(r0, r1);
153        r.pairing = this.pairing;
154        r.pairingString = this.pairingString;
155        return r;
156     }
157 }