View Javadoc

1   /*
2   $Id: VariableOrNumber.java 726 2005-04-05 12:09:09Z guest $
3   */
4   
5   
6   /*
7   Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  For further information on the GNU Lesser General Public License,
24  see: http://www.gnu.org/copyleft/lesser.html
25  For further information on this library, contact: mainline@essi.fr
26  */
27  
28  
29  package fr.ove.openmath.jome.model;
30  
31  import fr.ove.openmath.jome.model.*;
32  import fr.ove.openmath.jome.model.events.*;
33  import fr.ove.openmath.jome.model.evaluation.*;
34  
35  /***
36  * Elements in the formula such as variables (letters) and numbers.<BR>
37  * <CODE>VariableOrNumber</CODE> represents a node in the formula tree.
38  * Terminal node, so leaves.
39  *
40  * @author © 1999 DIRAT Laurent
41  * @version 2.0 24/06/1999
42  */
43  public class VariableOrNumber extends Constant {
44      /***
45      * To check if the Constant is a number or not.
46      */
47      private boolean isNumber; 
48      
49      /***
50      * To check if the number represented by the Constant is an integer or a float.
51      * So if the value is <CODE>true</CODE>, it's an integer; Otherwise, it's a float.
52      */
53      private boolean isInteger;
54      
55      /***
56      * To indicate that we have a constant with a specific signification.<BR>
57      * In this particular case, it's a request variable (from the mfd2 CD). <BR>
58      * By default, it is not a request variable.
59      */
60      private boolean isRequestVariable = false;  // ? voir plus tard mais probablement ? virer.
61      
62  
63      /***
64      * The default constructor.<BR>
65      * Constructs a template.
66      */
67      public VariableOrNumber() {
68          this("VARIABLE", "[?]", false, false);
69          setIsTemplate(true);
70      }
71      
72      /***
73      * The Constructor.
74      *
75      * @param value the value of the constant.
76      * @param isNumber to set if the constant is a number or not.
77      * @param isInteger to set, if the constant is a numbern if it is an integer or a float.
78      */
79      public VariableOrNumber(String resourceIdentifier, String value, boolean isNumber, boolean isInteger) {
80          super();
81          setResourceIdentifier(resourceIdentifier);
82          setValue(value);
83          this.isNumber = isNumber;
84          this.isInteger = isInteger;
85      }
86      
87      /***
88      * Sets the constant as a number.
89      * @param isNumber <CODE>true</CODE> if it is a number.<CODE>false</CODE>
90      * otherwise.
91      */
92      public void setIsNumber(boolean isNumber) {
93          this.isNumber = isNumber;
94      }
95      
96      /***
97      * Returns <CODE>true</CODE> if it is a number.<BR>
98      * <CODE>false</CODE> otherwise.
99      */
100     public boolean isNumber() {
101         return isNumber;
102     }
103     
104     /***
105     * Sets the constant, which is a number, as an integer or as a float.
106     * @param isInteger <CODE>true</CODE> if it is an integer.<CODE>false</CODE>
107     * if it is a float.
108     */
109     public void setIsInteger(boolean isInteger) {
110         this.isInteger = isInteger;
111     }
112     
113     /***
114     * Returns <CODE>true</CODE> if it is an integer.<CODE>false</CODE>
115     * if it is a float.
116     */
117     public boolean isInteger() {
118         return isInteger;
119     }
120 
121     /***
122     * Sets the constant as a request variable.
123     */
124     public void setIsRequestVariable(boolean isRequestVariable) {
125         this.isRequestVariable = isRequestVariable;
126     }
127     
128     /***
129     * Returns <CODE>true</CODE> if it is a request variable.<BR>
130     * <CODE>false</CODE> otherwise.
131     */
132     public boolean isRequestVariable() {
133         return isRequestVariable;
134     }
135 
136     /***
137     * Inserts the operator instance in the formula tree, from the current insertion position.
138     * (checks the priorities and goes up in the tree if necessary).
139     *
140     * @param ope the current insertion position.
141     * @return the new insertion position.
142     */
143     public FormulaTreeStructure insert(FormulaTreeStructure current) {
144         if (isTemplate()) {
145             if (isRequestVariable)
146                 return super.insert(current);
147                 
148             // Si l'instance que l'on doit insÈrer est un template, alors on l'insËre
149             // directement ? la position courante (heuu... on sait ce qu'on fait ;o) )
150             current.addChild(this);
151             return this;
152         }
153         else {  // L'instance n'est pas un template.
154             if (current.getAsOperatorPriority() == resourcesManager.getAsOperatorPriority("constantPriorities")) {
155                 // A ce stade 2 solutions. Soit :
156                 //  * current est un template, alors encore 2 possibilitÈs. Soit :
157                 //      - current est une request variable, alors si l'instance est une variable, c'est qu'on
158                 //        est en train de saisir sa valeur. Sinon, c'est une erreur syntaxique, on remplace
159                 //        la request variable, par l'instance.
160                 //      - current n'est pas une request variable, on remplace current par l'instance
161                 //  * current n'est pas un template, il faut donc crÈer une multiplication implicite.
162                 if (current.isTemplate()) {
163                     if (((VariableOrNumber) current).isRequestVariable && !isNumber) {
164                         current.setIsTemplate(false);
165                         ((VariableOrNumber) current).setValue(getValue());
166                         return current;
167                     }
168                     else {
169                         FormulaTreeStructure father = (FormulaTreeStructure) current.getFather();
170                         father.addChild(this, current.getRank());
171                         father.removeChild(current);
172                         
173                       /*  if(father instanceof Function2) {
174 							VariableOrNumber template = new VariableOrNumber();
175 							father.addChild(template);
176 							return template;
177                         }*/
178                         
179                         return this;
180                     }                    
181                 }
182                 else {// on crÈÈ donc une multiplication implicite.
183                     //current = (new Multiplication()).insert(current);
184                     /*FormulaTreeStructure father = (FormulaTreeStructure)current.getFather();
185                     if(father instanceof Slot){
186 						current =((Function2)father.getFather()).addElement();
187 						current = insert(current);
188 						return current;
189                     }else*/{
190 						//current = (new Multiplication()).insert(current);
191 						current = (new HorizentalGroup()).insert(current);						
192         	            current = insert(current);
193             	        return current;
194                     }
195                 }
196             }
197             else { // On est dans le cas de l'insertion courante d'une constante dans la FTS
198                 if ((current.getFather() == null) && (current.getNbChildren() == 0)) {
199                     // On est dans ce cas l?, seulement au tout dÈbut de la saisie de la 
200                     // formule. On est obligÈ de faire ce test ? cause de la multiplication
201                     // implicite avec les parenthËses (Ex: (a+b)c ==> (a+b)*c, dans ce cas l?,
202                     // current.getNbChildren() est != de 0)
203                     //On insËre directement ? la position courante
204                     current.addChild(this);
205                     
206                     return this;
207                 }
208                 else { // On est dans aucun des cas prÈcÈdents, on crÈÈ donc une
209                     // multiplication implicite.
210                     ///current = (new Multiplication()).insert(current);
211 					current = (new HorizentalGroup()).insert(current);
212                     current = insert(current);
213                     return current;
214                 }
215             }
216         }
217     }
218     
219     /***
220     * The Creation of the corresponding linear expression of the formula.
221     */
222     public String createLinear(String linear) {
223         if (isRequestVariable)
224             linear += "?";
225             
226         if (!isTemplate())
227             linear += getValue();
228             
229         return linear;
230     }
231 }