COPASI API  4.16.103
CMIRIAMResource.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 // Copyright (C) 2008 - 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 // These are treated as external includes and must appear first
14 #include "WebServicesIssues/MiriamWebServicesSoapBinding.nsmap"
15 
16 #include "copasi.h"
17 
18 #include "CMIRIAMResource.h"
19 #include "CConstants.h"
20 
23 
24 #include <sbml/xml/XMLNode.h>
25 #include <sbml/xml/XMLAttributes.h>
26 #include <sbml/xml/XMLToken.h>
27 #include <sbml/xml/XMLInputStream.h>
28 
29 CMIRIAMResources::CMIRIAMResources(const std::string & name,
30  const CCopasiContainer * pParent) :
31  CCopasiParameterGroup(name, pParent),
32  mpLastUpdateDate(NULL),
33  mpUpdateFrequency(NULL),
34  mpMIRIAMResources(NULL)
36 
38  const CCopasiContainer * pParent):
39  CCopasiParameterGroup(src, pParent),
40  mpLastUpdateDate(NULL),
41  mpUpdateFrequency(NULL),
42  mpMIRIAMResources(NULL)
44 
46  const CCopasiContainer * pParent):
47  CCopasiParameterGroup(group, pParent),
48  mpLastUpdateDate(NULL),
49  mpUpdateFrequency(NULL),
50  mpMIRIAMResources(NULL)
52 
54 {
56  (unsigned C_INT32) getActDateInSeconds())->getValue().pUINT;
58  (unsigned C_INT32) 604800)->getValue().pUINT;
59  mpMIRIAMResources = assertGroup("Resources");
60 
62 
64  createURIMap();
65 }
66 
68 {
69  mpMIRIAMResources->addParameter(mimriamResource);
70 }
71 
73 {return * this;}
74 
76 {return * mpMIRIAMResources;}
77 
79 {return * mpLastUpdateDate;}
80 
82 {return * mpUpdateFrequency;}
83 
85  const std::string& filename)
86 {
87  bool success = true;
88 
89  CCopasiParameterGroup * pTmpCpyCMIRIAMResources = new CCopasiParameterGroup("Resources");
90  CMIRIAMResource * pMIRIAMResource = NULL;
91  std::string Name, URI, Deprecated, Pattern, IsDeprecated;
92  int itNames = 0, itURIs = 0, sizeNames = 0, sizeURIs = 0;
93  unsigned C_INT32 processStep = 0, processSteps = 1;
94 
95  size_t hUpdateStep;
96 
97  if (pProcessReport)
98  hUpdateStep = pProcessReport->addItem("Parsing MIRIAM XML", processStep, &processSteps);
99 
100  // parse file into memory
101  XMLInputStream stream(filename.c_str());
102 
103  if (stream.isError()) return false;
104 
105  XMLNode root(stream);
106 
107  ++processStep;
108 
109  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
110  return false;
111 
112  sizeNames = root.getNumChildren();
113  processSteps = sizeNames + 2;
114 
115  if (pProcessReport)
116  hUpdateStep = pProcessReport->addItem("Add Items", processStep, &processSteps);
117 
118  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
119  return false;
120 
121  // read all datatype elements
122  for (itNames = 0; itNames < sizeNames; itNames++)
123  {
124  XMLNode dataType = root.getChild(itNames);
125 
126  // only parse dataType elements
127  if (dataType.getName() != "datatype") continue;
128 
129  // extract name
130  Name = dataType.getChild("name").getChild(0).getCharacters();
131  pMIRIAMResource = new CMIRIAMResource(Name, NULL);
132 
133  XMLNode uris = dataType.getChild("uris");
134  sizeURIs = uris.getNumChildren();
135 
136  // extract uri & deprecated uris
137  URI.clear();
138 
139  if (sizeURIs != 0)
140  {
141  for (itURIs = 0; itURIs < sizeURIs; itURIs++)
142  {
143  XMLNode uri = uris.getChild(itURIs);
144 
145  if (URI.empty())
146  URI = uri.getChild(0).getCharacters();
147 
148  Deprecated = uri.getAttrValue("deprecated");
149 
150  if (Deprecated == "true")
151  pMIRIAMResource->addDeprecatedURL(uri.getChild(0).getCharacters());
152  }
153  }
154 
155  // set name
156  pMIRIAMResource->setMIRIAMDisplayName(Name);
157 
158  if (URI.empty() || Name.empty())
159  {
160  // ignore invalid item
161  delete pMIRIAMResource;
162  continue;
163  }
164 
165  pMIRIAMResource->setMIRIAMURI(URI);
166  pMIRIAMResource->setMIRIAMPattern(dataType.getAttrValue("pattern"));
167  pMIRIAMResource->setMIRIAMCitation(URI == "urn:miriam:arxiv" ||
168  URI == "urn:miriam:doi" ||
169  URI == "urn:miriam:pubmed" ||
170  URI == "urn:miriam:isbn");
171 
172  pTmpCpyCMIRIAMResources->addParameter(pMIRIAMResource);
173 
174  processStep++;
175 
176  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
177  return false;
178  }
179 
180  processStep++;
181 
182  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
183  return false;
184 
185  if (success)
186  {
187  // TODO add a resource for local objects, i.e., within the current model.
188 
190  *mpMIRIAMResources = *pTmpCpyCMIRIAMResources;
191  elevateChildren();
193  createURIMap();
194  }
195 
196  pdelete(pTmpCpyCMIRIAMResources);
197 
198  processStep++;
199 
200  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
201  return false;
202 
203  return success;
204 }
205 
207 {
208  if (pProcessReport)
209  pProcessReport->setName("MIRIAM Resources Update...");
210 
211  bool success = true;
213 
214  CCopasiParameterGroup * pTmpCpyCMIRIAMResources = new CCopasiParameterGroup("Resources");
215 
216  CMIRIAMResource * pMIRIAMResource = NULL;
217  std::string Name, URI, Deprecated, Pattern, IsDeprecated;
218  int itNames = 0, itURIs = 0, sizeNames = 0, sizeURIs = 0;
219  unsigned C_INT32 processStep = 0, processSteps;
220  size_t hUpdateStep;
221  struct ns2__getDataTypesNameResponse DataTypesName;
222 
223  if (pProxy->getDataTypesName(DataTypesName) == SOAP_OK)
224  {
225  sizeNames = DataTypesName.getDataTypesNameReturn->__size;
226  processSteps = sizeNames + 2;
227  hUpdateStep = pProcessReport->addItem("Update Process", processStep, &processSteps);
228 
229  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
230  return false;
231 
232  for (itNames = 0; itNames < sizeNames; itNames++)
233  {
234  struct ns2__getDataTypeURIsResponse DataTypeURIs;
235 
236  if (DataTypesName.getDataTypesNameReturn->__ptr[itNames] != "")
237  Name = DataTypesName.getDataTypesNameReturn->__ptr[itNames];
238 
239  pMIRIAMResource = new CMIRIAMResource(Name, NULL);
240 
241  if ((pProxy->getDataTypeURI(Name, URI) == SOAP_OK)
242  && (pProxy->getDataTypeURIs(Name, DataTypeURIs) == SOAP_OK)
243  && (pProxy->getDataTypePattern(Name, Pattern) == SOAP_OK))
244  {
245  sizeURIs = DataTypeURIs._getDataTypeURIsReturn->__size;
246 
247  if (sizeURIs != 0)
248  {
249  for (itURIs = 0; itURIs < sizeURIs; itURIs++)
250  {
251  Deprecated = DataTypeURIs._getDataTypeURIsReturn->__ptr[itURIs];
252 
253  if (Deprecated != URI)
254  pMIRIAMResource->addDeprecatedURL(Deprecated);
255  }
256  }
257 
258  pMIRIAMResource->setMIRIAMDisplayName(Name);
259  pMIRIAMResource->setMIRIAMURI(URI);
260  pMIRIAMResource->setMIRIAMPattern(Pattern);
261  pMIRIAMResource->setMIRIAMCitation(URI == "urn:miriam:arxiv" ||
262  URI == "urn:miriam:doi" ||
263  URI == "urn:miriam:pubmed" ||
264  URI == "urn:miriam:isbn");
265 
266  pTmpCpyCMIRIAMResources->addParameter(pMIRIAMResource);
267  }
268  else
269  success = false;
270 
271  processStep++;
272 
273  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
274  return false;
275  }
276 
277  processStep++;
278 
279  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep))
280  return false;
281  }
282  else
283  success = false;
284 
285  if (success)
286  {
287  // TODO add a resource for local objects, i.e., within the current model.
288 
290  *mpMIRIAMResources = *pTmpCpyCMIRIAMResources;
291  elevateChildren();
293  createURIMap();
294  }
295  else
297  MCAnnotation + 3,
298  pProxy->soap_fault_string(),
299  pProxy->soap_fault_detail());
300 
301  pdelete(pTmpCpyCMIRIAMResources);
302 
303  processStep++;
304 
305  if (pProcessReport && !pProcessReport->progressItem(hUpdateStep)) return false;
306 
307  if (pProcessReport) pProcessReport->finishItem(hUpdateStep);
308 
309  pdelete(pProxy);
310 
311  return success;
312 }
313 
316 
318 {*mpUpdateFrequency = (unsigned C_INT32)(days * 24 * 60 * 60);}
319 
321 {return (unsigned C_INT32) time(NULL);}
322 
324 {
326  {return updateMIRIAMResources(pProcessReport);}
327 
328  return false;
329 }
330 
332 {
333  bool success = true;
334 
337 
338  for (; it != end; ++it)
339  *it = elevate<CMIRIAMResource, CCopasiParameterGroup>(*it);
340 
341  return success;
342 }
343 
345 {
346  mDisplayName2Resource.clear();
347 
348  size_t Index = 0;
349 
352 
353  for (; it != end; ++it)
354  {
356  Index++;
357  }
358 }
359 
361 {
362  mURI2Resource.clear();
363 
364  size_t Index = 0;
365  CMIRIAMResource * pResource = NULL;
366 
369 
370  for (; it != end; ++it)
371  {
372  pResource = static_cast< CMIRIAMResource * >(*it);
373  mURI2Resource[pResource->getMIRIAMURI() + ":"] = Index;
374  mURI2Resource[pResource->getIdentifiersOrgURL() + "/"] = Index;
375 
376  const CCopasiParameterGroup * pDeprecated = &pResource->getMIRIAMDeprecated();
377  CCopasiParameterGroup::index_iterator itDeprecated = pDeprecated->beginIndex();
378  CCopasiParameterGroup::index_iterator endDeprecated = pDeprecated->endIndex();
379 
380  for (; itDeprecated != endDeprecated; ++itDeprecated)
381  {
382  std::string Deprecated = *(*itDeprecated)->getValue().pSTRING;
383 
384  //Deprecated URL style URIs do not use the ':' separator.
385  if (Deprecated[Deprecated.length() - 1] != '/')
386  {
387  Deprecated += ":";
388  }
389 
390  mURI2Resource[Deprecated] = Index;
391  }
392 
393  Index++;
394  }
395 }
396 
397 const CMIRIAMResource & CMIRIAMResources::getMIRIAMResource(const size_t index) const
398 {
399  if (index >= mpMIRIAMResources->size())
401 
402  return * static_cast< CMIRIAMResource * >(mpMIRIAMResources->getGroup(index));
403 }
404 
405 size_t CMIRIAMResources::getMIRIAMResourceIndex(const std::string & URI) const
406 {
407  size_t index = C_INVALID_INDEX;
408 
409  std::map< std::string , size_t >::const_iterator it = mURI2Resource.lower_bound(URI);
410  std::map< std::string , size_t >::const_iterator end = mURI2Resource.upper_bound(URI);
411 
412  if (it == mURI2Resource.begin())
413  return index;
414 
415  it--;
416 
417  for (; it != end; ++it)
418  {
419  // Check whether the URI base of the candidate matches.
420  if (URI.compare(0, it->first.length(), it->first) == 0)
421  {
422  index = it->second;
423  break;
424  }
425  }
426 
427  return index;
428 }
429 
430 size_t CMIRIAMResources::getResourceIndexFromDisplayName(const std::string & displayName) const
431 {
432  // Check if the display name is a know resource
433  std::map< std::string, size_t>::const_iterator it =
434  mDisplayName2Resource.find(displayName.c_str());
435 
436  // If we did not find the resource we set it to unknown
437  if (it == mDisplayName2Resource.end())
438  {
439  // unknown is indicated by an invalid index.
440  return C_INVALID_INDEX;
441  }
442 
443  return it->second;
444 }
445 
446 /*void CMIRIAMResources::printResources(const std::string & sourceClass,
447  const std::string & sourceFunction) const
448  {
449  size_t Index = 0;
450 
451  CCopasiParameterGroup::index_iterator it = mpMIRIAMResources->beginIndex();
452  CCopasiParameterGroup::index_iterator end = mpMIRIAMResources->endIndex();
453 
454  printf("\nClass: %s\n", sourceClass.c_str());
455  printf("Function: %s\n", sourceFunction.c_str());
456  printf("MIRIAM RESOURCES:\n");
457  for (; it != end; ++it)
458  {
459  printf("Index: %ld\n", Index);
460  getMIRIAMResource(Index).printResource();
461  Index++;
462  printf("\n");
463  }
464  }*/
465 
467 {}
468 
469 /////////////////////////////////////////////////////////////////////////////////////
470 CMIRIAMResource::CMIRIAMResource(const std::string & name,
471  const CCopasiContainer * pParent) :
472  CCopasiParameterGroup(name, pParent),
473  mpDisplayName(NULL),
474  mpURI(NULL),
475  mpCitation(NULL),
476  mpDeprecated(NULL)
478 
480  const CCopasiContainer * pParent):
481  CCopasiParameterGroup(src, pParent),
482  mpDisplayName(NULL),
483  mpURI(NULL),
484  mpCitation(NULL),
485  mpDeprecated(NULL)
487 
489  const CCopasiContainer * pParent):
490  CCopasiParameterGroup(group, pParent),
491  mpDisplayName(NULL),
492  mpURI(NULL),
493  mpCitation(NULL),
494  mpDeprecated(NULL)
496 
498 {
500  (std::string) "")->getValue().pSTRING;
502  (std::string) "")->getValue().pSTRING;
504  (std::string) "")->getValue().pSTRING;
506  false)->getValue().pBOOL;
507  mpDeprecated = assertGroup("Deprecated");
508 }
509 
511 {}
512 /*
513 void CMIRIAMResource::printResource() const
514  {
515  printf("Name: %s\t\n\tURL: %s\t\n\tURI: %s\n",
516  (*mpDisplayName).c_str(),
517  (*mpURL).c_str(),
518  (*mpURI).c_str());
519  printf("\tDeprecated URLs:\n");
520  CCopasiParameterGroup::index_iterator it = (*mpDeprecated).beginIndex();
521  CCopasiParameterGroup::index_iterator end = (*mpDeprecated).endIndex();
522  for (; it != end; ++it)
523  {printf("\t\tURL: %s\n", (*(*it)->getValue().pSTRING).c_str());}
524 
525  printf("\tPattern: %s\n", (*mpPattern).c_str());
526 
527  if (*mpRegExp)
528  {printf("\tRegExp: true\n\n");}
529  else
530  {printf("\tRegExp: false\n\n");}
531  }*/
532 
534 {return * this;}
535 
536 void CMIRIAMResource::setMIRIAMDisplayName(const std::string & displayName)
537 {*mpDisplayName = displayName;}
538 
539 const std::string & CMIRIAMResource::getMIRIAMDisplayName() const
540 {return *mpDisplayName;}
541 
542 void CMIRIAMResource::setMIRIAMURI(const std::string & URI)
543 {*mpURI = URI;}
544 
545 const std::string & CMIRIAMResource::getMIRIAMURI() const
546 {return * mpURI;}
547 
549 {
550  return "http://identifiers.org/" + mpURI->substr(11);
551 }
552 
553 void CMIRIAMResource::setMIRIAMPattern(const std::string & pattern)
554 {*mpPattern = pattern;}
555 
556 const std::string & CMIRIAMResource::getMIRIAMPattern() const
557 {return * mpPattern;}
558 
559 void CMIRIAMResource::setMIRIAMCitation(const bool & citation)
560 {*mpCitation = citation;}
561 
563 {return * mpCitation;}
564 
565 void CMIRIAMResource::addDeprecatedURL(const std::string & URL)
567 
569 {return * mpDeprecated;}
const std::string & getMIRIAMURI() const
std::string getIdentifiersOrgURL() const
#define pdelete(p)
Definition: copasi.h:215
const CMIRIAMResource & getMIRIAMMIRIAMResource() const
const CMIRIAMResource & getMIRIAMResource(const size_t index) const
virtual int getDataTypesName(struct ns2__getDataTypesNameResponse &_param_7)
Web service operation 'getDataTypesName' (returns error code or SOAP_OK)
const CMIRIAMResources & getMIRIAMResources() const
const CCopasiParameterGroup & getResourceList() const
CCopasiParameterGroup * mpDeprecated
virtual int getDataTypePattern(std::string _nickname, std::string &_getDataTypePatternReturn)
Web service operation 'getDataTypePattern' (returns error code or SOAP_OK)
std::string * mpDisplayName
void setMIRIAMURI(const std::string &URI)
virtual bool setName(const std::string &name)
std::string * mpURI
CCopasiParameterGroup * mpMIRIAMResources
bool autoUpdateMIRIAMResources(CProcessReport *pProcessReport)
index_iterator endIndex() const
#define C_INVALID_INDEX
Definition: copasi.h:222
void setMIRIAMCitation(const bool &isCitation)
void addDeprecatedURL(const std::string &URL)
virtual int getDataTypeURIs(std::string _name, struct ns2__getDataTypeURIsResponse &_param_2)
Web service operation 'getDataTypeURIs' (returns error code or SOAP_OK)
void addMIRIAMResource(CMIRIAMResource *pMIRIAMResource)
virtual const char * soap_fault_string()
Get SOAP Fault string (NULL when absent)
const unsigned C_INT32 & getMIRIAMLastUpdateDate() const
#define C_INT32
Definition: copasi.h:90
bool updateMIRIAMResourcesFromFile(CProcessReport *pProcessReport, const std::string &filename)
const std::string & getMIRIAMPattern() const
virtual bool progressItem(const size_t &handle)
CMIRIAMResources(const std::string &name="MIRIAM Resources", const CCopasiContainer *pParent=NULL)
const std::string & getMIRIAMDisplayName() const
void setMIRIAMUpdateFrequencyInDays(const size_t &days)
ArrayOf_USCORExsd_USCOREstring * getDataTypesNameReturn
Definition: soapStub.h:521
virtual ~CMIRIAMResources()
const unsigned C_INT32 & getMIRIAMUpdateFrequency() const
#define MCAnnotation
size_t addItem(const std::string &name, const std::string &value, const std::string *pEndValue=NULL)
CMIRIAMResource(const std::string &name="MIRIAM Resource", const CCopasiContainer *pParent=NULL)
ArrayOf_USCORExsd_USCOREstring * _getDataTypeURIsReturn
Definition: soapStub.h:259
unsigned C_INT32 * mpUpdateFrequency
index_iterator beginIndex() const
const Value & getValue() const
void setMIRIAMPattern(const std::string &pattern)
unsigned C_INT32 * pUINT
CCopasiParameterGroup * assertGroup(const std::string &name)
virtual ~CMIRIAMResource()
const CCopasiParameterGroup & getMIRIAMDeprecated() const
virtual bool finishItem(const size_t &handle)
virtual bool elevateChildren()
size_t getResourceIndexFromDisplayName(const std::string &displayName) const
virtual const char * soap_fault_detail()
Get SOAP Fault detail as string (NULL when absent)
unsigned C_INT32 getActDateInSeconds()
std::string * mpPattern
static const CMIRIAMResource & getUnknownMiriamResource()
#define SOAP_OK
Definition: stdsoap2.h:995
void setMIRIAMDisplayName(const std::string &displayName)
bool addParameter(const CCopasiParameter &parameter)
virtual int getDataTypeURI(std::string _name, std::string &_getDataTypeURIReturn)
Web service operation 'getDataTypeURI' (returns error code or SOAP_OK)
CCopasiParameter * assertParameter(const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
CCopasiParameterGroup * getGroup(const std::string &name)
unsigned C_INT32 * mpLastUpdateDate
size_t getMIRIAMResourceIndex(const std::string &URI) const
parameterGroup::iterator index_iterator
bool updateMIRIAMResources(CProcessReport *pProcessReport)
std::map< std::string, size_t > mURI2Resource
std::map< std::string, size_t > mDisplayName2Resource
const bool & getMIRIAMCitation() const