[pypy-svn] r21693 - in pypy/dist/pypy/lib/pyontology: . test

ale at codespeak.net ale at codespeak.net
Wed Jan 4 15:39:30 CET 2006


Author: ale
Date: Wed Jan  4 15:39:29 2006
New Revision: 21693

Modified:
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Added support for TransitiveProperty + test


Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Wed Jan  4 15:39:29 2006
@@ -127,6 +127,13 @@
     
     def __init__(self, name='', values=[], bases = []):
         Property.__init__(self, name, values, bases)
+        self.constraint = InverseFunctionalCardinality(name, 1)
+
+class TransitiveProperty(Property):
+    
+    def __init__(self, name='', values=[], bases = []):
+        Property.__init__(self, name, values, bases)
+        self.constraint = TransitiveConstraint(name)
 
 class DataRange:
     
@@ -147,14 +154,14 @@
 ##               'DeprecatedClass' : DeprecatedClass,
 ##               'DeprecatedProperty' : DeprecatedProperty,
                'FunctionalProperty' : FunctionalProperty,
-##               'InverseFunctionalProperty' : InverseFunctionalProperty,
+               'InverseFunctionalProperty' : InverseFunctionalProperty,
 ##               'Nothing' : Nothing,
 ##               'ObjectProperty' : ObjectProperty,
 ##               'Ontology' : Ontology,
 ##               'OntologyProperty' : OntologyProperty,
                'Restriction' : Restriction,
 ##               'SymmetricProperty' : SymmetricProperty,
-##               'TransitiveProperty' : TransitiveProperty
+               'TransitiveProperty' : TransitiveProperty
               }
   
 class Ontology(Graph):
@@ -482,10 +489,11 @@
 
     def __init__(self, variable):
         AbstractConstraint.__init__(self, [variable])
+        self.variable = variable
         self.__cost = 1 
 
     def __repr__(self):
-        return '<%s  %s,%i>' % (self.__class__.__name__, str(self._variables[0]),self.cardinality)
+        return '<%s  %s>' % (self.__class__.__name__, str(self._variables[0]))
 
     def estimateCost(self, domains):
         return self.__cost
@@ -495,9 +503,8 @@
     """Contraint: all values must be distinct"""
 
     def __init__(self, variable, cardinality):
-        OwlConstraint.__init__(self, [variable])
-        # worst case complexity
-        self.__cost = 1 #len(variables) * (len(variables) - 1) / 2
+        OwlConstraint.__init__(self, variable)
+        self.__cost = 1
         self.cardinality = cardinality
 
     def narrow(self, domains):
@@ -635,15 +642,25 @@
                 vals.append(val)
         superdom.setValues(vals)
 
-class FunctionalCardinality(OwlConstraint):
-    """Contraint: all values must be distinct"""
+class EquivalentPropertyConstraint(OwlConstraint):
 
-    def __init__(self, variable, cardinality):
+    def __init__(self, variable, cls_or_restriction):
         OwlConstraint.__init__(self, variable)
-        self.cardinality = cardinality
+        self.super = cls_or_restriction
         self.variable = variable
 
     def narrow(self, domains):
+        subdom = domains[self.variable]
+        superdom = domains[self.super]
+        vals = get_values(superdom, domains, 'getValues')  
+        for val in subdom.getValues():
+            if not val in vals:
+                raise ConsistencyFailure("Value not in prescribed range")
+
+class FunctionalCardinality(MaxCardinality):
+    """Contraint: all values must be distinct"""
+
+    def narrow(self, domains):
         """narrowing algorithm for the constraint"""
         domain = domains[self.variable].getValues()
         for cls, val in domain:
@@ -652,17 +669,32 @@
         else:
             return 0
 
-class EquivalentPropertyConstraint(OwlConstraint):
+class InverseFunctionalCardinality(MaxCardinality):
+    """Contraint: all values must be distinct"""
 
-    def __init__(self, variable, cls_or_restriction):
-        OwlConstraint.__init__(self, variable)
-        self.super = cls_or_restriction
-        self.variable = variable
+    def narrow(self, domains):
+        """narrowing algorithm for the constraint"""
+        domain = domains[self.variable].getValues()
+        vals = {}
+        for cls, val in domain:
+            for v in val:
+                if vals.has_key(v):
+                    raise ConsistencyFailure("Maxcardinality exceeded")
+                else:
+                    vals[v] = 1
+        else:
+            return 0
+
+class TransitiveConstraint(OwlConstraint):
+    """Contraint: all values must be distinct"""
 
     def narrow(self, domains):
-        subdom = domains[self.variable]
-        superdom = domains[self.super]
-        vals = get_values(superdom, domains, 'getValues')  
-        for val in subdom.getValues():
-            if not val in vals:
-                raise ConsistencyFailure("Value not in prescribed range")
+        """narrowing algorithm for the constraint"""
+        domain = domains[self.variable].getValues()
+        domain_dict = dict( domain )
+        for cls, val in domain:
+            for v in val:
+                if v in domain_dict:
+                    val.extend(domain_dict[v])
+
+

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	Wed Jan  4 15:39:29 2006
@@ -208,6 +208,52 @@
     #add another valueof the property
     O.variables['p_'].setValues([('individ_',42),('individ_',43)])
     #check that consistency raises
-    print O.constraints
-    print O.variables
-    py.test.raises(ConsistencyFailure, O.consistency)
\ No newline at end of file
+    py.test.raises(ConsistencyFailure, O.consistency)
+
+def test_inversefunctionalproperty():
+    
+    O = Ontology()
+    #Make functional property
+    sub = URIRef('p')
+    obj = URIRef(namespaces['owl']+'#InverseFunctionalProperty')
+    O.type(sub, None, obj)
+    #Make class
+    sub = URIRef('c')
+    obj = URIRef(namespaces['owl']+'#Class')
+    O.type(sub, None, obj)
+    #Make individual with a value of the property
+    sub = URIRef('individ')
+    obj = URIRef('c')
+    O.type(sub, None, obj)
+    O.variables['p_'].setValues([('individ_',42)])
+    assert len(O.constraints) == 2
+    #add another individual with the same value for the property
+    sub = URIRef('individ2')
+    obj = URIRef('c')
+    O.type(sub, None, obj)
+    O.variables['p_'].setValues([('individ2_',42)])
+    #check that consistency raises
+    py.test.raises(ConsistencyFailure, O.consistency)
+    
+def test_Transitiveproperty():
+    
+    O = Ontology()
+    #Make functional property
+    sub = URIRef('subRegionOf')
+    obj = URIRef(namespaces['owl']+'#TransitiveProperty')
+    O.type(sub, None, obj)
+    #Make class
+    sub = URIRef('c')
+    obj = URIRef(namespaces['owl']+'#Class')
+    O.type(sub, None, obj)
+    #Make individual with a value of the property
+    sub = URIRef('Italy')
+    obj = URIRef('c')
+    O.type(sub, None, obj)
+    sub = URIRef('Tuscanny')
+    O.type(sub, None, obj)
+    sub = URIRef('Chianti')
+    O.type(sub, None, obj)
+    O.variables['subRegionOf_'].setValues([('Italy_','Tuscanny_'),('Tuscanny_','Chianti_')])
+    O.consistency()
+    assert ('Italy_', ['Tuscanny_', 'Chianti_']) in O.variables['subRegionOf_'].getValues()
\ No newline at end of file



More information about the Pypy-commit mailing list