6 #include "knowrob/storage/Transaction.h"
7 #include "knowrob/storage/ReifiedTriple.h"
8 #include "knowrob/storage/ReificationContainer.h"
9 #include "knowrob/semweb/rdfs.h"
10 #include "knowrob/semweb/owl.h"
11 #include "knowrob/semweb/rdf.h"
12 #include "knowrob/storage/ReifiedQuery.h"
13 #include "knowrob/knowrob.h"
14 #include "knowrob/integration/python/gil.h"
19 static void setReificationVariable(
20 const std::shared_ptr<GraphTerm> &t,
22 switch (t->termType()) {
24 auto &
pattern = std::static_pointer_cast<GraphPattern>(t)->value();
25 pattern->setSubjectTerm(variable);
30 auto connective = std::static_pointer_cast<GraphConnective>(t);
31 for (
auto &
term: connective->terms()) {
32 setReificationVariable(
term, variable);
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);
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());
59 static auto v_reification = std::make_shared<Variable>(
"reification");
63 return commit(triple,
nullptr);
76 auto &backend = definedBackend->value();
78 if (!reification) reification = std::make_shared<ReifiedTriple>(triple,
vocabulary_, reifiedName);
79 for (
auto &reified: *reification) {
80 success = success &&
doCommit(*reified.ptr, backend);
91 static auto v_reification = std::make_shared<Variable>(
"reification");
107 ReifiedNames reifiedNames = std::make_shared<std::vector<IRIAtomPtr>>();
108 reifiedNames->reserve(estimatedSize);
109 for (
auto &triple: *triples) {
113 reifiedNames->push_back(
nullptr);
117 if (reifiedNames->size() < estimatedSize) {
118 reifiedNames->resize(reifiedNames->size());
120 return commit(triples, reifiedNames);
122 return commit(triples, {});
128 std::vector<std::shared_ptr<ThreadPool::Runner>> transactions;
133 for (
auto &triple: *triples) {
138 auto &backend = definedBackend->value();
141 if (!reified) reified = std::make_shared<ReificationContainer>(triples,
vocabulary_, reifiedNames);
142 backendTriples = &reified;
144 backendTriples = &triples;
146 auto worker = std::make_shared<ThreadPool::LambdaRunner>(
148 transactions.push_back(worker);
151 [&definedBackend](
const std::exception &exc) {
152 KB_ERROR(
"transaction failed for backend '{}': {}", definedBackend->name(),
157 for (
auto &transaction: transactions) transaction->join();
174 auto perTripleWorker =
176 std::for_each(triples->begin(), triples->end(), fn);
179 [](
const std::exception &exc) {
180 KB_ERROR(
"failed to perform per triple work: {}", exc.what());
182 return perTripleWorker;
186 return backend->insertOne(triple);
190 return backend->removeOne(triple);
194 return backend->insertAll(triples);
198 return backend->removeAll(triples);
206 sub->addDirectParent(sup, triple.
graph());
211 sub->addDirectParent(sup, triple.
graph());
216 static std::set<std::string_view> skippedTypes = {
240 if (triple.
graph()) {
241 vocabulary_->importHierarchy()->addDirectImport(triple.
graph().value(), importedGraph);
243 KB_WARN(
"import statement without graph");
257 sub->removeDirectParent(sup, triple.
graph());
261 sub->removeDirectParent(sup, triple.
graph());
static std::string getNameFromURI(const std::string &uriString)
static uint32_t batchSize()
static bool isReifiable(const Triple &triple)
std::function< bool()> StopChecker
virtual std::string_view valueAsString() const =0
virtual std::optional< std::string_view > graph() const =0
virtual std::string_view subject() const =0
virtual std::string_view predicate() const =0
static std::string resolve(const std::string_view &uriString)
bool doCommit(const Triple &triple, const StoragePtr &backend) override
void updateVocabulary(const Triple &triple) override
void updateVocabulary(const Triple &triple) override
bool doCommit(const Triple &triple, const StoragePtr &backend) override
std::shared_ptr< Vocabulary > vocabulary_
bool commit(const Triple &triple)
IRIAtomPtr queryReifiedName(const Triple &triple)
bool commitProtected(const TripleContainerPtr &triple, const StoragePtr &backend)
std::shared_ptr< QueryableStorage > queryable_
std::vector< std::shared_ptr< NamedBackend > > backends_
static std::shared_ptr< ThreadPool::Runner > createTripleWorker(const TripleContainerPtr &triples, const std::function< void(const TriplePtr &)> &fn)
virtual void updateVocabulary(const Triple &triple)=0
virtual bool doCommit(const Triple &triple, const StoragePtr &backend)=0
const IRIAtomPtr inverseOf
const IRIAtomPtr ObjectProperty
const IRIAtomPtr NamedIndividual
const IRIAtomPtr Restriction
const IRIAtomPtr AnnotationProperty
const IRIAtomPtr DatatypeProperty
GraphTermRule & pattern()
FunctionRule & function()
const IRIAtomPtr Property
const IRIAtomPtr subPropertyOf
const IRIAtomPtr subClassOf
std::shared_ptr< TripleContainer > TripleContainerPtr
bool isSubClassOfIRI(std::string_view iri)
std::shared_ptr< ReifiedTriple > ReifiedTriplePtr
std::shared_ptr< const Bindings > BindingsPtr
bool isTypeIRI(std::string_view iri)
std::shared_ptr< ThreadPool > DefaultThreadPool()
bool isSubPropertyOfIRI(std::string_view iri)
std::shared_ptr< IRIAtom > IRIAtomPtr
bool isInverseOfIRI(std::string_view iri)
std::shared_ptr< std::vector< IRIAtomPtr > > ReifiedNames
std::shared_ptr< Variable > VariablePtr
std::shared_ptr< Storage > StoragePtr