[pypy-svn] r33780 - in pypy/dist/pypy/lib/pyontology: . test
ale at codespeak.net
ale at codespeak.net
Thu Oct 26 18:00:14 CEST 2006
Author: ale
Date: Thu Oct 26 18:00:12 2006
New Revision: 33780
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
Log:
Bugfixes, among other things a stupid error in intersectionOf. The same error is in unionOf, which is the next thing to fix
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 Thu Oct 26 18:00:12 2006
@@ -53,6 +53,7 @@
raise ConsistencyFailure
class NothingConstraint(AbstractConstraint):
+ cost = 1
def __init__(self, variable):
AbstractConstraint.__init__(self, [variable])
@@ -82,7 +83,7 @@
superdom = domains[self.object]
vals = []
vals += list(superdom.getValues())
- vals += list(subdom.getValues()) +[self.variable]
+ vals += list(subdom.getValues()) #+[self.variable]
superdom.setValues(vals)
return 0
@@ -102,6 +103,7 @@
Thing_uri = URIRef(u'http://www.w3.org/2002/07/owl#Thing')
class PropertyConstrain(AbstractConstraint):
+ cost = 1
def __init__(self, prop, variable, cls_or_restriction):
AbstractConstraint.__init__(self, [ prop])
@@ -119,6 +121,7 @@
dom.removeValue(p)
class PropertyConstrain2(AbstractConstraint):
+ cost = 1
def __init__(self, prop, variable, cls_or_restriction):
AbstractConstraint.__init__(self, [ prop])
self.object = cls_or_restriction
@@ -142,6 +145,7 @@
sub.removeValues([v for v in sub.getValues() if not v in keep])
class MemberConstraint(AbstractConstraint):
+ cost = 1
def __init__(self, variable, cls_or_restriction):
AbstractConstraint.__init__(self, [ cls_or_restriction])
@@ -182,7 +186,7 @@
rangedom = domains[self.object]
for cls,pval in propdom.getValues():
if pval not in rangedom:
- raise ConsistencyFailure("Value %r of property %r not in range %r"%(pval, self.variable, self.object))
+ raise ConsistencyFailure("Value %r of property %r not in range %r : %r"%(pval, self.variable, self.object, rangedom.size()))
class DomainConstraint(SubClassConstraint):
@@ -193,7 +197,7 @@
domaindom = domains[self.object]
for cls,val in propdom.getValues():
if cls not in domaindom:
- raise ConsistencyFailure("Value %r of property %r not in domain %r : %r"%(cls, self.variable, self.object, domaindom.getValues()))
+ raise ConsistencyFailure("Value %r of property %r not in domain %r : %r"%(cls, self.variable, self.object, domaindom.size()))
class SubPropertyConstraint(SubClassConstraint):
@@ -434,16 +438,11 @@
def narrow(self, domains):
val = list(domains[self.List].getValues())
- intersection = domains[val[0]].getValues()
+ inter = set(domains[val[0]].getValues())
for v in val[1:]:
- vals= domains[v]
- remove = []
- for u in intersection:
- if not u in vals:
- remove.append(u)
- for u in remove:
- intersection.remove(u)
- cls = domains[self.variable].setValues(intersection)
+ inter = inter.intersection(set(domains[v].getValues()))
+ assert len(inter) > 0
+ cls = domains[self.variable].setValues(inter)
term = {}
for l in [domains[x] for x in val]:
if hasattr(l,'TBox'):
Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py (original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py Thu Oct 26 18:00:12 2006
@@ -12,7 +12,7 @@
log = py.log.Producer("Pyontology")
from pypy.tool.ansi_print import ansi_log
py.log.setconsumer("Pyontology", None)
-#py.log.setconsumer("Pyontology.exception", ansi_log)
+#py.log.setconsumer("Pyontology.ontfinish", ansi_log)
namespaces = {
'rdf' : 'http://www.w3.org/1999/02/22-rdf-syntax-ns',
@@ -131,7 +131,6 @@
elif ('owl_Thing' in variables.keys() and isinstance(self, ClassDomain)
and self.size() == 0):
variables[self.name].setValues(list(variables['owl_Thing'].getValues()))
-# log.finish("setting the domain %s to all individuals %r"%(self.name,variables[self.name]))
variables.update(self.domains)
glob_constraints.extend(self.in_constraint)
assert len([x for x in glob_constraints if type(x)==list])==0
@@ -154,12 +153,11 @@
self.removeValue(val)
def removeValue(self, value):
- log.removeValue("Removing %r of %r" % (value ,self.values))
if value in self.values:
self.values.pop(value)
if not self.values:
log.removeValue("Removed the lastvalue of the Domain")
- raise ConsistencyFailure("Removed the lastvalue of the Domain")
+ raise ConsistencyFailure("Removed the lastvalue of the Domain %r" % self)
def getBases(self):
return self._bases
@@ -234,7 +232,7 @@
(not hasattr(other,'uri') and self.uri == other) or
other in self.sameas):
return True
- if other in self.differentfrom:
+ if not other or other in self.differentfrom:
return False
else:
return None
@@ -290,8 +288,6 @@
return self._dict.items()
def addValue(self, key, val):
- if key == None:
- raise RuntimeError
self._dict.setdefault(key, [])
self._dict[key].append(val)
@@ -309,7 +305,7 @@
if not self._dict[k]:
self._dict.pop(k)
if not self._dict:
- raise ConsistencyFailure
+ raise ConsistencyFailure("Removed the last value of %s" % self.name)
def __contains__(self, (cls, val)):
if not cls in self._dict:
@@ -497,17 +493,22 @@
log("=============================")
def finish(self):
- for constraint in self.constraints:
- log.exception("Trying %r" %constraint)
+ cons = [(c.cost,c) for c in self.constraints if hasattr(c, 'cost')]
+ cons.sort()
+ for i,constraint in cons:
+ log.ontfinish("Trying %r of %d/%d " %(constraint,cons.index((i, constraint)),len(cons)))
for key in constraint.affectedVariables():
- log.exception("FINISHING %s" % key)
+ log.ontfinish("FINISHING %s" % key)
+ if not ( self.variables.get(key)):
+ break
if isinstance( self.variables[key], fd):
continue
self.variables[key].finish(self.variables, self.constraints)
-# try:
- constraint.narrow(self.variables)
-# except ConsistencyFailure, e:
-# print "FAilure", e
+ else:
+# try:
+ constraint.narrow(self.variables)
+# except ConsistencyFailure, e:
+# print "FAilure", e
def _sparql(self, query):
qe = SP.Query.parseString(query)[0]
@@ -669,9 +670,9 @@
res = pred(s, o)
#avar = self.make_var(ClassDomain, s)
#else:
- avar = self.make_var(Property, p)
+ pvar = self.make_var(Property, p)
# Set the values of the property p to o
-# self.type(s, Thing_uri)
+ self.type(s, Thing_uri)
sub = self.mangle_name(s)
if type(o) == URIRef:
obj = self.mangle_name(o)
@@ -681,7 +682,7 @@
val = Individual(obj, o)
else:
val = o
- propdom = self.variables[avar]
+ propdom = self.variables[pvar]
res = propdom.addValue(Individual(sub,s), val)
def resolve_item(self, item):
@@ -896,18 +897,12 @@
self.variables[svar].setValues(res)
def intersectionOf(self, s, var):
- var_list = self.flatten_rdf_list(var)
- vals = [self.make_var(ClassDomain, x) for x in self.variables[var_list].getValues()]
-
- res = vals[0]
- for l in vals[1:]:
- result = []
- for v in res:
- if v in self.variables[l].getValues() :
- result.append(v)
- res = result
+ avar = self.flatten_rdf_list(var)
+ res = [self.mangle_name(x) for x in self.variables[avar]]
+ self.variables[avar] = ClassDomain(avar, var, res)
svar = self.make_var(ClassDomain, s)
- self.variables[svar].setValues(res)
+ cons = IntersectionofConstraint(svar, avar)
+ self.constraints.append(cons)
#---Property Axioms---#000000#FFFFFF--------------------------------------------
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 Thu Oct 26 18:00:12 2006
@@ -111,36 +111,41 @@
c = ClassDomain('C')
con = SubClassConstraint('b','a')
con2 = SubClassConstraint('c','b')
+ indi = URIRef('indi')
+ c.setValues([Individual('indi_',indi)])
con.narrow({'a': a, 'b': b, 'c': c})
con2.narrow({'a': a, 'b': b, 'c': c})
con.narrow({'a': a, 'b': b, 'c': c})
- assert 'b' in a
- assert 'c' in a
+ assert Individual('indi_', indi) in a
def test_subClassconstraintMulti():
a = ClassDomain('A')
b = ClassDomain('B')
c = ClassDomain('C')
+ indi = URIRef('indi')
+ c.setValues([Individual('indi_',indi)])
con = SubClassConstraint('c','a')
con2 = SubClassConstraint('c','b')
con.narrow({'a': a, 'b': b, 'c': c})
con2.narrow({'a': a, 'b': b, 'c': c})
- assert 'c' in a
- assert 'c' in b
+ assert Individual('indi_', indi) in a
+ assert Individual('indi_', indi) in b
def test_subClassconstraintMulti2():
a = ClassDomain('A')
b = ClassDomain('B')
c = ClassDomain('C')
+ indi = URIRef('indi')
+ c.setValues([Individual('indi_',indi)])
con = SubClassConstraint('c','a')
con2 = SubClassConstraint('c','b')
con3 = SubClassConstraint('a','c')
con.narrow({'a': a, 'b': b, 'c': c})
con2.narrow({'a': a, 'b': b, 'c': c})
con3.narrow({'a': a, 'b': b, 'c': c})
- assert 'c' in a
- assert 'c' in b
- assert 'a' in c
+ assert Individual('indi_', indi) in a
+ assert Individual('indi_', indi) in b
+ assert Individual('indi_', indi) in c
def test_equivalentClass():
O = Ontology()
More information about the Pypy-commit
mailing list