View Javadoc

1   // MapBasedTranslator.java, created Oct 24, 2004 12:19:42 AM 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.order;
5   
6   import java.util.Collection;
7   import java.util.HashMap;
8   import java.util.Iterator;
9   import java.util.LinkedList;
10  import java.util.Map;
11  import jwutil.util.Assert;
12  import net.sf.bddbddb.Attribute;
13  import net.sf.bddbddb.FindBestDomainOrder;
14  import net.sf.bddbddb.InferenceRule;
15  import net.sf.bddbddb.Relation;
16  import net.sf.bddbddb.RuleTerm;
17  import net.sf.bddbddb.Variable;
18  
19  /***
20   * Translator based on a map.
21   * 
22   * @author jwhaley
23   * @version $Id: MapBasedTranslator.java 448 2005-03-07 06:58:48Z cs343 $
24   */
25  public class MapBasedTranslator implements OrderTranslator {
26      
27      Map m;
28      
29      public MapBasedTranslator(Map m) {
30          this.m = m;
31      }
32      
33      /***
34       * direction == true means map from rule to relation.
35       * direction == false means map from relation to rule.
36       * 
37       * @param ir  rule
38       * @param r  relation
39       * @param direction  true=rule->relation, false=relation->rule
40       */
41      public MapBasedTranslator(InferenceRule ir, Relation r, boolean direction) {
42          m = new HashMap();
43          for (Iterator i = ir.getSubgoals().iterator(); i.hasNext(); ) {
44              RuleTerm rt = (RuleTerm) i.next();
45              if (rt.getRelation() != r) continue;
46              int rsize = r.getAttributes().size();
47              Assert._assert(rsize == rt.getVariables().size());
48              for (int j = 0; j < rsize; ++j) {
49                  Attribute a = (Attribute) r.getAttribute(j);
50                  Variable v = (Variable) rt.getVariable(j);
51                  // Note: this doesn't match exactly if a relation appears
52                  // twice in a rule.
53                  if (direction)
54                      m.put(v, a);
55                  else
56                      m.put(a, v);
57              }
58          }
59      }
60      
61      /***
62       * direction == true means map from ruleterm to relation.
63       * direction == false means map from relation to ruleterm.
64       * 
65       * @param rt  ruleterm
66       * @param direction  true=ruleterm->relation, false=relation->ruleterm
67       */
68      public MapBasedTranslator(RuleTerm rt, boolean direction) {
69          m = new HashMap();
70          Relation r = rt.getRelation();
71          int rsize = r.getAttributes().size();
72          Assert._assert(rsize == rt.getVariables().size());
73          for (int j = 0; j < rsize; ++j) {
74              Attribute a = (Attribute) r.getAttribute(j);
75              Variable v = (Variable) rt.getVariable(j);
76              if (direction)
77                  m.put(v, a);
78              else
79                  m.put(a, v);
80          }
81      }
82      
83      /* (non-Javadoc)
84       * @see net.sf.bddbddb.FindBestDomainOrder.OrderTranslator#translate(net.sf.bddbddb.FindBestDomainOrder.Order)
85       */
86      public Order translate(Order o) {
87          if (FindBestDomainOrder.TRACE > 3) System.out.print("Translating "+o);
88          LinkedList result = new LinkedList();
89          for (Iterator i = o.iterator(); i.hasNext(); ) {
90              Object a = i.next();
91              if (a instanceof Collection) {
92                  Collection result2 = new LinkedList();
93                  for (Iterator j = ((Collection) a).iterator(); j.hasNext(); ) {
94                      Object a2 = j.next();
95                      Object b2 = m.get(a2);
96                      if (b2 != null) result2.add(b2);
97                  }
98                  if (result2.size() > 1) {
99                      result.add(result2);
100                 } else if (!result2.isEmpty()) {
101                     result.add(result2.iterator().next());
102                 }
103             } else {
104                 Object b = m.get(a);
105                 if (b != null) result.add(b);
106             }
107         }
108         if (FindBestDomainOrder.TRACE > 3) System.out.println(" -> "+result);
109         return new Order(result);
110     }
111     
112 }