knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Transaction.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_TRANSACTION_H
7 #define KNOWROB_TRANSACTION_H
8 
9 #include "memory"
10 #include "knowrob/ThreadPool.h"
11 #include "knowrob/storage/Storage.h"
12 #include "knowrob/semweb/TripleContainer.h"
13 #include "QueryableStorage.h"
14 #include "knowrob/storage/ReificationContainer.h"
15 
16 namespace knowrob::transaction {
23  class Transaction {
24  public:
25  Transaction(const std::shared_ptr<QueryableStorage> &queryable,
26  const std::shared_ptr<Vocabulary> &vocabulary,
27  bool isRemoval)
28  : vocabulary_(vocabulary),
29  queryable_(queryable),
30  isRemoval_(isRemoval) {}
31 
32  virtual ~Transaction() = default;
33 
38  void addBackend(const std::shared_ptr<NamedBackend> &backend) { backends_.push_back(backend); }
39 
45  bool commit(const Triple &triple);
46 
53  bool commit(const Triple &triple, const IRIAtomPtr &reifiedName);
54 
60  bool commit(const TripleContainerPtr &triples);
61 
68  bool commit(const TripleContainerPtr &triples, const ReifiedNames &reifiedNames);
69 
70  protected:
71  std::shared_ptr<Vocabulary> vocabulary_;
72  std::shared_ptr<QueryableStorage> queryable_;
73  std::vector<std::shared_ptr<NamedBackend>> backends_;
74  bool isRemoval_;
75 
76  bool commitProtected(const TripleContainerPtr &triple, const StoragePtr &backend);
77 
78  virtual bool doCommit(const Triple &triple, const StoragePtr &backend) = 0;
79 
80  virtual bool doCommit(const TripleContainerPtr &triples, const StoragePtr &backend) = 0;
81 
82  virtual void updateVocabulary(const Triple &triple) = 0;
83 
84  static std::shared_ptr<ThreadPool::Runner> createTripleWorker(
85  const TripleContainerPtr &triples,
86  const std::function<void(const TriplePtr &)> &fn);
87 
89  };
90 
94  class Insert : public Transaction {
95  public:
96  Insert(const std::shared_ptr<QueryableStorage> &queryable, const std::shared_ptr<Vocabulary> &vocabulary)
97  : Transaction(queryable, vocabulary, false) {}
98 
99  protected:
100  bool doCommit(const Triple &triple, const StoragePtr &backend) override;
101 
102  bool doCommit(const TripleContainerPtr &triples, const StoragePtr &backend) override;
103 
104  void updateVocabulary(const Triple &triple) override;
105  };
106 
110  class Remove : public Transaction {
111  public:
112  Remove(const std::shared_ptr<QueryableStorage> &queryable, const std::shared_ptr<Vocabulary> &vocabulary)
113  : Transaction(queryable, vocabulary, true) {}
114 
115  protected:
116  bool doCommit(const Triple &triple, const StoragePtr &backend) override;
117 
118  bool doCommit(const TripleContainerPtr &triples, const StoragePtr &backend) override;
119 
120  void updateVocabulary(const Triple &triple) override;
121  };
122 }
123 
124 #endif //KNOWROB_TRANSACTION_H
bool doCommit(const Triple &triple, const StoragePtr &backend) override
bool doCommit(const TripleContainerPtr &triples, const StoragePtr &backend) override
void updateVocabulary(const Triple &triple) override
Insert(const std::shared_ptr< QueryableStorage > &queryable, const std::shared_ptr< Vocabulary > &vocabulary)
Definition: Transaction.h:96
bool doCommit(const TripleContainerPtr &triples, const StoragePtr &backend) override
void updateVocabulary(const Triple &triple) override
bool doCommit(const Triple &triple, const StoragePtr &backend) override
Remove(const std::shared_ptr< QueryableStorage > &queryable, const std::shared_ptr< Vocabulary > &vocabulary)
Definition: Transaction.h:112
void addBackend(const std::shared_ptr< NamedBackend > &backend)
Definition: Transaction.h:38
bool commit(const TripleContainerPtr &triples)
std::shared_ptr< Vocabulary > vocabulary_
Definition: Transaction.h:71
bool commitProtected(const TripleContainerPtr &triple, const StoragePtr &backend)
IRIAtomPtr queryReifiedName(const Triple &triple)
std::shared_ptr< QueryableStorage > queryable_
Definition: Transaction.h:72
bool commit(const TripleContainerPtr &triples, const ReifiedNames &reifiedNames)
Transaction(const std::shared_ptr< QueryableStorage > &queryable, const std::shared_ptr< Vocabulary > &vocabulary, bool isRemoval)
Definition: Transaction.h:25
static std::shared_ptr< ThreadPool::Runner > createTripleWorker(const TripleContainerPtr &triples, const std::function< void(const TriplePtr &)> &fn)
virtual bool doCommit(const TripleContainerPtr &triples, const StoragePtr &backend)=0
std::vector< std::shared_ptr< NamedBackend > > backends_
Definition: Transaction.h:73
virtual void updateVocabulary(const Triple &triple)=0
bool commit(const Triple &triple)
virtual bool doCommit(const Triple &triple, const StoragePtr &backend)=0
bool commit(const Triple &triple, const IRIAtomPtr &reifiedName)
FunctionRule & function()
Definition: terms.cpp:140
std::shared_ptr< TripleContainer > TripleContainerPtr
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57
std::shared_ptr< std::vector< IRIAtomPtr > > ReifiedNames
std::shared_ptr< Storage > StoragePtr
Definition: Storage.h:154