knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
UnReificationContainer.cpp
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 #include "knowrob/storage/UnReificationContainer.h"
7 #include "knowrob/storage/reification.h"
8 #include "knowrob/semweb/rdf.h"
9 #include "knowrob/semweb/Property.h"
10 #include "knowrob/Logger.h"
11 
12 using namespace knowrob;
13 
15  return [this, it = triples_.begin()]() mutable -> const TriplePtr * {
16  if (it == triples_.end()) return nullptr;
17  auto &triple = it->second;
18  ++it;
19  return &triple;
20  };
21 }
22 
24  auto it = triples_.find(subject);
25  if (it == triples_.end()) {
26  auto [jt, success] = triples_.emplace(subject, new TripleView());
27  return jt->second;
28  }
29  return it->second;
30 }
31 
32 void UnReificationContainer::add(const Triple &triple) {
33  using PropertyHandler = std::function<void(const Triple &triple, TriplePtr &unReified)>;
34  static std::map<std::string_view, PropertyHandler> propertyHandler = {
35  {rdf::type->stringForm(), [](auto &triple, auto &unReified) {
36  unReified->setPredicate(semweb::Property::unReifiedIRI(triple.valueAsString())->stringForm());
37  }},
38  {reification::hasSubject->stringForm(), [](auto &triple, auto &unReified) {
40  unReified->setSubjectBlank(triple.valueAsString());
41  } else {
42  unReified->setSubject(triple.valueAsString());
43  }
44  }},
45  {reification::hasObject->stringForm(), [](auto &triple, auto &unReified) {
47  unReified->setObjectBlank(triple.valueAsString());
48  } else {
49  unReified->setObjectIRI(triple.valueAsString());
50  }
51  }},
52  {reification::hasLiteral->stringForm(), [](auto &triple, auto &unReified) {
53  unReified->setXSDValue(triple.valueAsString(), triple.xsdType().value());
54  }},
55  {reification::hasPerspective->stringForm(), [](auto &triple, auto &unReified) {
56  unReified->setPerspective(triple.valueAsString());
57  }},
58  {reification::isUncertain->stringForm(), [](auto &triple, auto &unReified) {
59  unReified->setIsUncertain(triple.valueAsBoolean());
60  }},
61  {reification::isOccasional->stringForm(), [](auto &triple, auto &unReified) {
62  unReified->setIsOccasional(triple.valueAsBoolean());
63  }},
64  {reification::hasConfidence->stringForm(), [](auto &triple, auto &unReified) {
65  unReified->setConfidence(triple.valueAsDouble());
66  }},
67  {reification::hasBeginTime->stringForm(), [](auto &triple, auto &unReified) {
68  unReified->setBegin(triple.valueAsDouble());
69  }},
70  {reification::hasEndTime->stringForm(), [](auto &triple, auto &unReified) {
71  unReified->setEnd(triple.valueAsDouble());
72  }}
73  };
74 
75  auto it = propertyHandler.find(triple.predicate());
76  if (it == propertyHandler.end()) {
77  KB_WARN("No property handler for {}.", triple.predicate());
78  return;
79  }
80  auto &handler = it->second;
81  handler(triple, getUnReifiedTriple(triple.subject()));
82 }
#define KB_WARN
Definition: Logger.h:27
std::function< const TriplePtr *()> ConstGenerator
virtual std::string_view valueAsString() const =0
auto xsdType() const
Definition: Triple.h:64
virtual void setSubject(std::string_view subject)=0
virtual double valueAsDouble() const =0
void setConfidence(double confidence)
Definition: Triple.h:307
virtual std::string_view subject() const =0
virtual void setPerspective(std::string_view perspective)=0
void setIsUncertain(bool isUncertain)
Definition: Triple.h:292
void setIsOccasional(bool isOccasional)
Definition: Triple.h:287
virtual std::string_view predicate() const =0
void setBegin(double begin)
Definition: Triple.h:297
void setEnd(double end)
Definition: Triple.h:302
virtual void setObjectIRI(std::string_view object)=0
virtual void setObjectBlank(std::string_view str)=0
virtual void setSubjectBlank(std::string_view str)=0
virtual void setPredicate(std::string_view predicate)=0
virtual bool valueAsBoolean() const =0
void setXSDValue(std::string_view v, XSDType type)
Definition: Triple.cpp:32
std::map< std::string_view, TriplePtr > triples_
ConstGenerator cgenerator() const override
TriplePtr & getUnReifiedTriple(std::string_view subject)
static knowrob::IRIAtomPtr unReifiedIRI(std::string_view iri)
Definition: Property.cpp:43
FunctionRule & function()
Definition: terms.cpp:140
const IRIAtomPtr type
Definition: rdf.h:15
const IRIAtomPtr hasSubject
Definition: reification.h:15
const IRIAtomPtr isUncertain
Definition: reification.h:18
const IRIAtomPtr hasConfidence
Definition: reification.h:21
const IRIAtomPtr isOccasional
Definition: reification.h:19
const IRIAtomPtr hasEndTime
Definition: reification.h:23
const IRIAtomPtr hasLiteral
Definition: reification.h:17
const IRIAtomPtr hasBeginTime
Definition: reification.h:22
const IRIAtomPtr hasPerspective
Definition: reification.h:20
const IRIAtomPtr hasObject
Definition: reification.h:16
RDFNodeType rdfNodeTypeGuess(std::string_view str)
Definition: RDFNode.cpp:11
TripleTemplate< std::string_view > TripleView
Definition: Triple.h:581