knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::transaction::Transaction Class Referenceabstract

#include <Transaction.h>

Inheritance diagram for knowrob::transaction::Transaction:

Public Member Functions

 Transaction (const std::shared_ptr< QueryableStorage > &queryable, const std::shared_ptr< Vocabulary > &vocabulary, bool isRemoval)
 
virtual ~Transaction ()=default
 
void addBackend (const std::shared_ptr< NamedBackend > &backend)
 
bool commit (const Triple &triple)
 
bool commit (const Triple &triple, const IRIAtomPtr &reifiedName)
 
bool commit (const TripleContainerPtr &triples)
 
bool commit (const TripleContainerPtr &triples, const ReifiedNames &reifiedNames)
 
 Transaction (const std::shared_ptr< QueryableStorage > &queryable, const std::shared_ptr< Vocabulary > &vocabulary, bool isRemoval)
 
virtual ~Transaction ()=default
 
void addBackend (const std::shared_ptr< NamedBackend > &backend)
 
bool commit (const Triple &triple)
 
bool commit (const Triple &triple, const IRIAtomPtr &reifiedName)
 
bool commit (const TripleContainerPtr &triples)
 
bool commit (const TripleContainerPtr &triples, const ReifiedNames &reifiedNames)
 

Protected Member Functions

bool commitProtected (const TripleContainerPtr &triple, const StoragePtr &backend)
 
virtual bool doCommit (const Triple &triple, const StoragePtr &backend)=0
 
virtual bool doCommit (const TripleContainerPtr &triples, const StoragePtr &backend)=0
 
virtual void updateVocabulary (const Triple &triple)=0
 
IRIAtomPtr queryReifiedName (const Triple &triple)
 
bool commitProtected (const TripleContainerPtr &triple, const StoragePtr &backend)
 
virtual bool doCommit (const Triple &triple, const StoragePtr &backend)=0
 
virtual bool doCommit (const TripleContainerPtr &triples, const StoragePtr &backend)=0
 
virtual void updateVocabulary (const Triple &triple)=0
 
IRIAtomPtr queryReifiedName (const Triple &triple)
 

Static Protected Member Functions

static std::shared_ptr< ThreadPool::RunnercreateTripleWorker (const TripleContainerPtr &triples, const std::function< void(const TriplePtr &)> &fn)
 
static std::shared_ptr< ThreadPool::RunnercreateTripleWorker (const TripleContainerPtr &triples, const std::function< void(const TriplePtr &)> &fn)
 

Protected Attributes

std::shared_ptr< Vocabularyvocabulary_
 
std::shared_ptr< QueryableStoragequeryable_
 
std::vector< std::shared_ptr< NamedBackend > > backends_
 
bool isRemoval_
 

Detailed Description

Baseclass for transactions. A set of backends can be added to a transaction. When a transaction is committed, the triple is committed to all backends. If a backend does not support the context of the triple, the triple is reified and the reified triples are committed to the backend.

Definition at line 23 of file Transaction.h.

Constructor & Destructor Documentation

◆ Transaction() [1/2]

knowrob::transaction::Transaction::Transaction ( const std::shared_ptr< QueryableStorage > &  queryable,
const std::shared_ptr< Vocabulary > &  vocabulary,
bool  isRemoval 
)
inline

Definition at line 25 of file Transaction.h.

28  : vocabulary_(vocabulary),
29  queryable_(queryable),
30  isRemoval_(isRemoval) {}
std::shared_ptr< Vocabulary > vocabulary_
Definition: Transaction.h:71
std::shared_ptr< QueryableStorage > queryable_
Definition: Transaction.h:72

◆ ~Transaction() [1/2]

virtual knowrob::transaction::Transaction::~Transaction ( )
virtualdefault

◆ Transaction() [2/2]

knowrob::transaction::Transaction::Transaction ( const std::shared_ptr< QueryableStorage > &  queryable,
const std::shared_ptr< Vocabulary > &  vocabulary,
bool  isRemoval 
)
inline

Definition at line 25 of file Transaction.h.

28  : vocabulary_(vocabulary),
29  queryable_(queryable),
30  isRemoval_(isRemoval) {}

◆ ~Transaction() [2/2]

virtual knowrob::transaction::Transaction::~Transaction ( )
virtualdefault

Member Function Documentation

