COPASI API  4.16.103
CModelMIRIAMInfo.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) 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 // Copyright (C) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <iostream>
16 #include <fstream>
17 #include <string>
18 
19 #include "copasi.h"
20 
21 #include "CModelMIRIAMInfo.h"
22 #include "CRDFWriter.h"
23 #include "CRDFLiteral.h"
24 #include "CRDFParser.h"
25 #include "CConstants.h"
26 #include "CRDFObject.h"
27 #include "CRDFPredicate.h"
28 #include "CRDFGraph.h"
29 
30 #include "model/CModelValue.h"
31 #include "model/CEvent.h"
32 #include "model/CReaction.h"
33 #include "function/CFunction.h"
34 #include "report/CKeyFactory.h"
36 
38  CCopasiContainer("CMIRIAMInfoObject", NULL, "CMIRIAMInfo"),
39  mKey(""),
40  mCreators("Creators", this),
41  mReferences("References", this),
42  mModifications("Modifieds", this),
43  mBiologicalDescriptions("BiologicalDescriptions", this),
44  mCreatedObj(),
45  mpRDFGraph(NULL),
46  mTriplet(NULL, CRDFPredicate::about, NULL),
47  mCreated()
48 {}
49 
52 
54 {return mpRDFGraph;}
55 
57 {return mCreators;}
58 
59 CCreator* CMIRIAMInfo::createCreator(const std::string & /* objectName */)
60 {
61  const CRDFSubject & Subject = mpRDFGraph->getAboutNode()->getSubject();
62  CRDFObject Object;
64  std::string Id = mpRDFGraph->generatedNodeId();
65  Object.setBlankNodeId(Id);
66 
67  CRDFTriplet Triplet =
68  mpRDFGraph->addTriplet(Subject,
70  Object);
71 
72  if (!Triplet)
73  return NULL;
74 
75  CCreator * pCreator = new CCreator(Triplet);
76 
77  if (!mCreators.add(pCreator, true))
78  {
79  delete pCreator;
80  return NULL;
81  }
82 
83  return pCreator;
84 }
85 
86 bool CMIRIAMInfo::removeCreator(int position)
87 {
88  CCreator * pCreator = mCreators[position];
89 
90  if (!pCreator)
91  return false;
92 
93  const CRDFTriplet & Triplet = pCreator->getTriplet();
94 
97  Triplet.pObject);
98 
99  return mCreators.remove(pCreator);
100 }
101 
103 {
104  mCreators.cleanup();
105 
106  CRDFPredicate::ePredicateType Predicates[] =
107  {
111  };
112 
114  std::set< CRDFTriplet > Triples;
115 
116  CRDFPredicate::ePredicateType * pPredicate = Predicates;
117  std::set< CRDFTriplet >::iterator it;
118  std::set< CRDFTriplet >::iterator end;
119 
120  for (; *pPredicate != CRDFPredicate::end; ++pPredicate)
121  {
122  Triples =
124  it = Triples.begin();
125  end = Triples.end();
126 
127  for (; it != end; ++it)
128  mCreators.add(new CCreator(*it), true);
129  }
130 
131  return;
132 }
133 
135 {return mReferences;}
136 
137 CReference* CMIRIAMInfo::createReference(const std::string & /* objectName */)
138 {
139  const CRDFSubject & Subject = mpRDFGraph->getAboutNode()->getSubject();
140  CRDFObject Object;
142  std::string Id = mpRDFGraph->generatedNodeId();
143  Object.setBlankNodeId(Id);
144 
145  CRDFTriplet Triplet =
146  mpRDFGraph->addTriplet(Subject,
148  Object);
149 
150  if (!Triplet)
151  return NULL;
152 
153  CReference * pReference = new CReference(Triplet);
154 
155  if (!mReferences.add(pReference, true))
156  {
157  delete pReference;
158  return NULL;
159  }
160 
161  return pReference;
162 }
163 
165 {
166  CReference * pReference = mReferences[position];
167 
168  if (!pReference)
169  return false;
170 
171  const CRDFTriplet & Triplet = pReference->getTriplet();
172 
175  Triplet.pObject);
176 
177  return mReferences.remove(pReference);
178 }
179 
181 {
183 
184  CRDFPredicate::ePredicateType Predicates[] =
185  {
190  };
191 
193  std::set< CRDFTriplet > Triples;
194 
195  CRDFPredicate::ePredicateType * pPredicate = Predicates;
196  std::set< CRDFTriplet >::iterator it;
197  std::set< CRDFTriplet >::iterator end;
198 
199  for (; *pPredicate != CRDFPredicate::end; ++pPredicate)
200  {
201  Triples = mTriplet.pObject->getDescendantsWithPredicate(*pPredicate);
202  it = Triples.begin();
203  end = Triples.end();
204 
205  for (; it != end; ++it)
206  mReferences.add(new CReference(*it), true);
207  }
208 
209  return;
210 }
211 
212 const std::string CMIRIAMInfo::getCreatedDT() const
213 {
214  if (!mCreated)
215  return "";
216 
218 }
219 
220 void CMIRIAMInfo::setCreatedDT(const std::string& dt)
221 {
222  std::string Date = dt;
223 
224  if (Date == "0000-00-00T00:00:00")
225  Date = ""; // This causes deletion of the edge
226 
227  if (!mCreated)
228  {
229  const CRDFSubject & Subject = mTriplet.pObject->getSubject();
230  CRDFObject Object;
232  std::string Id = mpRDFGraph->generatedNodeId();
233  Object.setBlankNodeId(Id);
234 
235  mCreated = mpRDFGraph->addTriplet(Subject,
237  Object);
238  // Debugging
239  assert(!mCreated == false);
240  }
241 
243 }
244 
246 {return mModifications;}
247 
248 CModification * CMIRIAMInfo::createModification(const std::string& dateTime)
249 {
250  const CRDFSubject & Subject = mpRDFGraph->getAboutNode()->getSubject();
251  CRDFObject Object;
253  std::string Id = mpRDFGraph->generatedNodeId();
254  Object.setBlankNodeId(Id);
255 
256  CRDFTriplet Triplet =
257  mpRDFGraph->addTriplet(Subject,
259  Object);
260 
261  if (!Triplet)
262  return NULL;
263 
264  CModification * pModification = new CModification(Triplet);
265 
266  if (dateTime.size())
267  pModification->setDate(dateTime);
268 
269  if (!mModifications.add(pModification, true))
270  {
271  delete pModification;
272  return NULL;
273  }
274 
275  return pModification;
276 }
277 
279 {
280  CModification * pModified = mModifications[position];
281 
282  if (!pModified)
283  return false;
284 
285  const CRDFTriplet & Triplet = pModified->getTriplet();
286 
289  Triplet.pObject);
290 
291  return mModifications.remove(pModified);
292 }
293 
295 {
297 
298  std::set< CRDFTriplet > Triples =
300  std::set< CRDFTriplet >::iterator it = Triples.begin();
301  std::set< CRDFTriplet >::iterator end = Triples.end();
302 
303  for (; it != end; ++it)
304  mModifications.add(new CModification(*it), true);
305 
306  return;
307 }
308 
310 {return mBiologicalDescriptions;}
311 
313 {
314  const CRDFSubject & Subject = mpRDFGraph->getAboutNode()->getSubject();
315  CRDFObject Object;
317  Object.setResource("", false);
318 
319  CRDFTriplet Triplet = mpRDFGraph->addTriplet(Subject, std::string("---"), Object);
320 
321  if (!Triplet)
322  return NULL;
323 
324  CBiologicalDescription * pBiologicalDescription =
325  new CBiologicalDescription(Triplet);
326 
327  if (!mBiologicalDescriptions.add(pBiologicalDescription, true))
328  {
329  delete pBiologicalDescription;
330  return NULL;
331  }
332 
333  return pBiologicalDescription;
334 }
335 
337 {
338  CBiologicalDescription * pBiologicalDescription =
339  mBiologicalDescriptions[position];
340 
341  if (!pBiologicalDescription)
342  return false;
343 
344  const CRDFTriplet & Triplet = pBiologicalDescription->getTriplet();
345 
347  Triplet.Predicate,//CRDFPredicate::getURI(Triplet.Predicate),
348  Triplet.pObject);
349 
350  return mBiologicalDescriptions.remove(pBiologicalDescription);
351 }
352 
354 {
356 
357  CRDFPredicate::ePredicateType Predicates[] =
358  {
379  };
380 
382  std::set< CRDFTriplet > Triples;
383 
384  CRDFPredicate::ePredicateType * pPredicate = Predicates;
385  std::set< CRDFTriplet >::iterator it;
386  std::set< CRDFTriplet >::iterator end;
387 
388  for (; *pPredicate != CRDFPredicate::end; ++pPredicate)
389  {
390  Triples = mTriplet.pObject->getDescendantsWithPredicate(*pPredicate);
391  it = Triples.begin();
392  end = Triples.end();
393 
394  for (; it != end; ++it)
396  }
397 }
398 
399 void CMIRIAMInfo::load(const std::string& key)
400 {
402 
403  mKey = key;
404  CCopasiObject * pCopasiObject = dynamic_cast< CCopasiObject * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
405 
406  if (pCopasiObject != NULL)
407  {
408  const std::string * pMiriamAnnotation = NULL;
409 
410  CAnnotation * pAnnotation = CAnnotation::castObject(pCopasiObject);
411 
412  if (pAnnotation != NULL)
413  {
414  pMiriamAnnotation = &pAnnotation->getMiriamAnnotation();
415  }
416 
417  if (pMiriamAnnotation && *pMiriamAnnotation != "")
418  mpRDFGraph = CRDFParser::graphFromXml(*pMiriamAnnotation);
419  }
420 
421  if (mpRDFGraph == NULL)
422  mpRDFGraph = new CRDFGraph;
423 
424  // We make sure that we always have an about node.
425 
426  if (pCopasiObject != NULL)
427  mTriplet.pObject = mpRDFGraph->createAboutNode(pCopasiObject->getKey());
428  else
430 
431  // Load the created date if set;
433  std::set< CRDFTriplet > Triples =
435 
436  if (Triples.size() > 0)
437  mCreated = *Triples.begin();
438  else
439  mCreated = CRDFTriplet(); // This is an invalid triplet, i.e., !mCreated is true.
440 
441  loadCreators();
442  loadReferences();
445 
446  return;
447 }
448 
450 {
451  CCopasiObject * pCopasiObject = dynamic_cast< CCopasiObject * >(CCopasiRootContainer::getKeyFactory()->get(mKey));
452 
453  if (pCopasiObject && mpRDFGraph)
454  {
455  mpRDFGraph->clean();
457 
458  std::string XML = CRDFWriter::xmlFromGraph(mpRDFGraph);
459 
460  CAnnotation * pAnnotation = CAnnotation::castObject(pCopasiObject);
461 
462  if (pAnnotation == NULL)
463  {
464  return false;
465  }
466 
467  pAnnotation->setMiriamAnnotation(XML, pAnnotation->getKey(), pAnnotation->getKey());
468 
469  return true;
470  }
471 
472  return false;
473 }
474 
475 const std::string & CMIRIAMInfo::getKey() const
476 {return mKey;}
const CCopasiVector< CCreator > & getCreators() const
CRDFNode * createAboutNode(const std::string &key)
Definition: CRDFGraph.cpp:416
#define pdelete(p)
Definition: copasi.h:215
Header file of class CModelEntity and CModelValue.
void load(const std::string &key="")
void removeTriplet(CRDFNode *pSubject, const CRDFPredicate &predicate, CRDFNode *pObject)
Definition: CRDFGraph.cpp:277
void updateNamespaces()
Definition: CRDFGraph.cpp:558
std::set< CRDFTriplet > getDescendantsWithPredicate(const CRDFPredicate &predicate) const
Definition: CRDFNode.cpp:398
CRDFNode * pSubject
Definition: CRDFTriplet.h:38
const CCopasiVector< CBiologicalDescription > & getBiologicalDescriptions() const
CCopasiVector< CCreator > mCreators
const CRDFTriplet & getTriplet() const
Definition: CModified.cpp:61
const CRDFTriplet & getTriplet() const
Definition: CCreator.cpp:65
CCopasiObject * get(const std::string &key)
const CRDFSubject & getSubject() const
Definition: CRDFNode.cpp:69
CCopasiVector< CBiologicalDescription > mBiologicalDescriptions
bool removeModification(int position)
void setMiriamAnnotation(const std::string &miriamAnnotation, const std::string &newId, const std::string &oldId)
CReference * createReference(const std::string &objectName)
const std::string & getMiriamAnnotation() const
void loadBiologicalDescriptions()
virtual const std::string & getKey() const
void setType(const eObjectType &type)
Definition: CRDFObject.cpp:82
virtual const std::string & getKey() const
bool setFieldValue(const CMIRIAMResourceObject &value, const CRDFPredicate::ePredicateType &predicate, const CRDFPredicate::Path &nodePath)
Definition: CRDFNode.cpp:142
static CAnnotation * castObject(CCopasiObject *pObject)
Definition: CAnnotation.cpp:32
CRDFTriplet addTriplet(const CRDFSubject &subject, const CRDFPredicate &predicate, const CRDFObject &object)
Definition: CRDFGraph.cpp:141
CRDFTriplet mTriplet
bool removeReference(int position)
CRDFTriplet mCreated
const std::string getCreatedDT() const
CRDFPredicate::Path getPath() const
Definition: CRDFNode.cpp:110
CCreator * createCreator(const std::string &objectName)
CRDFGraph * getRDFGraph()
virtual void cleanup()
virtual bool add(const CType &src)
const CCopasiVector< CReference > & getReferences() const
CCopasiVector< CModification > mModifications
bool removeCreator(int position)
void setDate(const std::string &date)
Definition: CModified.cpp:67
CCopasiVector< CReference > mReferences
std::string mKey
CRDFPredicate Predicate
Definition: CRDFTriplet.h:40
virtual void remove(const size_t &index)
static CKeyFactory * getKeyFactory()
void setResource(const std::string &resource, const bool &isLocal)
Definition: CRDFObject.cpp:88
const std::string & getURI() const
CRDFGraph * mpRDFGraph
const std::string & getFieldValue(const CRDFPredicate::ePredicateType &predicate) const
Definition: CRDFNode.cpp:113
CModification * createModification(const std::string &objectName)
bool removeBiologicalDescription(int position)
void clean()
Definition: CRDFGraph.cpp:511
const CRDFNode * getAboutNode() const
Definition: CRDFGraph.cpp:82
Header file of class CEvent.
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFObject.cpp:100
const CRDFTriplet & getTriplet() const
Definition: CReference.cpp:88
void loadModifications()
void setCreatedDT(const std::string &dt)
CBiologicalDescription * createBiologicalDescription()
const CCopasiVector< CModification > & getModifications() const
std::string generatedNodeId(const std::string &existingId="")
Definition: CRDFGraph.cpp:380
CRDFNode * pObject
Definition: CRDFTriplet.h:42
static CRDFGraph * graphFromXml(const std::string &xml)
Definition: CRDFParser.cpp:30
const CRDFTriplet & getTriplet() const
std::vector< ePredicateType > Path
Definition: CRDFPredicate.h:91
virtual const std::string & getKey() const
static std::string xmlFromGraph(const CRDFGraph *pGraph)
Definition: CRDFWriter.cpp:26