Query
lokf query loads a bundle into an in-memory
GraphStore (backed by pyoxigraph) and runs SPARQL over
it. Nothing is written; the store lives only for the length of the command.
uv run lokf query examples/acme-knowledge \ 'SELECT ?s ?name WHERE { ?s schema:name ?name } ORDER BY ?name'The preset prefixes
Section titled “The preset prefixes”You never write a PREFIX block. The store prepends the LOKF schema
namespaces to every query, so the common prefixes are already bound:
lokf, schema, prov, dcterms, skos, foaf, rdf, rdfs, owl,
xsd, and dcat.
Write schema:name, prov:wasDerivedFrom, or lokf:Metric straight away.
You can still declare your own PREFIX/BASE lines at the top of a query if
you need one that is not preset.
With --schema domain.yaml, a domain schema’s own prefixes are preset as well,
and the graph is projected under it as
lokf convert does.
Worked queries over the reference bundle
Section titled “Worked queries over the reference bundle”Every named thing in the bundle:
uv run lokf query examples/acme-knowledge \ 'SELECT ?s ?name WHERE { ?s schema:name ?name } ORDER BY ?name's namehttps://acme.example/knowledge/glossary/active-user Active Userhttps://acme.example/knowledge/services/analytics-api Analytics API...Does the bundle contain a Metric? (boolean questions need a
machine-readable --format, e.g. json):
uv run lokf query examples/acme-knowledge \ 'ASK { ?s a lokf:Metric }' --format json{"head":{},"boolean":true}Pull a subgraph back out as Turtle (CONSTRUCT/DESCRIBE always emit RDF):
uv run lokf query examples/acme-knowledge \ 'CONSTRUCT { ?s ?p ?o } WHERE { ?s a lokf:Metric ; ?p ?o }'Output formats
Section titled “Output formats”--format / -f controls how results come back:
| Value | Applies to | Result |
|---|---|---|
table (default) |
SELECT | aligned columns |
json / csv / tsv |
SELECT / ASK | SPARQL results |
ttl |
CONSTRUCT / DESCRIBE | Turtle (the default for those forms) |
The just query recipe wraps the command:
just query examples/acme-knowledge 'SELECT ?s WHERE { ?s a lokf:Metric }'From Python
Section titled “From Python”lokf query is a wrapper around GraphStore. The store
gives you the same queries, plus rdflib_graph(), serialize_results(), and
dump():
from lokf.store import GraphStore
store = GraphStore.from_bundle("examples/acme-knowledge")len(store) # triple count
store.select("SELECT ?s WHERE { ?s a lokf:Metric }")# -> [{'s': 'https://acme.example/knowledge/metrics/weekly-active-users'}]
store.ask("ASK { ?s a lokf:Metric }") # -> Truestore.construct("CONSTRUCT { ?s ?p ?o } WHERE { ?s a lokf:Metric ; ?p ?o }")The prefixes are preset on the store too — store.prefixes is the dict it
prepends. To publish the same store over HTTP, see Serve.