The following example shows how to take account of scopes in the Python API to 4RDF.
#What Joe's Web catalog says SOURCE1 = "http://joewebcat.net" RDF1 = """ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> <rdf:Description about="http://www.w3.org"> <dc:title>W3C</dc:title> <dc:description>A consortium for promoting Web standards</dc:description> <dc:format>text/html</dc:format> <dc:language>en</dc:language> </rdf:Description> </rdf:RDF> """ #What Tito's directory entry says SOURCE2 = "http://titoworld.net" RDF2 = """ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> <rdf:Description about="http://www.w3.org"> <dc:title>The World Wide Web Consortium</dc:title> <dc:description>The World Wide Web Consortium (W3C) develops interoperable technologies (specifications, guidelines, software, and tools) to lead the Web to its full potential.</dc:description> <dc:format>text/xhtml</dc:format> <dc:language>en</dc:language> </rdf:Description> </rdf:RDF> """ NSMAP = { "dc": "http://purl.org/dc/elements/1.1/", } from Ft.Rdf import Util from Ft.Xml import Domlette from Ft.Rdf.Serializers.Dom import Serializer #Use the memory driver #Create the first model by reading in the source doc model, db = Util.DeserializeFromString(RDF1, scope=SOURCE1) #To use the Postgres driver (Pygres required), uncomment the following 2 lines #from Ft.Rdf.Drivers import Postgres #model, db = Util.DeserializeFromString(RDF1, driver=Postgres, dbName='scopetest', create=1, scope=SOURCE1) #Use "dropdb ft__scopetest" to clean up reader = Domlette.NonvalidatingReader doc = reader.parseString(RDF2, SOURCE2) ser = Serializer() ser.deserialize(model, doc, scope=SOURCE2) #Show all statements in the model stmts = model.complete(None, None, None) for stmt in stmts: print stmt #As an illustration, show all the statements sorted back into scopes stmt_dict = {} for stmt in stmts: if not stmt_dict.has_key(stmt.scope): stmt_dict[stmt.scope] = [] stmt_dict[stmt.scope].extend([stmt]) print; print for key in stmt_dict.keys(): print print "Scope: ", key print "-"*50 for stmt in stmt_dict[key]: print stmt print #Now query the title of the W3C Web site result = Util.VersaQuery("@'http://www.w3.org' - dc:title -> *", model, nsMap=NSMAP) print "@'http://www.w3.org' - dc:title -> * result: ", result #Hmm. Now let us say we only want to see what Joe had to say result = Util.VersaQuery("@'http://www.w3.org' - dc:title -> *", model, scope=SOURCE1, nsMap=NSMAP) print "@'http://www.w3.org' - dc:title -> * result (scoped): ", result
