View Javadoc

1   package fr.ove.openmath.jome.formaters.om;
2   
3   import fr.ove.utils.*;
4   import fr.ove.openmath.jome.model.*;
5   import fr.ove.openmath.jome.formaters.om.*;
6   
7   /***
8   * Formats the formula tree structure as its corresponding OpenMath object.<BR>
9   * This is the formater of operators which have the sigma format :
10  *
11  * @author © 2000 DIRAT Laurent
12  * @version 1.0 21/02/2000
13  */
14  public class MapsToSigmaFormater implements Formater {
15      /***
16      * Returns the specified object formatted as a string.
17      * @param formatedObject the formatted object (for structured object, could represents the beginning).
18      * @param formaterRepository where the different other formaters are.
19      * @param obj the object to format.
20      */
21      public String format(String formatedObject, FormaterRepository formaterRepository, Object obj) {
22          OpenMathFormater omFormater = (OpenMathFormater) formaterRepository;
23          FormulaTreeStructure fts = (FormulaTreeStructure) obj;
24          
25          Formater formater;
26          
27          String objectId = fts.getResourceIdentifier();
28          
29          // Si le nbre de fils est 3, alors a priori, on a un sigma sÈmantiquement juste (vis ? vis OM au moins)
30          if (fts.getNbChildren() == 3) {
31              // Pour facilitÈ de manip, on rÈcupËre les fils des slots, i.e. les ÈlÈments du sigma
32              FormulaTreeStructure argument = (FormulaTreeStructure) fts.getChild(0).getChild(0);
33              FormulaTreeStructure lowerBound = (FormulaTreeStructure) fts.getChild(1).getChild(0);
34              FormulaTreeStructure upperBound = (FormulaTreeStructure) fts.getChild(2).getChild(0);
35              // Si lowerBound n'est pas de la forme var = integer, alors on n'a pas un sigma sÈmantiquement juste
36              // (vis ? vis OM au moins).
37              // Si c'est le cas, on rÈcupËre var, qui va Ítre la variable liÈe pour le bind de la fonction, et 
38              // integer, qui va Ítre la borne inf de l'intervalle pour la syntaxe OM
39              if ((lowerBound.isOperator() && ((Operator) lowerBound).getTheOperator().equals("="))) {
40                  if (lowerBound.getNbChildren() == 2) {
41                      FormulaTreeStructure child0 = (FormulaTreeStructure) lowerBound.getChild(0);
42                      FormulaTreeStructure child1 = (FormulaTreeStructure) lowerBound.getChild(1);
43                      if (child0.getResourceIdentifier().equals("VARIABLE")/* && child1.getResourceIdentifier().equals("INTEGER")*/) {
44                          //if (upperBound.getResourceIdentifier().equals("INTEGER")) {
45                              // A ce stade, tout colle pourqu'il y ait un objet OM 
46                              formatedObject += omFormater.writeStartApplication();
47                              // On rÈcupËre le nom du symbole
48                              formatedObject += omFormater.writeSymbol(objectId);
49                              // CrÈation de l'integer_interval nÈcessaire
50                              formatedObject += omFormater.writeStartApplication();
51                              formatedObject += omFormater.writeSymbol("interval", "integer_interval");
52                              
53                              objectId = child1.getResourceIdentifier();
54                              formater = formaterRepository.getFormater(objectId);
55                              formatedObject = formater.format(formatedObject, formaterRepository, child1);
56                              // On sait que child1 et upperBound sont tous les 2 des entiers et ont donc
57                              // le mÍme formatteur
58                              
59                              objectId = upperBound.getResourceIdentifier();
60                              formater = formaterRepository.getFormater(objectId);
61                              
62                              formatedObject = formater.format(formatedObject, formaterRepository, upperBound);
63                              
64                              formatedObject += omFormater.writeEndApplication();
65                              
66                              // CrÈation du bind
67                              formatedObject += omFormater.writeStartBinding();
68                              formatedObject += omFormater.writeSymbol("lambda");
69                              
70                              // On met la variable liÈe.
71                              formatedObject += omFormater.writeStartBoundVariables();
72                              
73                              objectId = child0.getResourceIdentifier();
74                              formater = formaterRepository.getFormater(objectId);
75                              formatedObject = formater.format(formatedObject, formaterRepository, child0);
76                              
77                              formatedObject += omFormater.writeEndBoundVariables();
78                              
79                              // On met maintenant l'objet du bind, i.e. l'argument.
80                              objectId = argument.getResourceIdentifier();
81                              formater = formaterRepository.getFormater(objectId);
82                              formatedObject = formater.format(formatedObject, formaterRepository, argument);
83                              
84                              formatedObject += omFormater.writeEndBinding();
85                              formatedObject += omFormater.writeEndApplication();
86                          //}
87                      }
88                  }
89              }
90          }
91          
92          return formatedObject;
93      }
94  }