View Javadoc

1   // Rename.java, created Jun 29, 2004 12:25:20 PM 2004 by jwhaley
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.highlevel;
5   
6   import java.util.Collections;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Map;
10  import net.sf.bddbddb.Attribute;
11  import net.sf.bddbddb.Relation;
12  import net.sf.bddbddb.ir.Operation;
13  
14  /***
15   * Rename
16   * 
17   * @author jwhaley
18   * @version $Id: Rename.java 328 2004-10-16 02:45:30Z joewhaley $
19   */
20  public class Rename extends HighLevelOperation {
21      Relation r0, r1;
22      Map/*<Attribute,Attribute>*/ renames;
23  
24      /***
25       * @param r0
26       * @param r1
27       */
28      public Rename(Relation r0, Relation r1, Map/*<Attribute,Attribute>*/ renames) {
29          super();
30          this.r0 = r0;
31          this.r1 = r1;
32          this.renames = renames;
33      }
34  
35      /*
36       * (non-Javadoc)
37       * 
38       * @see java.lang.Object#toString()
39       */
40      public String toString() {
41          return r0.toString() + " = " + getExpressionString();
42      }
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see net.sf.bddbddb.ir.Operation#getExpressionString()
48       */
49      public String getExpressionString() {
50          StringBuffer sb = new StringBuffer();
51          sb.append("rename(");
52          sb.append(r1.toString());
53          for (Iterator i = renames.entrySet().iterator(); i.hasNext();) {
54              Map.Entry p = (Map.Entry) i.next();
55              sb.append(',');
56              Attribute a1 = (Attribute) p.getKey();
57              sb.append(a1.getRelation());
58              sb.append('.');
59              sb.append(a1.toString());
60              sb.append("->");
61              Attribute a2 = (Attribute) p.getValue();
62              sb.append(a2.getRelation());
63              sb.append('.');
64              sb.append(a2.toString());
65          }
66          sb.append(")");
67          return sb.toString();
68      }
69  
70      /*
71       * (non-Javadoc)
72       * 
73       * @see net.sf.bddbddb.ir.Operation#visit(net.sf.bddbddb.ir.HighLevelOperationVisitor)
74       */
75      public Object visit(HighLevelOperationVisitor i) {
76          return i.visit(this);
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see net.sf.bddbddb.ir.Operation#getDest()
83       */
84      public Relation getRelationDest() {
85          return r0;
86      }
87  
88      /*
89       * (non-Javadoc)
90       * 
91       * @see net.sf.bddbddb.ir.Operation#getSrcs()
92       */
93      public List getSrcs() {
94          return Collections.singletonList(r1);
95      }
96  
97      /***
98       * @return Returns the source relation.
99       */
100     public Relation getSrc() {
101         return r1;
102     }
103 
104     /***
105      * @return  the rename map
106      */
107     public Map getRenameMap() {
108         return renames;
109     }
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see net.sf.bddbddb.ir.Operation#copy()
115      */
116     public Operation copy() {
117         return new Rename(r0, r1, renames);
118     }
119 
120     /*
121      * (non-Javadoc)
122      * 
123      * @see net.sf.bddbddb.ir.Operation#replaceSrc(net.sf.bddbddb.Relation,
124      *      net.sf.bddbddb.Relation)
125      */
126     public void replaceSrc(Relation r_old, Relation r_new) {
127         if (r1 == r_old) r1 = r_new;
128     }
129 
130     /*
131      * (non-Javadoc)
132      * 
133      * @see net.sf.bddbddb.ir.Operation#setRelationDest(net.sf.bddbddb.Relation)
134      */
135     public void setRelationDest(Relation r0) {
136         this.r0 = r0;
137     }
138 }