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: PREFIX rdfs: 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: PREFIX rdfs: 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: SELECT (COUNT(?car) as ?count) WHERE { ?car a ex:Coupe . } """, "4. Average Price of cars": """ PREFIX ex: SELECT (AVG(?price) as ?avg_price) WHERE { ?car ex:hasPriceValue ?price . } """, "5. (Federated) Get Manufacturer Description from DBpedia": """ PREFIX ex: PREFIX owl: PREFIX rdfs: PREFIX dbo: SELECT ?manu_name ?desc WHERE { ?manu a ex:Manufacturer ; rdfs:label ?manu_name ; owl:sameAs ?dbpedia_link . SERVICE { ?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: PREFIX 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: 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()