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 java.util.*;
32 import java.awt.*;
33
34 import fr.ove.openmath.jome.model.processor.*;
35 import fr.ove.openmath.jome.model.events.ModelEvent;
36 import fr.ove.openmath.jome.model.evaluation.*;
37 import fr.ove.openmath.jome.behaviour.Modifiable;
38 import fr.ove.openmath.jome.ctrl.linear.events.*;
39 import fr.ove.utils.Factory;
40 import fr.ove.openmath.jome.formaters.om.*;
41 import fr.ove.openmath.jome.formaters.pmml.PMathMLFormater;
42 import fr.ove.openmath.jome.formaters.mml.*;
43
44 /***
45 * The formula.<BR>
46 * The root of the formula tree structure.
47 *
48 * @author © 2000 DIRAT Laurent
49 * @version 2.0 28/06/99
50 */
51 public class Formula extends FormulaTreeStructure implements LinearParserListener {
52 /***
53 * The current insertion position in the formula (tree structure).
54 */
55 public FormulaTreeStructure current;
56
57 /***
58 * The OpenMath formater for exportation of the formula as an OpenMath object
59 */
60 OpenMathFormater omFormater = new OpenMathFormater();
61
62 /***
63 * The MathML formater for exportation of the formula as a MathML object
64 */
65 MathMLFormater mmlFormater = new MathMLFormater();
66
67 PMathMLFormater pmmlFormater = new PMathMLFormater();
68
69
70
71
72 private Stack openingStack = new Stack();
73
74 /***
75 * Flag to deal with a possible processing of the model of the formula before rendering.<BR>
76 * The default is <CODE>false</CODE>, i.e. we want the formula to be displayed
77 * normally (no processing).
78 */
79 private boolean doProcessing = false;
80
81 /***
82 * The processor which will process the model of the formula before rendering if wanted.<BR>
83 * By default, no processor is set. One processor has to added when desired.
84 */
85 private Processor processor = null;
86
87 /***
88 * The Constructor.
89 */
90 public Formula () {
91 setResourceIdentifier("FORMULA");
92 setAsOperatorPriority(resourcesManager.getAsOperatorPriority("formulaPriorities"));
93 setAsOperandPriority(resourcesManager.getAsOperandPriority("formulaPriorities"));
94 current = this;
95 }
96
97 /***
98 * To check is the instance is an operator.
99 * @return <CODE>true</CODE> if it is an operator. <CODE>false</CODE> otherwise.
100 */
101 public boolean isOperator() {
102 return false;
103 }
104
105 /***
106 * Inserts the operator instance in the formula tree, from the current insertion position.
107 * (checks the priorities and goes up in the tree if necessary).<BR>
108 * At the moment, we can't insert a formula in an another formula.
109 * @param current the current insertion position.
110 */
111 public FormulaTreeStructure insert(FormulaTreeStructure current) {
112
113
114 return null;
115 }
116
117 /***
118 * The Creation of the corresponding linear expression of the formula.
119 */
120 public String createLinear(String linear) {
121 if (getNbChildren() > 0)
122 linear = ((FormulaTreeStructure) getChild(0)).createLinear(linear);
123
124 return linear;
125 }
126
127 /***
128 * clears the formula.
129 */
130 public void clear() {
131
132 removeAll();
133
134 current = this;
135 }
136
137 /***
138 * Returns the linear expression of the formula.
139 */
140 public String getLinear() {
141 return createLinear("");
142 }
143
144 /***
145 * Returns the OpenMath representation of the formula.
146 */
147 public String getOpenMath() {
148 return omFormater.getOpenMath(this);
149 }
150
151 /***
152 * Returns the MathML representation of the formula.
153 */
154 public String getMathML() {
155 return mmlFormater.getMathML(this);
156 }
157
158 public String getPMML(){
159 return pmmlFormater.getPMML(this);
160 }
161
162 /***
163 * Evaluates the instance.
164 */
165 public String evaluate() {
166 if (getNbChildren() > 0)
167 return ((FormulaTreeStructure) getChild(0)).evaluate();
168 else
169 return "";
170 }
171
172 /***
173 * Treats the event received.
174 * @param linearParserEvent the event received.
175 */
176 public void consumeLinearParserEvent(LinearParserEvent linearParserEvent) {
177 int identifier = linearParserEvent.getIdentifier();
178 String value = linearParserEvent.getValue();
179 ModelEvent modelEvent;
180
181 switch (identifier) {
182 case LinearParserEvent.START_EXPRESSION :
183 clear();
184 openingStack.setSize(0);
185 modelEvent = new ModelEvent(this);
186 modelEvent.setAction(ModelEvent.CLEAR, null);
187 fireModelEvent(modelEvent);
188 break;
189
190 case LinearParserEvent.END_EXPRESSION :
191
192
193
194 if (doProcessing && (processor != null)) {
195
196 processor.init();
197 processor.doProcess();
198 }
199
200 modelEvent = new ModelEvent(this);
201 modelEvent.setAction(ModelEvent.CREATE, null);
202 fireModelEvent(modelEvent);
203 break;
204
205 default :
206 String strIdentifier = linearParserEvent.getIdentifierAsString();
207 String className = resourcesManager.getClassName(strIdentifier);
208 FormulaTreeStructure newFts = null;
209
210
211
212
213 if (className.equals("null")) {
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 if (identifier == linearParserEvent.SEPARATOR) {
229
230 current = current.goTo(resourcesManager.getAsOperatorPriority("slotPriorities"));
231
232
233
234
235
236 FormulaTreeStructure currentFather = (FormulaTreeStructure) current.getFather();
237 if(currentFather instanceof NaryOperator2)
238 current = ((NaryOperator2)currentFather).addElement();
239 else{
240 current = ((Function2)currentFather).addElement();
241 Variable var = new Variable();
242 var.setResourceIdentifier(strIdentifier);
243 var.setValue(value);
244 var.setAsOperatorPriority(resourcesManager.getAsOperatorPriority("constantPriorities"));
245 var.setAsOperandPriority(resourcesManager.getAsOperandPriority("constantPriorities"));
246 current = var.insert(current);
247 current = ((Function2)currentFather).addElement();
248 }
249
250 }
251 else {
252
253 if (!openingStack.isEmpty()) {
254 current = current.goTo(resourcesManager.getAsOperatorPriority("slotPriorities"));
255 FormulaTreeStructure currentFather = (FormulaTreeStructure) current.getFather();
256 if(currentFather instanceof Function2 && value != null){
257 current = ((Function2)currentFather).addElement();
258 Variable var = new Variable();
259 var.setValue(value);
260 var.setResourceIdentifier(strIdentifier);
261 var.setAsOperatorPriority(resourcesManager.getAsOperatorPriority("constantPriorities"));
262 var.setAsOperandPriority(resourcesManager.getAsOperandPriority("constantPriorities"));
263 current = var.insert(current);
264 }
265
266 current = (FormulaTreeStructure) openingStack.pop();
267
268
269 current = (FormulaTreeStructure) current.getFather();
270 }
271 }
272 }
273 else {
274 if (className.equals("refine")) {
275 String[] properties = resourcesManager.getResourceStrings(value);
276 if (properties.length != 0)
277 strIdentifier = value;
278 else {
279 if (identifier == linearParserEvent.RESERVED)
280
281
282 strIdentifier = "VARIABLE";
283 else
284
285
286
287 strIdentifier = "defaultFunction";
288 }
289 }
290
291 newFts = (FormulaTreeStructure) Factory.getClassInstance(resourcesManager.getClassName(strIdentifier));
292 newFts.setResourceIdentifier(strIdentifier);
293 String prioritiesId = resourcesManager.getPrioritiesIdentifier(strIdentifier);
294 newFts.setAsOperatorPriority(resourcesManager.getAsOperatorPriority(prioritiesId));
295 newFts.setAsOperandPriority(resourcesManager.getAsOperandPriority(prioritiesId));
296
297 if (value != null)
298 newFts.setValue(value);
299
300 Integer arity = resourcesManager.getArity(strIdentifier);
301 if (arity != null)
302 ((KaryOperator) newFts).setOperatorArity(arity.intValue());
303
304 switch (identifier) {
305 case LinearParserEvent.FUNCTION :
306 case LinearParserEvent.FUNCTION2 :
307
308
309
310
311 openingStack.push(newFts);
312 }
313
314 current = newFts.insert(current);
315 }
316 }
317 }
318
319 /***
320 * Sets if we had to process the instance before rendering.
321 * @param doProcessing <CODE>true</CODE> if the instance needs a processing.
322 * <CODE>false</CODE> otherwise.
323 */
324 public void setDoProcessing(boolean doProcessing) {
325 this.doProcessing = doProcessing;
326 }
327
328 /***
329 * Returns <CODE>true</CODE> if the instance needs a processing.
330 * <CODE>false</CODE> otherwise.
331 */
332 public boolean getDoProcessing() {
333 return doProcessing;
334 }
335
336 /***
337 * Sets the processor of the instance.
338 * @param processor the processor.
339 */
340 public void setProcessor(Processor processor) {
341 this.processor = processor;
342 }
343
344 /***
345 * Returns the processor of the instance.
346 */
347 public Processor getProcessor() {
348 return processor;
349 }
350
351 /***
352 * Processes the instance
353 */
354 public void doProcess() {
355 if (doProcessing && (processor != null))
356 processor.doProcess();
357 }
358
359 public static void main(String args[]) {
360 String exp = "sum(i^n,0,sum(i^n,0,5))t";
361 Formula formula = new Formula();
362 fr.ove.openmath.jome.ctrl.linear.LinearParser linearParser = new fr.ove.openmath.jome.ctrl.linear.LinearParser();
363 linearParser.addLinearParserListener(formula);
364 linearParser.parse(exp);
365 System.out.println("la formule saisie est : \t\t" + exp);
366 System.out.println("la formule construite est : \t\t" + formula.getLinear());
367
368 try {
369 Thread.sleep(5000);
370 }
371 catch (Exception e) {
372 e.printStackTrace();
373 }
374 }
375
376 }