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.evaluation.*;
6   
7   /***
8   * @author © 1999 DIRAT Laurent
9   * @version 2.0 25/06/1999
10  */
11  public class BinaryOperator extends Operator {
12      /***
13      * Inserts the operator instance in the formula tree, from the current insertion position.
14      * (checks the priorities and goes up in the tree if necessary).
15      *
16      * @param ope the current insertion position.
17      * @return the new insertion position.
18      */
19      public FormulaTreeStructure insert(FormulaTreeStructure current) {
20          VariableOrNumber template;
21          
22          // On cherche la position d'insertion de notre operateur
23          current = findLocation(current);
24          
25          // Pas de problËme de prioritÈ pour ce type d'opÈrateur.
26          // On doit juste savoir si current est une instance de Formula pour gÈrer le cas
27          // particulier o?, la formule vide, on veut saisir un tel opÈrateur et donc insertion
28          // des templates adequat ==> [?] ope [?].
29          
30          // On ajoute l'opÈrateur comme fils ? l'opÈrateur courant
31          current.addChild(this);
32  
33          if ((current.getFather() == null) && (current.getNbChildren() == 1)) {
34              template = new VariableOrNumber();
35              addChild(template); // On ajoute le premier template.
36          }
37          else {
38              FormulaTreeStructure fts = (FormulaTreeStructure) current.getChild(getRank() - 1);
39              // ? cause des prioritÈs sur les opÈrateurs, on ajoute comme fils l'opÈrateur
40              // dont le rang est juste infÈrieur ? notre instance
41              addChild(fts);
42              
43              // On doit faire attention ? quel type d'opÈrateur on a affaire.
44              // Si fts est les parenthËse, elles ne sont pas visibles si l'instance courante est une fraction.
45              // Dans les autres cas, elles le sont.
46              int prio = fts.getAsOperatorPriority();
47              if (prio ==  resourcesManager.getAsOperatorPriority("bracketPriorities")) {
48                  prio = getAsOperatorPriority();
49                  if (prio ==  resourcesManager.getAsOperatorPriority("dividePriorities"))
50                      ((Bracket) fts).setIsVisible(false);
51                  else
52                      ((Bracket) fts).setIsVisible(true);
53              }
54              
55              
56              // on enlËve l'opÈrateur qu'on a fait "descendre", de la liste
57              // de son prÈcÈdent pËre (son grand pËre maintenant)
58              current.removeChild(fts);
59          }
60  
61          // On ajoute un template ? la division
62          template = new VariableOrNumber();
63          addChild(template);
64  
65          // On retourne la refÈrence de notre dernier point d'insertion.
66          return template;
67      }
68      
69      /***
70      * The Creation of the corresponding linear expression of the formula.
71      */
72      public String createLinear(String linear) {
73          linear = ((FormulaTreeStructure) getChild(0)).createLinear(linear);
74          linear += getTheOperator();
75          linear = ((FormulaTreeStructure) getChild(1)).createLinear(linear);
76          return linear;
77      }
78  
79      /***
80      * Evaluates the instance.
81      */
82      public String evaluate() {
83          return Evaluator.evaluate(((FormulaTreeStructure) getChild(0)).evaluate(),
84                          ((FormulaTreeStructure) getChild(1)).evaluate(),
85                          getTheOperator());
86      }
87              
88  }