|
|
from rdflib import Graph, Namespace |
|
|
import pandas as pd |
|
|
|
|
|
def run_validation(): |
|
|
g = Graph() |
|
|
g.parse("cars_knowledge_graph.ttl", format="turtle") |
|
|
|
|
|
EX = Namespace("http://example.org/cars/") |
|
|
|
|
|
queries = { |
|
|
"1. List all cars manufactured by Ferrari": """ |
|
|
PREFIX ex: <http://example.org/cars/> |
|
|
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
|
|
|
|
|
SELECT ?car_name |
|
|
WHERE { |
|
|
?car ex:hasManufacturer ?manu . |
|
|
?manu rdfs:label ?manu_name . |
|
|
FILTER (REGEX(?manu_name, "Ferrari", "i")) |
|
|
?car rdfs:label ?car_name . |
|
|
} |
|
|
""", |
|
|
"2. Cars with HorsePower > 800": """ |
|
|
PREFIX ex: <http://example.org/cars/> |
|
|
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
|
|
|
|
|
SELECT ?car_name ?hp |
|
|
WHERE { |
|
|
?car ex:hasHorsePowerValue ?hp . |
|
|
?car rdfs:label ?car_name . |
|
|
FILTER (?hp > 800) |
|
|
} |
|
|
""", |
|
|
"3. Count of 2-Seater cars": """ |
|
|
PREFIX ex: <http://example.org/cars/> |
|
|
|
|
|
SELECT (COUNT(?car) as ?count) |
|
|
WHERE { |
|
|
?car a ex:Coupe . |
|
|
} |
|
|
""", |
|
|
"4. Average Price of cars": """ |
|
|
PREFIX ex: <http://example.org/cars/> |
|
|
|
|
|
SELECT (AVG(?price) as ?avg_price) |
|
|
WHERE { |
|
|
?car ex:hasPriceValue ?price . |
|
|
} |
|
|
""", |
|
|
"5. (Federated) Get Manufacturer Description from DBpedia": """ |
|
|
PREFIX ex: <http://example.org/cars/> |
|
|
PREFIX owl: <http://www.w3.org/2002/07/owl#> |
|
|
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
|
|
PREFIX dbo: <http://dbpedia.org/ontology/> |
|
|
|
|
|
SELECT ?manu_name ?desc |
|
|
WHERE { |
|
|
?manu a ex:Manufacturer ; |
|
|
rdfs:label ?manu_name ; |
|
|
owl:sameAs ?dbpedia_link . |
|
|
|
|
|
SERVICE <http://dbpedia.org/sparql> { |
|
|
?dbpedia_link dbo:abstract ?desc . |
|
|
FILTER (LANG(?desc) = 'en') |
|
|
} |
|
|
} |
|
|
LIMIT 3 |
|
|
""" |
|
|
} |
|
|
|
|
|
print(f"Loaded Graph with {len(g)} triples.\n") |
|
|
|
|
|
|
|
|
for title, query in list(queries.items())[:-1]: |
|
|
print(f"--- {title} ---") |
|
|
try: |
|
|
results = g.query(query) |
|
|
for row in results: |
|
|
print(row) |
|
|
except Exception as e: |
|
|
print(f"Query Error: {e}") |
|
|
print("\n") |
|
|
|
|
|
|
|
|
print(f"--- 5. (Federated) Get Manufacturer Description from DBpedia ---") |
|
|
print("Attempting to run SERVICE query via rdflib (may fail due to DBpedia restrictions)...") |
|
|
fed_query = queries["5. (Federated) Get Manufacturer Description from DBpedia"] |
|
|
|
|
|
try: |
|
|
results = g.query(fed_query) |
|
|
for row in results: |
|
|
print(row) |
|
|
except Exception as e: |
|
|
print(f"Standard SERVICE query failed ({e}).\nData is likely interlinked, but local engine cannot negotiate with DBpedia.") |
|
|
print("Fallback: Verifying Interlinking manually via SPARQLWrapper...") |
|
|
|
|
|
try: |
|
|
from SPARQLWrapper import SPARQLWrapper, JSON |
|
|
sparql = SPARQLWrapper("http://dbpedia.org/sparql") |
|
|
|
|
|
|
|
|
manu_link_query = """ |
|
|
PREFIX ex: <http://example.org/cars/> |
|
|
PREFIX owl: <http://www.w3.org/2002/07/owl#> |
|
|
SELECT ?link WHERE { ?m a ex:Manufacturer ; owl:sameAs ?link . } LIMIT 1 |
|
|
""" |
|
|
res = g.query(manu_link_query) |
|
|
link = list(res)[0][0] |
|
|
print(f"Found Interlink: {link}") |
|
|
|
|
|
|
|
|
dbpedia_q = f""" |
|
|
PREFIX dbo: <http://dbpedia.org/ontology/> |
|
|
SELECT ?desc WHERE {{ <{link}> dbo:abstract ?desc . FILTER (LANG(?desc) = 'en') }} LIMIT 1 |
|
|
""" |
|
|
sparql.setQuery(dbpedia_q) |
|
|
sparql.setReturnFormat(JSON) |
|
|
results = sparql.query().convert() |
|
|
|
|
|
for result in results["results"]["bindings"]: |
|
|
print(f"Description from DBpedia: {result['desc']['value'][:200]}...") |
|
|
|
|
|
except ImportError: |
|
|
print("SPARQLWrapper not installed. Cannot run fallback.") |
|
|
except Exception as ex: |
|
|
print(f"Fallback failed: {ex}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
run_validation() |
|
|
|