DEV Community

webdecoy
webdecoy

Posted on

How to model missing data in RDF and query it with SPARQL

A table of crawlers has a blank cell under “verification method.” That blank could mean nobody has reviewed the record. It could also mean someone reviewed the operator’s documentation and found no published method. If your report sends both rows to the review queue, you’ll spend time checking the same records again.

Here’s a small RDF model that keeps those cases distinct. All three crawlers in this example are fictional.

Model the review as a fact

Save this as crawlers.ttl. The verificationMethod property says what method the dataset records. The verificationReview property says how far the review got.

@prefix ex: <https://example.org/crawlers/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:orchard a ex:Crawler ;
    rdfs:label "OrchardBot" ;
    ex:verificationMethod ex:PublishedIPRanges ;
    ex:verificationReview ex:MethodDocumented .

ex:cedar a ex:Crawler ;
    rdfs:label "CedarBot" ;
    ex:verificationReview ex:ReviewedNoMethod .

ex:harbor a ex:Crawler ;
    rdfs:label "HarborFetch" .
Enter fullscreen mode Exit fullscreen mode

The ReviewedNoMethod value records a review outcome. It means the reviewer found no published method in the sources they checked. Keep that scope if you use the value in a dashboard or audit.

Query the work queue with SPARQL

A query for records with no verificationMethod triple returns CedarBot and HarborFetch. Query for the missing review instead. Save this as needs-review.rq.

PREFIX ex: <https://example.org/crawlers/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?name WHERE {
  ?crawler a ex:Crawler ; rdfs:label ?name .
  FILTER NOT EXISTS { ?crawler ex:verificationReview ?review }
}
ORDER BY ?name
Enter fullscreen mode Exit fullscreen mode

Run it locally with Python and RDFLib.

python3 -m venv .venv
.venv/bin/python -m pip install rdflib==7.1.4
.venv/bin/python - <<'PY'
from pathlib import Path
from rdflib import Graph

graph = Graph().parse('crawlers.ttl', format='turtle')
query = Path('needs-review.rq').read_text()
for row in graph.query(query):
    print(row.name)
PY
Enter fullscreen mode Exit fullscreen mode
HarborFetch
Enter fullscreen mode Exit fullscreen mode

Now change ex:verificationReview ?review in the filter to ex:verificationMethod ?method and rerun it. The output is CedarBot followed by HarborFetch. That’s a legitimate query for missing method data in this graph. It answers a different question from which records need review.

FILTER NOT EXISTS checks the current graph for a matching pattern. Under OWL’s open-world assumption, a missing statement can acquire evidence in another source or in a later version of the graph. The explicit review outcome lets you say what happened in your review while leaving room for new information. SPARQL 1.1 query language · OWL 2 Primer

The same model works for SDK compatibility tables and data catalog audits. Give each workflow an explicit review state when it needs to distinguish work that’s complete from work that’s still waiting.

I used AI assistance to draft this article.

Top comments (0)