COPASI API  4.16.103
CRDFGraph.h
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/MIRIAM/CRDFGraph.h,v $
3 // $Revision: 1.27 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2010/07/16 19:00:07 $
7 // End CVS Header
8 
9 // Copyright (C) 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 #ifndef COPASI_CRDFGraph
24 #define COPASI_CRDFGraph
25 
26 #include <map>
27 #include <vector>
28 #include <set>
29 
31 #include "copasi/MIRIAM/CRDFNode.h"
32 
33 class CRDFObject;
34 class CRDFSubject;
35 
36 class CRDFGraph
37 {
38 public:
39  typedef std::set< CRDFTriplet >::iterator iterator;
40  typedef std::set< CRDFTriplet >::const_iterator const_iterator;
41 
42  typedef std::multimap< CRDFNode *, CRDFTriplet > Node2Triplet;
43  typedef std::pair< Node2Triplet::iterator, Node2Triplet::iterator> Node2TripletRange;
44  typedef std::pair< Node2Triplet::const_iterator, Node2Triplet::const_iterator> Node2TripletConstRange;
45 
46  typedef std::multimap< CRDFPredicate, CRDFTriplet > Predicate2Triplet;
47  typedef std::pair< Predicate2Triplet::iterator, Predicate2Triplet::iterator> Predicate2TripletRange;
48  typedef std::pair< Predicate2Triplet::const_iterator, Predicate2Triplet::const_iterator> Predicate2TripletConstRange;
49 
50  // Operations
51  /**
52  * Default Constructor
53  */
54  CRDFGraph();
55 
56  /**
57  * Destructor
58  */
59  ~CRDFGraph();
60 
61  /**
62  * Retrieve the about/top node of the graph.
63  * @return const CRDFNode * pAboutNode
64  */
65  const CRDFNode * getAboutNode() const;
66 
67  /**
68  * Retrieve the map of prefixes to namespaces
69  * @return const std::map< std::string, std::string > & nameSpaceMap
70  */
71  const std::map< std::string, std::string > & getNameSpaceMap() const;
72 
73  /**
74  * Retrieve the map of IDs to blank nodes
75  * @return const std::map< std::string, CRDFNode * > & blankNodeMap
76  */
77  const std::map< std::string, CRDFNode * > & getBlankNodeMap() const;
78 
79  /**
80  * Retrieve the map of URIs to local resource nodes
81  * @return const std::map< std::string, CRDFNode * > & localResourceNodeMap
82  */
83  const std::map< std::string, CRDFNode * > & getLocalResourceNodeMap() const;
84 
85  /**
86  * Retrieve the vector of remote resource nodes
87  * @return const std::vector< CRDFNode * > & remoteResourceNodes
88  */
89  const std::vector< CRDFNode * > & getRemoteResourceNodes() const;
90 
91  /**
92  * Guess the graph root element. If a unique root element is found
93  * true is returned other wise falls
94  * @param const std::string & about (default: <space>)
95  * @return bool success
96  */
97  bool guessGraphRoot(const std::string & about = " ");
98 
99  /**
100  * Add a name space including prefix to the graph. It returns false if the
101  * prefix was already used with another namespace
102  * @param const std::string & prefix
103  * @param const std::string & namespace
104  * @return bool success;
105  */
106  bool addNameSpace(const std::string & prefix, const std::string & uri);
107 
108  /**
109  * Add a triplet to the graph
110  * @param const CRDFSubject & subject
111  * @param const CRDFPredicate & predicate
112  * @param const CRDFObject & object
113  * @return CRDFTriplet triplet
114  */
115  CRDFTriplet addTriplet(const CRDFSubject & subject,
116  const CRDFPredicate & predicate,
117  const CRDFObject & object);
118 
119 private:
120  /**
121  * Add a triplet to the graph. This method should only be used by CRDFNode
122  * @param const CRDFTriplet & triplet
123  * @return bool success
124  */
125  bool addTriplet(const CRDFTriplet & triplet);
126  friend bool CRDFNode::addTripletToGraph(const CRDFTriplet & triplet) const;
127 
128 public:
129  /**
130  * Remove triplet from the graph. Please note, this will also remove all unreferenced
131  * local resources created by removing the edge.
132  * @param CRDFNode * pSubject
133  * @param const CRDFPredicate & predicate
134  * @param CRDFNode * pObject
135  */
136  void removeTriplet(CRDFNode * pSubject,
137  const CRDFPredicate & predicate,
138  CRDFNode * pObject);
139 
140  /**
141  * Destroy a CRDFNode
142  * @param CRDFNode * pNode
143  */
144  void destroyUnreferencedNode(CRDFNode * pNode);
145 
146 private:
147  /**
148  * Remove triplet from the graph. This method should only be used by CRDFNode
149  * Please note, this will also remove all unreferenced
150  * local resources created by removing the edge.
151  * @param const CRDFTriplet & triplet
152  * @return bool success
153  */
154  void removeTriplet(const CRDFTriplet & triplet);
155  friend void CRDFNode::removeTripletFromGraph(const CRDFTriplet & triplet) const;
156 
157 public:
158  /**
159  * Move a edge from one node to another
160  * @param CRDFNode * pNewSubject
161  * @param CRDFNode * const CRDFTriplet & triplet
162  * @return CRDFGraph::CRDFTriplet triplet
163  */
164  CRDFTriplet moveTriplet(CRDFNode * pNewSubject, const CRDFTriplet & triplet);
165 
166  /**
167  * Retrieve all triplets.
168  * @return const std::set< CRDFTriplet > & triplets
169  */
170  const std::set< CRDFTriplet > & getTriplets() const;
171 
172  /**
173  * Retrieve all triplets with the specified predicate, this will collapse
174  * predicate.rdf:li to predicate.
175  * @param const CRDFPredicate & predicate
176  * @param const bool & expandBag (default: true)
177  * @return std::set< CRDFTriplet > triplets
178  */
179  std::set< CRDFTriplet > getTriplets(const CRDFPredicate & predicate,
180  const bool & expandBag = true) const;
181 
182  /**
183  * Retrieve all triplets with the specified subject
184  * @param const CRDFNode * pSubject
185  * @return std::set< CRDFTriplet > triplets
186  */
187  std::set< CRDFTriplet > getTriplets(const CRDFNode * pSubject) const;
188 
189  /**
190  * Retrieve all triplets with the specified subject and predicate
191  * @param const CRDFNode * pSubject
192  * @param const CRDFPredicate & predicate
193  * @return std::set< CRDFTriplet > triplets
194  */
195  std::set< CRDFTriplet > getTriplets(const CRDFNode * pSubject,
196  const CRDFPredicate & predicate) const;
197 
198  /**
199  * Retrieve all triples pointing to the object
200  * @param const CRDFNode * pObject
201  * @return std::set< CRDFTriplet > triplets
202  */
203  std::set< CRDFTriplet > getIncomingTriplets(const CRDFNode * pObject) const;
204 
205  /**
206  * Retrieve all parent subjects of the object
207  * @param const CRDFNode * pObject
208  * @return std::set< const CRDFNode * > nodes
209  */
210  std::set< const CRDFNode * > getParentSubjects(const CRDFNode * pObject) const;
211 
212  /**
213  * Retrieve the predicate path to the ginve node.
214  * @param const CRDFNode * pNode
215  * @return CRDFPredicate::Path
216  */
218 
219 public:
220  /**
221  * Generate a unique blank node id.
222  * @param const std::string & existingId (default: "")
223  * @return std::string blankNodeId
224  */
225  std::string generatedNodeId(const std::string & existingId = "");
226 
227  /**
228  * If no about node exists a node is created with the attribute:
229  * rdf:about="#key
230  * @param const std::string & key
231  * @return CRDFNode * pAbout
232  */
233  CRDFNode * createAboutNode(const std::string & key);
234 
235  /**
236  * Removes empty blank nodes .
237  * This should be called before CRDFWriter::xmlFromGraph
238  */
239  void clean();
240 
241  /**
242  * Removes all unused name spaces.
243  */
244  void updateNamespaces();
245 
246 private:
247  /**
248  * Removes all empty blank nodes. It returns true if a node was removed
249  * @return bool haveRemoved
250  */
251  bool removeEmptyNodes();
252 
253  // Attributes
254 private:
255  /**
256  * The subject node this RDF graph is about
257  */
259 
260  /**
261  * A map of prefixes to namespaces
262  */
263  std::map< std::string, std::string > mPrefix2Namespace;
264 
265  /**
266  * A map of blank node ids to nodes of the graph.
267  */
268  std::map< std::string, CRDFNode * > mBlankNodeId2Node;
269 
270  /**
271  * A map of resource URIs to local resource nodes of the graph, i
272  */
273  std::map< std::string, CRDFNode * > mLocalResource2Node;
274 
275  /**
276  * A vector of all remote resource nodes of the graph
277  */
278  std::vector< CRDFNode * > mRemoteResourceNodes;
279 
280  /**
281  * A vector of all literal nodes of the graph
282  */
283  std::vector< CRDFNode * > mLiteralNodes;
284 
285  /**
286  * All the triplets of the graph
287  */
288  std::set< CRDFTriplet > mTriplets;
289 
290  /**
291  *
292  */
294 
295  /**
296  *
297  */
299 
300  /**
301  *
302  */
304 
305  /**
306  * Memory for generated Ids
307  */
308  std::set< unsigned int > mGeneratedIds;
309 
310  /**
311  * Mapping from existing Ids to newly created ones.
312  */
313  std::map< std::string, std::string > mIdMap;
314 };
315 
316 #endif // COPASI_CRDFGraph
const std::map< std::string, CRDFNode * > & getBlankNodeMap() const
Definition: CRDFGraph.cpp:88
CRDFNode * createAboutNode(const std::string &key)
Definition: CRDFGraph.cpp:416
Node2Triplet mSubject2Triplet
Definition: CRDFGraph.h:293
bool removeEmptyNodes()
Definition: CRDFGraph.cpp:516
CRDFNode * mpAbout
Definition: CRDFGraph.h:258
void removeTriplet(CRDFNode *pSubject, const CRDFPredicate &predicate, CRDFNode *pObject)
Definition: CRDFGraph.cpp:277
void updateNamespaces()
Definition: CRDFGraph.cpp:558
const std::map< std::string, CRDFNode * > & getLocalResourceNodeMap() const
Definition: CRDFGraph.cpp:91
std::vector< CRDFNode * > mLiteralNodes
Definition: CRDFGraph.h:283
std::set< const CRDFNode * > getParentSubjects(const CRDFNode *pObject) const
Definition: CRDFGraph.cpp:499
std::multimap< CRDFPredicate, CRDFTriplet > Predicate2Triplet
Definition: CRDFGraph.h:46
std::map< std::string, CRDFNode * > mBlankNodeId2Node
Definition: CRDFGraph.h:268
std::set< CRDFTriplet >::iterator iterator
Definition: CRDFGraph.h:39
CRDFTriplet moveTriplet(CRDFNode *pNewSubject, const CRDFTriplet &triplet)
Definition: CRDFGraph.cpp:334
bool guessGraphRoot(const std::string &about=" ")
Definition: CRDFGraph.cpp:97
const std::vector< CRDFNode * > & getRemoteResourceNodes() const
Definition: CRDFGraph.cpp:94
std::map< std::string, std::string > mPrefix2Namespace
Definition: CRDFGraph.h:263
std::pair< Predicate2Triplet::iterator, Predicate2Triplet::iterator > Predicate2TripletRange
Definition: CRDFGraph.h:47
std::set< CRDFTriplet > getIncomingTriplets(const CRDFNode *pObject) const
Definition: CRDFGraph.cpp:487
std::multimap< CRDFNode *, CRDFTriplet > Node2Triplet
Definition: CRDFGraph.h:42
Node2Triplet mObject2Triplet
Definition: CRDFGraph.h:298
CRDFTriplet addTriplet(const CRDFSubject &subject, const CRDFPredicate &predicate, const CRDFObject &object)
Definition: CRDFGraph.cpp:141
CRDFPredicate::Path getPredicatePath(const CRDFNode *pNode)
Definition: CRDFGraph.cpp:349
const std::set< CRDFTriplet > & getTriplets() const
Definition: CRDFGraph.cpp:434
std::set< unsigned int > mGeneratedIds
Definition: CRDFGraph.h:308
std::pair< Node2Triplet::iterator, Node2Triplet::iterator > Node2TripletRange
Definition: CRDFGraph.h:43
std::map< std::string, CRDFNode * > mLocalResource2Node
Definition: CRDFGraph.h:273
const std::map< std::string, std::string > & getNameSpaceMap() const
Definition: CRDFGraph.cpp:85
Predicate2Triplet mPredicate2Triplet
Definition: CRDFGraph.h:303
std::set< CRDFTriplet > mTriplets
Definition: CRDFGraph.h:288
bool addNameSpace(const std::string &prefix, const std::string &uri)
Definition: CRDFGraph.cpp:128
std::map< std::string, std::string > mIdMap
Definition: CRDFGraph.h:313
void clean()
Definition: CRDFGraph.cpp:511
const CRDFNode * getAboutNode() const
Definition: CRDFGraph.cpp:82
std::vector< CRDFNode * > mRemoteResourceNodes
Definition: CRDFGraph.h:278
std::pair< Node2Triplet::const_iterator, Node2Triplet::const_iterator > Node2TripletConstRange
Definition: CRDFGraph.h:44
std::pair< Predicate2Triplet::const_iterator, Predicate2Triplet::const_iterator > Predicate2TripletConstRange
Definition: CRDFGraph.h:48
void destroyUnreferencedNode(CRDFNode *pNode)
Definition: CRDFGraph.cpp:610
std::string generatedNodeId(const std::string &existingId="")
Definition: CRDFGraph.cpp:380
std::vector< ePredicateType > Path
Definition: CRDFPredicate.h:91
std::set< CRDFTriplet >::const_iterator const_iterator
Definition: CRDFGraph.h:40
bool addTripletToGraph(const CRDFTriplet &triplet) const
Definition: CRDFGraph.cpp:678
void removeTripletFromGraph(const CRDFTriplet &triplet) const
Definition: CRDFGraph.cpp:683