◆ addBackend() [1/2]

void knowrob::transaction::Transaction::addBackend ( const std::shared_ptr< NamedBackend > &  backend)
inline

Adds a backend to the transaction.

Parameters
backendthe backend to add.

Definition at line 38 of file Transaction.h.

38 { backends_.push_back(backend); }
std::vector< std::shared_ptr< NamedBackend > > backends_
Definition: Transaction.h:73

◆ addBackend() [2/2]

void knowrob::transaction::Transaction::addBackend ( const std::shared_ptr< NamedBackend > &  backend)
inline

Adds a backend to the transaction.

Parameters
backendthe backend to add.

Definition at line 38 of file Transaction.h.

38 { backends_.push_back(backend); }

◆ commit() [1/8]

bool Transaction::commit ( const Triple triple)

Commits a triple to all backends.

Parameters
triplethe triple to commit.
Returns
true if the triple was committed to all backends, false otherwise.

Definition at line 58 of file Transaction.cpp.

58  {
59  static auto v_reification = std::make_shared<Variable>("reification");
60  if (isRemoval_ && ReifiedTriple::isReifiable(triple)) {
61  return commit(triple, queryReifiedName(triple));
62  } else {
63  return commit(triple, nullptr);
64  }
65 }
static bool isReifiable(const Triple &triple)
bool commit(const Triple &triple)
Definition: Transaction.cpp:58
IRIAtomPtr queryReifiedName(const Triple &triple)
Definition: Transaction.cpp:41

◆ commit() [2/8]

bool knowrob::transaction::Transaction::commit ( const Triple triple)

Commits a triple to all backends.

Parameters
triplethe triple to commit.
Returns
true if the triple was committed to all backends, false otherwise.

◆ commit() [3/8]

bool Transaction::commit ( const Triple triple,
const IRIAtomPtr reifiedName 
)

Commits a triple to all backends.

Parameters
triplethe triple to commit.
reifiedNamethe reified name of the triple.
Returns
true if the triple was committed to all backends, false otherwise.

Definition at line 67 of file Transaction.cpp.

67  {
68  ReifiedTriplePtr reification;
69  bool success = true;
70 
71  // make sure the vocabulary is updated before committing the triple as the vocabulary
72  // could be used within some backends.
73  updateVocabulary(triple);
74 
75  for (auto &definedBackend: backends_) {
76  auto &backend = definedBackend->value();
77  if (!backend->supports(StorageFeature::TripleContext) && ReifiedTriple::isReifiable(triple)) {
78  if (!reification) reification = std::make_shared<ReifiedTriple>(triple, vocabulary_, reifiedName);
79  for (auto &reified: *reification) {
80  success = success && doCommit(*reified.ptr, backend);
81  }
82  } else {
83  success = doCommit(triple, backend);
84  }
85  if (!success) break;
86  }
87  return success;
88 }
virtual void updateVocabulary(const Triple &triple)=0
virtual bool doCommit(const Triple &triple, const StoragePtr &backend)=0
std::shared_ptr< ReifiedTriple > ReifiedTriplePtr
Definition: ReifiedTriple.h:75

◆ commit() [4/8]

bool knowrob::transaction::Transaction::commit ( const Triple triple,
const IRIAtomPtr reifiedName 
)

Commits a triple to all backends.

Parameters
triplethe triple to commit.
reifiedNamethe reified name of the triple.
Returns
true if the triple was committed to all backends, false otherwise.

◆ commit() [5/8]

bool Transaction::commit ( const TripleContainerPtr triples)

Commits a set of triples to all backends.

Parameters
triplesthe triples to commit.
Returns
true if the triples were committed to all backends, false otherwise.

Definition at line 90 of file Transaction.cpp.

