File size: 4,724 Bytes
1d12e97 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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")
# Run Local Queries
for title, query in list(queries.items())[:-1]: # Skip the last one for special handling
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")
# Run Federated Query (Special Handling)
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")
# Get a manufacturer link from our graph
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}")
# Query DBpedia for that 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()
|