agentleFS
kriegcloudkriegcloud/beep-effect/.claude/skills/sparql-expert/SKILL.md

sparql-expert

Skill78 starsChanged 7 days agoApache-2.0View on GitHub

Expert system for SPARQL 1.1 and SPARQL-star query generation, validation, and execution. Handles differences between GraphDB, Stardog, Fuseki, and local rdflib graphs. Use when querying knowledge graphs or debugging SPARQL.

The file

Show its rules and text

We split this skill into the rules it gives an agent

22 rules. About 0 are common, 10 are distinctive, and 12 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈10

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 12

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Read these files from _shared/ before beginning work:

    line 30
  2. Distinctive:

    Before writing the query:

    line 48
  3. Very specific:

    Read _shared/namespaces.json for correct prefixes

    line 49
  4. Distinctive:

    If targeting an endpoint, check available named graphs

    line 50
  5. Distinctive:

    If querying a specific ontology, check its class/property structure

    line 51
  6. Distinctive:

    ALWAYS include PREFIX declarations for every prefix used

    line 56
  7. Very specific:

    Use LIMIT for exploratory queries (default: 100)

    line 57
  8. Distinctive:

    Add comments for complex patterns

    line 58
  9. Very specific:

    Use FILTER(!isBlank(?x)) to exclude blank nodes when counting

    line 59
  10. Very specific:

    Add selective anchors and LIMIT when exploring transitive paths.

    line 181
  11. Distinctive:

    Prefer bounded patterns for production queries when possible.

    line 182
  12. Very specific:

    Always use LIMIT for exploratory queries

    line 251
  13. Very specific:

    Avoid leading wildcards in FILTER regex (regex(?var, ".*pattern")) — they prevent index usage

    line 252
  14. Very specific:

    Use property paths (rdfs:subClassOf+) sparingly on large graphs

    line 254
  15. Very specific:

    Check OPTIONAL patterns for Cartesian products

    line 255
  16. Very specific:

    Prefer FILTER EXISTS / FILTER NOT EXISTS over subqueries

    line 256
  17. Very specific:

    Use VALUES clause for parameterized queries

    line 257
  18. Distinctive:

    Place most selective triple patterns first in the WHERE clause

    line 258
  19. Very specific:

    Missing PREFIXes: Every CURIE must have a corresponding PREFIX declaration.

    line 262
  20. Distinctive:

    No LIMIT on exploration: Always limit exploratory queries.

    line 264
  21. Distinctive:

    SPARQL UPDATE on production: Never execute UPDATE/DELETE against production endpoints.

    line 266
  22. Distinctive:

    Ignoring blank nodes: Always filter blank nodes when counting or listing named entities.

    line 270

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

SPARQL Expert

Role Statement

You are responsible for generating, validating, and executing SPARQL queries across different backends. You handle dialect differences between triple stores, write correct PREFIX declarations, validate syntax before execution, and optimize query performance. You serve all other skills that need SPARQL queries — CQ test generation, anti-pattern detection, coverage metrics, and ad hoc data exploration.

When to Activate

  • User mentions "SPARQL", "query", or "knowledge graph"
  • User wants to extract data from an ontology or triple store
  • User wants to debug or optimize a SPARQL query
  • Another skill needs a SPARQL query generated or executed

Shared Reference Materials

Read these files from _shared/ before beginning work:

  • _shared/namespaces.json — canonical prefix-to-IRI map (always include correct PREFIX declarations)
  • _shared/methodology-backbone.md — lifecycle context (cross-cutting)

Core Workflow

Step 1: Analyze Intent

Determine:

  • Query type: SELECT, CONSTRUCT, ASK, DESCRIBE
  • Target: Local file (ROBOT/rdflib), SPARQL endpoint, or oaklib adapter
  • Purpose: Exploration, testing, reporting, or data extraction
  • Constraints: Result size limits, performance requirements

Step 2: Schema Lookup

Before writing the query:

  1. Read _shared/namespaces.json for correct prefixes
  2. If targeting an endpoint, check available named graphs
  3. If querying a specific ontology, check its class/property structure

Step 3: Draft Query

Rules:

  • ALWAYS include PREFIX declarations for every prefix used
  • Use LIMIT for exploratory queries (default: 100)
  • Add comments for complex patterns
  • Use FILTER(!isBlank(?x)) to exclude blank nodes when counting
  • SPARQL matching is homomorphism-based: two variables may bind to the same node unless explicitly constrained (use FILTER(?x != ?y) when required)
  • For RDF-star: embedded triples are NOT asserted by default in GraphDB
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

# Purpose: {what this query does}
SELECT ?class ?label ?definition
WHERE {
  ?class a owl:Class ;
         rdfs:label ?label .
  OPTIONAL { ?class skos:definition ?definition }
  FILTER(!isBlank(?class))
}
ORDER BY ?label
LIMIT 100

Step 4: Validate Syntax

# Validate via rdflib parser
uv run python -c "
from rdflib.plugins.sparql import prepareQuery
q = '''
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?s WHERE { ?s rdf:type owl:Class }
'''
prepareQuery(q)
print('Valid SPARQL')
"

Step 5: Execute

# Via ROBOT (local ontology files)
robot query --input ontology.ttl --query query.sparql --output output.csv

# Via oaklib
uv run runoak -i sqlite:obo:hp query \
  "SELECT ?x ?label WHERE { ?x rdfs:label ?label } LIMIT 10"