90  {
91  static auto v_reification = std::make_shared<Variable>("reification");
93  // FIXME: The name lookup only works in case the queryable backend does store reified triples.
94  // - current: commit generates a ReificationContainer over triples, and the container forms
95  // ground triples with explicit names of reified individuals for removal.
96  // (1) we could search for a queryable backend without context support and use this instead
97  // (2) we could store the reified names also in backends that support context, such they
98  // can be queried from any queryable backend.
99  // (3) remove for reified backends could use "removeAllMatching" instead, where the reified
100  // name is used as a variable in the query. but this interface was removed from DataBackend class!
101  // or allow none value as subject in removals, could be ok in the backends.
102  // Note: the container type does not provide a size method because it internally uses a generator
103  // without knowing when it will end. Also, container with additional filtering could be implemented.
104  // So we need to resize the reifiedNames vector while looping over the triples, but we can use
105  // the default batch size as initial size.
106  auto estimatedSize = GlobalSettings::batchSize();
107  ReifiedNames reifiedNames = std::make_shared<std::vector<IRIAtomPtr>>();
108  reifiedNames->reserve(estimatedSize);
109  for (auto &triple: *triples) {
110  if (ReifiedTriple::isReifiable(*triple)) {
111  reifiedNames->push_back(queryReifiedName(*triple));
112  } else {
113  reifiedNames->push_back(nullptr);
114  }
115  }
116  // If fewer elements were added, resize the vector
117  if (reifiedNames->size() < estimatedSize) {
118  reifiedNames->resize(reifiedNames->size());
119  }
120  return commit(triples, reifiedNames);
121  } else {
122  return commit(triples, {});
123  }
124 }
static uint32_t batchSize()
Definition: knowrob.h:68
std::shared_ptr< std::vector< IRIAtomPtr > > ReifiedNames

◆ commit() [6/8]

bool knowrob::transaction::Transaction::commit ( const TripleContainerPtr triples)

Commits a set of triples to all backends.

Parameters
triplesthe triples to commit.
Returns
true if the triples were committed to all backends, false otherwise.

◆ commit() [7/8]

bool Transaction::commit ( const TripleContainerPtr triples,
const ReifiedNames reifiedNames 
)

Commits a set of triples to all backends.

Parameters
triplesthe triples to commit.
reifiedNamesthe reified names of the triples.
Returns
true if the triples were committed to all backends, false otherwise.

Definition at line 126 of file Transaction.cpp.

126  {
127  TripleContainerPtr reified;
128  std::vector<std::shared_ptr<ThreadPool::Runner>> transactions;
129  bool success = true;
130 
131  // make sure the vocabulary is updated before committing the triples as the vocabulary
132  // could be used within some backends.
133  for (auto &triple: *triples) {
134  updateVocabulary(*triple);
135  }
136 
137  for (auto &definedBackend: backends_) {
138  auto &backend = definedBackend->value();
139  const TripleContainerPtr *backendTriples;
140  if (!backend->supports(StorageFeature::TripleContext)) {
141  if (!reified) reified = std::make_shared<ReificationContainer>(triples, vocabulary_, reifiedNames);
142  backendTriples = &reified;
143  } else {
144  backendTriples = &triples;
145  }
146  auto worker = std::make_shared<ThreadPool::LambdaRunner>(
147  [&](const std::function<bool()> &) { success = success && commitProtected(*backendTriples, backend); });
148  transactions.push_back(worker);
149 
150  DefaultThreadPool()->pushWork(worker,
151  [&definedBackend](const std::exception &exc) {
152  KB_ERROR("transaction failed for backend '{}': {}", definedBackend->name(),
153  exc.what());
154  });
155  }
156 
157  for (auto &transaction: transactions) transaction->join();
158 
159  return success;
160 }
#define KB_ERROR
Definition: Logger.h:28
bool commitProtected(const TripleContainerPtr &triple, const StoragePtr &backend)
FunctionRule & function()
Definition: terms.cpp:140
std::shared_ptr< TripleContainer > TripleContainerPtr
std::shared_ptr< ThreadPool > DefaultThreadPool()
Definition: ThreadPool.cpp:19

◆ commit() [8/8]

bool knowrob::transaction::Transaction::commit ( const TripleContainerPtr triples,
const ReifiedNames reifiedNames 
)

Commits a set of triples to all backends.

Parameters
triplesthe triples to commit.
reifiedNamesthe reified names of the triples.
Returns
true if the triples were committed to all backends, false otherwise.

◆ commitProtected() [1/2]

bool Transaction::commitProtected ( const TripleContainerPtr triple,
const StoragePtr backend 
)
protected

Definition at line 162 of file Transaction.cpp.

162  {
163  if (backend->storageLanguage() == PluginLanguage::PYTHON) {
164  py::gil_lock lock;
165  return doCommit(triples, backend);
166  } else {
167  return doCommit(triples, backend);
168  }
169 }

