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

ale at codespeak.net ale at codespeak.net
Sun Jan 8 16:04:19 CET 2006


Author: ale
Date: Sun Jan  8 16:04:18 2006
New Revision: 21818

Modified:
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Added support for hasValue and allValuesFrom, with tests


Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Sun Jan  8 16:04:18 2006
@@ -468,18 +468,16 @@
         self.constraints.append(constrain)
 
     def hasValue(self, s, var):
-        if type(var) != URIRef:
-            value = var
-        else:
-            value = self.make_var(None, var)
-        prop = self.find_prop(s)
-        cls = self.make_var(None, self.find_cls(s))
-        con = HasvalueConstraint(prop, cls, value)
-        self.constraints.append(con)
+        svar = self.make_var(Restriction, s)
+        avar = self.make_var(None, var)
+        constrain = HasvalueConstraint(svar, avar)
+        self.constraints.append(constrain)
         
     def allValuesFrom(self, s, var):
-        # TODO: implement this 
-        pass
+        svar = self.make_var(Restriction, s)
+        avar = self.make_var(None, var)
+        constrain = AllValueConstraint(svar, avar)
+        self.constraints.append(constrain)
 
     def someValuesFrom(self, s, var):
         svar = self.make_var(Restriction, s)
@@ -815,7 +813,6 @@
         return 100
         
     def narrow(self, domains):
-        print self.variable,self.List
         val = domains[self.List].getValues()
         property = domains[self.variable].property
         cls = domains[self.variable].cls
@@ -828,5 +825,46 @@
                     "The value of the property %s in the class %s has no values from %r"
                         %(property, cls, val))
 
-class HasvalueConstraint:
-    pass
+class AllValueConstraint(AbstractConstraint):
+
+    def __init__(self, variable, List):
+        AbstractConstraint.__init__(self, [variable, List])
+        self.variable = variable
+        self.List = List
+
+    def estimateCost(self, domains):
+        return 100
+        
+    def narrow(self, domains):
+        val = domains[self.List].getValues()
+        property = domains[self.variable].property
+        cls = domains[self.variable].cls
+        prop = Linkeddict(domains[property].getValues())
+        for v in prop[cls]:
+            if not v in val:
+                raise ConsistencyFailure(
+                    "The value of the property %s in the class %s has a value not from %r"
+                        %(property, cls, val))
+
+class HasvalueConstraint(AbstractConstraint):
+    def __init__(self, variable, List):
+        AbstractConstraint.__init__(self, [variable])
+        self.variable = variable
+        self.List = List
+
+    def estimateCost(self, domains):
+        return 100
+        
+    def narrow(self, domains):
+        val = self.List #domains[self.List].getValues()
+        property = domains[self.variable].property
+        cls = domains[self.variable].cls
+        prop = Linkeddict(domains[property].getValues())
+        for v in prop[cls]:
+            if v == val:
+                break
+        else:
+            raise ConsistencyFailure(
+                    "The value of the property %s in the class %s has a value not from %r"
+                        %(property, cls, val))
+

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 Jan  8 16:04:18 2006
@@ -307,16 +307,22 @@
     O.variables['ownedby_'].setValues([('Fiat_','Bob_')])
     O.consistency()
     
-def no_test_hasvalue():
+def test_hasvalue():
     O = Ontology()
-    own = URIRef('owner')
+    restrict = BNode('anon1')
+    obj = URIRef(namespaces['owl']+'#Restriction')
+    O.type(restrict, obj)
+    p = URIRef('p')
+    O.onProperty(restrict,p)
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(own, obj)
-    O.hasValue(own, 42)
+    O.type(p, obj)
+    O.hasValue(restrict, 2)
+    cls = URIRef('class')
+    obj = URIRef(namespaces['owl']+'#Class')
+    O.type(cls, obj)
+    O.subClassOf(cls,restrict)
+    O.variables['p_'].setValues([(cls,1)])
     py.test.raises(ConsistencyFailure, O.consistency)
-    #Make class
-    O.variables['owner_'].setValues([('Fiat_', 42)])
-    O.consistency()
 
 def test_List():
     O = Ontology()
@@ -421,3 +427,32 @@
     O.subClassOf(cls,restrict)
     O.variables['p_'].setValues([(cls,1)])
     py.test.raises(ConsistencyFailure, O.consistency)
+
+def test_allvaluesfrom_datarange():
+    O = Ontology()
+    datarange = BNode('anon')
+    own = URIRef('favlist')
+    obj = URIRef(namespaces['rdf']+'#List')
+    O.type(own, obj)
+    O.first(own, URIRef('first'))
+    O.rest(own,  URIRef('1'))
+    O.first( URIRef('1'), URIRef('second'))
+    O.rest( URIRef('1'),  URIRef('2'))
+    O.first( URIRef('2'), URIRef('third'))
+    O.rest( URIRef('2'),  URIRef(namespaces['rdf']+'#nil'))
+    O.oneOf(datarange, own)
+    O.type(datarange, namespaces['owl']+'#DataRange')
+    restrict = BNode('anon1')
+    obj = URIRef(namespaces['owl']+'#Restriction')
+    O.type(restrict, obj)
+    p = URIRef('p')
+    O.onProperty(restrict,p)
+    obj = URIRef(namespaces['owl']+'#ObjectProperty')
+    O.type(p, obj)
+    O.allValuesFrom(restrict, datarange)
+    cls = URIRef('class')
+    obj = URIRef(namespaces['owl']+'#Class')
+    O.type(cls, obj)
+    O.subClassOf(cls,restrict)
+    O.variables['p_'].setValues([(cls,1)])
+    py.test.raises(ConsistencyFailure, O.consistency)



More information about the Pypy-commit mailing list