MICO Platform  1.0.0
 All Classes Namespaces Functions Variables Friends
rdf_model.hpp
1 #ifndef HAVE_RDF_MODEL_H
2 #define HAVE_RDF_MODEL_H 1
3 
4 #include <string>
5 #include <iostream>
6 
7 #include <boost/multiprecision/cpp_int.hpp>
8 #include <boost/multiprecision/cpp_dec_float.hpp>
9 
10 using boost::multiprecision::cpp_int;
11 using boost::multiprecision::cpp_dec_float_50;
12 
13 /*
14  * This module contains an implementation of the Sesame data model in C++ for use in C++ components
15  * of the MICO platform
16  */
17 
18 namespace mico {
19  namespace rdf {
20  namespace model {
21 
22  enum ValueTypes { TYPE_URI, TYPE_BNODE, TYPE_PLAIN_LITERAL, TYPE_LANGUAGE_LITERAL, TYPE_TYPED_LITERAL };
23 
27  class Value {
28 
29  friend bool operator==(const Value& l, const Value& r);
30  friend bool operator!=(const Value& l, const Value& r);
31  friend std::ostream& operator<<(std::ostream&, Value&);
32  friend std::ostream& operator<<(std::ostream&, Value*);
33 
34  protected:
38  virtual bool equals(const Value& other) const = 0;
39 
40  virtual std::ostream& print(std::ostream& os) const = 0;
41 
42  public:
43 
44  virtual ~Value() {};
45 
49  virtual const std::string& stringValue() const = 0;
50 
54  virtual const ValueTypes getType() const = 0;
55  };
56 
60  class Resource : public virtual Value {
61  };
62 
74  class URI : public virtual Resource {
75 
76  private:
77  std::string uri;
78 
79  // find split position according to algorithm in class description
80  std::size_t split() const;
81 
85  bool equals(const Value& other) const;
86 
87  std::ostream& print(std::ostream& os) const { os << "URI("<<uri<<")"; return os; } ;
88 
89  public:
90 
91  URI(const std::string& uri) : uri(uri) {};
92  URI(const char* uri) : uri(uri) {};
93  URI(const URI& uri) : uri(uri.uri) {};
94 
98  std::string getLocalName() const { return uri.substr(split()); };
99 
103  std::string getNamespace() const { return uri.substr(0,split()); };
104 
105 
109  inline const std::string& stringValue() const { return uri; } ;
110 
111 
112  inline const ValueTypes getType() const { return TYPE_URI; };
113 
114 
115  inline bool operator==(const std::string& s) const { return uri == s; };
116  inline bool operator==(const char* s) const { return uri == s; };
117  inline bool operator!=(const std::string& s) const { return uri != s; };
118  inline bool operator!=(const char* s) const { return uri != s; };
119  };
120 
121 
125  class BNode : public virtual Resource {
126 
127  private:
128  std::string id;
129 
133  bool equals(const Value& other) const;
134 
135  std::ostream& print(std::ostream& os) const { os << "BNode("<<id<<")"; return os; } ;
136  public:
140  BNode() : BNode(std::to_string(rand())) {};
141 
145  BNode(const std::string& id) : id(id) {};
146  BNode(const char* id) : id(id) {};
147  BNode(const BNode& id) : id(id.id) {};
148 
149 
153  inline const std::string& getID() const { return id; };
154 
155 
159  inline const std::string& stringValue() const { return id; };
160 
161  inline const ValueTypes getType() const { return TYPE_BNODE; };
162 
163  inline bool operator==(const std::string& s) const { return id == s; };
164  inline bool operator==(const char* s) const { return id == s; };
165  inline bool operator!=(const std::string& s) const { return id != s; };
166  inline bool operator!=(const char* s) const { return id != s; };
167  };
168 
172  class Literal : public virtual Value {
173  protected:
174  std::string label;
175 
179  virtual bool equals(const Value& other) const;
180 
181  virtual std::ostream& print(std::ostream& os) const { os << "Literal("<<label<<")"; return os; };
182  public:
183 
184  Literal(const std::string& label) : label(label) {};
185  Literal(const char* label) : label(label) {};
186  Literal(int8_t i) : label(std::to_string((int)i)) {};
187  Literal(int16_t i) : label(std::to_string((int)i)) {};
188  Literal(int32_t i) : label(std::to_string((long int)i)) {};
189  Literal(int64_t i) : label(std::to_string((long long)i)) {};
190  Literal(double d) : label(std::to_string(d)) {};
191  Literal(float d) : label(std::to_string(d)) {};
192  Literal(bool b) : label(b ? "true" : "false") {};
193 
194  virtual ~Literal() {};
195 
199  bool booleanValue() const;
200 
204  int8_t byteValue() const;
205 
206 
210  cpp_dec_float_50 decimalValue() const;
211 
215  double doubleValue() const;
216 
220  float floatValue() const;
221 
225  cpp_int integerValue() const;
226 
230  int32_t intValue() const;
231 
235  int64_t longValue() const;
236 
240  int16_t shortValue() const;
241 
245  inline const std::string& getLabel() const { return label; };
246 
250  inline const std::string& stringValue() const { return label; };
251 
252 
253  inline const ValueTypes getType() const { return TYPE_PLAIN_LITERAL; };
254 
255 
256 
257  inline bool operator==(const std::string& s) const { return label == s; };
258  //inline bool operator==(const char* s) const { return label == s; };
259  inline bool operator!=(const std::string& s) const { return label != s; };
260  //inline bool operator!=(const char* s) const { return label != s; };
261 
262 
263  inline operator bool() const { return booleanValue(); }
264  inline operator int() const { return intValue(); }
265  inline operator long int() const { return longValue(); }
266  inline operator long long int() const { return longValue(); }
267  inline operator float() const { return floatValue(); }
268  inline operator double() const { return doubleValue(); }
269  inline operator const char*() const { return label.c_str(); }
270 
271  };
272 
276  class LanguageLiteral : public virtual Literal {
277  private:
278  std::string lang;
279 
283  bool equals(const Value& other) const;
284 
285  std::ostream& print(std::ostream& os) const { os << "LanguageLiteral("<<label<<","<<lang<<")"; return os; } ;
286  public:
287 
288  LanguageLiteral(const std::string& label, const std::string& language) : Literal(label), lang(language) {};
289  LanguageLiteral(const char* label, const char* language) : Literal(label), lang(language) {};
290 
294  inline const std::string& getLanguage() const { return lang; };
295 
296  inline const ValueTypes getType() const { return TYPE_LANGUAGE_LITERAL; };
297 
298  };
299 
300 
304  class DatatypeLiteral : public virtual Literal {
305  private:
306  URI datatype;
307 
311  bool equals(const Value& other) const;
312 
313  std::ostream& print(std::ostream& os) const { os << "DatatypeLiteral("<<label<<","<<datatype.stringValue()<<")"; return os; } ;
314 
315  public:
316  DatatypeLiteral(const std::string& label, const URI& datatype) : Literal(label), datatype(datatype) {};
317  DatatypeLiteral(const char* label, const URI& datatype) : Literal(label), datatype(datatype) {};
318  DatatypeLiteral(int8_t i) : Literal(std::to_string((int)i)), datatype("http://www.w3.org/2001/XMLSchema#byte") {};
319  DatatypeLiteral(int16_t i) : Literal(std::to_string((int)i)), datatype("http://www.w3.org/2001/XMLSchema#short") {};
320  DatatypeLiteral(int32_t i) : Literal(std::to_string((long int)i)), datatype("http://www.w3.org/2001/XMLSchema#int") {};
321  DatatypeLiteral(int64_t i) : Literal(std::to_string((long long)i)), datatype("http://www.w3.org/2001/XMLSchema#long") {};
322  DatatypeLiteral(double d) : Literal(std::to_string(d)), datatype("http://www.w3.org/2001/XMLSchema#double") {};
323  DatatypeLiteral(float d) : Literal(std::to_string(d)), datatype("http://www.w3.org/2001/XMLSchema#float") {};
324  DatatypeLiteral(bool b) : Literal(b ? "true" : "false"), datatype("http://www.w3.org/2001/XMLSchema#boolean") {};
325 
329  const URI& getDatatype() const { return datatype; };
330 
331 
332  inline const ValueTypes getType() const { return TYPE_TYPED_LITERAL; };
333  };
334 
335 
339  class Statement {
340  private:
341  Resource& subject;
342  URI& predicate;
343  Value& object;
344 
345  public:
346  // provide a copy constructor for every possible combination (for convenience)
347  Statement(Resource& s, URI& p, Value& o) : subject(s), predicate(p), object(o) {};
348 
352  inline const Resource& getSubject() const { return subject; };
353 
357  inline const URI& getPredicate() const { return predicate; };
358 
359 
363  inline const Value& getObject() const { return object; };
364 
365  };
366 
367 
368 
369  inline bool operator==(const std::string& l,const URI& r) {
370  return r == l;
371  }
372 
373  inline bool operator!=(const std::string& l,const URI& r) {
374  return r != l;
375  }
376 
377  inline bool operator==(const std::string& l,const BNode& r) {
378  return r == l;
379  }
380 
381  inline bool operator!=(const std::string& l,const BNode& r) {
382  return r != l;
383  }
384 
385  inline bool operator==(const std::string& l,const Literal& r) {
386  return r == l;
387  }
388 
389  inline bool operator!=(const std::string& l,const Literal& r) {
390  return r != l;
391  }
392 
393 
394  // convenience: test double value of a literal
395  /*
396  inline bool operator==(const double d,const Literal& l) {
397  return l.doubleValue() == d;
398  }
399 
400  inline bool operator==(const Literal& l,const double d) {
401  return l.doubleValue() == d;
402  }
403 
404  inline bool operator!=(const double d,const Literal& l) {
405  return l.doubleValue() != d;
406  }
407 
408  inline bool operator!=(const Literal& l,const double d) {
409  return l.doubleValue() != d;
410  }
411 */
412 
413 
414 
415  inline bool operator==(const Value& l,const Value& r) {
416  return l.equals(r);
417  }
418 
419  inline bool operator!=(const Value& l,const Value& r) {
420  return !l.equals(r);
421  }
422 
423 
424  inline std::ostream& operator<<(std::ostream& os, Value& v) {
425  return v.print(os);
426  }
427 
428  inline std::ostream& operator<<(std::ostream& os, Value* v) {
429  return v->print(os);
430  }
431 
432  }
433  }
434 }
435 
436 #endif
const ValueTypes getType() const
Return type information (to avoid dynamic casts if possible)
Definition: rdf_model.hpp:332
BNode(const std::string &id)
Create a new BNode with the given ID.
Definition: rdf_model.hpp:145
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
Definition: http_client.cpp:23
The supertype of all RDF resources (URIs and blank nodes).
Definition: rdf_model.hpp:60
cpp_int integerValue() const
Returns the integer value of this literal.
Definition: rdf_model.cpp:108
float floatValue() const
Returns the float value of this literal.
Definition: rdf_model.cpp:101
Representation of an RDF triple, consisting of subject, predicate and object.
Definition: rdf_model.hpp:339
The supertype of all RDF model objects (URIs, blank nodes and literals).
Definition: rdf_model.hpp:27
virtual bool equals(const Value &other) const
Internal polymorphic implementation of equals.
Definition: rdf_model.cpp:64
virtual const std::string & stringValue() const =0
Returns the String-value of a Value object.
const URI & getPredicate() const
Gets the predicate of this statement.
Definition: rdf_model.hpp:357
A literal with a language tag.
Definition: rdf_model.hpp:276
int32_t intValue() const
Returns the 32 bit int value of this literal.
Definition: rdf_model.cpp:115
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
RDF blank node, represented with an internal string identifier.
Definition: rdf_model.hpp:125
const std::string & getLabel() const
Gets the label of this literal.
Definition: rdf_model.hpp:245
const URI & getDatatype() const
Gets the datatype for this literal.
Definition: rdf_model.hpp:329
virtual bool equals(const Value &other) const =0
Internal polymorphic implementation of equals and print.
const std::string & stringValue() const
Returns the String-value of a Value object.
Definition: rdf_model.hpp:250
cpp_dec_float_50 decimalValue() const
Returns the decimal value of this literal.
Definition: rdf_model.cpp:87
bool booleanValue() const
Returns the boolean value of this literal.
Definition: rdf_model.cpp:72
int64_t longValue() const
Returns the 64 bit long value of this literal.
Definition: rdf_model.cpp:122
const std::string & getID() const
retrieves this blank node's identifier.
Definition: rdf_model.hpp:153
const Resource & getSubject() const
Gets the subject of this statement.
Definition: rdf_model.hpp:352
const Value & getObject() const
Gets the object of this statement.
Definition: rdf_model.hpp:363
const ValueTypes getType() const
Return type information (to avoid dynamic casts if possible)
Definition: rdf_model.hpp:253
const std::string & getLanguage() const
Gets the language tag for this literal, normalized to lower case.
Definition: rdf_model.hpp:294
An RDF literal consisting of a label (the value) and optionally a language tag or a datatype (but not...
Definition: rdf_model.hpp:172
A URI.
Definition: rdf_model.hpp:74
const ValueTypes getType() const
Return type information (to avoid dynamic casts if possible)
Definition: rdf_model.hpp:161
const ValueTypes getType() const
Return type information (to avoid dynamic casts if possible)
Definition: rdf_model.hpp:112
A literal with a datatype.
Definition: rdf_model.hpp:304
const ValueTypes getType() const
Return type information (to avoid dynamic casts if possible)
Definition: rdf_model.hpp:296
const std::string & stringValue() const
Returns the String-value of a Value object.
Definition: rdf_model.hpp:109
BNode()
Create a new BNode with a random ID.
Definition: rdf_model.hpp:140
std::string getNamespace() const
Gets the namespace of this URI.
Definition: rdf_model.hpp:103
virtual const ValueTypes getType() const =0
Return type information (to avoid dynamic casts if possible)
int8_t byteValue() const
Returns the byte value of this literal.
Definition: rdf_model.cpp:79
int16_t shortValue() const
Returns the 16 bit short value of this literal.
Definition: rdf_model.cpp:129
std::string getLocalName() const
Gets the local name of this URI.
Definition: rdf_model.hpp:98
double doubleValue() const
Returns the double value of this literal.
Definition: rdf_model.cpp:94
const std::string & stringValue() const
Returns the String-value of a Value object.
Definition: rdf_model.hpp:159