My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from pprint import pprint, pformat
from sets import Set
from FuXi.Rete import *
from FuXi.Syntax.InfixOWL import *
from FuXi.Rete.AlphaNode import SUBJECT,PREDICATE,OBJECT,VARIABLE
from FuXi.Rete.RuleStore import N3RuleStore
from FuXi.Rete.Util import renderNetwork,generateTokenSet
from FuXi.Horn.PositiveConditions import Uniterm, BuildUnitermFromTuple
from FuXi.Horn.HornRules import HornFromN3
from FuXi.DLP import MapDLPtoNetwork, non_DHL_OWL_Semantics
from FuXi.Rete.Magic import MagicSetTransformation, iterCondition, AdornLiteral
from rdflib.Namespace import Namespace
from rdflib import plugin,RDF,RDFS,URIRef,URIRef
from rdflib.OWL import FunctionalProperty
from rdflib.store import Store
from cStringIO import StringIO
from rdflib.Graph import Graph,ReadOnlyGraphAggregate,ConjunctiveGraph
from rdflib.syntax.NamespaceManager import NamespaceManager
from glob import glob
from rdflib.sparql.bison import Parse
import unittest, os, time, itertools

RDFLIB_CONNECTION=''
RDFLIB_STORE='IOMemory'

CWM_NS = Namespace("http://cwmTest/")
DC_NS = Namespace("http://purl.org/dc/elements/1.1/")
STRING_NS = Namespace("http://www.w3.org/2000/10/swap/string#")
MATH_NS = Namespace("http://www.w3.org/2000/10/swap/math#")
FOAF_NS = Namespace("http://xmlns.com/foaf/0.1/")
OWL_NS = Namespace("http://www.w3.org/2002/07/owl#")
TEST_NS = Namespace("http://metacognition.info/FuXi/DL-SHIOF-test.n3#")
LOG = Namespace("http://www.w3.org/2000/10/swap/log#")
RDF_TEST = Namespace('http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#')
OWL_TEST = Namespace('http://www.w3.org/2002/03owlt/testOntology#')

MAGIC_PROOFS = True

queryNsMapping={'test':'http://metacognition.info/FuXi/test#',
'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'foaf':'http://xmlns.com/foaf/0.1/',
'dc':'http://purl.org/dc/elements/1.1/',
'rss':'http://purl.org/rss/1.0/',
'rdfs':'http://www.w3.org/2000/01/rdf-schema#',
'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'owl':OWL_NS,
'rdfs':RDF.RDFNS,
}

nsMap = {
u'rdfs' :RDFS.RDFSNS,
u'rdf' :RDF.RDFNS,
u'rete' :RETE_NS,
u'owl' :OWL_NS,
u'' :TEST_NS,
u'otest':OWL_TEST,
u'rtest':RDF_TEST,
u'foaf' :URIRef("http://xmlns.com/foaf/0.1/"),
u'math' :URIRef("http://www.w3.org/2000/10/swap/math#"),
}

MANIFEST_QUERY = \
"""
SELECT ?status ?premise ?conclusion ?feature ?descr
WHERE {
[
a otest:PositiveEntailmentTest;
otest:feature ?feature;
rtest:description ?descr;
rtest:status ?status;
rtest:premiseDocument ?premise;
rtest:conclusionDocument ?conclusion
]
}"""
PARSED_MANIFEST_QUERY = Parse(MANIFEST_QUERY)

Features2Skip = [
URIRef('http://www.w3.org/2002/07/owl#sameClassAs'),
]

Features2SkipMagic = [
#FunctionalProperty
]

Tests2Skip = [
#'OWL/oneOf/Manifest003.rdf', #logical set equivalence? (works for magic sets)
#'OWL/differentFrom/Manifest002.rdf' ,#needs log:notEqualTo (works for magic sets)
#'OWL/distinctMembers/Manifest001.rdf',#needs log:notEqualTo (works for magic sets)
#'OWL/unionOf/Manifest002.rdf',#can't implement set theoretic union for owl:unionOf. (works for magic sets)
'OWL/InverseFunctionalProperty/Manifest001.rdf',#owl:sameIndividualAs deprecated
'OWL/FunctionalProperty/Manifest001.rdf', #owl:sameIndividualAs deprecated
'OWL/Nothing/Manifest002.rdf',# owl:sameClassAs deprecated
#'OWL/AllDifferent/Manifest001.rdf',#requires support for built-ins (log:notEqualTo), works for magic sets


#Fix for magic set implementation
#'OWL/TransitiveProperty/Manifest001.rdf', #recursive logic program
#'OWL/InverseFunctionalProperty/Manifest002.rdf', #need to investigate
]

