Knowledge graphs · SPARQL · Engineering
SPARQL: finding missing evidence in engineering knowledge graphs
How do you find engineering decisions that are missing evidence? A knowledge-graph query can expose that gap. The crucial distinction is whether you mean “no reference exists” or “no qualifying evidence is recorded”. This guide presents three executable SPARQL queries and explains their different results.
The SHACL guide covers rules for a review export. Here the complementary focus is on actionable lists and on why an apparently complete report can hide gaps. All data and identifiers are invented teaching examples. No employer or client information is used.
Four decisions, three different gaps
The example export contains four decisions in the ex:ReadyForReview state. The custom ex: vocabulary is not an industry standard. For this experiment, qualifying evidence means only an IRI linked through ex:evidence with the recorded statement ex:passed true. This deliberately narrow rule does not constitute engineering approval.
Save the next block as decisions.ttl. d1 has two sources and a test marked as passed. d2 has no evidence reference. d3 references a test marked as failed. d4 contains a text literal instead of a resource identifier.
@prefix ex: <https://example.org/engineering/> .
ex:d1 a ex:Decision ;
ex:status ex:ReadyForReview ;
ex:source ex:docA, ex:docB ;
ex:evidence ex:testA .
ex:d2 a ex:Decision ;
ex:status ex:ReadyForReview ;
ex:source ex:docC .
ex:d3 a ex:Decision ;
ex:status ex:ReadyForReview ;
ex:source ex:docD ;
ex:evidence ex:testB .
ex:d4 a ex:Decision ;
ex:status ex:ReadyForReview ;
ex:source ex:docE ;
ex:evidence "test-C" .
ex:testA ex:passed true .
ex:testB ex:passed false .
Selecting decisions is part of the validation brief: we consider explicitly classified decisions with this exact recorded status. A record without the type or status does not appear in the following work lists. Establish separately which identifiers are expected before interpreting the results.
Query 1: no evidence reference exists
FILTER NOT EXISTS checks whether the inner pattern has no match for the current outer solution. The variable ?decision connects the two parts. The normative reference is SPARQL 1.1, section 8.1.1 [1]. Save the first query as missing.rq:
PREFIX ex: <https://example.org/engineering/>
SELECT DISTINCT ?decision
WHERE {
?decision a ex:Decision ;
ex:status ex:ReadyForReview .
FILTER NOT EXISTS { ?decision ex:evidence ?evidence . }
}
ORDER BY ?decision
Result: only ex:d2. d3 has a reference even though its test failed. The literal on d4 also satisfies the simple triple pattern. Calling this list “decisions without adequate evidence” therefore promises more than the query checks.
I recommend a precise report label: “no evidence reference in the checked data snapshot”. This leaves open whether evidence exists elsewhere, has not been imported yet or is actually missing.
Query 2: no evidence meeting the agreed rule
The second query adds the agreed conditions inside the inner pattern. isIRI checks the RDF term's kind; it does not check a source's availability or correctness. The function is defined in section 17.4.2.1 [1]. Save this as qualified.rq:
PREFIX ex: <https://example.org/engineering/>
SELECT DISTINCT ?decision
WHERE {
?decision a ex:Decision ;
ex:status ex:ReadyForReview .
FILTER NOT EXISTS {
?decision ex:evidence ?evidence .
FILTER(isIRI(?evidence))
?evidence ex:passed true .
}
}
ORDER BY ?decision
Result: ex:d2, ex:d3 and ex:d4. This list combines different causes: no reference, no test recorded as passed or an unsuitable value type. Add a cause-oriented view when assigning the work. A common “not satisfied” condition is useful for filtering but is not yet a practical instruction.
This rule also has a clear limit. An outdated or inapplicable test with ex:passed true still satisfies the pattern. The “passed” statement is read, not independently checked. Version, applicability and actual test results therefore belong in the technical assessment. The additional relationship needed depends on the specific engineering question.
Query 3: OPTIONAL retains incomplete records
An overview should retain decisions even when supplementary details are missing. OPTIONAL can add available bindings and preserves the outer solution when its optional pattern has no match. This is described in section 6 [1]. Save this as report.rq:
PREFIX ex: <https://example.org/engineering/>
SELECT ?decision ?source ?evidence
WHERE {
?decision a ex:Decision ;
ex:status ex:ReadyForReview .
OPTIONAL { ?decision ex:source ?source . }
OPTIONAL { ?decision ex:evidence ?evidence . }
}
ORDER BY ?decision ?source ?evidence
Result: five rows for four decisions. d1 appears twice because it has two sources. The evidence variable remains unbound for d2. Writing both optional triple patterns as required patterns would remove precisely that problematic record from the report.
Do not count result rows as decisions without checking the result's grain. To obtain unique identifiers, SELECT DISTINCT ?decision is sufficient here. If source and evidence are also projected, their different combinations remain different even with DISTINCT. Duplicate elimination is specified in section 15.3 [1].
Another common trap is applying a filter on an optionally bound variable outside the optional block. This can remove solutions whose value is missing. Whether the filter belongs inside the block or should deliberately exclude the decision follows from the reporting question. Do not exchange those variants without a counterexample.
Run locally with Python and RDFLib
Place the four files in a separate working directory. You need Python and RDFLib; the example was checked with Python 3.14 and RDFLib 7.6.0. Install the library in a separate environment with python -m pip install rdflib==7.6.0. Save the following block as query_decisions.py and run python query_decisions.py in that directory:
from pathlib import Path
from rdflib import Graph
graph = Graph().parse("decisions.ttl", format="turtle")
for name in ("missing", "qualified", "report"):
query = Path(name + ".rq").read_text(encoding="utf-8")
print(name)
for row in graph.query(query):
print(tuple(None if value is None else str(value)
for value in row))
The script reads only the specified local files and uses no remote SPARQL services. It does not modify the graph. RDFLib evaluates queries through Graph.query(); iteration returns the variable values for each result row. See the official RDFLib documentation [2]. This example explicitly prints missing bindings as None.
Counterexamples before adoption
- Add evidence: link
d2toex:testA. The decision must disappear from both gap lists. - Add only a second source reference: the gap lists must stay unchanged; the overview gains another combination.
- Remove a decision's type: it disappears from the selected population. This demonstrates a selection boundary, not a successful repair.
- Query an empty graph: no results mean no checked decisions here. Expected identifiers therefore require a separate reconciliation.
These counterexamples were run automatically against the teaching example. For a production report, document the data snapshot, query version, graph being queried and expected population. When using multiple named graphs, also define which graph supplies decisions and evidence. Searching the wrong data scope can otherwise look like an engineering evidence gap.
How do SPARQL and SHACL complement each other?
For this workflow, I recommend SPARQL for targeted questions and traceable work lists, and SHACL for an agreed structural contract. Both require domain-defined rules and a checked selection of records. The introduction to knowledge graphs in machine vision illustrates how decisions, configurations and evidence can connect.
Primary sources
- W3C: SPARQL 1.1 Query Language – especially sections 6, 8.1.1, 15.3 and 17.4.2.1
- RDFLib: Querying with SPARQL
Sources checked on 13 November 2026. Example data, queries and workflow recommendations are original teaching and modelling proposals. They contain no operational information and make no approval claim.
