# As a note, this program actually takes a few minutes to run. # Don't take that as a general problem with RDF though! from rdflib import Graph, Namespace, Literal # Define some namespaces rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#"); dc = Namespace("http://purl.org/dc/elements/1.1/") foaf = Namespace("http://xmlns.com/foaf/0.1/"); time = Namespace("http://pervasive.semanticweb.org/ont/2004/06/time#"); census = Namespace("tag:govshare.info,2005:rdf/census/") usgovt = Namespace("tag:govshare.info,2005:rdf/usgovt/"); pol = Namespace("tag:govshare.info,2005:rdf/politico/"); # Create a store to hold RDF data store = Graph(); # Load the N3 file with the states data store.parse("states.n3", None, 'n3'); store.parse("people.rdf", None, 'xml'); store.parse("orgs.rdf", None, 'xml'); # Loop through every statement in the graph of the form # ( ____ rdf:type usgovt:State). maxpop = 0 maxstate = None; for statement in store.triples((None, rdf["type"], usgovt["State"])): state = statement[0] # the subject of the statement population = int(store.value(state, census["population"], None)) if (population > maxpop) : maxpop = population maxstate = state print maxpop print maxstate for office in store.subjects(pol["represents"], maxstate) : for role in store.subjects(pol["forOffice"], office) : enddate1 = store.value(role, time["to"], None) ; enddate2 = store.value(enddate1, time["at"], None) ; if (str(enddate2) > "2006-03-27") : for person in store.subjects(pol["hasRole"], role) : print store.value(person, foaf["name"], None) ;