1 package fr.ove.openmath.jome.model.processor;
2
3 import fr.ove.openmath.jome.model.*;
4 import fr.ove.openmath.jome.model.events.*;
5 import java.util.*;
6 import java.awt.*;
7
8 /***
9 * A processor for the depth of the formula.
10 *
11 * @author © 2000 DIRAT Laurent
12 * @version 1.0 21/08/2000
13 */
14 public class DepthProcessor extends ProcessorImpl implements DepthProcessorInterface {
15 /***
16 * The maximal depth of the formula.
17 */
18 private int maxDepthValue = 0;
19
20 /***
21 * The constructor.
22 * @param formula the formula to process.
23 */
24 public DepthProcessor(Formula formula) {
25 super(formula);
26 }
27
28 /***
29 * Sets the depth level form which the processing will be considering.<BR>
30 * For convenience only, in most cases this method is equivalent to the
31 * @see setLevel of the @see Processor interface.
32 * @param depthLevel the depth level.
33 */
34 public void setDepthLevel(int depthLevel) {
35 setLevel(depthLevel);
36 }
37
38 /***
39 * Returns the depth level form which the processing will be considering.<BR>
40 * For convenience only, in most cases this method is equivalent to the
41 * @see getLevel of the @see Processor interface.
42 */
43 public int getDepthLevel() {
44 return getLevel();
45 }
46
47 /***
48 * Returns the max depth value of the formula
49 */
50 public int getMaxDepthValue() {
51 return maxDepthValue;
52 }
53
54 /***
55 * Does processing.
56 */
57 public void doProcess() {
58 Formula formula = getFormula();
59
60 if (formula != null) {
61 switch (getProcessingId()) {
62 case COMPUTE :
63
64
65
66 maxDepthValue = 0;
67 computeMaxDepthValue(formula);
68 break;
69 case SIMPLE_ICONIFICATION :
70
71
72 setUpdateDisplay(true);
73 iconifyTheFTS(formula, getLevel());
74 setUpdateDisplay(false);
75 break;
76 case RECURSIVE_ICONIFICATION :
77
78
79 setUpdateDisplay(true);
80 iconifyAllTheFTS(formula, getLevel());
81 setUpdateDisplay(false);
82 break;
83 case COMPUTE_AND_ICONIFY :
84
85
86
87 maxDepthValue = 0;
88 computeMaxDepthValue(formula);
89
90
91
92
93 setLevel(maxDepthValue / 2);
94 iconifyAllTheFTS(formula, getLevel());
95 }
96 }
97 }
98
99 /***
100 * Iconifies the descendance of the instance which has a depth greater
101 * than the specified.
102 * @param depth the specified depth value.
103 */
104 private void iconifyTheFTS(FormulaTreeStructure fts, int depth) {
105
106
107
108
109 FormulaTreeStructure child = null;
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 if (fts.isIcon()) {
125 FormulaTreeStructure father = (FormulaTreeStructure) fts.getFather();
126 fts.uniconify();
127
128
129
130 ModelEvent modelEvent = new ModelEvent(father);
131 modelEvent.setAction(ModelEvent.REBUILD, null);
132 father.fireModelEvent(modelEvent);
133
134 iconifyTheFTS(father, depth);
135 }
136
137 if (fts.getDepth() < depth) {
138 if (fts.getNbChildren() > 0) {
139 for (Enumeration e = fts.getChildren().elements(); e.hasMoreElements(); )
140 iconifyTheFTS((FormulaTreeStructure) e.nextElement(), depth);
141 }
142 }
143 else {
144 if (fts.getNbChildren() > 0)
145 iconify(fts, depth);
146 }
147
148 }
149
150 /***
151 * Iconifies all the descendance of the instance which has a Strahler number lower
152 * than the specified.
153 * @param depth the specified Strahler number.
154 */
155 private void iconifyAllTheFTS(FormulaTreeStructure fts, int depth) {
156
157
158
159
160 if (fts.getNbChildren() > 0) {
161 for (Enumeration e = fts.getChildren().elements(); e.hasMoreElements(); )
162 iconifyAllTheFTS((FormulaTreeStructure) e.nextElement(), depth);
163
164 if (fts.getDepth() >= depth)
165 iconify(fts, depth);
166 }
167 }
168
169 /***
170 * Iconifie the instance if it has a Strahler number lower than the specified.
171 * @param depth the specified Strahler number
172 */
173 private void iconify(FormulaTreeStructure fts, int depth) {
174 if (fts.getFather() != null) {
175 Icon icon = new Icon(fts);
176
177 icon.addIconified(fts);
178
179
180 FormulaTreeStructure father = (FormulaTreeStructure) fts.getFather();
181
182 father.addChild(icon, fts.getRank());
183
184 father.removeChild(fts);
185
186 if (getUpdateDisplay()) {
187
188 ModelEvent modelEvent = new ModelEvent(father);
189 modelEvent.setAction(ModelEvent.ADD, icon);
190 father.fireModelEvent(modelEvent);
191 }
192 }
193 }
194
195 /***
196 * Computes the max depth value of the formula
197 */
198 private void computeMaxDepthValue(FormulaTreeStructure fts) {
199 if (fts.getNbChildren() > 0) {
200 for (Enumeration e = fts.getChildren().elements(); e.hasMoreElements(); )
201 computeMaxDepthValue((FormulaTreeStructure) e.nextElement());
202 }
203
204 maxDepthValue = (int) Math.max(fts.getDepth(), maxDepthValue);
205 }
206
207 public static void main(String args[]) {
208 String exp = "1+2/3*(4+3/5)^2+2/((3+5^9)*(1+2+3))";
209 Formula formula = new Formula();
210 formula.setDoProcessing(true);
211
212 DepthProcessor p = new DepthProcessor(formula);
213
214 formula.setProcessor(p);
215
216 fr.ove.openmath.jome.ctrlview.bidim.FormulaDisplay display = new fr.ove.openmath.jome.ctrlview.bidim.FormulaDisplay();
217 formula.addModelListener(display);
218 display.addControlListener(formula);
219
220 java.awt.Frame f = new java.awt.Frame();
221 f.setLayout(new java.awt.BorderLayout());
222 f.setBounds(50, 50, 250, 80);
223 f.add("Center", display);
224 f.setVisible(true);
225
226 fr.ove.openmath.jome.ctrl.linear.LinearParser linearParser = new fr.ove.openmath.jome.ctrl.linear.LinearParser();
227 linearParser.addLinearParserListener(formula);
228 linearParser.parse(exp);
229 System.out.println("la formule saisie est : \t\t" + exp);
230 System.out.println("la formule construite est : \t\t" + formula.getLinear());
231 }
232 }