COPASI API  4.16.103
CRDFNode.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAM/CRDFNode.cpp,v $
3 // $Revision: 1.18 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2011/03/07 19:30:18 $
7 // End CVS Header
8 
9 // Copyright (C) 2011 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 #include "copasi.h"
24 
25 #include "CRDFGraph.h"
26 
27 #include "CRDFSubject.h"
28 #include "CRDFObject.h"
29 #include "CRDFLiteral.h"
30 #include "CConstants.h"
31 
33  mGraph(graph),
34  mId(""),
35  mpSubject(NULL),
36  mpObject(NULL),
37  mIsBlankNode(false)
38 {}
39 
41 {
44 }
45 
46 void CRDFNode::setId(const std::string & id)
47 {mId = id;}
48 
49 const std::string & CRDFNode::getId() const
50 {return mId;}
51 
52 void CRDFNode::setSubject(const CRDFSubject & subject)
53 {
55  mpSubject = new CRDFSubject(subject);
56 
58  {
59  mIsBlankNode = true;
61 
63  mpObject = new CRDFObject;
66  }
67 }
68 
70 {
71  if (mpSubject == NULL)
72  mpSubject = new CRDFSubject;
73 
74  return *mpSubject;
75 }
76 
77 void CRDFNode::setObject(const CRDFObject & object)
78 {
80  mpObject = new CRDFObject(object);
81 
83  {
84  mIsBlankNode = true;
86 
88  mpSubject = new CRDFSubject;
91  }
92 }
93 
95 {
96  if (mpObject == NULL)
97  mpObject = new CRDFObject;
98 
99  return *mpObject;
100 }
101 
103 {
104  if (mpObject == NULL)
105  mpObject = new CRDFObject;
106 
107  return *mpObject;
108 }
109 
111 {return mGraph.getPredicatePath(this);}
112 
113 const std::string & CRDFNode::getFieldValue(const CRDFPredicate::ePredicateType & predicate) const
114 {
115  static std::string Empty = "";
116 
117  const std::set< CRDFTriplet > Triplets = getDescendantsWithPredicate(predicate);
118 
119  if (Triplets.size() > 0)
120  {
121  const CRDFObject & Object = Triplets.begin()->pObject->getObject();
122 
123  switch (Object.getType())
124  {
125  case CRDFObject::LITERAL:
126  return Object.getLiteral().getLexicalData();
127  break;
128 
130  return Object.getResource();
131  break;
132 
134  return Object.getBlankNodeID();
135  break;
136  }
137  }
138 
139  return Empty;
140 }
141 
143  const CRDFPredicate::ePredicateType & predicate,
144  const CRDFPredicate::Path & nodePath)
145 {
146  // If a node is associated with the resource we can just set the field value
147  if (value.getNode() != NULL)
148  return value.getNode()->setFieldValue(value.getURI(), predicate, nodePath);
149 
150  // We have no node and the value is invalid, i.e., nothing to do.
151  if (!value.isValid())
152  return true;
153 
154  // We need to create a node
155 
156  // We build each missing ancestor as a blank node.
157  CRDFPredicate::sAllowedLocation const * pLocation = NULL;
158  CRDFNode * pParent = createMissingAncestors(nodePath, predicate, pLocation);
159 
160  if (pParent == NULL) return false;
161 
162  // Create the missing object
163  CRDFObject object;
165  object.setResource(value.getURI(), false);
166 
167  bool success = true;
168 
169  if (!mGraph.addTriplet(pParent->getSubject(), CRDFPredicate::getURI(predicate), object))
170  success = false;
171 
172  return success;
173 }
174 
175 bool CRDFNode::setFieldValue(const std::string & value,
176  const CRDFPredicate::ePredicateType & predicate,
177  const CRDFPredicate::Path & nodePath)
178 {
179  std::set< CRDFTriplet > Triplets =
180  getDescendantsWithPredicate(predicate);
181 
182  CRDFNode * pObject = NULL;
183 
184  if (Triplets.size() > 0)
185  pObject = Triplets.begin()->pObject;
186 
187  // Check whether the value is not empty.
188  if (value != "")
189  {
190  // We do not have an object
191  if (pObject == NULL)
192  {
193  // We build each missing ancestor as a blank node.
194  CRDFPredicate::sAllowedLocation const * pLocation = NULL;
195  CRDFNode * pParent = createMissingAncestors(nodePath, predicate, pLocation);
196 
197  if (pParent == NULL) return false;
198 
199  // Create the missing object
200  CRDFObject object;
201  object.setType(pLocation->Type);
202 
203  if (object.getType() == CRDFObject::BLANK_NODE)
204  object.setBlankNodeId(mGraph.generatedNodeId());
205 
206  CRDFTriplet Triplet =
207  mGraph.addTriplet(pParent->getSubject(), CRDFPredicate::getURI(predicate), object);
208 
209  if (!Triplet)
210  return false;
211 
212  pObject = Triplet.pObject;
213 
214  // Debugging
215  assert(pObject != NULL);
216  }
217 
218  // Now we finally can save the information.
219  CRDFObject & Object = pObject->getObject();
220 
221  switch (Object.getType())
222  {
223  case CRDFObject::LITERAL:
224  {
225  CRDFLiteral & Literal = Object.getLiteral();
226  Literal.setType(CRDFLiteral::PLAIN);
227  Literal.setLexicalData(value);
228  }
229  break;
230 
232  Object.setResource(value, false);
233  break;
234 
236  fatalError();
237  break;
238  }
239  }
240  else if (pObject != NULL)
241  removeTripletFromGraph(*Triplets.begin());
242 
243  return true;
244 }
245 
247  const CRDFPredicate::ePredicateType & predicate,
248  CRDFPredicate::sAllowedLocation const *& pLocation)
249 {
250  pLocation = NULL;
251 
252  // If this is not a subject node we are not able to do anything.
253  if (!isSubjectNode()) return NULL;
254 
255  // Determine the path leading to the field
257  size_t i, imax = Locations.size();
258  size_t SubPathIndex = C_INVALID_INDEX;
259 
260  for (i = 0; i < imax; i++)
261  {
262  // We ignore read only locations.
263  if (Locations[i].ReadOnly)
264  continue;
265 
266  SubPathIndex = CRDFPredicate::getSubPathIndex(Locations[i].Location, nodePath);
267 
268  if (SubPathIndex != C_INVALID_INDEX)
269  break;
270  }
271 
272  // The value can not be inserted
273  if (SubPathIndex == C_INVALID_INDEX)
274  return NULL;
275 
276  // Now we build each missing ancestor as a blank node.
277  CRDFNode * pParent = createMissingAncestors(Locations[i].Location, SubPathIndex);
278 
279  if (pParent != NULL)
280  pLocation = &Locations[i];
281 
282  return pParent;
283 }
284 
286  const size_t & level)
287 {
288  CRDFNode * pNode = this;
289 
290  size_t i, imax = predicatePath.size() - 1; // We only create the ancestors
291 
292  for (i = level; i < imax; i++)
293  {
294  std::set< CRDFTriplet > Triplets =
295  getDescendantsWithPredicate(predicatePath[i]);
296 
297  // Check whether the predicate exists.
298  if (Triplets.size() > 0)
299  {
300  pNode = Triplets.begin()->pObject;
301  }
302  else
303  {
304  CRDFObject object;
306  object.setBlankNodeId(mGraph.generatedNodeId());
307 
308  CRDFTriplet Triplet =
309  mGraph.addTriplet(pNode->getSubject(), CRDFPredicate::getURI(predicatePath[i]), object);
310 
311  if (!Triplet)
312  return NULL;
313  else
314  pNode = Triplet.pObject;
315  }
316  }
317 
318  return pNode;
319 }
320 
322 {
323  CRDFTriplet Failed;
324  CRDFTriplet Triplet(this, predicate, pObject);
325 
326  // We do not want any duplicate triplets;
327  if (mGraph.getTriplets().count(Triplet) > 0)
328  return Failed;
329 
330  // If this is a bag node the only predicate allowed is rdf_li.
331  if (isBagNode() && predicate != CRDFPredicate::rdf_li)
332  return Failed;
333 
334  // If the predicate is rdf:li and this is not a bag node we bagify it.
335  if (!isBagNode() && predicate == CRDFPredicate::rdf_li)
336  {
337  // Bagify the node.
338  CRDFObject Object;
340  Object.setResource("http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag", false);
341 
342  if (!mGraph.addTriplet(this->getSubject(), CRDFPredicate(CRDFPredicate::rdf_type), Object))
343  return Failed;
344  }
345 
346  // We now can safely insert any edge with the predicate rdf_li
347  if (predicate == CRDFPredicate::rdf_li)
348  {
349  if (!addTripletToGraph(Triplet))
350  return Failed;
351 
352  return Triplet;
353  }
354 
355  if (!addTripletToGraph(Triplet))
356  return Failed;
357 
358  return Triplet;
359 }
360 
361 void CRDFNode::removeEdge(const CRDFPredicate & predicate, CRDFNode * pObject)
362 {
363  // Determine whether the predicate points to a bag node
364  std::set< CRDFTriplet > Triplets = mGraph.getTriplets(this, predicate);
365 
366  // Debugging
367  assert(Triplets.size() > 0);
368 
369  CRDFNode * pTarget = Triplets.begin()->pObject;
370 
371  if (pTarget->isBagNode() && pTarget != pObject)
372  {
373  pTarget->removeEdge(CRDFPredicate::rdf_li, pObject);
374 
375  Triplets = mGraph.getTriplets(pTarget, CRDFPredicate::rdf_li);
376 
377  switch (Triplets.size())
378  {
379  case 0:
380  // If pTarget is an empty bag node we remove it.
381  // Note, this will destroy pTarget, i.e., no need to unbag
382  removeEdge(predicate, pTarget);
383  break;
384 
385  default:
386  // We have more than 1 rdf_li element left.
387  break;
388  }
389 
390  return;
391  }
392 
393  removeTripletFromGraph(CRDFTriplet(this, predicate, pObject));
394 
395  return;
396 }
397 
398 std::set< CRDFTriplet > CRDFNode::getDescendantsWithPredicate(const CRDFPredicate & predicate) const
399 {
400  std::set< CRDFTriplet > Triplets;
402 
403  std::set< CRDFTriplet > Predicates = mGraph.getTriplets(predicate);
404  std::set< CRDFTriplet >::const_iterator it = Predicates.begin();
405  std::set< CRDFTriplet >::const_iterator end = Predicates.end();
406 
407  for (; it != end; ++it)
408  if (it->pObject->hasAncestor(this))
409  Triplets.insert(*it);
410 
411  return Triplets;
412 }
413 
415 {return mpSubject != NULL;}
416 
418 {return mpObject != NULL;}
419 
421 {return mIsBlankNode;}
422 
424 {
425  std::set< CRDFTriplet > Triplets = mGraph.getTriplets(this, CRDFPredicate::rdf_type);
426  std::set< CRDFTriplet >::const_iterator it = Triplets.begin();
427  std::set< CRDFTriplet >::const_iterator end = Triplets.end();
428 
429  for (; it != end; ++it)
430  if (Triplets.begin()->pObject->getObject().getResource() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag")
431  return true;
432 
433  return false;
434 }
435 
437 {
439  return CRDFPredicate::isReadOnly(NodePath);
440 }
441 
442 bool CRDFNode::hasAncestor(const CRDFNode * pNode) const
443 {
444  // We consider each node to be its own ancestor.
445  if (pNode == this)
446  return true;
447 
448  bool hasAncestor = false;
449 
450  std::set< const CRDFNode * > Parents = mGraph.getParentSubjects(this);
451  std::set< const CRDFNode * >::const_iterator it = Parents.begin();
452  std::set< const CRDFNode * >::const_iterator end = Parents.end();
453 
454  for (; it != end && !hasAncestor; ++it)
455  hasAncestor = (*it)->hasAncestor(pNode);
456 
457  return hasAncestor;
458 }
bool isBlankNode() const
Definition: CRDFNode.cpp:420
bool isValid() const
Definition: CConstants.cpp:182
#define pdelete(p)
Definition: copasi.h:215
CRDFSubject * mpSubject
Definition: CRDFNode.h:241
static bool isReadOnly(const Path &currentPath)
std::string getURI() const
Definition: CConstants.cpp:133
const eObjectType & getType() const
Definition: CRDFObject.cpp:85
CRDFObject * mpObject
Definition: CRDFNode.h:246
std::set< CRDFTriplet > getDescendantsWithPredicate(const CRDFPredicate &predicate) const
Definition: CRDFNode.cpp:398
const std::string & getBlankNodeID() const
Definition: CRDFObject.cpp:103
void setSubject(const CRDFSubject &subject)
Definition: CRDFNode.cpp:52
const CRDFSubject & getSubject() const
Definition: CRDFNode.cpp:69
const std::string & getBlankNodeID() const
Definition: CRDFSubject.cpp:91
#define fatalError()
std::set< const CRDFNode * > getParentSubjects(const CRDFNode *pObject) const
Definition: CRDFGraph.cpp:499
std::vector< sAllowedLocation > AllowedLocationList
CRDFGraph & mGraph
Definition: CRDFNode.h:231
bool isBagNode() const
Definition: CRDFNode.cpp:423
const std::string & getId() const
Definition: CRDFNode.cpp:49
#define C_INVALID_INDEX
Definition: copasi.h:222
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFSubject.cpp:88
~CRDFNode()
Definition: CRDFNode.cpp:40
CRDFNode * getNode() const
Definition: CConstants.cpp:160
CRDFObject::eObjectType Type
Definition: CRDFPredicate.h:98
void setType(const eObjectType &type)
Definition: CRDFObject.cpp:82
bool setFieldValue(const CMIRIAMResourceObject &value, const CRDFPredicate::ePredicateType &predicate, const CRDFPredicate::Path &nodePath)
Definition: CRDFNode.cpp:142
CRDFObject & getObject()
Definition: CRDFNode.cpp:94
CRDFTriplet addTriplet(const CRDFSubject &subject, const CRDFPredicate &predicate, const CRDFObject &object)
Definition: CRDFGraph.cpp:141
const std::set< CRDFTriplet > & getTriplets() const
Definition: CRDFGraph.cpp:434
CRDFPredicate::Path getPredicatePath(const CRDFNode *pNode)
Definition: CRDFGraph.cpp:349
CRDFPredicate::Path getPath() const
Definition: CRDFNode.cpp:110
bool hasAncestor(const CRDFNode *pNode) const
Definition: CRDFNode.cpp:442
bool isReadOnly() const
Definition: CRDFNode.cpp:436
bool isSubjectNode() const
Definition: CRDFNode.cpp:414
bool mIsBlankNode
Definition: CRDFNode.h:251
void setType(const eLiteralType &type)
Definition: CRDFLiteral.cpp:39
static size_t getSubPathIndex(const Path &fullPath, const Path &currentPath)
static const AllowedLocationList & getAllowedLocationList(const ePredicateType &predicate)
void removeEdge(const CRDFPredicate &predicate, CRDFNode *pObject)
Definition: CRDFNode.cpp:361
void setLexicalData(const std::string &lexicalData)
Definition: CRDFLiteral.cpp:57
std::string mId
Definition: CRDFNode.h:236
void setResource(const std::string &resource, const bool &isLocal)
Definition: CRDFObject.cpp:88
void setId(const std::string &id)
Definition: CRDFNode.cpp:46
bool isObjectNode() const
Definition: CRDFNode.cpp:417
const std::string & getURI() const
const std::string & getFieldValue(const CRDFPredicate::ePredicateType &predicate) const
Definition: CRDFNode.cpp:113
const std::string & getLexicalData() const
Definition: CRDFLiteral.cpp:60
CRDFNode * createMissingAncestors(const CRDFPredicate::Path &nodePath, const CRDFPredicate::ePredicateType &predicate, CRDFPredicate::sAllowedLocation const *&pLocation)
Definition: CRDFNode.cpp:246
void setBlankNodeId(const std::string &blankNodeId)
Definition: CRDFObject.cpp:100
void setObject(const CRDFObject &object)
Definition: CRDFNode.cpp:77
void setType(const eSubjectType &type)
Definition: CRDFSubject.cpp:70
std::string generatedNodeId(const std::string &existingId="")
Definition: CRDFGraph.cpp:380
CRDFNode * pObject
Definition: CRDFTriplet.h:42
CRDFTriplet addEdge(const CRDFPredicate &predicate, CRDFNode *pObject)
Definition: CRDFNode.cpp:321
std::vector< ePredicateType > Path
Definition: CRDFPredicate.h:91
const std::string & getResource() const
Definition: CRDFObject.cpp:94
const eSubjectType & getType() const
Definition: CRDFSubject.cpp:73
bool addTripletToGraph(const CRDFTriplet &triplet) const
Definition: CRDFGraph.cpp:678
CRDFLiteral & getLiteral()
Definition: CRDFObject.cpp:112
void removeTripletFromGraph(const CRDFTriplet &triplet) const
Definition: CRDFGraph.cpp:683