#!/usr/bin/python """An RDF diff program.""" __author__ = 'Sean B. Palmer' __license__ = 'Copyright (C) 2002 Sean B. Palmer. GNU GPL 2' import sys from eep3 import api, query, ntuples def lists(store): ground, vars, uvars = api.TripleStore(), api.TripleStore(), [] for tuple in store: if len(tuple) == 3: var = 0 for t in tuple: if t.type == 'Univar': var = 2 elif t.type == 'Exivar': var = 1 [ground, vars, uvars][var].append(tuple) # , None) return ground, vars def diff(StoreA, StoreB): # for each graph, make a store of ground and vared tuples groundA, varsA = lists(StoreA) groundB, varsB = lists(StoreB) # do the grounds first print 'Ground triples in A, B:', `len(groundA)`+',', len(groundB) # print groundA, groundB AinB = 0 AnotB = api.TripleStore() BnotA = api.TripleStore() for triple in groundA: if triple not in groundB: AnotB.append(triple) else: AinB += 1 for triple in groundB: if triple not in groundA: BnotA.append(triple) print 'Triples common to A and B:', AinB print 'Triples in A not in B:', len(AnotB) print 'Triples in B not in A:', len(BnotA) print 'Triples removed:-' print ntuples.serialize(AnotB) print 'Triples added:-' print ntuples.serialize(BnotA) if __name__=="__main__": if len(sys.argv) == 3: import n3 # a = n3.parse(open(sys.argv[1], 'r').read(), '') # b = n3.parse(open(sys.argv[2], 'r').read(), '') a = ntuples.parsent(open(sys.argv[1], 'r').read()) b = ntuples.parsent(open(sys.argv[2], 'r').read()) diff(a, b) else: print __doc__ a = ntuples.parsent('<#x> <#y> <#z> . <#p> <#q> <#r> .') b = ntuples.parsent('<#s> <#t> <#u> . <#x> <#y> <#z> .') diff(a, b)