[pypy-svn] r32997 - in pypy/dist/pypy/lib/pyontology: . test
ale at codespeak.net
ale at codespeak.net
Sun Oct 8 14:51:07 CEST 2006
Author: ale
Date: Sun Oct 8 14:51:04 2006
New Revision: 32997
Modified:
pypy/dist/pypy/lib/pyontology/constraint_classes.py
pypy/dist/pypy/lib/pyontology/pyontology.py
pypy/dist/pypy/lib/pyontology/test/test_ontology.py
pypy/dist/pypy/lib/pyontology/test/test_sparql.py
Log:
More sparql + bugfixes
Modified: pypy/dist/pypy/lib/pyontology/constraint_classes.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/constraint_classes.py (original)
+++ pypy/dist/pypy/lib/pyontology/constraint_classes.py Sun Oct 8 14:51:04 2006
@@ -115,11 +115,32 @@
dom = domains[self.prop]
vals = list(dom.getValues())
for p in vals:
-# objs = domains[p].getValuesPrKey(self.variable)
if not ((self.variable, self.object) in domains[p]):
dom.removeValue(p)
-
+class PropertyConstrain2(AbstractConstraint):
+ def __init__(self, prop, variable, cls_or_restriction):
+ AbstractConstraint.__init__(self, [ prop])
+ self.object = cls_or_restriction
+ self.variable = variable
+ self.prop = prop
+
+ def narrow(self, domains):
+ # Narrow the list of properties (instances of some property type)
+ # to those who has a pair (self.variable, self.object)
+ dom = domains[self.prop]
+ sub = domains[self.variable]
+ vals = list(dom.getValues())
+ keep = []
+ for p in vals:
+ items = domains[p].getValuesPrKey()
+ for key, obj_list in items:
+ if not self.object in obj_list:
+ dom.removeValue(p)
+ else:
+ keep.append(key)
+ sub.removeValues([v for v in sub.getValues() if not v in keep])
+
class MemberConstraint(AbstractConstraint):
def __init__(self, variable, cls_or_restriction):
Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py (original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py Sun Oct 8 14:51:04 2006
@@ -85,12 +85,20 @@
self.bases = []
self.finished = False
self.value = None
+ self.type = []
def finish(self, variables, glob_constraints):
# The finish method constructs the constraints
if not self.finished:
log.finish("%s" % self.name)
# Try to initialise constraints for this class
+ if len(self.type) > 1:
+ #try to merge the domains of the types
+ expr = []
+ for typ in self.type:
+ expr +=[ "%s == %s" % (self.name, typ)]
+ expr = ' and '.join(expr)
+ self.in_constraint.append(Expression([self.name]+self.type, expr))
prop = getattr(self, 'property')
val = getattr(self, 'value')
if prop:
@@ -433,7 +441,7 @@
self.nr_of_triples = 0
self.time = time.time()
for pr in builtin_voc:
- name = self.make_var(ClassDomain, pr)
+ name = self.mangle_name(pr)
# Instantiate ClassDomains to record instances of the types
name = name + "_type"
self.variables[name] = ClassDomain(name, pr)
@@ -488,8 +496,8 @@
new = []
for trip in triples:
- case = 1
- inc = 0
+ case = 0
+ inc = 1
newtrip = []
trip_ = [trip.Subject[0], trip.Verb[0], trip.Object[0]]
for item in trip_:
@@ -501,10 +509,7 @@
elif item.VAR1:
newtrip.append(URIRef('query_'+item.VAR1[0][0]))
case += trip_.index(item) + inc
- if inc:
- inc = 1
- else:
- inc = 2
+ inc = 2
else:
newtrip.append(item[0][0])
newtrip.append(case)
@@ -532,14 +537,19 @@
case = trip.pop(-1)
if case == 0:
# Check if this triple entails
- self.consider_triple(trip)
+ sub = self.mangle_name(trip[0])
+ prop = self.mangle_name(trip[1])
+ obj = self.mangle_name(trip[2])
+ if not obj in self.variables[prop].getValuesPrKey(sub):
+ raise ConsistencyFailure
elif case == 1:
# Add a HasValue constraint
var = self.make_var(Restriction, URIRef(trip[0]))
self.onProperty(var, URIRef(trip[1]))
self.hasValue(var, trip[2])
elif case == 2:
- # for all p's return p if p[0]==s and p[1]==o
+ # for all p's return p if p[0]==s and p[1]==o
+
prop_name = self.make_var(ClassDomain, URIRef(trip[1]))
indi_name = self.mangle_name(trip[0])
indi = Individual(indi_name, trip[0])
@@ -548,9 +558,7 @@
obj = self.variables[self.mangle_name(trip[2])]
else:
obj = trip[2]
- self.type(URIRef(trip[1]), URIRef(namespaces['rdf']+'#Property'))
prop = self.variables[prop_name]
-
prop.setValues(list(self.variables['rdf_Property_type'].getValues()))
# Get all properties by looking at 'rdf_Property_type'
# add a constraint trip[0] in domains[prop] and trip[2] in domains[prop].getValuesPrKey(trip[0])
@@ -565,7 +573,19 @@
self.variables[var].setValues((p_vals))
elif case == 4:
# for all p's return p[0] if p[1]==o
- pass
+
+ sub_name = self.make_var(ClassDomain, URIRef(trip[0]))
+ prop_name = self.make_var(ClassDomain, URIRef(trip[1]))
+ sub = self.variables[sub_name]
+ sub.setValues(list(self.variables['owl_Thing'].getValues()))
+ prop = self.variables[prop_name]
+ prop.setValues(list(self.variables['rdf_Property_type'].getValues()))
+ obj_name = self.mangle_name(trip[2])
+ if obj_name in self.variables:
+ obj = self.variables[self.mangle_name(trip[2])]
+ else:
+ obj = trip[2]
+ self.constraints.append(PropertyConstrain2(prop_name, sub_name, obj))
elif case == 5:
# return the values of p
prop = self.make_var(Property, URIRef(trip[1]))
@@ -722,7 +742,7 @@
# var is not one of the builtin classes -> it is a Thing
self.type(s, Thing_uri)
svar = self.make_var(Individual, s)
-# self.variables[svar].type.append(avar)
+ self.variables[svar].type.append(avar)
self.constraints.append(MemberConstraint(svar, avar))
else:
# var is a builtin class
Modified: pypy/dist/pypy/lib/pyontology/test/test_ontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/test_ontology.py (original)
+++ pypy/dist/pypy/lib/pyontology/test/test_ontology.py Sun Oct 8 14:51:04 2006
@@ -143,9 +143,13 @@
O = Ontology()
O.make_var(ClassDomain, obj)
O.type(sub, obj)
+ O.type(obj, namespaces['owl']+"#Class")
assert O.variables[O.make_var(None, sub)].__class__ == Individual
+# test for multiple types
+# test for type hierarchy
+
def test_ObjectProperty():
sub = URIRef('a')
obj = URIRef(namespaces['owl']+'#ObjectProperty')
@@ -626,8 +630,8 @@
def test_allvalues_file():
O = Ontology()
- O.add_file('approved/allValuesfrom/premises002.rdf')
- O.add_file('approved/allValuesfrom/nonconclusions002.rdf')
+ O.add_file('approved/allValuesFrom/premises002.rdf')
+ O.add_file('approved/allValuesFrom/nonconclusions002.rdf')
def test_import():
O = Ontology()
@@ -714,4 +718,4 @@
O.consider_triple((first, URIRef(namespaces['rdf']+'#type'), URIRef(namespaces['owl']+'#SymmetricProperty')))
assert isinstance(O.variables['first_'], SymmetricProperty)
assert 'first_' in O.variables['rdf_Property_type'].getValues()
-
+ assert 'first_' in O.variables['owl_ObjectProperty_type'].getValues()
Modified: pypy/dist/pypy/lib/pyontology/test/test_sparql.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/test_sparql.py (original)
+++ pypy/dist/pypy/lib/pyontology/test/test_sparql.py Sun Oct 8 14:51:04 2006
@@ -74,7 +74,7 @@
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing">
<owl:oneOf rdf:parseType="Collection">
- <owl:Thing rdf:about="#s"/>
+ <owl:Thing rdf:about="http://example.org/ns#s"/>
</owl:oneOf>
</owl:Class>
<owl:Thing rdf:about="http://example.org/ns#sub">
@@ -96,11 +96,11 @@
def test_case_0():
""" Check if the triple is entailed """
- py.test.skip("Doesn't work yet")
- query = qt_proto % ('?x', 'ns:sub ns:p 123 .')
+ query = qt_proto % ('?x', 'ns:sub ns:p "a123" .')
O = Ontology()
O.add_file(StringIO(Onto))
+ O.attach_fd()
raises(ConsistencyFailure, O.sparql, query)
def test_case_1():
@@ -138,7 +138,6 @@
def test_case_4():
""" search for s in p """
- py.test.skip("Doesn't work yet")
query = qt_proto % ('?x ?y', '?x ?y 123 .')
O = Ontology()
@@ -147,6 +146,7 @@
O.sparql(query)
assert list(O.variables['query_x_'].getValues())[0].uri == u'http://example.org/ns#sub'
+ assert list(O.variables['query_y_'].getValues())[0] == 'ns_p' #u'http://example.org/ns#p'
def test_case_5():
""" for all p's return p[0] if p[1]==o """
More information about the Pypy-commit
mailing list