1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import unittest
from tempfile import mkdtemp
import shutil
from rdflib.term import URIRef
from rdflib.term import BNode
from rdflib.term import Literal
from rdflib.term import Variable
from rdflib.namespace import Namespace
from rdflib.graph import Graph
LOG = Namespace("http://www.w3.org/2000/10/swap/log#")
try:
from pychinko import terms
from pychinko.interpreter import Interpreter
def _convert(node):
if isinstance(node, Variable):
return terms.Variable(node)
# return node
elif isinstance(node, BNode):
return terms.Exivar(node)
elif isinstance(node, URIRef):
# return terms.URI(node)
return node
elif isinstance(node, Literal):
return node
else:
raise Exception("Unexpected Type: %s" % type(node))
def patterns(g):
for s, p, o in g:
yield terms.Pattern(_convert(s), _convert(p), _convert(o))
def facts(g):
for s, p, o in g:
if (
p != LOG.implies
and not isinstance(s, BNode)
and not isinstance(o, BNode)
):
yield terms.Fact(_convert(s), _convert(p), _convert(o))
class PychinkoTestCase(unittest.TestCase):
backend = "default"
tmppath = None
def setUp(self):
self.g = Graph(store=self.backend)
self.tmppath = mkdtemp()
self.g.open(configuration=self.tmppath)
self.g.parse("test/a.n3", format="n3")
def tearDown(self):
self.g.close()
shutil.rmtree(tmppath)
def testPychinko(self):
rules = []
for s, p, o in self.g.triples((None, LOG.implies, None)):
lhs = list(patterns(s))
rhs = list(patterns(o))
rules.append(terms.Rule(lhs, rhs, (s, p, o)))
interp = Interpreter(rules)
f = Graph()
f.parse("http://eikeon.com/")
source = f
source = self.g
interp.addFacts(set(facts(source)), initialSet=True)
interp.run()
# _logger.debug("inferred facts: %s" % interp.inferredFacts)
except ImportError as e:
print("Could not test Pychinko: %s" % e)
|