1 /*
2 $Id: NaryOperator2.java 710 2005-03-21 16:34:22Z 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 java.util.*;
32 import fr.ove.openmath.jome.model.*;
33 import fr.ove.openmath.jome.model.events.ModelEvent;
34
35 /***
36 *
37 * @author © 2000 DIRAT Laurent
38 * @version 2.1 10/01/2000
39 */
40 public class NaryOperator2 extends Operator {
41 /***
42 * The ending of the linear syntax of the operator represented by the instance.
43 * The default is the closing bracket.
44 */
45 private String ending = ")";
46
47 /***
48 * Returns the ending of the operator.
49 */
50 public String getEnding() {
51 return ending;
52 }
53
54 /***
55 * Sets the ending of the operator.
56 * @param the ending of the operator.
57 */
58 public void setEnding(String ending) {
59 this.ending = ending;
60 }
61
62 /***
63 * Inserts the instance in the formula tree structure.<BR>
64 * @param current the position in the formula tree where the operator is to be insert.
65 * @return the new current position int hte formula tree.
66 */
67 public FormulaTreeStructure insert(FormulaTreeStructure current) {
68 ModelEvent modelEvent;
69
70 if ((current.getFather() == null) && (current.getNbChildren() == 0)) {
71 // On est dans ce cas l?, seulement au tout dÈbut de la saisie de la
72 // formule. On est obligÈ de faire ce test ? cause de la multiplication
73 // implicite avec les parenthËses (Ex: (a+b)c ==> (a+b)*c, dans ce cas l?,
74 // current.getNbChildren() est != de 0)
75 //On insËre directement ? la position courante
76 current.addChild(this);
77 }
78 else if ((current.getAsOperatorPriority() == resourcesManager.getAsOperatorPriority("constantPriorities")) &&
79 current.isTemplate()) {
80 // Le cas classique (on va mÍme dire normal !!!)
81 // La position d'insertion est un template.
82 // On doit remplacer le template par l'instance courante
83 int rank = current.getRank();
84 FormulaTreeStructure father = (FormulaTreeStructure) current.getFather();
85 father.addChild(this, rank);
86 father.removeChild(current);
87 }
88 else { // On est dans aucun des cas prÈcÈdents, on crÈÈ donc une
89 // multiplication implicite.
90 //if( "OPEN_CURLY".equals(getResourceIdentifier()) )
91 current = (new HorizentalGroup()).insert(current);
92 //else
93 // current = (new Multiplication()).insert(current);
94
95 current = insert(current);
96 return current;
97 }
98
99 // Maintenant, on va rajouter un slot comme ÈlÈment de l'opÈrateur
100 Slot aSlot = new Slot();
101 addChild(aSlot);
102 aSlot.setNextSlot(this);
103
104 // ...puis un template comme ÈlÈment de ce slot
105 VariableOrNumber template = new VariableOrNumber();
106 aSlot.addChild(template);
107
108 return template;
109 }
110
111 /***
112 * Adds a new element (template) to the end of the list.
113 * Returns the new element.
114 */
115 public FormulaTreeStructure addElement() {
116 Slot aSlot = new Slot();
117 addChild(aSlot);
118
119 // ...puis un template comme ÈlÈment de ce slot
120 VariableOrNumber template = new VariableOrNumber();
121 aSlot.addChild(template);
122
123 aSlot.setNextSlot(this);
124 ((Slot) getChild(getNbChildren()-2)).setNextSlot(template);
125
126 return template;
127 }
128
129 /***
130 * The Creation of the corresponding linear expression of the formula.
131 */
132 public String createLinear(String linear) {
133 FormulaTreeStructure child;
134
135 // L? encore, avec ce genre d'opÈrateur, la saisie se fait globalement comme la saisie
136 // d'une fonction. Ex : sum(i=0,n,i^n).
137 // Ou alors c'est qque chose qui est encadrÈ par des symboles par exemple une liste {1, 2, 3}.
138 // Donc, l'opÈrateur est ce qui "ouvre" la syntaxe et l'ending, ce qui "ferme" la syntaxe.
139 linear += getTheOperator();
140 for (int i = 0; i < getNbChildren(); i++) {
141 child = (FormulaTreeStructure) getChild(i);
142 if (i == 0)
143 linear += child.createLinear(linear);
144 else {
145 linear += "," + child.createLinear(linear);
146 }
147 }
148 return linear + ending;
149 }
150
151 /***
152 * Evaluates the instance.
153 */
154 public String evaluate() {
155 return "";
156 }
157 }