View Javadoc

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          // On cherche la position d'insertion de notre opÈrateur
24          current = findLocation(current);
25  
26          if (current.getAsOperatorPriority() == getAsOperandPriority()) {
27              // On a dÈj? tapÈ dans la formule un opÈrateur de ce genre, on va donc seulement insÈrer
28              // un template.
29              template = new VariableOrNumber();
30              current.addChild(template);
31              
32              // On retourne la refÈrence de notre dernier point d'insertion.
33              return template;
34          }
35          else { 
36              // On est dans le cas o? on commence ? saisir un tel opÈrateur.
37              
38              // On ajoute l'opÈrateur comme fils ? l'opÈrateur courant
39              current.addChild(this);
40              
41              // On teste s'il on est dans le cas o? l'on a dÈj? tapÈ qque chose dans
42              // la formule. Si non, current est forcÈment de type Formula, par consÈquent
43              // on est dans le cas particulier o? cela doit produire [?]*[?] puisque
44              // normalement on ne devrait, dans ce cas l?, pas pouvoir taper de "*"
45              if ((current.getFather() == null) && (current.getNbChildren() == 1)) {
46                  template = new VariableOrNumber();
47                  addChild(template); // On ajoute le premier template.
48              }
49              else {
50                  FormulaTreeStructure fts = (FormulaTreeStructure) current.getChild(getRank() - 1);
51                  // ? cause des prioritÈs sur les opÈrateurs, on ajoute comme fils l'opÈrateur
52                  // dont le rang est juste infÈrieur ? notre "*"
53                  addChild(fts);
54                  
55                  // on enlËve l'opÈrateur qu'on a fait "descendre", de la liste
56                  // de son prÈcÈdent pËre (son grand pËre maintenant)
57                  current.removeChild(fts);
58              }
59              
60              // On ajoute un template ? la multiplication
61              template = new VariableOrNumber();
62              addChild(template);
63              
64              // On retourne la refÈrence de notre dernier point d'insertion.
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          // On commence par Èvaluer tous les fils de l'instance
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          // Maintenant, on parcourre toutes les Èvaluations calculÈes et on essaye de leur appliquer
100         // l'opÈrateur courant
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         // On a appliquÈ l'opÈrateur courant, on retourne maintenant le rÈsultat, en prenant soin
119         // d'intercaler des opÈrateur entre les Èvaluations si nÈcessaire
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