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

ale at codespeak.net ale at codespeak.net
Wed Jan 4 16:55:57 CET 2006


Author: ale
Date: Wed Jan  4 16:55:56 2006
New Revision: 21695

Modified:
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Added support for SymmetricProperty and 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 16:55:56 2006
@@ -104,8 +104,7 @@
 
 class Thing:
 
-    def __init__(self):
-        pass
+    pass
 
 class AllDifferent(ClassDomain):
     # A special class whose members are distinct
@@ -135,6 +134,12 @@
         Property.__init__(self, name, values, bases)
         self.constraint = TransitiveConstraint(name)
 
+class SymmetricProperty(Property):
+    
+    def __init__(self, name='', values=[], bases = []):
+        Property.__init__(self, name, values, bases)
+        self.constraint = SymmetricConstraint(name)
+
 class DataRange:
     
     def __init__(self):
@@ -150,7 +155,7 @@
                'AllDifferent' : AllDifferent ,
 ##               'AnnotationProperty' : AnnotationProperty,
 ##               'DataRange' : DataRange,
-##               'DatatypeProperty' : DatatypeProperty,
+              'DatatypeProperty' : DatatypeProperty,
 ##               'DeprecatedClass' : DeprecatedClass,
 ##               'DeprecatedProperty' : DeprecatedProperty,
                'FunctionalProperty' : FunctionalProperty,
@@ -160,7 +165,7 @@
 ##               'Ontology' : Ontology,
 ##               'OntologyProperty' : OntologyProperty,
                'Restriction' : Restriction,
-##               'SymmetricProperty' : SymmetricProperty,
+               'SymmetricProperty' : SymmetricProperty,
                'TransitiveProperty' : TransitiveProperty
               }
   
@@ -311,7 +316,7 @@
            (var in [URIRef(namespaces['owl']+'#'+x) for x in builtin_voc])):
             # var is not one of the builtin classes
             avar = self.make_var(ClassDomain, var)
-            self.variables[svar].values +=  self.variables[avar].values
+            self.variables[svar].setValues(self.variables[avar].getValues())
             constrain = BinaryExpression([svar, avar],"%s in %s" %(svar,  avar))
             self.constraints.append(constrain)
         else:
@@ -696,5 +701,18 @@
             for v in val:
                 if v in domain_dict:
                     val.extend(domain_dict[v])
+            domain_dict[cls] = val
+        domains[self.variable].setValues(domain_dict.items())
 
+class SymmetricConstraint(OwlConstraint):
+    """Contraint: all values must be distinct"""
 
+    def narrow(self, domains):
+        """narrowing algorithm for the constraint"""
+        domain = domains[self.variable].getValues()
+        domain_dict = dict( domain )
+        for cls, val in domain:
+            for v in val:
+                domain_dict.setdefault(v, [])
+                domain_dict[v].append(cls)
+        domains[self.variable].setValues(domain_dict.items())

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 16:55:56 2006
@@ -256,4 +256,25 @@
     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
+    assert ('Italy_', ['Tuscanny_', 'Chianti_']) in O.variables['subRegionOf_'].getValues()
+    
+def test_symmetricproperty():
+    
+    O = Ontology()
+    #Make functional property
+    sub = URIRef('friend')
+    obj = URIRef(namespaces['owl']+'#SymmetricProperty')
+    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('Bob')
+    obj = URIRef('c')
+    O.type(sub, None, obj)
+    sub = URIRef('Alice')
+    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



More information about the Pypy-commit mailing list