1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package fr.ove.openmath.jome.model;
30
31 import fr.ove.openmath.jome.model.*;
32 import fr.ove.openmath.jome.model.events.*;
33 import fr.ove.openmath.jome.model.evaluation.*;
34
35 /***
36 * Elements in the formula such as variables (letters) and numbers.<BR>
37 * <CODE>VariableOrNumber</CODE> represents a node in the formula tree.
38 * Terminal node, so leaves.
39 *
40 * @author © 1999 DIRAT Laurent
41 * @version 2.0 24/06/1999
42 */
43 public class VariableOrNumber extends Constant {
44 /***
45 * To check if the Constant is a number or not.
46 */
47 private boolean isNumber;
48
49 /***
50 * To check if the number represented by the Constant is an integer or a float.
51 * So if the value is <CODE>true</CODE>, it's an integer; Otherwise, it's a float.
52 */
53 private boolean isInteger;
54
55 /***
56 * To indicate that we have a constant with a specific signification.<BR>
57 * In this particular case, it's a request variable (from the mfd2 CD). <BR>
58 * By default, it is not a request variable.
59 */
60 private boolean isRequestVariable = false;
61
62
63 /***
64 * The default constructor.<BR>
65 * Constructs a template.
66 */
67 public VariableOrNumber() {
68 this("VARIABLE", "[?]", false, false);
69 setIsTemplate(true);
70 }
71
72 /***
73 * The Constructor.
74 *
75 * @param value the value of the constant.
76 * @param isNumber to set if the constant is a number or not.
77 * @param isInteger to set, if the constant is a numbern if it is an integer or a float.
78 */
79 public VariableOrNumber(String resourceIdentifier, String value, boolean isNumber, boolean isInteger) {
80 super();
81 setResourceIdentifier(resourceIdentifier);
82 setValue(value);
83 this.isNumber = isNumber;
84 this.isInteger = isInteger;
85 }
86
87 /***
88 * Sets the constant as a number.
89 * @param isNumber <CODE>true</CODE> if it is a number.<CODE>false</CODE>
90 * otherwise.
91 */
92 public void setIsNumber(boolean isNumber) {
93 this.isNumber = isNumber;
94 }
95
96 /***
97 * Returns <CODE>true</CODE> if it is a number.<BR>
98 * <CODE>false</CODE> otherwise.
99 */
100 public boolean isNumber() {
101 return isNumber;
102 }
103
104 /***
105 * Sets the constant, which is a number, as an integer or as a float.
106 * @param isInteger <CODE>true</CODE> if it is an integer.<CODE>false</CODE>
107 * if it is a float.
108 */
109 public void setIsInteger(boolean isInteger) {
110 this.isInteger = isInteger;
111 }
112
113 /***
114 * Returns <CODE>true</CODE> if it is an integer.<CODE>false</CODE>
115 * if it is a float.
116 */
117 public boolean isInteger() {
118 return isInteger;
119 }
120
121 /***
122 * Sets the constant as a request variable.
123 */
124 public void setIsRequestVariable(boolean isRequestVariable) {
125 this.isRequestVariable = isRequestVariable;
126 }
127
128 /***
129 * Returns <CODE>true</CODE> if it is a request variable.<BR>
130 * <CODE>false</CODE> otherwise.
131 */
132 public boolean isRequestVariable() {
133 return isRequestVariable;
134 }
135
136 /***
137 * Inserts the operator instance in the formula tree, from the current insertion position.
138 * (checks the priorities and goes up in the tree if necessary).
139 *
140 * @param ope the current insertion position.
141 * @return the new insertion position.
142 */
143 public FormulaTreeStructure insert(FormulaTreeStructure current) {
144 if (isTemplate()) {
145 if (isRequestVariable)
146 return super.insert(current);
147
148
149
150 current.addChild(this);
151 return this;
152 }
153 else {
154 if (current.getAsOperatorPriority() == resourcesManager.getAsOperatorPriority("constantPriorities")) {
155
156
157
158
159
160
161
162 if (current.isTemplate()) {
163 if (((VariableOrNumber) current).isRequestVariable && !isNumber) {
164 current.setIsTemplate(false);
165 ((VariableOrNumber) current).setValue(getValue());
166 return current;
167 }
168 else {
169 FormulaTreeStructure father = (FormulaTreeStructure) current.getFather();
170 father.addChild(this, current.getRank());
171 father.removeChild(current);
172
173
174
175
176
177
178
179 return this;
180 }
181 }
182 else {
183
184
185
186
187
188
189 {
190
191 current = (new HorizentalGroup()).insert(current);
192 current = insert(current);
193 return current;
194 }
195 }
196 }
197 else {
198 if ((current.getFather() == null) && (current.getNbChildren() == 0)) {
199
200
201
202
203
204 current.addChild(this);
205
206 return this;
207 }
208 else {
209
210
211 current = (new HorizentalGroup()).insert(current);
212 current = insert(current);
213 return current;
214 }
215 }
216 }
217 }
218
219 /***
220 * The Creation of the corresponding linear expression of the formula.
221 */
222 public String createLinear(String linear) {
223 if (isRequestVariable)
224 linear += "?";
225
226 if (!isTemplate())
227 linear += getValue();
228
229 return linear;
230 }
231 }