1
2 package hep.wired.heprep.projection;
3
4 import java.util.*;
5
6 import org.jdom.Element;
7 import org.jdom.DataConversionException;
8
9 import org.freehep.xml.io.XMLIO;
10 import org.freehep.xml.io.XMLIOManager;
11
12 import hep.wired.variable.Variable;
13 import hep.wired.variable.NumberVariable;
14 import hep.wired.variable.DoubleVariable;
15 import hep.wired.variable.IntegerVariable;
16 import hep.wired.variable.BooleanVariable;
17
18 import hep.wired.heprep.feature.NamedVariables;
19
20 /***
21 * Base class for projections based on named variables.
22 *
23 * @author Mark Donszelmann
24 * @version $Id: VariableProjection.java 2161 2005-08-02 22:47:40Z duns $
25 */
26
27 public abstract class VariableProjection extends AbstractProjection implements NamedVariables {
28
29 private String name;
30 private Map
31
32 /***
33 * Creates a named projection.
34 */
35 public VariableProjection(String name) {
36 this.name = name;
37 }
38
39 public String getName() {
40 return name;
41 }
42
43 /***
44 * Adds a named variable with minimum, maximum and default value.
45 */
46 protected DoubleVariable addVariable(String name, double min, double max, double def, String unit, String description) {
47 DoubleVariable variable = new DoubleVariable(name, this, min, max, def, unit, description);
48 variables.put(name, variable);
49 return variable;
50 }
51
52 /***
53 * Adds a named variable with minimum, maximum and default value.
54 */
55 protected IntegerVariable addVariable(String name, int min, int max, int def, String unit, String description) {
56 IntegerVariable variable = new IntegerVariable(name, this, min, max, def, unit, description);
57 variables.put(name, variable);
58 return variable;
59 }
60
61 /***
62 * Adds a named variable with default value.
63 */
64 protected BooleanVariable addVariable(String name, boolean def, String description) {
65 BooleanVariable variable = new BooleanVariable(name, this, def, description);
66 variables.put(name, variable);
67 return variable;
68 }
69
70 /***
71 * Return true if variable with name exists.
72 */
73 public boolean exists(String name) {
74 return variables.containsKey(name);
75 }
76
77 /***
78 * Return set of variable names
79 */
80 public Set
81 return variables.keySet();
82 }
83
84 /***
85 * Returns a list of all variables
86 */
87 public Collection
88 return variables.values();
89 }
90
91 /***
92 * Return the unit, or null;
93 */
94 public String getUnit(String name) {
95 Variable var = (Variable)variables.get(name);
96 return (var != null) ? var.getUnit() : null;
97 }
98
99 /***
100 * Return the description
101 */
102 public String getDescription(String name) {
103 Variable var = (Variable)variables.get(name);
104 return (var != null) ? var.getDescription() : null;
105 }
106
107 public double getDoubleVariable(String name) {
108 NumberVariable var = (NumberVariable)variables.get(name);
109 if (var == null)
110 throw new IllegalArgumentException("Variable with name '"+name+"' does not exist.");
111
112 return (var instanceof DoubleVariable) ? ((DoubleVariable)var).getDoubleVariable() : ((IntegerVariable)var).getIntegerVariable();
113 }
114
115 public int getIntegerVariable(String name) {
116 NumberVariable var = (NumberVariable)variables.get(name);
117 if (var == null)
118 throw new IllegalArgumentException("Variable with name '"+name+"' does not exist.");
119
120 return (var instanceof DoubleVariable) ? (int)((DoubleVariable)var).getDoubleVariable() : ((IntegerVariable)var).getIntegerVariable();
121 }
122
123 public boolean getBooleanVariable(String name) {
124 BooleanVariable var = (BooleanVariable)variables.get(name);
125 if (var == null)
126 throw new IllegalArgumentException("Variable with name '"+name+"' does not exist.");
127
128 return var.getBooleanVariable();
129 }
130
131
132 /***
133 * Sets the value of a named variable.
134 */
135 public void setVariable(String name, double value) {
136 DoubleVariable var = (DoubleVariable)variables.get(name);
137 if (var == null)
138 throw new IllegalArgumentException("Variable with name '"+name+"' does not exist.");
139
140 var.setVariable(value);
141 }
142
143 /***
144 * Sets the value of a named variable.
145 */
146 public void setVariable(String name, int value) {
147 NumberVariable var = (NumberVariable)variables.get(name);
148 if (var == null)
149 throw new IllegalArgumentException("Variable with name '"+name+"' does not exist.");
150
151 if (var instanceof DoubleVariable) {
152 ((DoubleVariable)var).setVariable(value);
153 } else {
154 ((IntegerVariable)var).setVariable(value);
155 }
156 }
157
158 /***
159 * Sets the value of a named variable.
160 */
161 public void setVariable(String name, boolean value) {
162 BooleanVariable var = (BooleanVariable)variables.get(name);
163 if (var == null)
164 throw new IllegalArgumentException("Variable with name '"+name+"' does not exist.");
165
166 var.setVariable(value);
167 }
168
169 public void multiplyVariable(String name, double factor) {
170 setVariable(name, getDoubleVariable(name)*factor);
171 }
172
173 public void multiplyVariable(String name, int factor) {
174 if (variables.get(name) instanceof IntegerVariable) {
175 setVariable(name, getIntegerVariable(name)*factor);
176 } else {
177 setVariable(name, getDoubleVariable(name)*factor);
178 }
179 }
180
181 public void addVariable(String name, double add) {
182 setVariable(name, getDoubleVariable(name)+add);
183 }
184
185 public void addVariable(String name, int add) {
186 if (variables.get(name) instanceof IntegerVariable) {
187 setVariable(name, getIntegerVariable(name)+add);
188 } else {
189 setVariable(name, getDoubleVariable(name)+add);
190 }
191 }
192
193 public String toString() {
194 StringBuffer sb = new StringBuffer();
195 sb.append(super.toString());
196 sb.append(" - [");
197 for (Iterator i=variables.entrySet().iterator(); i.hasNext(); ) {
198 Map.Entry entry = (Map.Entry)i.next();
199 String name = (String)entry.getKey();
200 Variable variable = (Variable)entry.getValue();
201
202 sb.append(name);
203 sb.append("=");
204 sb.append(variable.toString());
205 sb.append(variable.getUnit() != null ? variable.getUnit() : "");
206 if (i.hasNext()) sb.append(", ");
207 }
208 sb.append("]");
209 return sb.toString();
210 }
211
212
213
214
215 public void save(XMLIOManager xmlioManager,
216 org.jdom.Element nodeEl) {
217 super.save(xmlioManager, nodeEl);
218 for (Iterator i=variables.entrySet().iterator(); i.hasNext(); ) {
219 Map.Entry entry = (Map.Entry)i.next();
220 String name = (String)entry.getKey();
221 Variable variable = (Variable)entry.getValue();
222
223
224 Element element = xmlioManager.save(variable);
225 nodeEl.addContent(element);
226
227
228 Element value = new Element(name);
229 variable.saveValue(value);
230 element.addContent(value);
231 }
232 }
233
234 public void restore(XMLIOManager xmlioManager,
235 org.jdom.Element nodeEl) {
236 super.restore(xmlioManager, nodeEl);
237 for (Iterator i = nodeEl.getChildren().iterator(); i.hasNext(); ) {
238 Element element = (Element)i.next();
239 Variable variable = (Variable)xmlioManager.restore(element);
240 variables.put(name, variable);
241
242 Element value = (Element)element.getChildren().iterator().next();
243 String name = value.getAttributeValue("name");
244 variable.restoreValue(value);
245 }
246 }
247 }