0

I have created an ontology by grouping together many ontologies.

I want to use SPARQL to identify all middle terms (relationships) from one ontology from the group ontology.

The following approach only produces a pyparsing error.

g = rdflib.Graph()
result = g.parse("without-bfo.owl")

qres = g.query(
 """ PREFIX sudo: <http://purl.url/sudo/ontology#>
     SELECT ?v
    WHERE {
      ?s sudo:?v ?o.
   }""")

If I remove the sudo: prefix, this query returns all triples.

mac389
  • 3,004
  • 5
  • 38
  • 62

2 Answers2

1

You can check if the relation starts with your namespace with CONTAINS

SELECT ?v
WHERE {
  ?s ?v ?o.
  FILTER CONTAINS(?v, "http://purl.url/sudo/ontology#")
}

You can also try STRSTARTS

see w3 documentation

Arcturus
  • 26,677
  • 10
  • 92
  • 107
0

@Arcturus was close.

The following worked for me. One has to declare ?v as a string using STR. This SO post suggested the syntax.

qres = g.query(
""" SELECT DISTINCT ?v
    WHERE {
      ?s ?v ?o . 
      FILTER CONTAINS(STR(?v), "sudo")
   }""")

for row in qres: print row

mac389
  • 3,004
  • 5
  • 38
  • 62