View Javadoc

1   package fr.ove.openmath.jome.ctrl.amto;
2   
3   import java.io.Serializable;
4   import java.util.*;
5   import fr.ove.utils.Factory;
6   import fr.ove.openmath.jome.ctrl.linear.events.*;
7   import fr.ove.openmath.jome.ctrl.amto.*;
8   
9   /***
10  * An abstract tree structure of a mathematical object.
11  *
12  * @author © 1999 DIRAT Laurent
13  * @version 1.0  29/10/1999
14  */
15  public abstract class AbstractMathTreeObject implements Serializable, Parametrable {
16      /***
17      * The identifier of the mathematical notion represented by the instance.
18      */
19      protected String identifier; // l'identificateur de l'objet
20  
21      /***
22      * The parameter
23      */
24      protected String param;
25  
26      /***
27      * The list of operands of the instance.
28      */
29      private Vector children; // les fils de l'instance (en qque sorte,les opÈrandes de l'objet en question)
30      
31      /***
32      * The father of the instance in the tree structure
33      */
34      private AbstractMathTreeObject father = null;
35  
36      /***
37      * The resources manager.<BR>
38      * It is in charge of getting information according to the identifier.
39      */
40      private static AMTO_ResourcesManager resourcesManager = new AMTO_ResourcesManager("fr.ove.openmath.jome.ctrl.amto.AMTOResources");
41      
42      /***
43      * The event source during the "flush" of the instance.<BR>
44      */
45      private /*static*/ LinearParserEventSource eventSource;
46      
47      // * This source is shared by all the instance
48      // En fait il faudrait que ce soient toutes les instances filles qui partagent.
49      // A voir plus tard une espËce de RootAbstractMathTreeObject, qui aurait une rÈfÈrence
50      // sur la source et les filles de RootAbstractMathTreeObject partageraient cette rÈfÈrence.
51      
52      /***
53      * The token which will identify the instance by the mean of the event sent.
54      */
55      protected int token;
56  
57      /***
58      * The constructor.
59      * @param identifier the identifier of the instance.
60      * @param token the identifier of the instance by the mean of the event sent.
61      */
62      public AbstractMathTreeObject(String identifier, int token) {
63          this.identifier = identifier;
64          this.token = token;
65          children = new Vector();
66      }
67      
68      /***
69      * Sets the event source.<BR>
70      * The event source is shared by all the instances of AbstractMathTreeObject. So, the event source
71      * must be set by the top most AbstractMathTreeObject (in the tree, so lets say the root of the formula)
72      * object, and should only be done by this one.
73      * @param eventSource the event source.
74      */
75      public void setEventSource(LinearParserEventSource eventSource) {
76          this.eventSource = eventSource;
77      }
78      
79      /***
80      * Returns the event source.<BR>
81      */
82      public LinearParserEventSource getEventSource() {
83          return eventSource;
84      }
85      
86      /***
87      * Fires the specified event.
88      * @param linearParserEvent the event to fire.
89      */
90      public void fireLinearParserEvent(LinearParserEvent linearParserEvent) {
91          eventSource.fireLinearParserEvent(linearParserEvent);
92      }
93  
94      /***
95      * Adds the object characterised by the specified identifier and parameter.
96      * @param identifier the identifier of the object to add.
97      * @param parameter the possible parameter of the object to add.
98      * @returns the object added or <CODE>null</CODE>
99      */
100     public AbstractMathTreeObject add(String identifier, Object parameter) {
101         String className = resourcesManager.getClassName(identifier);
102         AbstractMathTreeObject newChild = (AbstractMathTreeObject) Factory.getClassInstance(className);
103         newChild.eventSource = eventSource;  // Rajout parce que eventSource plus static
104         if (newChild != null) {
105             newChild.setParameter(parameter);
106             children.addElement(newChild);
107             newChild.setFather(this);
108         }
109         
110         return newChild;
111     }
112     
113     /***
114     * Returns the child <CODE>AbstractMathTreeObject</CODE> at the specified index.
115     * @param index the specified index.
116     */
117     public AbstractMathTreeObject getChild(int index) {
118         return (AbstractMathTreeObject) children.elementAt(index);
119     }
120 
121     /***
122     * Returns the <CODE>AbstractMathTreeObject</CODE> children of the instance
123     */
124     public Vector getChildren() {
125         return children;
126     }
127 
128     /***
129     * Returns the number of children of the instance
130     */
131     public int getNbChildren() {
132         return children.size();
133     }
134     
135     /***
136     * Returns the father of the instance in the tree structure
137     */
138     public AbstractMathTreeObject getFather() {
139         return father;
140     }
141     
142     /***
143     * Sets the father of the instance.
144     * @param father the father to set to the instance
145     */
146     protected void setFather(AbstractMathTreeObject father) {
147         this.father = father;
148     }
149     
150     /***
151     * Removes the specified child from the instance.
152     * @param child the specified child to remove.
153     */
154     public void remove(AbstractMathTreeObject child) {
155         child.setFather(null);
156         children.removeElement(child);
157     }
158     
159     /***
160     * Removes the child at the specified index from the instance.
161     * @param index the specified index of the child to remove.
162     */
163     public void remove(int index) {
164         remove((AbstractMathTreeObject) children.elementAt(index));
165     }
166 
167     /***
168     * Removes all the children of the instance.
169     */
170     public void removeAll() {
171         for (int i = children.size() - 1; i >= 0; i--)
172             remove(0);
173             
174         children.trimToSize();
175     }
176     
177 
178     // En fait la plupart des objets ne sont pas paramÈtrales.
179     // Seuls des objets correspondants aux variables, aux chiffres, .... le sont.
180     
181     /***
182     * Sets the specified parameter.
183     * @param param the parameter to set.
184     */
185     public void setParameter(Object param) {
186         this.param = (String) param;
187     }
188     
189     /***
190     * Returns the parameter set before.
191     */
192     public Object getParameter() {
193         return param;
194     }
195     
196     // Fin de l'interface Parametrable
197     
198     
199     /***
200     * "Flushes" the object as a sequence of events for the building of the model.
201     */
202     public abstract void flush();    
203 
204     /***
205     * "Flushes" the specified child.<BR>
206     * According to their father, some children need to be between parenthesis to respect correctness of the
207     * expression. So, when needed, the specified child is "flushed" as if it was between parenthesis.<BR>
208     * @param child the child to "flush"
209     * @param index the child index in the list of children of the instance.
210     */
211     protected void flushChild(AbstractMathTreeObject child, int index) {
212         // The default is symply "flushing" the child.
213         child.flush();
214     }
215 }
216