С исправленными файлами примеров:
plant_data.ttl
:
@prefix ns1: <tag:a#> .
<http://www.siemens.com/ai/plant/data/PolishingStation1>
a ns1:Station;
a ns1:MaterialFlowObject;
ns1:hasName "PolishingStation1" .
...
get_Material_Flow.rq
:
PREFIX ns1: <tag:a#>
SELECT ?name ?type
WHERE {
?MaterialFlowObject a ns1:Station.
?MaterialFlowObject a ?type.
?MaterialFlowObject ns1:hasName ?name.
}
run_query.py
:
from rdflib.graph import ConjunctiveGraph
kg = ConjunctiveGraph()
input_ontology_path = "./plant_data.ttl"
kg.parse(input_ontology_path, format="ttl")
with open("get_Material_Flow.rq") as f:
sparql_query_string = f.read()
query_output = kg.query(sparql_query_string)
for row in query_output:
print(f'name={str(row["name"])},type={str(row["type"])}')
Это работает, как ожидалось, и дает результат для каждого совпадения:
name=PolishingStation1,type=tag:a#Station
name=PolishingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#MaterialFlowObject
name=QuenchingStation3,type=tag:a#Station
name=QuenchingStation4,type=tag:a#Station
name=QuenchingStation4,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#MaterialFlowObject
name=PolishingStation2,type=tag:a#Station
name=QuenchingStation2,type=tag:a#MaterialFlowObject
name=QuenchingStation2,type=tag:a#Station
name=QuenchingStation1,type=tag:a#MaterialFlowObject
name=QuenchingStation1,type=tag:a#Station