1
2
3
4 package net.sf.bddbddb.ir.lowlevel;
5
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.List;
9 import jwutil.collections.Pair;
10 import jwutil.util.Assert;
11 import net.sf.bddbddb.Attribute;
12 import net.sf.bddbddb.BDDRelation;
13 import net.sf.bddbddb.Relation;
14 import net.sf.bddbddb.ir.Operation;
15 import net.sf.javabdd.BDDDomain;
16 import net.sf.javabdd.BDDFactory;
17 import net.sf.javabdd.BDDVarSet;
18 import net.sf.javabdd.BDDFactory.BDDOp;
19
20 /***
21 * ApplyEx
22 *
23 * @author jwhaley
24 * @version $Id: ApplyEx.java 645 2006-07-17 05:20:20Z joewhaley $
25 */
26 public class ApplyEx extends LowLevelOperation {
27 BDDRelation r0, r1, r2;
28 BDDOp op;
29 List domainProjectSet;
30 List
31
32 /***
33 * @param r0
34 * @param r1
35 * @param r2
36 */
37 public ApplyEx(BDDRelation r0, BDDRelation r1, BDDOp op, BDDRelation r2) {
38 this.r0 = r0;
39 this.r1 = r1;
40 this.r2 = r2;
41 this.op = op;
42 this.attributes = new LinkedList(r1.getAttributes());
43 this.attributes.removeAll(r2.getAttributes());
44 this.attributes.addAll(r2.getAttributes());
45 this.attributes.removeAll(r0.getAttributes());
46 }
47
48
49
50
51
52
53 public Object visit(LowLevelOperationVisitor i) {
54 return i.visit(this);
55 }
56
57
58
59
60
61
62 public String toString() {
63 return (TRACE_VERBOSE ? r0.verboseToString() : r0.toString()) + " = " + getExpressionString();
64 }
65
66
67
68
69
70
71 public String getExpressionString() {
72 String opName;
73 if (op == BDDFactory.and) opName = "relprod";
74 else opName = op.toString() + "Ex";
75 return opName + "(" + (TRACE_VERBOSE ? r1.verboseToString() : r1.toString()) + "," + (TRACE_VERBOSE ? r2.verboseToString() : r2.toString()) + "," + attributes + ")";
76 }
77
78
79
80
81
82
83 public Relation getRelationDest() {
84 return r0;
85 }
86
87
88
89
90
91
92 public List getSrcs() {
93 return new Pair(r1, r2);
94 }
95
96 /***
97 * @return Returns the source relation.
98 */
99 public Relation getSrc1() {
100 return r1;
101 }
102
103 /***
104 * @return Returns the source relation.
105 */
106 public Relation getSrc2() {
107 return r2;
108 }
109
110 /***
111 * @return the set to project
112 */
113 public BDDVarSet getProjectSet() {
114 Assert._assert(domainProjectSet != null);
115 BDDVarSet b = r1.getBDD().getFactory().emptySet();
116 for (Iterator i = domainProjectSet.iterator(); i.hasNext();) {
117 BDDDomain d = (BDDDomain) i.next();
118 if (d != null) b.unionWith(d.set());
119 }
120 return b;
121 }
122
123 public void setProjectSet(){
124 domainProjectSet = new LinkedList();
125 for (Iterator i = attributes.iterator(); i.hasNext();) {
126 Attribute a = (Attribute) i.next();
127 BDDDomain d = r1.getBDDDomain(a);
128 if (d == null) d = r2.getBDDDomain(a);
129 if (d == null) {
130 System.out.println("Warning: Trying to project attribute "+a+" which is neither in "+r1+" "+r1.getAttributes()+" "+r1.getBDDDomains()+" nor "+r2+" "+r2.getAttributes()+" "+r2.getBDDDomains());
131 continue;
132 }
133 domainProjectSet.add(d);
134 }
135 }
136 /***
137 * @return Returns the attributes.
138 */
139 public List getAttributes() {
140 return attributes;
141 }
142
143 /***
144 * @return the BDD operation
145 */
146 public BDDOp getOp() {
147 return op;
148 }
149
150
151
152
153
154
155
156 public void replaceSrc(Relation r_old, Relation r_new) {
157 if (r1 == r_old) r1 = (BDDRelation) r_new;
158 if (r2 == r_old) r2 = (BDDRelation) r_new;
159
160 }
161
162
163
164
165
166
167 public void setRelationDest(Relation r0) {
168 this.r0 = (BDDRelation) r0;
169 }
170
171 public Operation copy() {
172 return new ApplyEx(r0, r1, op, r2);
173 }
174 }