1
2
3
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
49
50
51
52 public Object visit(LowLevelOperationVisitor i) {
53 return i.visit(this);
54 }
55
56
57
58
59
60
61 public String toString() {
62 return (TRACE_VERBOSE ? r0.verboseToString() : r0.toString()) + " = " + getExpressionString();
63 }
64
65
66
67
68
69
70 public Relation getRelationDest() {
71 return r0;
72 }
73
74
75
76
77
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
92
93
94
95 public List getSrcs() {
96 return Collections.singletonList(r1);
97 }
98
99
100
101
102
103
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
110
111
112
113
114
115 r1 = (BDDRelation) r_new;
116 }
117 }
118
119
120
121
122
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 }