🔍 SPARQL Query Cookbook

Common queries for the Knowledge Graph.


Setup

1
2
3
4
5
PREFIX sea: <http://sea-forge.com/schema/core#>
PREFIX sea-debt: <http://sea-forge.com/schema/debt#>
PREFIX sea-inc: <http://sea-forge.com/schema/incident#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

Entity Queries

List All Entities

1
2
3
4
5
6
7
SELECT ?entity ?name ?domain
WHERE {
  ?entity a sea:Entity ;
          rdfs:label ?name ;
          sea:inContext ?domain .
}
ORDER BY ?domain ?name

Find Entity by Name

1
2
3
4
5
6
SELECT ?entity ?domain
WHERE {
  ?entity a sea:Entity ;
          rdfs:label "Warehouse.A" ;
          sea:inContext ?domain .
}

Flow Queries

All Flows with Details

1
2
3
4
5
6
7
8
9
10
SELECT ?flow ?from_name ?to_name ?resource ?quantity
WHERE {
  ?flow a sea:Flow ;
        sea:from ?from ;
        sea:to ?to ;
        sea:resource ?resource ;
        sea:quantity ?quantity .
  ?from rdfs:label ?from_name .
  ?to rdfs:label ?to_name .
}

Flows to Specific Entity

1
2
3
4
5
6
7
8
SELECT ?flow ?from_name ?quantity
WHERE {
  ?flow a sea:Flow ;
        sea:from ?from ;
        sea:to <http://sea-forge.com/entity/warehouse-a> ;
        sea:quantity ?quantity .
  ?from rdfs:label ?from_name .
}

Aggregation Queries

Total Quantity by Destination

1
2
3
4
5
6
7
8
9
SELECT ?to_name (SUM(?quantity) as ?total)
WHERE {
  ?flow a sea:Flow ;
        sea:to ?to ;
        sea:quantity ?quantity .
  ?to rdfs:label ?to_name .
}
GROUP BY ?to_name
ORDER BY DESC(?total)

Entity Connection Count

1
2
3
4
5
6
7
8
SELECT ?entity ?name (COUNT(?flow) as ?connections)
WHERE {
  ?entity a sea:Entity ;
          rdfs:label ?name .
  { ?flow sea:from ?entity } UNION { ?flow sea:to ?entity }
}
GROUP BY ?entity ?name
ORDER BY DESC(?connections)

Debt Queries

Active Debt Items

1
2
3
4
5
6
7
8
SELECT ?debt ?label ?status ?concept
WHERE {
  ?debt a sea-debt:SemanticDebtItem ;
        rdfs:label ?label ;
        sea-debt:status ?status ;
        sea-debt:regardingConcept ?concept .
  FILTER (?status IN ("Blocking", "Open"))
}

Blockers by Context

1
2
3
4
5
6
7
8
SELECT ?debt ?label ?concept
WHERE {
  ?debt a sea-debt:SemanticDebtItem ;
        sea-debt:status "Blocking" ;
        rdfs:label ?label ;
        sea-debt:regardingConcept ?concept .
  ?concept sea:inContext <http://sea-forge.com/context/finance> .
}

Policy Hotspots

1
2
3
4
5
6
7
8
9
SELECT ?policy (COUNT(?debt) AS ?debtCount)
WHERE {
  ?debt a sea-debt:SemanticDebtItem ;
        sea-debt:regardingConcept ?policy .
  ?policy a sea:Policy .
}
GROUP BY ?policy
ORDER BY DESC(?debtCount)
LIMIT 10

Graph Exploration

Find All Triples for Subject

1
2
3
4
SELECT ?p ?o
WHERE {
  <http://sea-forge.com/entity/warehouse-a> ?p ?o .
}

Find Incoming References

1
2
3
4
SELECT ?s ?p
WHERE {
  ?s ?p <http://sea-forge.com/entity/warehouse-a> .
}

Running Queries

Via curl

1
2
3
curl -X POST http://localhost:7878/query \
  -H "Content-Type: application/sparql-query" \
  -d "SELECT * WHERE { ?s ?p ?o } LIMIT 10"

Via API

1
2
3
4
5
6
curl -X POST http://localhost:8000/graph/query \
  -H "Content-Type: application/json" \
  -d '{
    "language": "sparql",
    "query": "SELECT * WHERE { ?s ?p ?o } LIMIT 10"
  }'