◆ commitProtected() [2/2]

bool knowrob::transaction::Transaction::commitProtected ( const TripleContainerPtr triple,
const StoragePtr backend 
)
protected

◆ createTripleWorker() [1/2]

std::shared_ptr< ThreadPool::Runner > Transaction::createTripleWorker ( const TripleContainerPtr triples,
const std::function< void(const TriplePtr &)> &  fn 
)
staticprotected

Definition at line 171 of file Transaction.cpp.

173  {
174  auto perTripleWorker =
175  std::make_shared<ThreadPool::LambdaRunner>([fn, triples](const ThreadPool::LambdaRunner::StopChecker &) {
176  std::for_each(triples->begin(), triples->end(), fn);
177  });
178  DefaultThreadPool()->pushWork(perTripleWorker,
179  [](const std::exception &exc) {
180  KB_ERROR("failed to perform per triple work: {}", exc.what());
181  });
182  return perTripleWorker;
183 }
std::function< bool()> StopChecker
Definition: ThreadPool.h:152

◆ createTripleWorker() [2/2]

static std::shared_ptr<ThreadPool::Runner> knowrob::transaction::Transaction::createTripleWorker ( const TripleContainerPtr triples,
const std::function< void(const TriplePtr &)> &  fn 
)
staticprotected

◆ doCommit() [1/4]

virtual bool knowrob::transaction::Transaction::doCommit ( const Triple triple,
const StoragePtr backend 
)
protectedpure virtual

◆ doCommit() [2/4]

virtual bool knowrob::transaction::Transaction::doCommit ( const Triple triple,
const StoragePtr backend 
)
protectedpure virtual

◆ doCommit() [3/4]

virtual bool knowrob::transaction::Transaction::doCommit ( const TripleContainerPtr triples,
const StoragePtr backend 
)
protectedpure virtual

◆ doCommit() [4/4]

virtual bool knowrob::transaction::Transaction::doCommit ( const TripleContainerPtr triples,
const StoragePtr backend 
)
protectedpure virtual

◆ queryReifiedName() [1/2]

IRIAtomPtr Transaction::queryReifiedName ( const Triple triple)
protected

Definition at line 41 of file Transaction.cpp.

41  {
42  static auto v_reification = std::make_shared<Variable>("reification");
43  auto pat = std::make_shared<TriplePattern>(triple);
44  auto query = std::make_shared<GraphPathQuery>(pat);
45  auto reified = std::make_shared<ReifiedQuery>(query, vocabulary_);
46  setReificationVariable(reified->term(), v_reification);
47 
48  IRIAtomPtr reifiedName;
49  queryable_->query(reified, [&](const BindingsPtr &bindings) {
50  auto t_reifiedName = bindings->get(v_reification->name());
51  if (t_reifiedName && t_reifiedName->isIRI()) {
52  reifiedName = IRIAtom::Tabled(std::static_pointer_cast<IRIAtom>(t_reifiedName)->stringForm());
53  }
54  });
55  return reifiedName;
56 }
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151
std::shared_ptr< IRIAtom > IRIAtomPtr
Definition: IRIAtom.h:57

◆ queryReifiedName() [2/2]

IRIAtomPtr knowrob::transaction::Transaction::queryReifiedName ( const Triple triple)
protected

◆ updateVocabulary() [1/2]

virtual void knowrob::transaction::Transaction::updateVocabulary ( const Triple triple)
protectedpure virtual

◆ updateVocabulary() [2/2]

virtual void knowrob::transaction::Transaction::updateVocabulary ( const Triple triple)
protectedpure virtual

Member Data Documentation

◆ backends_

std::vector< std::shared_ptr< NamedBackend > > knowrob::transaction::Transaction::backends_
protected

Definition at line 73 of file Transaction.h.

◆ isRemoval_

bool knowrob::transaction::Transaction::isRemoval_
protected

Definition at line 74 of file Transaction.h.

◆ queryable_

std::shared_ptr< QueryableStorage > knowrob::transaction::Transaction::queryable_
protected

Definition at line 72 of file Transaction.h.

◆ vocabulary_

std::shared_ptr< Vocabulary > knowrob::transaction::Transaction::vocabulary_
protected

Definition at line 71 of file Transaction.h.


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