patterns2Skip = [
'OWL/cardinality',
'OWL/samePropertyAs'
]

class OwlTestSuite(unittest.TestCase):
def setUp(self):
store = plugin.get(RDFLIB_STORE,Store)()
store.open(RDFLIB_CONNECTION)
self.ruleStore=N3RuleStore()
self.ruleGraph = Graph(self.ruleStore)
self.ruleFactsGraph = Graph(store)
# if not MAGIC_PROOFS:
# self.ruleGraph.parse(StringIO(non_DHL_OWL_Semantics),format='n3')
self.network = ReteNetwork(self.ruleStore,nsMap=nsBinds)

#renderNetwork(self.network,nsMap=nsMap).write_graphviz('owl-rules.dot')
def tearDown(self):
pass

def calculateEntailments(self, factGraph):
# print self.network
print "Explicit (base) facts"
# pprint([BuildUnitermFromTuple(i) for i in factGraph])
pprint(list(factGraph))
start = time.time()
self.network.feedFactsToAdd(generateTokenSet(factGraph))
sTime = time.time() - start
if sTime > 1:
sTimeStr = "%s seconds"%sTime
else:
sTime = sTime * 1000
sTimeStr = "%s milli seconds"%sTime
print "Time to calculate closure on working memory: ",sTimeStr
print self.network

tNodeOrder = [tNode
for tNode in self.network.terminalNodes
if self.network.instanciations.get(tNode,0)]
tNodeOrder.sort(key=lambda x:self.network.instanciations[x],reverse=True)
for termNode in tNodeOrder:
print termNode
print "\t", termNode.clause
print "\t\t%s instanciations"%self.network.instanciations[termNode]
# for c in AllClasses(factGraph):
# print CastClass(c,factGraph)
print "=============="
self.network.inferredFacts.namespace_manager = factGraph.namespace_manager
# if self.network.inferredFacts:
# print "Implicit facts: "
# print self.network.inferredFacts.serialize(format='turtle')
print "ruleset after MST:"
pprint(list(self.network.rules))
# print "rate of reduction in the size of the program: ", len len(self.network.rules)
return sTimeStr

Comment by chimezie, Dec 20, 2008:
Below is a example of how to use the FuXi.Rete.Magic.MagicSetTransformation method to translate horn rulesets (parsed directly from N3 or generated from OWL-DL via DLP) into a corresponding ruleset that (when evaluated) cuts down on the irrelevant facts that are generated.

goals - a list of tuples that we are querying for 
rules - the original ruleset
factGraph - the base predicate relations (an RDF graph)
def MagicOWLProof(self,goals,rules,factGraph):
print "Goals",[AdornLiteral(goal) for goal in goals]
assert not self.network.rules
progLen = len(rules)
magicRuleNo = 0
for rule in MagicSetTransformation(factGraph,
rules,
goals):
magicRuleNo+=1
self.network.buildNetworkFromClause(rule.formula)
self.network.rules.add(rule)
print "rate of reduction in the size of the program: ", (100-(float(magicRuleNo)/float(progLen))*100)
for goal in goals:
factGraph.add(goal)
timing=self.calculateEntailments(factGraph)
for goal in goals:
if goal not in self.network.inferredFacts and goal not in factGraph:
print "missing triple %s"%(pformat(goal))
raise #Exception ("Failed test: "+feature)
else:
print "=== Passed! ==="
return timing

def testOwl(self):
testData = {}
for manifest in glob('OWL/*/Manifest*.rdf'):
if manifest in Tests2Skip:
continue
skip = False
for pattern2Skip in patterns2Skip:
if manifest.find(pattern2Skip) > -1:
skip = True
break
if skip:
continue
manifestStore = plugin.get(RDFLIB_STORE,Store)()
manifestGraph = Graph(manifestStore)
manifestGraph.parse(open(manifest))
rt = manifestGraph.query(
PARSED_MANIFEST_QUERY,
initNs=nsMap,
DEBUG = False)
#print list(manifestGraph.namespace_manager.namespaces())
for status,premise,conclusion, feature, description in rt:
if feature in Features2Skip:
continue
premise = manifestGraph.namespace_manager.compute_qname(premise)[-1]
conclusion = manifestGraph.namespace_manager.compute_qname(conclusion)[-1]
premiseFile = '/'.join(manifest.split('/')[:2]+[premise])
conclusionFile = '/'.join(manifest.split('/')[:2]+[conclusion])
print premiseFile
print conclusionFile
if status == 'APPROVED':
assert os.path.exists('.'.join([premiseFile,'rdf']))
assert os.path.exists('.'.join([conclusionFile,'rdf']))
print "<%s> :- <%s>"%('.'.join([conclusionFile,'rdf']),
'.'.join([premiseFile,'rdf']))
store = plugin.get(RDFLIB_STORE,Store)()
store.open(RDFLIB_CONNECTION)
factGraph = Graph(store)
factGraph.parse(open('.'.join([premiseFile,'rdf'])))
allFacts = ReadOnlyGraphAggregate([factGraph,self.ruleFactsGraph])
Individual.factoryGraph=factGraph

