COPASI API  4.16.103
CAnnotation.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 /*
7  * CAnnotation.cpp
8  *
9  * Created on: Aug 12, 2010
10  * Author: shoops
11  */
12 
13 #include <sstream>
14 
15 #include "copasi.h"
16 
17 #include "CAnnotation.h"
18 
19 #include "MIRIAM/CRDFUtilities.h"
21 #include "xml/CCopasiXMLParser.h"
22 #include "utilities/CVersion.h"
23 #include "report/CKeyFactory.h"
24 #include "model/CModelValue.h"
25 #include "model/CReaction.h"
26 #include "model/CEvent.h"
28 #include "function/CFunction.h"
30 
31 // static
33 {
34  CModelEntity * pEntity = NULL;
35  CEvent * pEvent = NULL;
36  CReaction * pReaction = NULL;
37  CFunction * pFunction = NULL;
38  CModelParameterSet * pParameterSet = NULL;
39 
40  if ((pEntity = dynamic_cast< CModelEntity * >(pObject)) != NULL)
41  return static_cast< CAnnotation * >(pEntity);
42 
43  if ((pEvent = dynamic_cast< CEvent * >(pObject)) != NULL)
44  return static_cast< CAnnotation * >(pEvent);
45 
46  if ((pReaction = dynamic_cast< CReaction * >(pObject)) != NULL)
47  return static_cast< CAnnotation * >(pReaction);
48 
49  if ((pParameterSet = dynamic_cast< CModelParameterSet * >(pObject)) != NULL)
50  return static_cast< CAnnotation * >(pParameterSet);
51 
52  if ((pFunction = dynamic_cast< CFunction * >(pObject)) != NULL)
53  return static_cast< CAnnotation * >(pFunction);
54 
55  return NULL;
56 }
57 
58 // static
60 {
61  const CModelEntity * pEntity = NULL;
62  const CEvent * pEvent = NULL;
63  const CReaction * pReaction = NULL;
64  const CFunction * pFunction = NULL;
65 
66  if ((pEntity = dynamic_cast< const CModelEntity * >(pObject)) != NULL)
67  return static_cast< const CAnnotation * >(pEntity);
68 
69  if ((pEvent = dynamic_cast< const CEvent * >(pObject)) != NULL)
70  return static_cast< const CAnnotation * >(pEvent);
71 
72  if ((pReaction = dynamic_cast< const CReaction * >(pObject)) != NULL)
73  return static_cast< const CAnnotation * >(pReaction);
74 
75  if ((pFunction = dynamic_cast< const CFunction * >(pObject)) != NULL)
76  return static_cast< const CAnnotation * >(pFunction);
77 
78  return NULL;
79 }
80 
82  mKey(""),
83  mNotes(),
84  mMiriamAnnotation(),
85  mXMLId(),
86  mUnsupportedAnnotations()
87 {}
88 
90  mKey(""),
91  mNotes(src.mNotes),
92  mMiriamAnnotation(src.mMiriamAnnotation),
93  mXMLId(src.mXMLId),
94  mUnsupportedAnnotations(src.mUnsupportedAnnotations)
95 {}
96 
98 {
100 }
101 
102 // virtual
103 const std::string & CAnnotation::getKey() const
104 {
105  return mKey;
106 }
107 
108 void CAnnotation::setMiriamAnnotation(const std::string & miriamAnnotation,
109  const std::string & newId,
110  const std::string & oldId)
111 {
112  mMiriamAnnotation = miriamAnnotation;
113  mXMLId = newId;
115 }
116 
117 const std::string & CAnnotation::getMiriamAnnotation() const
118 {return mMiriamAnnotation;}
119 
120 void CAnnotation::setNotes(const std::string & notes)
121 {
122  mNotes = notes;
123 }
124 const std::string & CAnnotation::getNotes() const
125 {
126  return mNotes;
127 }
128 
129 bool CAnnotation::operator == (const CAnnotation & rhs) const
130 {
131  if (mNotes != rhs.mNotes)
132  return false;
133 
134  std::string Annotation = mMiriamAnnotation;
136 
137  // We need to ignore white spaces when comparing.
138  std::string::const_iterator it = Annotation.begin();
139  std::string::const_iterator end = Annotation.end();
140  std::string::const_iterator itRhs = rhs.mMiriamAnnotation.begin();
141  std::string::const_iterator endRhs = rhs.mMiriamAnnotation.end();
142 
143  while (it != end && itRhs != endRhs)
144  {
145  if (*it == * itRhs)
146  {
147  ++it;
148  ++itRhs;
149 
150  continue;
151  }
152 
153  // Advance past white spaces
154  while (it != end)
155  {
156  if (*it == '\x20' || *it == '\x09' || *it == '\x0d' || *it == '\x0a')
157  {
158  ++it;
159 
160  continue;
161  }
162 
163  break;
164  }
165 
166  // Advance past white spaces
167  while (itRhs != endRhs)
168  {
169  if (*itRhs == '\x20' || *itRhs == '\x09' || *itRhs == '\x0d' || *itRhs == '\x0a')
170  {
171  ++itRhs;
172 
173  continue;
174  }
175 
176  break;
177  }
178 
179  if (it == end && itRhs == endRhs)
180  {
181  return true;
182  }
183 
184  if (it == end || itRhs == endRhs)
185  {
186  return false;
187  }
188 
189  if (*it != *itRhs)
190  {
191  return false;
192  }
193 
194  ++it;
195  ++itRhs;
196  }
197 
198  return true;
199 }
200 
202 {
204 }
205 
207 {
209 }
210 
211 bool CAnnotation::addUnsupportedAnnotation(const std::string & name, const std::string & xml)
212 {
213  // The name must not be empty
214  if (name == "")
215  {
217  return false;
218  }
219 
220  // The name must be unique
221  if (mUnsupportedAnnotations.find(name) != mUnsupportedAnnotations.end())
222  {
224  return false;
225  }
226 
227  // We need to check whether we have valid XML.
228  if (!isValidXML(xml))
229  {
231  return false;
232  }
233 
234  mUnsupportedAnnotations[name] = xml;
235 
236  return true;
237 }
238 
239 bool CAnnotation::replaceUnsupportedAnnotation(const std::string & name, const std::string & xml)
240 {
241  // We need to check whether we have valid XML.
242  if (!isValidXML(xml))
243  {
245  return false;
246  }
247 
248  // The annotation must exist
249  if (mUnsupportedAnnotations.find(name) == mUnsupportedAnnotations.end())
250  {
252  return false;
253  }
254 
255  mUnsupportedAnnotations[name] = xml;
256 
257  return true;
258 }
259 
260 bool CAnnotation::removeUnsupportedAnnotation(const std::string & name)
261 {
262  UnsupportedAnnotation::iterator it = mUnsupportedAnnotations.find(name);
263 
264  if (it == mUnsupportedAnnotations.end())
265  {
266  return false;
267  }
268 
269  mUnsupportedAnnotations.erase(it);
270 
271  return true;
272 }
273 
274 // static
275 bool CAnnotation::isValidXML(const std::string & xml)
276 {
277  std::istringstream XML;
278  XML.str(xml);
279  XML.imbue(std::locale::classic());
280  XML.precision(16);
281 
282  bool done = false;
283 
284  CVersion Version;
285  CCopasiXMLParser Parser(Version);
286 
287  size_t Size = CCopasiMessage::size();
288 
289 #define BUFFER_SIZE 0xfffe
290  char * pBuffer = new char[BUFFER_SIZE + 1];
291 
292  while (!done)
293  {
294  XML.get(pBuffer, BUFFER_SIZE, 0);
295 
296  if (XML.eof()) done = true;
297 
298  if (XML.fail() && !done)
299  {
300  done = true;
301  return false;
302  }
303 
304  if (!Parser.parse(pBuffer, -1, done))
305  {
306  done = true;
307  return false;
308  }
309  }
310 
311  delete [] pBuffer;
312 #undef BUFFER_SIZE
313 
314  // Remove error messages created by setExpression as this may fail
315  // due to incomplete model specification at this time.
316  while (CCopasiMessage::size() > Size)
318 
319  return true;
320 }
Definition: CEvent.h:152
bool remove(const std::string &key)
std::string mNotes
Definition: CAnnotation.h:125
Header file of class CModelEntity and CModelValue.
bool operator==(const CAnnotation &rhs) const
static unsigned C_INT32 fixLocalFileAboutReference(std::string &rdfXml, const std::string &newId, const std::string &oldId)
std::string mMiriamAnnotation
Definition: CAnnotation.h:130
void setNotes(const std::string &notes)
UnsupportedAnnotation mUnsupportedAnnotations
Definition: CAnnotation.h:141
void setMiriamAnnotation(const std::string &miriamAnnotation, const std::string &newId, const std::string &oldId)
const std::string & getMiriamAnnotation() const
virtual ~CAnnotation()
Definition: CAnnotation.cpp:97
std::string mKey
Definition: CAnnotation.h:119
static CAnnotation * castObject(CCopasiObject *pObject)
Definition: CAnnotation.cpp:32
#define MCAnnotation
const std::string & getNotes() const
static bool isValidXML(const std::string &xml)
static size_t size()
#define BUFFER_SIZE
bool removeUnsupportedAnnotation(const std::string &name)
bool addUnsupportedAnnotation(const std::string &name, const std::string &xml)
static CKeyFactory * getKeyFactory()
std::map< std::string, std::string > UnsupportedAnnotation
Definition: CAnnotation.h:17
std::string mXMLId
Definition: CAnnotation.h:135
static CCopasiMessage getLastMessage()
The class for handling a chemical kinetic function.
Definition: CFunction.h:29
UnsupportedAnnotation & getUnsupportedAnnotations()
bool replaceUnsupportedAnnotation(const std::string &name, const std::string &xml)
bool parse(const char *pszBuffer, int nLength=-1, bool fIsFinal=true)
Definition: CExpat.h:135
Header file of class CEvent.
virtual const std::string & getKey() const