6 #include "knowrob/semweb/GraphRestructuring.h"
7 #include "knowrob/semweb/SPARQLQuery.h"
8 #include "knowrob/queries/QueryParser.h"
13 #define GRAPH_RESTRUCTURING_SETTING_FILE "file"
21 rules_.push_back(std::move(rule));
30 }
catch (
const std::exception &e) {
31 KB_WARN(
"Error reading renaming from file {}: {}", *o_user, e.what());
39 std::ifstream file(filename);
42 while (std::getline(file, line,
'.')) {
44 if (file.peek() !=
'\n')
continue;
47 std::stringstream ss(entry);
51 std::getline(ss, part1,
'<');
53 std::getline(ss, part2);
54 if (part1.size() < 5 || part2.size() < 5) {
55 KB_WARN(
"Error reading transformation from file {}: Invalid Syntax.", filename);
60 part1 = part1.substr(1, part1.size() - 2);
61 part2 = part2.substr(1, part2.size() - 2);
63 std::vector<TriplePatternPtr> x, y;
64 std::stringstream ss1(part1), ss2(part2);
67 while (std::getline(ss1, item,
',')) {
72 KB_WARN(
"Error reading transformation from file {}: Invalid triple pattern \"{}\".", filename, item);
76 while (std::getline(ss2, item,
',')) {
81 KB_WARN(
"Error reading transformation from file {}: Invalid triple pattern \"{}\".", filename, item);
85 addRule(std::make_shared<GraphTransformationRule>(x, y));
95 if (!p)
return nullptr;
98 return std::make_shared<TriplePattern>(p_rdf);
99 }
catch (
const std::exception &e) {
100 KB_WARN(
"Error reading transformation from file: {}", e.what());
106 model_ = std::make_unique<RedlandModel>();
113 for (
auto &rule:
rules_) {
126 model_->insertAll(triples);
132 auto originals = std::make_shared<TriplePatternContainer>();
133 auto transformed = std::make_shared<TriplePatternContainer>();
137 for (auto &p: rule.to()) {
138 auto x = applyBindings(p, *bindings);
139 x->setGraphName(origin_);
140 transformed->push_back(x);
143 for (
auto &p: rule.
from()) {
144 originals->push_back(applyBindings(p, *bindings));
148 model_->removeAll(originals);
149 model_->insertAll(transformed);
#define GRAPH_RESTRUCTURING_SETTING_FILE
void finalizeTransformation() override
void doTransformation(GraphTransformationRule &rule)
void pushInputTriples(const TripleContainerPtr &triples) override
std::unique_ptr< RedlandModel > model_
void initializeTransformation() override
bool configure(const boost::property_tree::ptree &opts) override
static TriplePatternPtr readTriplePattern(const std::string &filename)
std::vector< std::shared_ptr< GraphTransformationRule > > rules_
void addRule(std::shared_ptr< GraphTransformationRule > rule)
bool readFromFile(const std::string &filename)
static PredicatePtr parsePredicate(const std::string &queryString)
static std::shared_ptr< Predicate > getRDFPredicate(const PredicatePtr &predicate)
std::shared_ptr< TriplePattern > TriplePatternPtr
std::shared_ptr< TripleContainer > TripleContainerPtr
std::shared_ptr< const Bindings > BindingsPtr