[pypy-svn] r21719 - in pypy/dist/pypy/lib/pyontology: . test
ale at codespeak.net
ale at codespeak.net
Thu Jan 5 14:35:50 CET 2006
Author: ale
Date: Thu Jan 5 14:35:49 2006
New Revision: 21719
Modified:
pypy/dist/pypy/lib/pyontology/pyontology.py
pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
A number of corrections and refactorings. Tests changed accordingly
Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py (original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py Thu Jan 5 14:35:49 2006
@@ -65,7 +65,10 @@
def setValues(self, values):
self.values = values
-
+
+class List(ClassDomain):
+ pass
+
class Property(ClassDomain):
# Property contains the relationship between a class instance and a value
# - a pair. To accomodate global assertions like 'range' and 'domain' anonymous
@@ -75,7 +78,7 @@
self._dict = {}
def getValues(self):
- return tuple(self._dict.items())
+ return self._dict.items()
def setValues(self, values):
for k,v in values:
@@ -98,12 +101,13 @@
pass
-class DataTypeProperty(Property):
-
+class DatatypeProperty(Property):
pass
-class Thing:
+class Thing(ClassDomain):
+ pass
+class DataRange(ClassDomain):
pass
class AllDifferent(ClassDomain):
@@ -111,7 +115,7 @@
# Syntactic sugar
pass
-class Nothing:
+class Nothing(ClassDomain):
pass
@@ -140,11 +144,6 @@
Property.__init__(self, name, values, bases)
self.constraint = SymmetricConstraint(name)
-class DataRange:
-
- def __init__(self):
- pass
-
class Restriction(ClassDomain):
pass
@@ -154,8 +153,8 @@
'ObjectProperty' : ObjectProperty,
'AllDifferent' : AllDifferent ,
## 'AnnotationProperty' : AnnotationProperty,
-## 'DataRange' : DataRange,
- 'DatatypeProperty' : DatatypeProperty,
+ 'DataRange' : DataRange,
+ 'DatatypeProperty' : DatatypeProperty,
## 'DeprecatedClass' : DeprecatedClass,
## 'DeprecatedProperty' : DeprecatedProperty,
'FunctionalProperty' : FunctionalProperty,
@@ -166,7 +165,8 @@
## 'OntologyProperty' : OntologyProperty,
'Restriction' : Restriction,
'SymmetricProperty' : SymmetricProperty,
- 'TransitiveProperty' : TransitiveProperty
+ 'TransitiveProperty' : TransitiveProperty,
+ 'List' : List
}
class Ontology(Graph):
@@ -191,11 +191,10 @@
else:
ns =''
func = p
-
- if ns in namespaces.items():
+ if ns in namespaces.values():
pred = getattr(self, func)
res = pred(s, p, o)
- if res == None:
+ if not res :
continue
if type(res) != list :
res = [res]
@@ -207,7 +206,7 @@
sub = self.make_var(fd, s)
obj = self.make_var(fd, o)
res = self.variables[avar].getValues()
- self.variables[avar].setValues(res + [(sub, obj)])
+ self.variables[avar].setValues(res + ((sub, obj),))
if self.variables.get(avar) and type(self.variables[avar]) == fd:
self.variables[avar] = fd(list(self.variables[avar].getValues()) + res)
else:
@@ -232,15 +231,14 @@
rep = Repository(self.variables.keys(), self.variables, self.constraints)
return Solver().solve(rep, verbose)
- def consistency(self):
+ def consistency(self, verbose=0):
rep = Repository(self.variables.keys(), self.variables, self.constraints)
- rep.consistency()
+ rep.consistency(verbose)
def get_list(self, subject):
res = []
first = list(self.objects(subject, rdf_first))
assert len(first) == 1
- self.seen[self.make_var(fd, subject, p)]= 1
if type(first[0]) == URIRef:
var = self.make_var(fd, first[0])
if var not in self.variables.keys():
@@ -248,7 +246,6 @@
res += first
rest = list(self.objects(subject, rdf_rest))
- self.seen[self.make_var(fd, subject, p)]= 1
if "#nil" in rest[0] :
return res
else:
@@ -311,16 +308,19 @@
#---------------- Implementation ----------------
def type(self, s, p, var):
- svar = self.make_var(ClassDomain, s)
- if (type(var) == URIRef and not
- (var in [URIRef(namespaces['owl']+'#'+x) for x in builtin_voc])):
+
+ #if (type(var) == URIRef and not
+ if not (var in [URIRef(namespaces['owl']+'#'+x) for x in builtin_voc]):
# var is not one of the builtin classes
+ svar = self.make_var(ClassDomain, s)
avar = self.make_var(ClassDomain, var)
self.variables[svar].setValues(self.variables[avar].getValues())
constrain = BinaryExpression([svar, avar],"%s in %s" %(svar, avar))
self.constraints.append(constrain)
- else:
+ else: # type(s) != BNode:
# var is a builtin class
+ svar = self.make_var(ClassDomain, s)
+ print "===",var
cls =builtin_voc[var.split('#')[-1]](name=svar)
if hasattr(cls, 'constraint'):
self.constraints.append(cls.constraint)
@@ -431,7 +431,7 @@
# TODO: implement this
pass
-#---Label---#000000#FFFFFF------------------------------------------------------
+#---Property restrictions------------------------------------------------------
def maxCardinality(self, s, p, var):
""" Len of finite domain of the property shall be less than or equal to var"""
@@ -512,6 +512,9 @@
self.__cost = 1
self.cardinality = cardinality
+ def __repr__(self):
+ return '<%s %s %i>' % (self.__class__.__name__, str(self._variables[0]), self.cardinality)
+
def narrow(self, domains):
"""narrowing algorithm for the constraint"""
if len(domains[self._variables[0]]) > self.cardinality:
@@ -525,7 +528,7 @@
"""narrowing algorithm for the constraint"""
if len(domains[self._variables[0]]) < self.cardinality:
- raise ConsistencyFailure()
+ raise ConsistencyFailure("MinCardinality not accomplished")
else:
return 1
@@ -535,7 +538,7 @@
"""narrowing algorithm for the constraint"""
if len(domains[self._variables[0]]) != self.cardinality:
- raise ConsistencyFailure()
+ raise ConsistencyFailure("Cardinality constraint not met")
else:
return 1
@@ -558,6 +561,9 @@
self.super = cls_or_restriction
self.variable = variable
+ def __repr__(self):
+ return '<%s %s %s>' % (self.__class__.__name__, str(self._variables[0]), self.super)
+
def narrow(self, domains):
subdom = domains[self.variable]
superdom = domains[self.super]
@@ -566,12 +572,7 @@
vals = get_values(subdom, domains, 'getValues')
superdom.values += [val for val in vals if val not in superdom.values]
-class DisjointClassConstraint(OwlConstraint):
-
- def __init__(self, variable, cls_or_restriction):
- OwlConstraint.__init__(self, [variable])
- self.super = cls_or_restriction
- self.variable = variable
+class DisjointClassConstraint(SubClassConstraint):
def narrow(self, domains):
subdom = domains[self.variable]
@@ -584,23 +585,13 @@
if i in vals2:
raise ConsistencyFailure()
-class ComplementClassConstraint(OwlConstraint):
-
- def __init__(self, variable, cls_or_restriction):
- OwlConstraint.__init__(self, variable)
- self.super = cls_or_restriction
- self.variable = variable
+class ComplementClassConstraint(SubClassConstraint):
def narrow(self, domains):
subdom = domains[self.variable]
superdom = domains[self.super]
-class RangeConstraint(OwlConstraint):
-
- def __init__(self, variable, cls_or_restriction):
- OwlConstraint.__init__(self, variable)
- self.super = cls_or_restriction
- self.variable = variable
+class RangeConstraint(SubClassConstraint):
def narrow(self, domains):
subdom = domains[self.variable]
@@ -614,12 +605,7 @@
res.append((k,v))
subdom.removeValues(res)
-class DomainConstraint(OwlConstraint):
-
- def __init__(self, variable, cls_or_restriction):
- OwlConstraint.__init__(self, variable)
- self.super = cls_or_restriction
- self.variable = variable
+class DomainConstraint(SubClassConstraint):
def narrow(self, domains):
subdom = domains[self.variable]
@@ -631,12 +617,7 @@
res.append((k,val))
subdom.removeValues(res)
-class SubPropertyConstraint(OwlConstraint):
-
- def __init__(self, variable, cls_or_restriction):
- OwlConstraint.__init__(self, variable)
- self.super = cls_or_restriction
- self.variable = variable
+class SubPropertyConstraint(SubClassConstraint):
def narrow(self, domains):
subdom = domains[self.variable]
@@ -647,12 +628,7 @@
vals.append(val)
superdom.setValues(vals)
-class EquivalentPropertyConstraint(OwlConstraint):
-
- def __init__(self, variable, cls_or_restriction):
- OwlConstraint.__init__(self, variable)
- self.super = cls_or_restriction
- self.variable = variable
+class EquivalentPropertyConstraint(SubClassConstraint):
def narrow(self, domains):
subdom = domains[self.variable]
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 Jan 5 14:35:49 2006
@@ -123,7 +123,7 @@
O.type(sub, pred , obj)
assert len(O.constraints) == 1
O.constraints[0].narrow(O.variables)
- assert O.variables['a_'].getValues() == ((None,[1,2,3,4]),)
+ assert O.variables['a_'].getValues() == [(None,[1,2,3,4]),]
def test_merge():
O = Ontology()
@@ -140,7 +140,7 @@
O.type(sub, pred , obj)
assert len(O.constraints) == 2
O.consistency()
- assert O.variables['a_'].getValues() == ((None, [3,4]),)
+ assert O.variables['a_'].getValues() == [(None, [3,4]),]
def test_domain():
O = Ontology()
@@ -154,7 +154,7 @@
O.type(sub, pred , obj)
assert len(O.constraints) == 1
O.constraints[0].narrow(O.variables)
- assert O.variables['a_'].getValues() == ((O.variables['b_'], [None]),)
+ assert O.variables['a_'].getValues() == [(O.variables['b_'], [None]),]
def test_domain_merge():
O = Ontology()
@@ -172,7 +172,7 @@
assert len(O.constraints) == 2
for con in O.constraints:
con.narrow(O.variables)
- assert O.variables['a_'].getValues() ==() #O.variables['b_']
+ assert O.variables['a_'].getValues() ==[] #O.variables['b_']
def test_subproperty():
O = Ontology()
@@ -277,4 +277,30 @@
O.type(sub, None, obj)
O.variables['friend_'].setValues([('Bob_','Alice_')])
O.consistency()
- assert ('Alice_', ['Bob_']) in O.variables['friend_'].getValues()
\ No newline at end of file
+ assert ('Alice_', ['Bob_']) in O.variables['friend_'].getValues()
+
+def no_test_maxcardinality():
+
+ O = Ontology()
+ #Make functional property
+ sub = URIRef('friend')
+ obj = URIRef(namespaces['owl']+'#ObjectProperty')
+ O.type(sub, None, obj)
+ #Make class
+ sub = URIRef('c')
+ obj = URIRef(namespaces['owl']+'#Class')
+ O.type(sub, None, obj)
+ #Make individual with a cardinality restriction of the property
+ sub = URIRef('Bob')
+ obj = URIRef('c')
+ O.type(sub, None, obj)
+ sub = BNode('anon')
+ obj = URIRef(namespaces['owl']+'#Restriction')
+ O.type(sub, None, obj)
+ O.onProperty(sub, None, URIRef('friend'))
+ O.maxCardinality(sub, None, 1)
+ O.variables['friend_'].setValues([('Bob_',['Alice_','Finn_'])])
+ O.consistency(verbose=5)
+ print O.variables
+ print O.constraints
+ assert not '_anon' in O.variables
\ No newline at end of file
More information about the Pypy-commit
mailing list