1 package fr.ove.openmath.jome.formaters.om;
2
3 import java.util.*;
4 import fr.ove.utils.*;
5 import fr.ove.openmath.jome.model.*;
6 import fr.ove.openmath.jome.formaters.om.*;
7
8 /***
9 * Formats the formula tree structure as its corresponding OpenMath object.<BR>
10 * This is the formater for integrals.
11 *
12 * @author © 2000 DIRAT Laurent
13 * @version 1.0 21/02/2000
14 */
15 public class DifferentiationFormater implements Formater {
16 /***
17 * Returns the specified object formatted as a string.
18 * @param formatedObject the formatted object (for structured object, could represents the beginning).
19 * @param formaterRepository where the different other formaters are.
20 * @param obj the object to format.
21 */
22 public String format(String formatedObject, FormaterRepository formaterRepository, Object obj) {
23 OpenMathFormater omFormater = (OpenMathFormater) formaterRepository;
24 Differentiation fts = (Differentiation) obj;
25
26 Formater formater;
27 FormulaTreeStructure child;
28
29 String objectId = fts.getResourceIdentifier();
30
31 String cdName = omFormater.getCdName(objectId);
32
33 if (fts.isPartial()) {
34 formatedObject += omFormater.writeStartApplication();
35 formatedObject += omFormater.writeSymbol(cdName, "partialdiff");
36
37
38 Vector variableList = new Vector();
39 Vector variableNameList = new Vector();
40 FormulaTreeStructure derivated = (FormulaTreeStructure) fts.getChild(1);
41 createVariableList(derivated, variableList, variableNameList);
42
43 formatedObject += omFormater.writeStartApplication();
44 formatedObject += omFormater.writeSymbol("list1", "list");
45
46 int count = fts.getNbChildren();
47 int index;
48 for (int i = 2; i < count; i++) {
49 index = findIndex(((Variable)((FormulaTreeStructure) fts.getChild(i)).getChild(0)).getValue(), variableNameList);
50 if (index != -1)
51 formatedObject += omFormater.writeInteger(""+index);
52 }
53 formatedObject += omFormater.writeEndApplication();
54
55
56 formatedObject += omFormater.writeStartBinding();
57 formatedObject += omFormater.writeSymbol("lambda");
58
59
60 formatedObject += omFormater.writeStartBoundVariables();
61 child = (FormulaTreeStructure) variableList.elementAt(0);
62 objectId = child.getResourceIdentifier();
63 formater = formaterRepository.getFormater(objectId);
64 formatedObject = formater.format(formatedObject, formaterRepository, child);
65 count = variableList.size();
66 for (int i = 1; i < count; i++) {
67 child = (FormulaTreeStructure) variableList.elementAt(i);
68 formatedObject = formater.format(formatedObject, formaterRepository, child);
69 }
70 formatedObject += omFormater.writeEndBoundVariables();
71
72
73 child = (FormulaTreeStructure) fts.getChild(1);
74 objectId = child.getResourceIdentifier();
75 formater = formaterRepository.getFormater(objectId);
76 formatedObject = formater.format(formatedObject, formaterRepository, child);
77
78 formatedObject += omFormater.writeEndBinding();
79 formatedObject += omFormater.writeEndApplication();
80 }
81 else {
82 int order = fts.getOrder();
83 for (int i = 0; i < order; i++) {
84 formatedObject += omFormater.writeStartApplication();
85
86 formatedObject += omFormater.writeSymbol(cdName, "diff");
87
88
89 formatedObject += omFormater.writeStartBinding();
90 formatedObject += omFormater.writeSymbol("fns1", "lambda");
91
92
93 formatedObject += omFormater.writeStartBoundVariables();
94 child = (FormulaTreeStructure) fts.getChild(2);
95
96
97 if (order > 1)
98 child = (FormulaTreeStructure) ((FormulaTreeStructure) child.getChild(0)).getChild(0);
99
100 objectId = child.getResourceIdentifier();
101 formater = formaterRepository.getFormater(objectId);
102 formatedObject = formater.format(formatedObject, formaterRepository, child);
103 formatedObject += omFormater.writeEndBoundVariables();
104 }
105
106
107 child = (FormulaTreeStructure) fts.getChild(1);
108 objectId = child.getResourceIdentifier();
109 formater = formaterRepository.getFormater(objectId);
110 formatedObject = formater.format(formatedObject, formaterRepository, child);
111
112 for (int i = 0; i < order; i++) {
113 formatedObject += omFormater.writeEndBinding();
114 formatedObject += omFormater.writeEndApplication();
115 }
116 }
117
118 return formatedObject;
119 }
120
121 private void createVariableList(FormulaTreeStructure fts, Vector variableList, Vector variableNameList) {
122 if (fts.getResourceIdentifier().equals("VARIABLE")) {
123 if (!fts.isTemplate()) {
124 String value = ((Variable) fts).getValue();
125 boolean addValue = true;
126 String stored;
127
128 for (Enumeration e = variableNameList.elements(); e.hasMoreElements(); ) {
129 stored = (String) e.nextElement();
130 if (value.equals(stored)) {
131 addValue = false;
132 break;
133 }
134 }
135
136 if (addValue) {
137 variableNameList.addElement(value);
138 variableList.addElement(fts);
139 }
140 }
141 }
142 else {
143 int count = fts.getNbChildren();
144 for (int i = 0; i < count; i++)
145 createVariableList((FormulaTreeStructure) fts.getChild(i), variableList, variableNameList);
146 }
147 }
148
149 private int findIndex(String varName, Vector variableNameList) {
150 int index = -1;
151 String stored;
152
153 int count = variableNameList.size();
154 for (int i = 0; i < count; i++ ) {
155 stored = (String) variableNameList.elementAt(i);
156 if (varName.equals(stored)) {
157 index = i + 1;
158 break;
159 }
160 }
161
162 return index;
163 }
164 }