View Javadoc

1   // RuleTerm.java, created Mar 16, 2004 12:42:16 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;
5   
6   import java.util.Iterator;
7   import java.util.LinkedList;
8   import java.util.List;
9   import java.util.Map;
10  import jwutil.util.Assert;
11  import org.jdom.Element;
12  
13  /***
14   * A term in a Datalog rule.
15   * 
16   * @author jwhaley
17   * @version $Id: RuleTerm.java 549 2005-05-17 10:17:33Z joewhaley $
18   */
19  public class RuleTerm {
20      
21      /***
22       * Relation for this rule term.
23       */
24      protected Relation relation;
25      
26      /***
27       * List of variables in this rule term.
28       */
29      protected List/*<Variable>*/ variables;
30  
31      /***
32       * Create a new RuleTerm with the given relation and list of variables.
33       * 
34       * @param relation
35       * @param variables
36       */
37      public RuleTerm(Relation relation, List variables) {
38          super();
39          this.relation = relation;
40          this.variables = variables;
41      }
42  
43      /*
44       * (non-Javadoc)
45       * 
46       * @see java.lang.Object#toString()
47       */
48      public String toString() {
49          StringBuffer sb = new StringBuffer();
50          sb.append(relation);
51          sb.append("(");
52          for (Iterator i = variables.iterator(); i.hasNext();) {
53              sb.append(i.next());
54              if (i.hasNext()) sb.append(",");
55          }
56          sb.append(")");
57          return sb.toString();
58      }
59  
60      /***
61       * @return Returns the relation.
62       */
63      public Relation getRelation() {
64          return relation;
65      }
66  
67      /***
68       * @return Returns the variables.
69       */
70      public List getVariables() {
71          return variables;
72      }
73  
74      /***
75       * @return  number of variables in this rule term
76       */
77      public int numberOfVariables() {
78          return variables.size();
79      }
80  
81      /***
82       * @param i  index
83       * @return  variable at the given index
84       */
85      public Variable getVariable(int i) {
86          return (Variable) variables.get(i);
87      }
88  
89      /***
90       * @param v  variable
91       * @return  index of the given variable
92       */
93      public int getVariableIndex(Variable v) {
94          return variables.indexOf(v);
95      }
96  
97      /***
98       * @param v  variable
99       * @return  attribute of the given variable
100      */
101     public Attribute getAttribute(Variable v) {
102         int index = getVariableIndex(v);
103         if (index < 0) return null;
104         return relation.getAttribute(index);
105     }
106     
107     public static RuleTerm fromXMLElement(Element e, Solver s, Map nameToVar) {
108         Relation r = s.getRelation(e.getAttributeValue("relation"));
109         List vars = new LinkedList();
110         for (Iterator i = e.getContent().iterator(); i.hasNext(); ) {
111             Element e2 = (Element) i.next();
112             Variable v = (Variable) nameToVar.get(e2.getName());
113             Domain d = r.getAttribute(vars.size()).getDomain();
114             if (v == null) nameToVar.put(e2.getName(), v = new Variable(e2.getName(), d));
115             Assert._assert(v.getDomain() == d);
116         }
117         return new RuleTerm(r, vars);
118     }
119     
120     public Element toXMLElement() {
121         Element e = new Element("ruleTerm");
122         e.setAttribute("relation", relation.toString());
123         for (Iterator i = variables.iterator(); i.hasNext(); ) {
124             Variable v = (Variable) i.next();
125             e.addContent(new Element(v.toString()));
126         }
127         return e;
128     }
129 }