View Javadoc

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                      // En fait le calcul de la profondeur est fait ? la construction de la formule.
64                      // donc rien de particulier ? faire ici, si ce n'est affecter la valeur de la 
65                      // profondeur max de l'arbre de la formule.
66                      maxDepthValue = 0; // initialisation
67                      computeMaxDepthValue(formula); // calcul.
68                      break;
69                  case SIMPLE_ICONIFICATION :
70                      // Iconifie les parties de la formule qui ont une profondeur supÈrieure
71                      // ? celle spÈcifiÈe.
72                      setUpdateDisplay(true);
73                      iconifyTheFTS(formula, getLevel());
74                      setUpdateDisplay(false);
75                      break;
76                  case RECURSIVE_ICONIFICATION :
77                      // Iconifie les parties de la formule qui ont une profondeur supÈrieure
78                      // ? celle spÈcifiÈe. Iconification rÈcursive.
79                      setUpdateDisplay(true);
80                      iconifyAllTheFTS(formula, getLevel());
81                      setUpdateDisplay(false);
82                      break;
83                  case COMPUTE_AND_ICONIFY :
84                      // En fait le calcul de la profondeur est fait ? la construction de la formule.
85                      // donc rien de particulier ? faire ici, si ce n'est affecter la valeur de la 
86                      // profondeur max de l'arbre de la formule.
87                      maxDepthValue = 0; // initialisation
88                      computeMaxDepthValue(formula); // calcul.
89                      // Iconifie les parties de la formule qui ont une profondeur supÈrieure
90                      // ? celle spÈcifiÈ. Iconification rÈcursive.
91                      // Le defaut est que l'on icionifie ? une valeur correspondant ? peu prËs ? la
92                      // moitiÈ de la profondeur de l'arbre.
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         // On parcourt tous les enfants de l'instance et on descend dans toute l'arboresnce
106         // jusqu'au feuilles. (DFS) ArrivÈe ? la feuille, on teste la valeur de la profondeur et si
107         // elle est spÈrieure ? celle spÈcifiÈe, on iconifie.
108         // On rÈitËre le process sur tous les enfants.
109         FormulaTreeStructure child = null;
110         
111         /*
112         if (fts.getNbChildren() > 0) {
113             for (Enumeration e = fts.getChildren().elements(); e.hasMoreElements(); ) {
114                 child = (FormulaTreeStructure) e.nextElement();
115                 if (child.getDepth() < depth)
116                     iconifyTheFTS(child, depth);
117                 else
118                     if (child.getNbChildren() > 0) // Si child n'est pas une feuille (i.e. constante), on iconifie
119                         iconify(child, depth);
120             }
121         }
122         */
123         
124         if (fts.isIcon()) {
125             FormulaTreeStructure father = (FormulaTreeStructure) fts.getFather();
126             fts.uniconify();
127             
128             // On envoie maintenant un ÈvÈnement comme quoi il faut reconstruire
129             // l'affichage.
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         // On parcourt tous les enfants de l'instance et on descend dans toute l'arboresnce
157         // jusqu'au feuilles. (DFS) ArrivÈe ? la feuille, on teste la valeur du strahler et si
158         // elle est infÈrieure ? celle spÈcifiÈe, on iconifie.
159         // On rÈitËre le process sur tous les enfants.
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             // On ajoute notre instance ? iconifier dans Icon (<=> on iconfie l'instance)
177             icon.addIconified(fts);
178             // On insËre maintenant notre icon ? la place de fts.
179             // On prend le pËre de l'instance.
180             FormulaTreeStructure father = (FormulaTreeStructure) fts.getFather();
181             // On insËre notre icone ? la place de l'instance
182             father.addChild(icon, fts.getRank());
183             // On enlËve l'instance de la liste des enfants de father.
184             father.removeChild(fts);
185             
186             if (getUpdateDisplay()) {
187                 // On ajoute un display pour notre icon.
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         //p.setLevel(3);
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 }