View Javadoc

1   // Variable.java, created Mar 16, 2004 12:43:38 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 org.jdom.Element;
7   
8   /***
9    * A Variable is a variable in a rule.
10   * 
11   * @author jwhaley
12   * @version $Id: Variable.java 353 2004-10-27 19:21:05Z joewhaley $
13   */
14  public class Variable {
15      
16      /***
17       * Name of variable.
18       */
19      protected String name;
20      
21      /***
22       * Domain of variable.
23       */
24      protected Domain domain;
25  
26      /***
27       * Create empty variable.
28       */
29      public Variable() {
30          this("_");
31      }
32  
33      /***
34       * Create a new variable with the given name.
35       * 
36       * @param name  name of variable
37       */
38      public Variable(String name) {
39          super();
40          this.name = name;
41      }
42  
43      /***
44       * Create a new variable with the given name and domain.
45       * 
46       * @param name  name of variable
47       * @param fd  domain of variable
48       */
49      public Variable(String name, Domain fd) {
50          super();
51          this.name = name;
52          this.domain = fd;
53      }
54  
55      /***
56       * @return Returns the name.
57       */
58      public String getName() {
59          return name;
60      }
61  
62      /***
63       * Set the name of this variable.
64       * 
65       * @param name
66       *            The name to set.
67       */
68      public void setName(String name) {
69          this.name = name;
70      }
71  
72      /***
73       * Get the domain of this variable.
74       * 
75       * @return  domain of this variable
76       */
77      public Domain getDomain() {
78          return domain;
79      }
80  
81      /***
82       * Set the domain of this variable.
83       * 
84       * @param domain  the domain to set.
85       */
86      public void setDomain(Domain domain) {
87          this.domain = domain;
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see java.lang.Object#toString()
94       */
95      public String toString() {
96          return name;
97      }
98      
99      public static Variable fromXMLElement(Element e, XMLFactory f) {
100         String ruleName = e.getAttributeValue("rule");
101         InferenceRule ir = f.getRule(ruleName);
102         String variableName = e.getAttributeValue("name");
103         return ir.getVariable(variableName);
104     }
105     
106     public Element toXMLElement(InferenceRule ir) {
107         Element e = new Element("variable");
108         e.setAttribute("rule", "rule"+ir.id);
109         e.setAttribute("name", name);
110         return e;
111     }
112 }