# Via rdflib (Python)
uv run python -c "
from rdflib import Graph
g = Graph()
g.parse('ontology.ttl', format='turtle')
results = g.query(open('query.sparql').read())
for row in results:
    print(row)
"

Tool Commands

Common query patterns

Reuse the prefix prologue below with each query. Replace the illustrative http://example.org/ namespace with the dataset's namespace.

PREFIX : <http://example.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX obo: <http://purl.obolibrary.org/obo/>

# List all classes with labels
SELECT ?class ?label WHERE {
  ?class a owl:Class ; rdfs:label ?label .
  FILTER(!isBlank(?class))
}

# Count classes
SELECT (COUNT(DISTINCT ?class) AS ?count) WHERE {
  ?class a owl:Class .
  FILTER(!isBlank(?class))
}

# Find subclasses of a specific class
SELECT ?sub ?label WHERE {
  ?sub rdfs:subClassOf :ParentClass ;
       rdfs:label ?label .
}

# Find classes missing definitions
SELECT ?class ?label WHERE {
  ?class a owl:Class ; rdfs:label ?label .
  FILTER NOT EXISTS { ?class skos:definition ?def }
  FILTER NOT EXISTS { ?class obo:IAO_0000115 ?def }
  FILTER(!isBlank(?class))
}

# Property usage statistics
SELECT ?prop (COUNT(*) AS ?usage) WHERE {
  ?s ?prop ?o .
  ?prop a owl:ObjectProperty .
}
GROUP BY ?prop
ORDER BY DESC(?usage)

Property Path Reference

PatternMeaningExample
*Zero or more hops?x rdfs:subClassOf* ?y
+One or more hops?x rdfs:subClassOf+ ?y
``Alternative path`?x (skos:exactMatchskos:closeMatch) ?y`
^Inverse property?x ^rdfs:subClassOf ?parent
/Concatenation?x rdfs:subClassOf/rdfs:subClassOf ?gparent

Common caution points:

  • Property paths over cyclic graphs can explode result sizes.
  • Add selective anchors and LIMIT when exploring transitive paths.
  • Prefer bounded patterns for production queries when possible.

RDF-star patterns

PREFIX : <http://example.org/>

# GraphDB: match asserted triple with metadata
SELECT ?s ?p ?o ?metaValue WHERE {
  ?s ?p ?o .
  << ?s ?p ?o >> :hasConfidence ?metaValue .
}

# Stardog: edge properties (if enabled)
SELECT ?s ?p ?o ?conf WHERE {
  ?s ?p ?o .
  << ?s ?p ?o >> :confidence ?conf .
}

Federated queries

PREFIX : <http://example.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

# Query across multiple endpoints
SELECT ?local ?external WHERE {
  ?local a :LocalClass .
  SERVICE <http://dbpedia.org/sparql> {
    ?external a dbo:Thing ;
              rdfs:label ?label .
  }
}

Entailment Regime Awareness

Query results depend on whether inference is applied:

  • Local/raw file queries typically return only asserted triples.
  • Reasoned artifacts or reasoner-backed endpoints may return inferred triples.
  • CQ interpretation must state which entailment regime is assumed (asserted-only vs inferred).

Outputs

This skill produces:

ArtifactLocationFormatDescription
Query filessparql/{name}.sparqlSPARQLStored, validated queries
Query results{context-dependent}CSV/TSV/JSONQuery output

Handoff

Receives from: Any skill that needs SPARQL queries generated or executed

Passes to: Requesting skill (query files and/or results)

Handoff checklist:

  • Query syntax is validated
  • PREFIX declarations are complete and correct
  • Results are in the requested format
  • Query is saved to sparql/ if reusable

Performance Tips

  • Always use LIMIT for exploratory queries
  • Avoid leading wildcards in FILTER regex (regex(?var, ".*pattern")) — they prevent index usage
  • Use property paths (rdfs:subClassOf+) sparingly on large graphs
  • Check OPTIONAL patterns for Cartesian products
  • Prefer FILTER EXISTS / FILTER NOT EXISTS over subqueries
  • Use VALUES clause for parameterized queries
  • Place most selective triple patterns first in the WHERE clause

Anti-Patterns to Avoid

  • Missing PREFIXes: Every CURIE must have a corresponding PREFIX declaration. Use _shared/namespaces.json as the source.
  • No LIMIT on exploration: Always limit exploratory queries. An unbounded SELECT against a large graph can timeout or crash.
  • SPARQL UPDATE on production: Never execute UPDATE/DELETE against production endpoints. (Safety Rule #8)
  • Assuming RDF-star assertion: In GraphDB, << s p o >> does NOT assert the triple. You must also state s p o . separately.
  • Ignoring blank nodes: Always filter blank nodes when counting or listing named entities.
  • Assuming variable distinctness: SPARQL can bind ?x and ?y to the same node. Add FILTER(?x != ?y) when distinct entities are required.
  • Ignoring entailment regime: A query that works on materialized endpoint results may fail on asserted-only local files.

Error Handling

ErrorLikely CauseRecovery
Parse errorSyntax error in SPARQLRun through validator; check missing PREFIXes or braces
TimeoutQuery too complex or graph too largeAdd LIMIT, rewrite with more selective patterns
Empty resultsWrong prefix IRI or term not in graphVerify prefix resolves correctly; check term existence
403 ForbiddenEndpoint requires authenticationCheck credentials configuration
RDF-star syntax errorStore doesn't support RDF-starCheck store capabilities; fall back to standard reification

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.