1 package fr.ove.openmath.jome.model;
2
3 import java.util.*;
4 import fr.ove.openmath.jome.model.*;
5 import fr.ove.openmath.jome.model.events.*;
6 import fr.ove.openmath.jome.model.evaluation.*;
7
8 /***
9 *
10 * @author © 2000 DIRAT Laurent
11 * @version 2.1 10/01/2000
12 */
13 public class NaryOperator extends Operator {
14 /***
15 * Inserts the operator instance in the formula tree, from the current insertion position.
16 * (checks the priorities and goes up in the tree if necessary).
17 *
18 * @param ope the current insertion position.
19 * @return the new insertion position.
20 */
21 public FormulaTreeStructure insert(FormulaTreeStructure current) {
22 VariableOrNumber template;
23
24 current = findLocation(current);
25
26 if (current.getAsOperatorPriority() == getAsOperandPriority()) {
27
28
29 template = new VariableOrNumber();
30 current.addChild(template);
31
32
33 return template;
34 }
35 else {
36
37
38
39 current.addChild(this);
40
41
42
43
44
45 if ((current.getFather() == null) && (current.getNbChildren() == 1)) {
46 template = new VariableOrNumber();
47 addChild(template);
48 }
49 else {
50 FormulaTreeStructure fts = (FormulaTreeStructure) current.getChild(getRank() - 1);
51
52
53 addChild(fts);
54
55
56
57 current.removeChild(fts);
58 }
59
60
61 template = new VariableOrNumber();
62 addChild(template);
63
64
65 return template;
66 }
67 }
68
69 /***
70 * The Creation of the corresponding linear expression of the formula.
71 */
72 public String createLinear(String linear) {
73 FormulaTreeStructure child;
74
75 for (int i = 0; i < getNbChildren(); i++) {
76 child = (FormulaTreeStructure) getChild(i);
77 if (i == 0)
78 linear = child.createLinear(linear);
79 else {
80 linear += getTheOperator();
81 linear = child.createLinear(linear);
82 }
83 }
84 return linear;
85 }
86
87 /***
88 * Evaluates the instance.
89 */
90 public String evaluate() {
91 Vector evaluations = new Vector();
92
93
94 for (Enumeration e = getChildren().elements(); e.hasMoreElements(); )
95 evaluations.addElement(((FormulaTreeStructure) e.nextElement()).evaluate());
96
97 int nbEvaluations = evaluations.size();
98 String anEvaluation, currEvaluation;
99
100
101 for (int i = 1; i < nbEvaluations; i++) {
102 currEvaluation = (String) evaluations.elementAt(i);
103
104 for (int j = 0; j < i; j++) {
105 anEvaluation = (String) evaluations.elementAt(j);
106
107 if (Evaluator.type(currEvaluation) == Evaluator.type(anEvaluation)) {
108 evaluations.insertElementAt(Evaluator.evaluate(anEvaluation, currEvaluation, getTheOperator()), j);
109 evaluations.removeElement(anEvaluation);
110 evaluations.removeElement(currEvaluation);
111 i--;
112 nbEvaluations--;
113 break;
114 }
115 }
116 }
117
118
119
120 String result = (String) evaluations.firstElement();
121 for (int i = 1; i < nbEvaluations; i++)
122 result += getTheOperator() + (String) evaluations.elementAt(i);
123
124 return result;
125 }
126 }
127