COPASI API  4.16.103
Expression2PresentationMMLUnits.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2008 - 2009 by Sven Sahle and University of Heidelberg
12 // All rights reserved.
13 
15 #include <sbml/Model.h>
16 #include "CSBMLunitInterface.h"
17 
19  : Expression2PresentationMML(rootnode),
20  mpUnitInterface(NULL)
21 {
22 }
23 
25 {
26  mpUnitInterface = unitInterface;
27  mpUnitInterface2 = unitInterface2;
29 }
30 
31 #define SPC(level) std::string(level, ' ')
32 
33 void Expression2PresentationMMLUnits::writeMathMLName(std::ostream & out, const ASTNode* node, size_t l) const
34 {
35  std::string reactionID = mpReaction ? mpReaction->getId() : "";
36  CSBMLunitInformation* unitInformation = mpUnitInterface->getMappedUnitFromIdentifier(node->getName(),
38 
39  if (!unitInformation)
40  {
41  //use the base class implementation
43  return;
44  }
45 
46  std::string color;
47 
50  color = "#f0b0b0";
51  else
52  color = "#d0d0e0";
53 
54  CSBMLunitInformation * unitInformation2 = NULL;
55 
56  if (mpUnitInterface2)
57  unitInformation2 = mpUnitInterface2->getMappedUnitFromIdentifier(node->getName(),
59 
60  std::ostringstream oss;
62 
63  if (unitInformation2 != NULL)
64  {
65  if (CSBMLunitInformation::isEqual(*unitInformation, *unitInformation2))
66  writeMathMLBox(out, oss.str(), getMathML(*unitInformation), "", color, l);
67  else
68  writeMathMLBox(out, oss.str(), getMathML(*unitInformation), getMathML(*unitInformation2), color, l);
69  }
70  else
71  writeMathMLBox(out, oss.str(), getMathML(*unitInformation), color, l);
72 }
73 
74 void Expression2PresentationMMLUnits::writeMathMLNumber(std::ostream & out, const ASTNode* node, size_t l) const
75 {
77  CSBMLunitInformation* unitInformation2 = NULL;
78 
79  if (mpUnitInterface2)
80  unitInformation2 = mpUnitInterface2->getMappedUnitFromNumberNode(node);
81 
82  if (!unitInformation && !unitInformation2)
83  {
85  return;
86  }
87 
88  std::string color;
89 
92  color = "#f0b0b0";
93  else
94  color = "#d0e0d0";
95 
96  std::ostringstream oss;
98 
99  if (!unitInformation && unitInformation2)
100  {
101  writeMathMLBox(out, oss.str(), "<mi>unknown</mi>", getMathML(*unitInformation2), color, l);
102  }
103 
104  if (unitInformation && !unitInformation2)
105  {
106  writeMathMLBox(out, oss.str(), getMathML(*unitInformation), color, l);
107  }
108 
109  if (unitInformation && unitInformation2)
110  {
111  writeMathMLBox(out, oss.str(), getMathML(*unitInformation), getMathML(*unitInformation2), color, l);
112  }
113 }
114 
116 {
117  std::string tmp;
118  tmp += "<mrow>";
119 
120  if (ui.isConflict())
121  tmp += "<mi mathcolor = \"red\">conflict!</mi>";
122 
123  if (ui.getSymbolicExpExp() > 0)
124  tmp += "<mfenced>";
125 
126  switch (ui.getInfo())
127  {
129  tmp += "<mi mathcolor = \"orange\">unknown</mi>";
130  break;
131 
133  tmp += "<mstyle mathcolor=\"#000060\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
134  break;
135 
137  tmp += "<mstyle mathcolor=\"#0000a0\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
138  break;
139 
141  tmp += "<mstyle mathcolor=\"#2020ff\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
142  break;
143 
145  tmp += "<mstyle mathcolor=\"green\">" + getMathML(ui.getSBMLUnitDefinition()) + "</mstyle>";
146  break;
147  }
148 
149  if (ui.getSymbolicExpExp() > 0)
150  {
151  std::ostringstream ttt; ttt << ui.getSymbolicExpExp();
152  tmp += "</mfenced><mo>^</mo><mi mathcolor = \"#c06000\">" + ui.getSymbolicExponent() + "</mi><mo>^</mo><mn>" + ttt.str() + "</mn>";
153  }
154 
155  tmp += "</mrow>";
156  return tmp;
157 }
158 
159 void Expression2PresentationMMLUnits::writeMathMLBox(std::ostream & out, const std::string & a, const std::string & b,
160  const std::string & color, size_t l) const
161 {
162  out << SPC(l) << "<mstyle background=\"" << color << "\"><mfrac linethickness=\"0\">" << std::endl;
163  out << a;
164  out << SPC(l + 1) << "<mstyle scriptlevel=\"+1\">" << std::endl;
165  out << SPC(l + 2) << b << std::endl;
166  out << SPC(l + 1) << "</mstyle>" << std::endl;
167  out << SPC(l) << "</mfrac></mstyle>" << std::endl;
168 
169  //formating is done so that it looks best when a is already indented nicely while b
170  //is just a one line string
171 }
172 
173 void Expression2PresentationMMLUnits::writeMathMLBox(std::ostream & out, const std::string & a, const std::string & b, const std::string & c,
174  const std::string & color, size_t l) const
175 {
176  out << SPC(l) << "<mstyle background=\"" << color << "\"><mtable rowspacing=\"0.1 ex\">" << std::endl;
177  out << SPC(l + 1) << "<mtr><mtd>" << std::endl;
178  out << a;
179  out << SPC(l + 1) << "</mtd></mtr>" << std::endl;
180  out << SPC(l + 1) << "<mtr><mtd><mstyle scriptlevel=\"+1\">" << std::endl;
181  out << SPC(l + 2) << b << std::endl;
182  out << SPC(l + 1) << "</mstyle></mtd></mtr>" << std::endl;
183  out << SPC(l + 1) << "<mtr><mtd><mstyle scriptlevel=\"+1\">" << std::endl;
184  out << SPC(l + 2) << c << std::endl;
185  out << SPC(l + 1) << "</mstyle></mtd></mtr>" << std::endl;
186  out << SPC(l) << "</mtable></mstyle>" << std::endl;
187 
188  //formating is done so that it looks best when a is already indented nicely while b
189  //is just a one line string
190 }
191 
192 std::string Expression2PresentationMMLUnits::getMathML(const UnitDefinition & ud) const
193 {
194  std::string tmp;
195 
196  /*tmp += "<mrow>";
197  size_t i, imax = ud.getNumUnits();
198  for (i=0; i<imax; ++i)
199  {
200  if (i) tmp += "<mo>&CenterDot;</mo>";
201  tmp += getMathML(ud.getUnit(i), false);
202  }
203  tmp += "</mrow>";*/
204 
205  tmp += "<mrow>";
206  bool isFirst = true;
207  size_t i, imax = ud.getNumUnits();
208 
209  for (i = 0; i < imax; ++i)
210  {
211  if (ud.getUnit((unsigned int) i)->getExponent() >= 0)
212  {
213  if (!isFirst) tmp += "<mo>&CenterDot;</mo>";
214 
215  tmp += getMathML(ud.getUnit((unsigned int) i), true);
216  isFirst = false;
217  }
218  }
219 
220  if (isFirst) tmp += "<mn>1</mn>"; //there are no units with pos. exponent
221 
222  std::string tmp2;
223  isFirst = true;
224 
225  for (i = 0; i < imax; ++i)
226  {
227  if (ud.getUnit((unsigned int) i)->getExponent() < 0)
228  {
229  if (!isFirst) tmp2 += "<mo>&CenterDot;</mo>";
230 
231  tmp2 += getMathML(ud.getUnit((unsigned int) i), true);
232  isFirst = false;
233  }
234  }
235 
236  if (!isFirst) tmp += "<mo>/</mo>" + tmp2; //only create fraction when there is a denominator
237 
238  tmp += "</mrow>";
239 
240  return tmp;
241 }
242 
243 std::string Expression2PresentationMMLUnits::getMathML(const Unit * u, bool absExp) const
244 {
245  if (!u)
246  return "<mi>EEE</mi>";
247 
248  int exponent = absExp ? abs(u->getExponent()) : u->getExponent();
249  bool flagExp = (exponent != 1);
250  bool flag = (u->getMultiplier() != 1.0) || (u->getScale() != 0); //are brackets needed?
251 
252  std::ostringstream tmp;
253 
254  if (flagExp) tmp << "<msup>";
255 
256  //if (flag) tmp << "<mfenced>";
257  tmp << "<mrow>";
258 
259  if (flag) tmp << "<mo>(</mo>";
260 
261  if (u->getMultiplier() != 1.0)
262  tmp << "<mn>" << u->getMultiplier() << "</mn><mo>&CenterDot;</mo>";
263 
264  if (u->getScale() != 0)
265  tmp << "<msup><mn>10</mn><mn>" << u->getScale() << "</mn></msup><mo>&CenterDot;</mo>";
266 
267  tmp << "<mi>";
268  tmp << UnitKind_toString(u->getKind());
269  tmp << "</mi>";
270 
271  if (flag) tmp << "<mo>)</mo>";
272 
273  tmp << "</mrow>";
274  //if (flag) tmp << "</mfenced>";
275 
276  if (flagExp) tmp << "<mn>" << exponent << "</mn>";
277 
278  if (flagExp) tmp << "</msup>";
279 
280  //tmp << "</mrow>";
281 
282  return tmp.str();
283 }
the units is determined from the sbml defaults
Definition: CSBMLunit.h:108
virtual void writeMathMLNumber(std::ostream &out, const ASTNode *node, size_t l) const
the units is unknown
Definition: CSBMLunit.h:106
void setUnitInterface(CSBMLunitInterface *unitInterface, CSBMLunitInterface *unitInterface2)
Expression2PresentationMMLUnits(const ASTNode *rootnode)
CSBMLunitInformation * getMappedUnitFromIdentifier(const std::string &node, const CEnvironmentInformation &ei)
the units is determined from the model-wide definitions
Definition: CSBMLunit.h:110
virtual void writeMathMLNumber(std::ostream &out, const ASTNode *node, size_t l) const
INFO getInfo() const
get the status information
Definition: CSBMLunit.h:130
void writeMathMLBox(std::ostream &out, const std::string &a, const std::string &b, const std::string &color, size_t l) const
int getSymbolicExpExp() const
Definition: CSBMLunit.h:72
the units if provided for a specific object explicitly
Definition: CSBMLunit.h:112
CSBMLunitInformation * getMappedUnitFromNumberNode(const ASTNode *node)
long int flag
Definition: f2c.h:52
std::string getMathML(const CSBMLunitInformation &ui) const
UnitDefinition & getSBMLUnitDefinition()
Definition: CSBMLunit.h:69
static bool isEqual(const CSBMLunitInformation &unit1, const CSBMLunitInformation &unit2)
Definition: CSBMLunit.cpp:240
#define abs(x)
Definition: f2c.h:173
the units was determined by reasoning
Definition: CSBMLunit.h:114
#define SPC(level)
virtual void writeMathMLName(std::ostream &out, const ASTNode *node, size_t l) const
const std::string & getSymbolicExponent() const
Definition: CSBMLunit.h:73
virtual void writeMathMLName(std::ostream &out, const ASTNode *node, size_t l) const
bool isConflict() const
retrieve the conflict flag
Definition: CSBMLunit.h:136
const std::set< const ASTNode * > & getListOfConflictingNodes() const