1#include <knowCore/ConstrainedValue.h>
2#include <knowCore/QuantityValue.h>
3#include <knowCore/TypeDefinitions.h>
6#include <kDB/SPARQL/Algebra/Builders.h>
8#include <knowCore/Uris/askcore_db.h>
9#include <knowCore/Uris/askcore_types.h>
10#include <knowCore/Uris/qudt.h>
12#include <knowGIS/Uris/geo.h>
13#include <knowGIS/Uris/geof.h>
15#include "FocusNodeDeclarationsRegistry.h"
20 knowCore::ReturnValue<QList<kDB::SPARQL::Algebra::NodeCSP>> focus_node_filter_constructor(
const QList<QPair<knowCore::Uri, knowCore::ConstrainedValue>>& _constraints,
25 namespace SA = kDB::SPARQL::Algebra;
26 namespace SB = kDB::SPARQL::Algebra::Builders;
27 using askcore_types = knowCore::Uris::askcore_types;
28 using qudt = knowCore::Uris::qudt;
29 using askcore_sparql_functions_extra = knowCore::Uris::askcore_sparql_functions_extra;
33 for(
int i = 0; i < _constraints.size(); ++i)
37 SA::NodeCSP lhs_node_value;
38 SA::NodeCSP lhs_node_unit;
40 if((*path2node).contains(path))
43 lhs_node_value = nodes[0];
46 lhs_node_unit = nodes[1];
50 for(
int i = 0; i < datatypes.size(); ++i)
52 KNOWCORE_RETURN_VALUE_TRY(declaration, declaration_registry.declaration(datatypes[i]));
53 KNOWCORE_RETURN_VALUE_TRY(field_i, declaration.field(path),
"In accessing property {} for type {}: {}", path, datatypes[i]);
57 }
else if(field != field_i)
59 return kCrvError(
"Property {} has incompatible field in type {} and type {}", path, datatypes[0], datatypes[i]);
63 if(field.datatype() == askcore_types::quantityDecimal)
65 SB::Variable value_variable_value_i(clog_qt::qformat(
"?{}_value", i));
66 SB::Variable value_variable_unit_i(clog_qt::qformat(
"?{}_unit", i));
68 SB::BlankNode object_node;
71 triples_pattern->append((*uri_variable), field.path(), object_node);
72 triples_pattern->append(object_node, qudt::value, value_variable_value_i);
73 triples_pattern->append(object_node, qudt::unit, value_variable_unit_i);
75 lhs_node_value = value_variable_value_i;
76 lhs_node_unit = value_variable_unit_i;
77 (*path2node)[path] = {lhs_node_value, lhs_node_unit};
79 SB::Variable value_variable_i(clog_qt::qformat(
"?{}", i));
80 triples_pattern->append((*uri_variable), field.path(), value_variable_i);
81 lhs_node_value = value_variable_i;
82 (*path2node)[path] = {lhs_node_value};
96 lhs_node = SB::FunctionCall().name(askcore_sparql_functions_extra::convertQuantityValue).parameters(lhs_node_value, lhs_node_unit, SB::Term().term(qn->unit().uri()));
97 rhs_node = SB::Value().value(knowRDF::Literal::fromValue(qn->value()));
99 return kCrvError(
"Constraint for property '{}' should be a quantity number", path);
102 lhs_node = lhs_node_value;
105 rhs_node = SB::Term().term(constraint.value.value<
knowCore::Uri>().expectSuccess());
107 rhs_node = SB::Value().value(constraint.value);
110 switch(constraint.type)
112 case knowCore::ConstrainedValue::Type::Equal:
113 filters.append(SB::RelationalEqual().left(lhs_node).right(rhs_node));
115 case knowCore::ConstrainedValue::Type::Different:
116 filters.append(SB::RelationalDifferent().left(lhs_node).right(rhs_node));
119 filters.append(SB::RelationalInferiorEqual().left(lhs_node).right(rhs_node));
122 filters.append(SB::RelationalInferior().left(lhs_node).right(rhs_node));
125 filters.append(SB::RelationalSuperiorEqual().left(lhs_node).right(rhs_node));
128 filters.append(SB::RelationalSuperior().left(lhs_node).right(rhs_node));
131 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfOverlaps).parameters(lhs_node, rhs_node));
134 if(_intersectsPrecision > 0.0)
136 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfIntersects).parameters(lhs_node, rhs_node, knowRDF::Literal::fromValue( _intersectsPrecision)));
138 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfIntersects).parameters(lhs_node, rhs_node));
142 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfWithin).parameters(lhs_node, rhs_node));
145 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfWithin).parameters(lhs_node, rhs_node));
148 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfTouches).parameters(lhs_node, rhs_node));
151 filters.append(SB::FunctionCall().name(knowGIS::Uris::geof::sfDisjoint).parameters(lhs_node, rhs_node));
157 return kCrvError(
"In operator not supported.");
162 return kCrvSuccess(filters);
Definition FocusNodeDeclaration.h:22
Definition FocusNodeDeclarationsRegistry.h:6
Definition Builders.h:180
@ Superior
such as constraint.value < value
@ GeoWithin
such as value is within the constraint.value
@ NotContains
such as list does not contains a value
@ GeoDisjoint
such as value disjoint with constraint.value,
@ InferiorEqual
such as value <= constraint.value
@ GeoTouches
such as value touches with constraint.value,
@ Contains
such as list contains a value
@ Inferior
such as value < constraint.value
@ NotIn
such as one value is not part of a list
@ SuperiorEqual
such as constraint.value <= value
@ GeoIntersects
such as value intersects with constraint.value,
@ GeoContains
such as constraint.value is within the value
@ In
such as one value is part of a list
Definition ConstrainedValue.h:38