for c in AllClasses(factGraph):
if not isinstance(c.identifier,BNode):
print c.__repr__(True)

if MAGIC_PROOFS:
if feature in Features2SkipMagic:
continue
print premiseFile,feature,description
program=list(HornFromN3(StringIO(non_DHL_OWL_Semantics)))
program.extend(self.network.setupDescriptionLogicProgramming(
factGraph,
addPDSemantics=False,
constructNetwork=False))
print "Original program"
pprint(program)
timings=[]
#Magic set algorithm is initiated by a query, a single horn ground, 'fact'
try:
goals=set()
for triple in Graph(store).parse('.'.join([conclusionFile,'rdf'])):
if triple not in factGraph:
goals.add(triple)
timings.append(self.MagicOWLProof(goals,program,factGraph))
self.network._resetinstanciationStats()
self.network.reset()
self.network.clear()
except:
print "missing triple %s"%(pformat(goal))
print manifest, premiseFile
print "feature: ", feature
print description
# from FuXi.Rete.Util import renderNetwork
# pprint([BuildUnitermFromTuple(t) for t in self.network.inferredFacts])
# dot=renderNetwork(self.network,self.network.nsMap).write_jpeg('test-fail.jpeg')
raise #Exception ("Failed test: "+feature)

testData[manifest] = timings
continue
else:
self.network.setupDescriptionLogicProgramming(factGraph,addPDSemantics=True)
sTimeStr=self.calculateEntailments(factGraph)
expectedFacts = Graph(store)
for triple in expectedFacts.parse('.'.join([conclusionFile,'rdf'])):
closureGraph = ReadOnlyGraphAggregate([self.network.inferredFacts,factGraph])
if triple not in self.network.inferredFacts and triple not in factGraph:
print "missing triple %s"%(pformat(triple))
print manifest
print "feature: ", feature
print description
pprint(list(self.network.inferredFacts))
# pprint(programs)
raise Exception ("Failed test: "+feature)
else:
print "=== Passed! ==="
#pprint(list(self.network.inferredFacts))
print "\n"
testData[manifest] = sTimeStr
store.rollback()
self.network.reset()
self.network._resetinstanciationStats()

pprint(testData)

def runTests(profile=False):
suite = unittest.makeSuite(OwlTestSuite)
if profile:
#from profile import Profile
from hotshot import Profile, stats
p = Profile('fuxi.profile')
#p = Profile()
p.runcall(unittest.TextTestRunner(verbosity=5).run,suite)
p.close()
s = stats.load('fuxi.profile')
# s=p.create_stats()
s.strip_dirs()
s.sort_stats('time','cumulative','pcalls')
s.print_stats(.1)
s.print_callers(.05)
s.print_callees(.05)
else:
unittest.TextTestRunner(verbosity=5).run(suite)

if __name__ == '__main__':
runTests(profile=False)
Show details Hide details

Change log

r249 by chimezie on Dec 20, 2008   Diff
updated test suite to use magic set
implementation (covers additional tests
that original rules don't)
Go to: 
Sign in to write a code review

Older revisions

r228 by chimezie on Jul 31, 2008   Diff
Fixed OWL test to use
setupDescriptionLogicProgramming
method
r219 by chimezie on Jul 17, 2008   Diff
Minor fixes to test suite harness
r203 by chimezie on Jun 29, 2008   Diff
fixed OWL test suite harness to use
DLP functionality (all OWL tests input
documents are passed through the
transformation to convert them to a
ruleset which is run against the
...
All revisions of this file

File info

Size: 13777 bytes, 304 lines

File properties

svn:executable
*
Hosted by Google Code