knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::mongo::MongoTriplePattern Class Reference

#include <MongoTriplePattern.h>

Collaboration diagram for knowrob::mongo::MongoTriplePattern:

Public Member Functions

 MongoTriplePattern (const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
auto & document ()
 
auto bson ()
 
 MongoTriplePattern (const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
auto & document ()
 
auto bson ()
 

Static Public Member Functions

static void append (bson_t *selectorDoc, const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
static void appendGraphSelector (bson_t *selectorDoc, const TriplePattern &tripleExpression, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
static void appendEpistemicSelector (bson_t *selectorDoc, const TriplePattern &tripleExpression)
 
static void appendTimeSelector (bson_t *selectorDoc, const TriplePattern &tripleExpression)
 
static void setTripleVariables (Pipeline &pipeline, const TriplePattern &expr, const std::set< std::string_view > &knownGroundedVariables)
 
static void append (bson_t *selectorDoc, const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
static void appendGraphSelector (bson_t *selectorDoc, const TriplePattern &tripleExpression, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
static void appendEpistemicSelector (bson_t *selectorDoc, const TriplePattern &tripleExpression)
 
static void appendTimeSelector (bson_t *selectorDoc, const TriplePattern &tripleExpression)
 
static void setTripleVariables (Pipeline &pipeline, const TriplePattern &expr, const std::set< std::string_view > &knownGroundedVariables)
 

Static Protected Member Functions

static bson_t * create (const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
static const char * getOperatorString (knowrob::FilterType operatorType)
 
static bson_t * create (const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)
 
static const char * getOperatorString (knowrob::FilterType operatorType)
 

Protected Attributes

mongo::Document document_
 

Detailed Description

A class to represent a triple pattern in a MongoDB.

Definition at line 20 of file MongoTriplePattern.h.

Constructor & Destructor Documentation

◆ MongoTriplePattern() [1/2]

MongoTriplePattern::MongoTriplePattern ( const TriplePattern tripleExpression,
bool  b_isTaxonomicProperty,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)

Definition at line 19 of file MongoTriplePattern.cpp.

23  : document_(create(tripleExpression, b_isTaxonomicProperty, importHierarchy)) {
24 }
static bson_t * create(const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)

◆ MongoTriplePattern() [2/2]

knowrob::mongo::MongoTriplePattern::MongoTriplePattern ( const TriplePattern tripleExpression,
bool  b_isTaxonomicProperty,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)

Member Function Documentation

◆ append() [1/2]

void MongoTriplePattern::append ( bson_t *  selectorDoc,
const TriplePattern tripleExpression,
bool  b_isTaxonomicProperty,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)
static

Definition at line 35 of file MongoTriplePattern.cpp.

38  {
39  // "s"" field
40  MongoTerm::append(selectorDoc,
41  "s", tripleExpression.subjectTerm());
42  // "p" field
43  MongoTerm::append(selectorDoc,
44  (b_isTaxonomicProperty ? "p" : "p*"),
45  tripleExpression.propertyTerm());
46  // "o" field
47  const char *objectOperator = getOperatorString(tripleExpression.objectOperator());
48  MongoTerm::append(selectorDoc,
49  (b_isTaxonomicProperty ? "o*" : "o"),
50  tripleExpression.objectTerm(),
51  objectOperator);
52  // "g" field
53  appendGraphSelector(selectorDoc, tripleExpression, importHierarchy);
54  // epistemic fields
55  appendEpistemicSelector(selectorDoc, tripleExpression);
56  // temporal fields
57  appendTimeSelector(selectorDoc, tripleExpression);
58 }
auto objectOperator() const
auto & propertyTerm() const
Definition: TriplePattern.h:92
auto & subjectTerm() const
Definition: TriplePattern.h:81
auto & objectTerm() const
Definition: TriplePattern.h:97
static void append(bson_t *doc, const char *key, const TermPtr &term, const char *queryOperator=nullptr, bool matchNullValue=false, bool includeVariables=false)
Definition: MongoTerm.cpp:13
static const char * getOperatorString(knowrob::FilterType operatorType)
static void appendEpistemicSelector(bson_t *selectorDoc, const TriplePattern &tripleExpression)
static void appendTimeSelector(bson_t *selectorDoc, const TriplePattern &tripleExpression)
static void appendGraphSelector(bson_t *selectorDoc, const TriplePattern &tripleExpression, const std::shared_ptr< ImportHierarchy > &importHierarchy)

◆ append() [2/2]

static void knowrob::mongo::MongoTriplePattern::append ( bson_t *  selectorDoc,
const TriplePattern tripleExpression,
bool  b_isTaxonomicProperty,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)
static

◆ appendEpistemicSelector() [1/2]

void MongoTriplePattern::appendEpistemicSelector ( bson_t *  selectorDoc,
const TriplePattern tripleExpression 
)
static

Definition at line 97 of file MongoTriplePattern.cpp.

97  {
98  auto &ct = tripleExpression.confidenceTerm();
99  auto &at = tripleExpression.perspectiveTerm();
100  auto &u = tripleExpression.isUncertainTerm();
101 
102  if (!((u.has_grounding() && u.grounded()->asBoolean()) || u.has_variable())) {
103  // note: null value of "uncertain" field is seen as value "false"
105  selectorDoc,
106  "uncertain",
108  nullptr,
109  true);
110  }
111 
112  if (at) {
113  if (at->termType() == TermType::ATOMIC) {
115  selectorDoc,
116  "agent",
117  *at,
118  nullptr,
119  false);
120  } else {
121  KB_WARN("agent term {} has unexpected type", *at);
122  }
123  } else {
124  // make sure agent field is undefined: { agent: { $exists: false } }
125  // note: null value of agent field is seen as "self", i.e. the agent running the knowledge base
126  bson_t agentDoc;
127  BSON_APPEND_DOCUMENT_BEGIN(selectorDoc, "agent", &agentDoc);
128  BSON_APPEND_BOOL(&agentDoc, "$exists", false);
129  bson_append_document_end(selectorDoc, &agentDoc);
130  }
131 
132  if (ct) {
133  if (ct->termType() == TermType::ATOMIC) {
134  // note: null value of confidence is seen as larger than the requested threshold
136  selectorDoc,
137  "confidence",
138  *ct,
140  true);
141  } else {
142  KB_WARN("confidence term {} has unexpected type", *ct);
143  }
144  }
145 }
#define MONGO_OPERATOR_GTE
#define KB_WARN
Definition: Logger.h:27
static std::shared_ptr< Numeric > falseAtom()
Definition: Numeric.cpp:20
auto & isUncertainTerm() const
auto & perspectiveTerm() const
auto & confidenceTerm() const

◆ appendEpistemicSelector() [2/2]

static void knowrob::mongo::MongoTriplePattern::appendEpistemicSelector ( bson_t *  selectorDoc,
const TriplePattern tripleExpression 
)
static

◆ appendGraphSelector() [1/2]

void MongoTriplePattern::appendGraphSelector ( bson_t *  selectorDoc,
const TriplePattern tripleExpression,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)
static

Definition at line 60 of file MongoTriplePattern.cpp.

62  {
63  auto &gt = tripleExpression.graphTerm();
64  if (!gt) return;
65 
66  if (gt->termType() == TermType::ATOMIC) {
67  auto graphString = gt.grounded();
68  if (graphString->stringForm() == ImportHierarchy::ORIGIN_ANY || graphString->stringForm() == "*")
69  return;
70 
71  std::vector<TermPtr> childrenNames;
72  for (auto &child: importHierarchy->getImports(graphString->stringForm())) {
73  childrenNames.push_back(Atom::Tabled(child->name()));
74  }
75  if (childrenNames.empty()) {
76  MongoTerm::append(selectorDoc, "graph", graphString);
77  } else {
78  childrenNames.push_back(graphString);
79  MongoTerm::append(selectorDoc, "graph", childrenNames, "$in");
80  }
81  }
82  /*
83  else if (gt->termType() == TermType::FUNCTION) {
84  auto fn = (Function*) gt.get();
85  if (*fn->functor() == *ListTerm::listFunctor()) {
86  aggregation::appendArrayQuery(selectorDoc, "graph", fn->arguments(), "$in");
87  } else {
88  KB_WARN("graph term {} has unexpected function type", *gt);
89  }
90  }
91  */
92  else {
93  KB_WARN("graph term {} has unexpected type", *gt);
94  }
95 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
static constexpr std::string_view ORIGIN_ANY
auto & graphTerm() const

◆ appendGraphSelector() [2/2]

static void knowrob::mongo::MongoTriplePattern::appendGraphSelector ( bson_t *  selectorDoc,
const TriplePattern tripleExpression,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)
static

◆ appendTimeSelector() [1/2]

void MongoTriplePattern::appendTimeSelector ( bson_t *  selectorDoc,
const TriplePattern tripleExpression 
)
static

Definition at line 147 of file MongoTriplePattern.cpp.

147  {
148  static const bool allowNullValues = true;
149  static auto b_occasional = std::make_shared<Integer>(static_cast<int32_t>(true));
150  static auto b_always = std::make_shared<Integer>(static_cast<int32_t>(false));
151  auto bt = &tripleExpression.beginTerm();
152  auto et = &tripleExpression.endTerm();
153  auto &o = tripleExpression.isOccasionalTerm();
154 
155  if (!((o.has_grounding() && o.grounded()->asBoolean()) || o.has_variable())) {
156  // note: null value of "occasional" field is seen as value "false"
158  selectorDoc,
159  "occasional",
161  nullptr,
162  true);
163  } else {
164  auto swap = bt;
165  bt = et;
166  et = swap;
167  }
168 
169  if (bt->has_grounding()) {
171  selectorDoc,
172  "scope.time.since",
173  bt->get(),
175  allowNullValues);
176  }
177  if (et->has_grounding()) {
179  selectorDoc,
180  "scope.time.until",
181  et->get(),
183  allowNullValues);
184  }
185 }
#define MONGO_OPERATOR_LTE
auto & isOccasionalTerm() const
auto & beginTerm() const
auto & endTerm() const

◆ appendTimeSelector() [2/2]

static void knowrob::mongo::MongoTriplePattern::appendTimeSelector ( bson_t *  selectorDoc,
const TriplePattern tripleExpression 
)
static

◆ bson() [1/2]

auto knowrob::mongo::MongoTriplePattern::bson ( )
inline

Definition at line 29 of file MongoTriplePattern.h.

29 { return document_.bson(); }
bson_t * bson() const
Definition: Document.h:27

◆ bson() [2/2]

auto knowrob::mongo::MongoTriplePattern::bson ( )
inline

Definition at line 29 of file MongoTriplePattern.h.

29 { return document_.bson(); }

◆ create() [1/2]

bson_t * MongoTriplePattern::create ( const TriplePattern tripleExpression,
bool  b_isTaxonomicProperty,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)
staticprotected

Definition at line 26 of file MongoTriplePattern.cpp.

29  {
30  auto selectorDoc = bson_new();
31  append(selectorDoc, tripleExpression, b_isTaxonomicProperty, importHierarchy);
32  return selectorDoc;
33 }
static void append(bson_t *selectorDoc, const TriplePattern &tripleExpression, bool b_isTaxonomicProperty, const std::shared_ptr< ImportHierarchy > &importHierarchy)

◆ create() [2/2]

static bson_t* knowrob::mongo::MongoTriplePattern::create ( const TriplePattern tripleExpression,
bool  b_isTaxonomicProperty,
const std::shared_ptr< ImportHierarchy > &  importHierarchy 
)
staticprotected

◆ document() [1/2]

auto& knowrob::mongo::MongoTriplePattern::document ( )
inline

Definition at line 27 of file MongoTriplePattern.h.

27 { return document_; }

◆ document() [2/2]

auto& knowrob::mongo::MongoTriplePattern::document ( )
inline

Definition at line 27 of file MongoTriplePattern.h.

27 { return document_; }

◆ getOperatorString() [1/2]

const char * MongoTriplePattern::getOperatorString ( knowrob::FilterType  operatorType)
staticprotected

Definition at line 187 of file MongoTriplePattern.cpp.

187  {
188  switch (operatorType) {
189  case FilterType::EQ:
190  return nullptr;
191  case FilterType::NEQ:
192  return MONGO_OPERATOR_NEQ;
193  case FilterType::LEQ:
194  return MONGO_OPERATOR_LTE;
195  case FilterType::GEQ:
196  return MONGO_OPERATOR_GTE;
197  case FilterType::LT:
198  return MONGO_OPERATOR_LT;
199  case FilterType::GT:
200  return MONGO_OPERATOR_GT;
201  }
202  return nullptr;
203 }
#define MONGO_OPERATOR_LT
#define MONGO_OPERATOR_NEQ
#define MONGO_OPERATOR_GT

◆ getOperatorString() [2/2]

static const char* knowrob::mongo::MongoTriplePattern::getOperatorString ( knowrob::FilterType  operatorType)
staticprotected

◆ setTripleVariables() [1/2]

void MongoTriplePattern::setTripleVariables ( Pipeline pipeline,
const TriplePattern expr,
const std::set< std::string_view > &  knownGroundedVariables 
)
static

Definition at line 269 of file MongoTriplePattern.cpp.

271  {
272  std::list<std::pair<const char *, Variable *>> varList;
273 
274  // the object can be specified as a triple (Value, Operator, ActualValue) such that
275  // a value constraint is given plus a variable where the actual value of the triple should be stored.
276  TermPtr objectVar = expr.objectTerm();
277  if (objectVar && !objectVar->isVariable() && expr.objectVariable()) {
278  objectVar = expr.objectVariable();
279  }
280 
281  for (auto &it: {
282  std::make_pair("$next.s", expr.subjectTerm()),
283  std::make_pair("$next.p", expr.propertyTerm()),
284  std::make_pair("$next.o", objectVar),
285  std::make_pair("$next.graph", expr.graphTerm().get()),
286  std::make_pair("$next.confidence", expr.confidenceTerm().get()),
287  std::make_pair("$next.agent", expr.perspectiveTerm().get()),
288  std::make_pair("$next.scope.time.since", expr.beginTerm().get()),
289  std::make_pair("$next.scope.time.until", expr.endTerm().get()),
290  std::make_pair("$next.uncertain", expr.isUncertainTerm().get()),
291  std::make_pair("$next.occasional", expr.isOccasionalTerm().get())
292  }) {
293  if (!it.second || it.second->termType() != TermType::VARIABLE) continue;
294  auto var = (Variable *) it.second.get();
295  // skip variables that were instantiated in previous steps
296  if (knownGroundedVariables.count(var->name()) > 0) continue;
297  varList.emplace_back(it.first, var);
298  }
299 
300  if (!varList.empty()) {
301  auto setVariables = pipeline.appendStageBegin("$set");
302  for (auto &it: varList) {
303  appendSetVariable(setVariables, it.second->name(), it.first);
304  }
305  pipeline.appendStageEnd(setVariables);
306  }
307 }
auto & objectVariable() const
void appendStageEnd(bson_t *stage)
Definition: Pipeline.cpp:46
bson_t * appendStageBegin()
Definition: Pipeline.cpp:27
VariableRule & var()
Definition: terms.cpp:91
std::shared_ptr< Term > TermPtr
Definition: Term.h:117

◆ setTripleVariables() [2/2]

static void knowrob::mongo::MongoTriplePattern::setTripleVariables ( Pipeline pipeline,
const TriplePattern expr,
const std::set< std::string_view > &  knownGroundedVariables 
)
static

Member Data Documentation

◆ document_

mongo::Document knowrob::mongo::MongoTriplePattern::document_
protected

Definition at line 55 of file MongoTriplePattern.h.


The documentation for this class was generated from the following files: