# An implemention of 'assert' and 'report' for data validation.
from terms import Rule, Variable, Pattern, URI
from rdflib.TripleStore import TripleStore
from pychinkafon import PychinkoN3Sink, N3Parser
from helpers import removedups
import exception
firstURI = URI("http://www.daml.org/2001/03/daml+oil#first")
restURI = URI("http://www.daml.org/2001/03/daml+oil#rest")
assertURI = URI("http://www.mindswap.org/~katz/pychinko/builtins#assert")
reportURI = URI("http://www.mindswap.org/~katz/pychinko/builtins#report")
# ReportTriples = {}
# AssertTriples = {}
# GetReportVals = Rule([Pattern(Variable("s"), reportURI, Variable("listName")),
# Pattern(Variable("listName"), firstURI, Variable("p")),
# Pattern(Variable("elt"), restURI, Variable("rest")),
# Pattern(Variable("rest"), firstURI, Variable("o"))],
# [AddList(ReportTriples, Variable("s"), Variable("p"), Variable("o"))])
# GetReportVals = Rule([Pattern(Variable("s"), assertURI, Variable("listName")),
# Pattern(Variable("listName"), firstURI, Variable("p")),
# Pattern(Variable("elt"), restURI, Variable("rest")),
# Pattern(Variable("rest"), firstURI, Variable("o"))],
# [AddList(AssertTriples, Variable("s"), Variable("p"), Variable("o"))])
class AbstractStore(object):
"""Holder Pychinkafon store that simulates some RDFLib store functionality"""
def __init__(self, spo):
self.spo = spo
def triples(self, pattern=None):
for i in self.spo:
print i
return self.spo
def getAssertedTriples(store):
"""I retrieve (by query) and index the triples that are asserted."""
pass
def getReportedTriples(store):
"""I retrieve (by query) and index the triples that are reported."""
from simplequery import SimpleQueryEvaluator
querier = SimpleQueryEvaluator(store)
Query = """
# @prefix wn: .
# @prefix foaf: .
# @prefix dc: .
# @prefix math: .
@prefix string: .
@prefix builtins: .
@prefix daml: .
@prefix : .
?s builtins:report ?listName.
#?listName daml:first ?p.
#?listName daml:rest ?rest.
#?rest daml:first ?o.
"""
rows = removedups(querier.queryFromString(Query))
subj = Variable("s")
pred = Variable("p")
obj = Variable("o")
for i in rows:
if subj in i:
print "s:",i[subj]
if pred in i:
print "p:",i[pred]
if obj in i:
print "o:",i[obj]
class ValidationBuiltin(object):
"""I am a special builtin used to validate data in the KB."""
def __init__(self, name, index):
self.name = name
self.index = index
class Assert(ValidationBuiltin):
"""I am false if a triple (s p o) is *not found* in the fact base."""
def __init__(self, index):
ValidationBuiltin.__init__(self, "Assert", index)
def evaluate(self, subj, pred, obj):
if not self.index.match(subj, pred, obj):
raise exception.AssertError(subj, pred, obj)
else:
return True
class Report(ValidationBuiltin):
"""I am false if a triple (s p o) is *found* in the fact base."""
def __init__(self, store):
ValidationBuiltin.__init__(self, "Report", store)
def evaluate(self, subj, pred, obj):
if self.index.match(subj, pred, obj):
raise exception.ReportError(subj, pred, obj)
else:
return True
if __name__ == '__main__':
factsFile = 'file:///home/katz/semweb/svn.mindswap.org/pychinko/testfacts.n3'
# store = TripleStore()
# factsFile = 'testfacts2.n3'
# store.load(factsFile, 'nt')
store = PychinkoN3Sink()
p = N3Parser(store, factsFile)
p.load(factsFile)
queryStore = AbstractStore(store.facts)
getReportedTriples(queryStore)