knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Triple.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_FRAMED_TRIPLE_H
7 #define KNOWROB_FRAMED_TRIPLE_H
8 
9 #include "optional"
10 #include "variant"
11 #include "knowrob/terms/XSDType.h"
12 #include "knowrob/Printable.h"
13 
14 namespace knowrob {
20  class Triple : public Printable {
21  public:
22  explicit Triple()
23  : isOccasional_(false), isUncertain_(false), xsdType_(std::nullopt) {}
24 
26  : isOccasional_(false), isUncertain_(false), xsdType_(xsdType) {}
27 
28  virtual ~Triple() = default;
29 
34  bool mergeFrame(const Triple &other);
35 
39  bool isXSDLiteral() const { return xsdType_.has_value(); }
40 
44  bool isSubjectBlank() const { return isBlank(subject()); }
45 
49  bool isObjectBlank() const { return !isXSDLiteral() && isBlank(valueAsString()); }
50 
54  bool isObjectIRI() const { return !isXSDLiteral() && !isBlank(valueAsString()); }
55 
59  bool isSubjectIRI() const { return !isSubjectBlank(); }
60 
64  auto xsdType() const { return xsdType_; }
65 
69  auto xsdTypeIRI() const { return xsdTypeToIRI(xsdType_ ? *xsdType_ : XSDType::STRING); }
70 
76  void setXSDValue(std::string_view v, XSDType type);
77 
83 
87  virtual std::string_view subject() const = 0;
88 
92  virtual std::string_view predicate() const = 0;
93 
98  virtual std::string_view valueAsString() const = 0;
99 
104  virtual float valueAsFloat() const = 0;
105 
110  virtual double valueAsDouble() const = 0;
111 
116  virtual long valueAsLong() const = 0;
117 
122  virtual int valueAsInt() const = 0;
123 
128  virtual bool valueAsBoolean() const = 0;
129 
134  virtual short valueAsShort() const = 0;
135 
140  virtual unsigned long valueAsUnsignedLong() const = 0;
141 
146  virtual unsigned int valueAsUnsignedInt() const = 0;
147 
152  virtual unsigned short valueAsUnsignedShort() const = 0;
153 
157  virtual std::optional<std::string_view> graph() const = 0;
158 
162  virtual std::optional<std::string_view> perspective() const = 0;
163 
167  virtual void setSubject(std::string_view subject) = 0;
168 
172  virtual void setPredicate(std::string_view predicate) = 0;
173 
177  virtual void setObjectIRI(std::string_view object) = 0;
178 
182  virtual void setSubjectBlank(std::string_view str) = 0;
183 
187  virtual void setObjectBlank(std::string_view str) = 0;
188 
193  virtual void setStringValue(std::string_view v) = 0;
194 
199  virtual void setDoubleValue(double v) = 0;
200 
205  virtual void setFloatValue(float v) = 0;
206 
211  virtual void setIntValue(int v) = 0;
212 
217  virtual void setBooleanValue(bool v) = 0;
218 
223  virtual void setLongValue(long v) = 0;
224 
229  virtual void setShortValue(short v) = 0;
230 
235  virtual void setUnsignedIntValue(unsigned int v) = 0;
236 
241  virtual void setUnsignedLongValue(unsigned long v) = 0;
242 
247  virtual void setUnsignedShortValue(unsigned short v) = 0;
248 
252  virtual void setGraph(std::string_view graph) = 0;
253 
257  virtual void setPerspective(std::string_view perspective) = 0;
258 
262  bool isOccasional() const { return isOccasional_; }
263 
267  bool isUncertain() const { return isUncertain_; }
268 
272  auto begin() const { return begin_; }
273 
277  auto end() const { return end_; }
278 
282  auto confidence() const { return confidence_; }
283 
288 
293 
297  void setBegin(double begin) { begin_ = begin; }
298 
302  void setEnd(double end) { end_ = end; }
303 
308 
312  virtual void reset() = 0;
313 
318  bool operator==(const Triple &other) const;
319 
324  bool operator<(const Triple &other) const;
325 
326  // Override Printable
327  void write(std::ostream &os) const override;
328 
329  protected:
330  bool isOccasional_;
331  bool isUncertain_;
332  std::optional<double> begin_;
333  std::optional<double> end_;
334  std::optional<double> confidence_;
335  std::optional<XSDType> xsdType_;
336 
337  static bool isBlank(std::string_view str) {
338  return str.empty() || str[0] == '_';
339  }
340  };
341 
346  template<class StringType>
347  class TripleTemplate : public Triple {
348  protected:
349  using TypedObject = std::variant<StringType,
350  float,
351  double,
352  long,
353  int,
354  short,
355  bool,
356  unsigned long,
357  unsigned int,
358  unsigned short>;
359  public:
361  : Triple() {}
362 
363  TripleTemplate(std::string_view subject_iri, std::string_view predicate_iri)
364  : Triple(),
365  subject_(subject_iri),
366  predicate_(predicate_iri) {
367  xsdType_ = std::nullopt;
368  }
369 
370  TripleTemplate(std::string_view subject_iri, std::string_view predicate_iri, std::string_view object_iri)
371  : Triple(),
372  subject_(subject_iri),
373  predicate_(predicate_iri) {
374  object_ = StringType(object_iri);
375  xsdType_ = std::nullopt;
376  }
377 
378  template<typename ValT>
379  TripleTemplate(std::string_view subject, std::string_view predicate, const ValT &object, XSDType type)
380  : Triple(),
381  subject_(subject),
383  if (type == XSDType::STRING) {
384  object_ = StringType(object);
385  xsdType_ = type;
386  } else {
387  set(object, type);
388  }
389  }
390 
391  explicit TripleTemplate(const Triple &other)
392  : Triple() {
393  subject_ = other.subject();
394  predicate_ = other.predicate();
395  isOccasional_ = other.isOccasional();
396  isUncertain_ = other.isUncertain();
397  xsdType_ = other.xsdType();
398  graph_ = other.graph();
399  perspective_ = other.perspective();
400  begin_ = other.begin();
401  end_ = other.end();
402  confidence_ = other.confidence();
403 
404  if (!xsdType_) {
405  object_ = StringType(other.valueAsString());
406  } else {
407  switch (*xsdType_) {
408  case XSDType::STRING:
409  object_ = StringType(other.valueAsString());
410  break;
411  case XSDType::FLOAT:
412  object_ = other.valueAsFloat();
413  break;
414  case XSDType::DOUBLE:
415  object_ = other.valueAsDouble();
416  break;
417  case XSDType::LONG:
418  object_ = other.valueAsLong();
419  break;
421  case XSDType::INTEGER:
422  object_ = other.valueAsInt();
423  break;
424  case XSDType::BOOLEAN:
425  object_ = other.valueAsBoolean();
426  break;
427  case XSDType::SHORT:
428  object_ = other.valueAsShort();
429  break;
431  object_ = other.valueAsUnsignedLong();
432  break;
434  object_ = other.valueAsUnsignedInt();
435  break;
437  object_ = other.valueAsUnsignedShort();
438  break;
439  case XSDType::LAST:
440  break;
441  }
442  }
443  }
444 
445  // Override Triple
446  void setSubject(std::string_view subject) override { subject_ = subject; }
447 
448  // Override Triple
449  void setPredicate(std::string_view predicate) override { predicate_ = predicate; }
450 
451  // Override Triple
452  void setObjectIRI(std::string_view object) override {
453  object_ = StringType(object);
454  xsdType_ = std::nullopt;
455  }
456 
457  // Override Triple
458  void setSubjectBlank(std::string_view identifier) override { setSubject(identifier); }
459 
460  // Override Triple
461  void setObjectBlank(std::string_view identifier) override { setObjectIRI(identifier); }
462 
463  // Override Triple
464  void setStringValue(std::string_view v) override {
465  object_ = StringType(v);
467  }
468 
469  // Override Triple
470  void setDoubleValue(double v) override { set(v, XSDType::DOUBLE); }
471 
472  // Override Triple
473  void setFloatValue(float v) override { set(v, XSDType::FLOAT); }
474 
475  // Override Triple
476  void setIntValue(int v) override { set(v, XSDType::INTEGER); }
477 
478  // Override Triple
479  void setBooleanValue(bool v) override { set(v, XSDType::BOOLEAN); }
480 
481  // Override Triple
482  void setLongValue(long v) override { set(v, XSDType::LONG); }
483 
484  // Override Triple
485  void setShortValue(short v) override { set(v, XSDType::SHORT); }
486 
487  // Override Triple
488  void setUnsignedLongValue(unsigned long v) override { set(v, XSDType::UNSIGNED_LONG); }
489 
490  // Override Triple
491  void setUnsignedIntValue(unsigned int v) override { set(v, XSDType::UNSIGNED_INT); }
492 
493  // Override Triple
494  void setUnsignedShortValue(unsigned short v) override { set(v, XSDType::UNSIGNED_SHORT); }
495 
496  // Override Triple
497  void setGraph(std::string_view graph) override { graph_ = graph; }
498 
499  // Override Triple
500  void setPerspective(std::string_view perspective) override { perspective_ = perspective; }
501 
502  // Override Triple
503  std::string_view subject() const override { return subject_; }
504 
505  // Override Triple
506  std::string_view predicate() const override { return predicate_; }
507 
508  // Override Triple
509  std::string_view valueAsString() const override { return std::get<StringType>(object_); }
510 
511  // Override Triple
512  double valueAsDouble() const override { return std::get<double>(object_); }
513 
514  // Override Triple
515  float valueAsFloat() const override { return std::get<float>(object_); }
516 
517  // Override Triple
518  long valueAsLong() const override { return std::get<long>(object_); }
519 
520  // Override Triple
521  int valueAsInt() const override { return std::get<int>(object_); }
522 
523  // Override Triple
524  bool valueAsBoolean() const override { return std::get<bool>(object_); }
525 
526  // Override Triple
527  short valueAsShort() const override { return std::get<short>(object_); }
528 
529  // Override Triple
530  unsigned long valueAsUnsignedLong() const override { return std::get<unsigned long>(object_); }
531 
532  // Override Triple
533  unsigned int valueAsUnsignedInt() const override { return std::get<unsigned int>(object_); }
534 
535  // Override Triple
536  unsigned short valueAsUnsignedShort() const override { return std::get<unsigned short>(object_); }
537 
538  // Override Triple
539  std::optional<std::string_view> graph() const override {
540  if (graph_.has_value()) return graph_.value();
541  else return std::nullopt;
542  }
543 
544  // Override Triple
545  std::optional<std::string_view> perspective() const override {
546  if (perspective_.has_value()) return perspective_.value();
547  else return std::nullopt;
548  }
549 
550  void reset() override {
551  graph_ = std::nullopt;
552  begin_ = std::nullopt;
553  end_ = std::nullopt;
554  xsdType_ = std::nullopt;
555  isUncertain_ = false;
556  isOccasional_ = false;
557  }
558 
559  protected:
560  StringType subject_;
561  StringType predicate_;
563 
564  std::optional<StringType> graph_;
565  std::optional<StringType> perspective_;
566 
567  template<typename ValT>
568  void set(const ValT &value, XSDType type) {
569  object_ = value;
570  xsdType_ = type;
571  }
572  };
573 
577  using TripleCopy = TripleTemplate<std::string>;
581  using TripleView = TripleTemplate<std::string_view>;
582 
589  struct TriplePtr {
590  Triple *ptr;
591  // The ownership flag.
592  // It is marked as mutable to allow changing ownership even if we have a const reference to the object.
593  mutable bool owned;
594 
595  explicit TriplePtr() : ptr(nullptr), owned(false) {}
596 
597  explicit TriplePtr(Triple *ptr) : ptr(ptr), owned(true) {}
598 
599  TriplePtr(const TriplePtr &other) : ptr(other.ptr), owned(false) {}
600 
601  ~TriplePtr() { if (owned) delete ptr; }
602 
603  Triple &operator*() const { return *ptr; }
604 
605  Triple *operator->() const { return ptr; }
606 
607  bool operator==(const TriplePtr &other) const {
608  return ptr==other.ptr || (ptr && other.ptr && *ptr == *other.ptr);
609  }
610 
611  bool operator<(const TriplePtr &other) const {
612  return (ptr && other.ptr) ? (*ptr < *other.ptr) : (ptr < other.ptr);
613  }
614 
615  Triple &get() const { return *ptr; }
616  };
617 }
618 
619 #endif //KNOWROB_FRAMED_TRIPLE_H
virtual std::string_view valueAsString() const =0
auto xsdType() const
Definition: Triple.h:64
bool isOccasional_
Definition: Triple.h:330
void setXSDValue(std::string_view v, XSDType type)
virtual short valueAsShort() const =0
virtual std::optional< std::string_view > perspective() const =0
std::string createStringValue() const
bool isUncertain() const
Definition: Triple.h:267
virtual void setDoubleValue(double v)=0
virtual void reset()=0
static bool isBlank(std::string_view str)
Definition: Triple.h:337
virtual void setFloatValue(float v)=0
virtual unsigned int valueAsUnsignedInt() const =0
virtual void setSubject(std::string_view subject)=0
virtual std::optional< std::string_view > graph() const =0
std::optional< double > end_
Definition: Triple.h:333
bool isXSDLiteral() const
Definition: Triple.h:39
Triple(XSDType xsdType)
Definition: Triple.h:25
virtual double valueAsDouble() const =0
virtual void setGraph(std::string_view graph)=0
virtual void setIntValue(int v)=0
bool isUncertain_
Definition: Triple.h:331
std::optional< XSDType > xsdType_
Definition: Triple.h:335
auto end() const
Definition: Triple.h:277
virtual void setStringValue(std::string_view v)=0
virtual void setUnsignedLongValue(unsigned long v)=0
void setConfidence(double confidence)
Definition: Triple.h:307
virtual std::string_view subject() const =0
bool isSubjectIRI() const
Definition: Triple.h:59
std::optional< double > confidence_
Definition: Triple.h:334
virtual float valueAsFloat() const =0
auto confidence() const
Definition: Triple.h:282
virtual void setPerspective(std::string_view perspective)=0
bool operator==(const Triple &other) const
void setIsUncertain(bool isUncertain)
Definition: Triple.h:292
virtual void setLongValue(long v)=0
auto begin() const
Definition: Triple.h:272
void setIsOccasional(bool isOccasional)
Definition: Triple.h:287
bool isSubjectBlank() const
Definition: Triple.h:44
std::optional< double > begin_
Definition: Triple.h:332
virtual std::string_view predicate() const =0
void setBegin(double begin)
Definition: Triple.h:297
void setEnd(double end)
Definition: Triple.h:302
virtual long valueAsLong() const =0
bool operator<(const Triple &other) const
virtual void setObjectIRI(std::string_view object)=0
virtual void setUnsignedIntValue(unsigned int v)=0
void write(std::ostream &os) const override
bool isObjectIRI() const
Definition: Triple.h:54
virtual unsigned short valueAsUnsignedShort() const =0
virtual void setObjectBlank(std::string_view str)=0
bool mergeFrame(const Triple &other)
virtual unsigned long valueAsUnsignedLong() const =0
virtual void setUnsignedShortValue(unsigned short v)=0
virtual void setSubjectBlank(std::string_view str)=0
auto xsdTypeIRI() const
Definition: Triple.h:69
virtual void setPredicate(std::string_view predicate)=0
virtual void setShortValue(short v)=0
bool isOccasional() const
Definition: Triple.h:262
bool isObjectBlank() const
Definition: Triple.h:49
virtual int valueAsInt() const =0
virtual bool valueAsBoolean() const =0
virtual void setBooleanValue(bool v)=0
virtual ~Triple()=default
float valueAsFloat() const override
Definition: Triple.h:515
void setStringValue(std::string_view v) override
Definition: Triple.h:464
void setObjectIRI(std::string_view object) override
Definition: Triple.h:452
void set(const ValT &value, XSDType type)
Definition: Triple.h:568
double valueAsDouble() const override
Definition: Triple.h:512
void setPerspective(std::string_view perspective) override
Definition: Triple.h:500
void setLongValue(long v) override
Definition: Triple.h:482
int valueAsInt() const override
Definition: Triple.h:521
std::optional< std::string_view > perspective() const override
Definition: Triple.h:545
void setBooleanValue(bool v) override
Definition: Triple.h:479
void setShortValue(short v) override
Definition: Triple.h:485
void setFloatValue(float v) override
Definition: Triple.h:473
StringType predicate_
Definition: Triple.h:561
void setUnsignedLongValue(unsigned long v) override
Definition: Triple.h:488
TripleTemplate(std::string_view subject_iri, std::string_view predicate_iri, std::string_view object_iri)
Definition: Triple.h:370
std::optional< std::string_view > graph() const override
Definition: Triple.h:539
TripleTemplate(std::string_view subject_iri, std::string_view predicate_iri)
Definition: Triple.h:363
void setUnsignedShortValue(unsigned short v) override
Definition: Triple.h:494
TripleTemplate(const Triple &other)
Definition: Triple.h:391
std::string_view predicate() const override
Definition: Triple.h:506
long valueAsLong() const override
Definition: Triple.h:518
short valueAsShort() const override
Definition: Triple.h:527
unsigned short valueAsUnsignedShort() const override
Definition: Triple.h:536
void setDoubleValue(double v) override
Definition: Triple.h:470
unsigned int valueAsUnsignedInt() const override
Definition: Triple.h:533
std::optional< StringType > graph_
Definition: Triple.h:564
void setIntValue(int v) override
Definition: Triple.h:476
unsigned long valueAsUnsignedLong() const override
Definition: Triple.h:530
void setUnsignedIntValue(unsigned int v) override
Definition: Triple.h:491
TripleTemplate(std::string_view subject, std::string_view predicate, const ValT &object, XSDType type)
Definition: Triple.h:379
void setPredicate(std::string_view predicate) override
Definition: Triple.h:449
TypedObject object_
Definition: Triple.h:562
std::variant< StringType, float, double, long, int, short, bool, unsigned long, unsigned int, unsigned short > TypedObject
Definition: Triple.h:358
StringType subject_
Definition: Triple.h:560
std::string_view subject() const override
Definition: Triple.h:503
void reset() override
Definition: Triple.h:550
void setObjectBlank(std::string_view identifier) override
Definition: Triple.h:461
void setGraph(std::string_view graph) override
Definition: Triple.h:497
std::string_view valueAsString() const override
Definition: Triple.h:509
void setSubject(std::string_view subject) override
Definition: Triple.h:446
std::optional< StringType > perspective_
Definition: Triple.h:565
void setSubjectBlank(std::string_view identifier) override
Definition: Triple.h:458
bool valueAsBoolean() const override
Definition: Triple.h:524
TermRule & string()
Definition: terms.cpp:63
const IRIAtomPtr type
Definition: rdf.h:15
TripleTemplate< std::string > TripleCopy
Definition: Triple.h:577
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16
TripleTemplate< std::string_view > TripleView
Definition: Triple.h:581
bool operator==(const TriplePtr &other) const
Definition: Triple.h:607
Triple & operator*() const
Definition: Triple.h:603
Triple * ptr
Definition: Triple.h:590
Triple & get() const
Definition: Triple.h:615
TriplePtr(const TriplePtr &other)
Definition: Triple.h:599
TriplePtr(Triple *ptr)
Definition: Triple.h:597
bool operator<(const TriplePtr &other) const
Definition: Triple.h:611
Triple * operator->() const
Definition: Triple.h:605