kDBQueries is a kDB extension which allows to generate API to execute queries in kDB and return them as high-level structures.
Queries Description
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix askcore_db_queries: <http://askco.re/db/queries#> .
@prefix askcore_graph: <http://askco.re/graph#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix xsi: <http://www.w3.org/2001/XMLSchema-instance#> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
<salient_object_insert> dc:description "Insert a salient object in a graph" ;
foaf:name "salient_object_insert" ;
askcore_db_queries:query """PREFIX example: <http://example.org/>
PREFIX w3cGeo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
INSERT DATA { GRAPH %graph_uri { example:%object_id a %object_klass ;
w3cGeo:location [ w3cGeo:long %longitude ; w3cGeo:lat %latitude ; w3cGeo:alt %altitude ] } }""" ;
askcore_db_queries:binding [ foaf:name "graph_uri" ; xsi:type xsd:uri ] ;
askcore_db_queries:binding [ foaf:name "object_id" ; xsi:type xsd:string ; dc:description "unique ID representing the object" ] ;
askcore_db_queries:binding [ foaf:name "object_klass" ; xsi:type xsd:uri ] ;
askcore_db_queries:binding [ foaf:name "longitude" ; xsi:type xsd:decimal ] ;
askcore_db_queries:binding [ foaf:name "latitude" ; xsi:type xsd:decimal ] ;
askcore_db_queries:binding [ foaf:name "altitude" ; xsi:type xsd:decimal ] .
<salient_object_select_by_class> dc:description "Return the list of salient objects of a specific class" ;
foaf:name "salient_object_select_by_class" ;
askcore_db_queries:query """PREFIX example: <http://example.org/>
PREFIX w3cGeo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT * FROM %graph_uri WHERE { ?uri a example:%object_klass ;
w3cGeo:location [ w3cGeo:long ?longitude ; w3cGeo:lat ?latitude ; w3cGeo:alt ?altitude ] }""" ;
askcore_db_queries:binding [ foaf:name "graph_uri" ; xsi:type xsd:uri ] ;
askcore_db_queries:binding [ foaf:name "object_klass" ; xsi:type xsd:uri ] .
Programming with kDBQueries
kDBQueries is available for C++, Python and Qt/QML.
C++
kDBQueries for C++ relies on a code generator (called kDBQueries
) that reads the RDF description of the queries and generate a set of C++ classes.
The executable takes the following arguments:
kDBQueriesGenerator [quries_definition_graph] [namespaces_class] [output_prefix]
When compiling, CMake it is possible to use a macro to generate the C++ classes:
kdb_generate_queries([quries_definition_graph] [namespaces_class] [output_prefix])
In both cases, [quries_definition_graph]
corresponds to the RDF file with a description of the query, [namespaces_class]
is the namespace where the classes are generated and finally [output_prefix]
is the prefix for the .cpp
and .h
files generated.
Python
kDBQueries for Python read the RDF description of the queries from a RDF Graph stored in the database and dynamically generates the API.
import kDBQueries
import
connection =
lrs_queries_gen.salient_object_insert(graph_uri="http://example.org/sp_graph", object_klass="http://example.org/klass", longitude=1, latitude=2, altitude=3)
for obj in lrs_queries_gen.salient_object_select_by_class(graph_uri="http://example.org/sp_graph", object_klass="http://example.org/klass"):
print(obj.uri, obj.longitude, obj.latitude, obj.altitude)
Definition generator.py:72
Definition interface.py:4
Qt/QML
kDBQueries for QML provides the GeneratedQuery
qml object:
import kDBQueries
GeneratedQuery
{
queries: "http://example.org/queries"
queryName: "salient_object_insert"
parameters: { graph_uri: "http://example.org/sp_graph", object_klass: "http://example.org/klass", longitude: 1, latitude: 2, altitude: 3 }
}