1 /**
2 * @file translateL3Math.cpp
3 * @brief Translates infix formulas into MathML and vice-versa, using the L3 parser instead of the old L1 parser.
4 * @author Lucian Smith
5 * @author Sarah Keating
6 * @author Ben Bornstein
7 *
8 * <!--------------------------------------------------------------------------
9 * This sample program is distributed under a different license than the rest
10 * of libSBML. This program uses the open-source MIT license, as follows:
11 *
12 * Copyright (c) 2013-2018 by the California Institute of Technology
13 * (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
14 * and the University of Heidelberg (Germany), with support from the National
15 * Institutes of Health (USA) under grant R01GM070923. All rights reserved.
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be included in
25 * all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 * DEALINGS IN THE SOFTWARE.
34 *
35 * Neither the name of the California Institute of Technology (Caltech), nor
36 * of the European Bioinformatics Institute (EMBL-EBI), nor of the University
37 * of Heidelberg, nor the names of any contributors, may be used to endorse
38 * or promote products derived from this software without specific prior
39 * written permission.
40 * ------------------------------------------------------------------------ -->
41 */
42
43
44 #include <iostream>
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <sbml/SBMLTypes.h>
51
52
53 #define BUFFER_SIZE 1024
54
55
56 using namespace std;
57 LIBSBML_CPP_NAMESPACE_USE
58
59 char *translateInfix (const char *formula, const L3ParserSettings& settings);
60 char *translateMathML (const char *xml);
61
62
63 int
main(int argc,char * argv[])64 main (int argc, char* argv[])
65 {
66 char line[BUFFER_SIZE];
67 char* trimmed;
68 char* result;
69 char* str;
70 size_t len;
71 SBMLDocument* doc = NULL;
72 StringBuffer_t* sb = StringBuffer_create(1024);
73
74
75 cout << endl
76 << "This program translates L3 infix formulas into MathML and" << endl
77 << "vice-versa. Enter or return on an empty line triggers" << endl
78 << "translation. Ctrl-C quits" << endl
79 << endl;
80
81 L3ParserSettings settings;
82 while (1)
83 {
84 cout << "Enter infix formula, MathML expression, or change parsing rules with the keywords:\nLOG_AS_LOG10, LOG_AS_LN, LOG_AS_ERROR, EXPAND_UMINUS, COLLAPSE_UMINUS, TARGETL2, TARGETL3, NO_UNITS, UNITS, or FILE:<filename>\n(Ctrl-C to quit):"
85 << endl << endl;
86 cout << "> " ;
87
88 cin.getline(line, BUFFER_SIZE, '\n');
89
90 while (line != 0)
91 {
92 trimmed = util_trim(line);
93 len = strlen(trimmed);
94
95 if (len > 0)
96 {
97 if (strcmp(line, "LOG_AS_LOG10")==0) {
98 settings.setParseLog(L3P_PARSE_LOG_AS_LOG10);
99 cout << "Now parsing 'log(x)' as 'log10(x)'" << endl << endl << "> ";
100 }
101 else if (strcmp(line, "LOG_AS_LN")==0) {
102 settings.setParseLog(L3P_PARSE_LOG_AS_LN);
103 cout << "Now parsing 'log(x)' as 'ln(x)'" << endl << endl << "> ";
104 }
105 else if (strcmp(line, "LOG_AS_ERROR")==0) {
106 settings.setParseLog(L3P_PARSE_LOG_AS_ERROR);
107 cout << "Now parsing 'log(x)' as an error" << endl << endl << "> ";
108 }
109 else if (strcmp(line, "EXPAND_UMINUS")==0) {
110 settings.setParseCollapseMinus(L3P_EXPAND_UNARY_MINUS);
111 cout << "Will now leave multiple unary minuses expanded, and all negative numbers will be translated using the <minus> construct." << endl << endl << "> ";
112 }
113 else if (strcmp(line, "COLLAPSE_UMINUS")==0) {
114 settings.setParseCollapseMinus(L3P_COLLAPSE_UNARY_MINUS);
115 cout << "Will now collapse multiple unary minuses, and incorporate a negative sign into digits." << endl << endl << "> ";
116 }
117 else if (strcmp(line, "TARGETL2")==0) {
118 settings.setParseUnits(false);
119 settings.setParseAvogadroCsymbol(false);
120 cout << "Will now target SBML Level 2 MathML, with no units on numbers, and no csymbol 'avogadro'." << endl << endl << "> ";
121 }
122 else if (strcmp(line, "NO_UNITS")==0) {
123 settings.setParseUnits(false);
124 cout << "Will now target MathML but with no units on numbers." << endl << endl << "> ";
125 }
126 else if (strcmp(line, "UNITS")==0) {
127 settings.setParseUnits(true);
128 cout << "Will now target MathML but with units on numbers." << endl << endl << "> ";
129 }
130 else if (strcmp(line, "TARGETL3")==0) {
131 settings.setParseUnits(true);
132 settings.setParseAvogadroCsymbol(true);
133 cout << "Will now target SBML Level 3 MathML, including having units on numbers, and the csymbol 'avogadro'." << endl << endl << "> ";
134 }
135 else if (line[0] == 'F' && line[1] == 'I' && line[2]=='L' && line[3]=='E' && line[4]==':') {
136 string filename(line);
137 filename = filename.substr(5, filename.size());
138 delete doc;
139 doc = readSBMLFromFile(filename.c_str());
140 if (doc->getModel()==NULL) {
141 cout << "File '" << filename << "' not found or no model present. Clearing the Model parsing object." << endl << endl << "> ";
142 }
143 else {
144 cout << "Using model from file " << filename << " to parse infix: all symbols present in that model will not be translated as native MathML or SBML-defined elements." << endl << endl << "> ";
145 }
146 settings.setModel(doc->getModel());
147 }
148 else {
149 StringBuffer_append (sb, trimmed);
150 StringBuffer_appendChar(sb, ' ');
151 }
152 }
153 else
154 {
155 str = StringBuffer_getBuffer(sb);
156 result = (str[0] == '<') ? translateMathML(str) : translateInfix(str, settings);
157
158 cout << "Result:" << endl << endl << result << endl << endl << endl;
159
160 StringBuffer_reset(sb);
161 break;
162 }
163
164 cin.getline(line, BUFFER_SIZE, '\n');
165 }
166 }
167
168 StringBuffer_free(sb);
169 return 0;
170 }
171
172
173 /**
174 * Translates the given infix formula into MathML.
175 *
176 * @return the MathML as a string. The caller owns the memory and is
177 * responsible for freeing it.
178 */
179 char *
translateInfix(const char * formula,const L3ParserSettings & settings)180 translateInfix (const char* formula, const L3ParserSettings& settings)
181 {
182 char* result;
183 ASTNode* math = SBML_parseL3FormulaWithSettings(formula, &settings);
184
185 if (math==NULL) {
186 result = SBML_getLastParseL3Error();
187 }
188 else {
189 result = writeMathMLToString(math);
190 ASTNode_free(math);
191 }
192 return result;
193 }
194
195
196 /**
197 * Translates the given MathML into an infix formula. The MathML must
198 * contain no leading whitespace, but an XML header is optional.
199 *
200 * @return the infix formula as a string. The caller owns the memory and
201 * is responsible for freeing it.
202 */
203 char *
translateMathML(const char * xml)204 translateMathML (const char* xml)
205 {
206 char* result;
207 ASTNode_t* math;
208
209 math = readMathMLFromString(xml);
210 result = SBML_formulaToString(math);
211
212 ASTNode_free(math);
213 return result;
214 }
215