knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Pipeline.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_MONGO_PIPELINE_H
7 #define KNOWROB_MONGO_PIPELINE_H
8 
9 #include <mongoc.h>
10 #include <list>
11 #include <map>
12 #include <string_view>
13 #include "bson-helper.h"
14 #include "knowrob/semweb/GraphTerm.h"
15 #include "knowrob/semweb/TriplePattern.h"
16 #include "knowrob/semweb/GraphBuiltin.h"
17 #include "TripleStore.h"
18 #include "knowrob/semweb/GraphUnion.h"
19 
20 namespace knowrob::mongo {
24  class Pipeline {
25  public:
29  explicit Pipeline(bson_t *arrayDocument = nullptr);
30 
34  auto *arrayDocument() const { return arrayDocument_; }
35 
36  auto isNested() const { return isNested_; }
37 
39 
40  bson_t *appendStageBegin();
41 
42  bson_t *appendStageBegin(std::string_view stageOperator);
43 
44  void appendStageEnd(bson_t *stage);
45 
46  void append(const knowrob::GraphTerm &query, const TripleStore &tripleStore);
47 
48  void append(const knowrob::TriplePattern &query, const TripleStore &tripleStore);
49 
50  void appendBuiltin(const knowrob::GraphBuiltin &builtin);
51 
56  void limit(uint32_t maxDocuments);
57 
63  void unwind(std::string_view field, bool preserveNullAndEmptyArrays = false);
64 
69  void unset(std::string_view field);
70 
75  void project(std::string_view field);
76 
81  void project(const std::vector<std::string_view> &fields);
82 
87  void replaceRoot(std::string_view newRootField);
88 
93  void merge(std::string_view collection);
94 
99  void sortAscending(std::string_view field);
100 
105  void sortDescending(std::string_view field);
106 
112  void setUnion(std::string_view field, const std::vector<std::string_view> &sets);
113 
120  void addToArray(std::string_view key, std::string_view arrayKey, std::string_view elementKey);
121 
126  void matchEmptyArray(std::string_view arrayKey);
127 
134  static bson_t *loadFromJSON(std::string_view filename, const std::map<std::string, std::string> &parameters);
135 
136  protected:
137  bson_t *arrayDocument_;
138  uint32_t numStages_;
139  std::list<bson_wrapper> stages_;
140  std::list<bson_wrapper> stageOperators_;
141  bool isNested_ = false;
142 
143  bson_t *lastStage_;
144  bson_t *lastOperator_;
145 
146  void appendTerm_recursive(const knowrob::GraphTerm &query, const TripleStore &tripleStore,
147  std::set<std::string_view> &groundedVariables);
148 
149  void matchBinary(const knowrob::GraphBuiltin &builtin, std::string_view predicate);
150 
151  void setAccumulated(const knowrob::GraphBuiltin &builtin, std::string_view predicate);
152 
153  void bindValue(const knowrob::GraphBuiltin &builtin);
154 
155  void appendUnion(const knowrob::GraphUnion &unionTerm,
156  const TripleStore &tripleStore,
157  std::set<std::string_view> &groundedVariables);
158  };
159 
160 } // knowrob
161 
162 #endif //KNOWROB_MONGO_PIPELINE_H
bson_t * appendStageBegin(std::string_view stageOperator)
void setUnion(std::string_view field, const std::vector< std::string_view > &sets)
static bson_t * loadFromJSON(std::string_view filename, const std::map< std::string, std::string > &parameters)
void appendUnion(const knowrob::GraphUnion &unionTerm, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
void setIsNested(bool isNested)
Definition: Pipeline.h:38
void matchEmptyArray(std::string_view arrayKey)
Pipeline(bson_t *arrayDocument=nullptr)
auto isNested() const
Definition: Pipeline.h:36
void append(const knowrob::TriplePattern &query, const TripleStore &tripleStore)
void limit(uint32_t maxDocuments)
void sortDescending(std::string_view field)
void appendTerm_recursive(const knowrob::GraphTerm &query, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
auto * arrayDocument() const
Definition: Pipeline.h:34
void replaceRoot(std::string_view newRootField)
void matchBinary(const knowrob::GraphBuiltin &builtin, std::string_view predicate)
std::list< bson_wrapper > stageOperators_
Definition: Pipeline.h:140
void addToArray(std::string_view key, std::string_view arrayKey, std::string_view elementKey)
void sortAscending(std::string_view field)
std::list< bson_wrapper > stages_
Definition: Pipeline.h:139
void appendBuiltin(const knowrob::GraphBuiltin &builtin)
void unset(std::string_view field)
void setAccumulated(const knowrob::GraphBuiltin &builtin, std::string_view predicate)
void project(const std::vector< std::string_view > &fields)
void bindValue(const knowrob::GraphBuiltin &builtin)
void append(const knowrob::GraphTerm &query, const TripleStore &tripleStore)
void unwind(std::string_view field, bool preserveNullAndEmptyArrays=false)
void appendStageEnd(bson_t *stage)
void project(std::string_view field)
void merge(std::string_view collection)
PredicateRule & predicate()
Definition: formula.cpp:221