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

ale at codespeak.net ale at codespeak.net
Sat Jan 7 13:32:34 CET 2006


Author: ale
Date: Sat Jan  7 13:32:33 2006
New Revision: 21785

Modified:
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Removed an unused parameter and changed the tests accordingly


Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Sat Jan  7 13:32:33 2006
@@ -196,7 +196,7 @@
             if ns in namespaces.values():
                 #predicate is one of builtin OWL predicates
                 pred = getattr(self, func)
-                res = pred(s, p, o) 
+                res = pred(s, o) 
                 if not res :
                     continue
                 if type(res) != list :
@@ -211,20 +211,6 @@
                 res = self.variables[avar].getValues() 
                 self.variables[avar].setValues(res + [(sub, obj)])
 
-    def merge_constraints(self):
-        # Make the intersection of multiple rdfs:range constraints on the same variable
-        cons_dict = {}
-        new_cons =[]
-        for con in self.constraints:
-            if isinstance(con, RangeConstraint):
-                cons_dict.setdefault(con.variable, [])
-                cons_dict[con.variable].append(con)
-            else:
-                new_cons.append(con)
-        for k,v in cons_dict.items():
-            for con in v:
-                pass
-                
     def solve(self,verbose=0):
         #self.merge_constraints()
         rep = Repository(self.variables.keys(), self.variables, self.constraints)
@@ -299,7 +285,7 @@
     
 #---------------- Implementation ----------------
 
-    def type(self, s, p, var):
+    def type(self, s, var):
         if not (var in [URIRef(namespaces[ns]+'#'+x) for x in builtin_voc for ns in namespaces]):
             # var is not one of the builtin classes
             avar = self.make_var(ClassDomain, var)
@@ -314,18 +300,18 @@
             if hasattr(cls, 'constraint'):
                 self.constraints.append(cls.constraint)
 
-    def first(self, s, p, var):
+    def first(self, s, var):
         pass
 
-    def rest(self, s, p, var):
+    def rest(self, s, var):
         pass
 
-    def onProperty(self, s, p, var):
+    def onProperty(self, s, var):
         pass
 
 #---Class Axioms---#000000#FFFFFF-----------------------------------------------
 
-    def subClassOf(self, s, p, var):
+    def subClassOf(self, s, var):
         # s is a subclass of var means that the 
         # class extension of s is a subset of the
         # class extension of var. 
@@ -334,24 +320,24 @@
         cons = SubClassConstraint( svar, avar)
         self.constraints.append(cons)
 
-    def equivalentClass(self, s, p, var):
-        self.subClassOf(s, p, var)
-        self.subClassOf(var, p, s)
+    def equivalentClass(self, s, var):
+        self.subClassOf(s, var)
+        self.subClassOf(var, s)
 
-    def disjointWith(self, s, p, var):
+    def disjointWith(self, s, var):
         avar = self.make_var(None, var)
         svar = self.make_var(None, s)
         constrain = DisjointClassConstraint(svar, avar) 
         self.constraints.append(constrain)
 
-    def complementOf(self, s, p, var):
+    def complementOf(self, s, var):
         # add constraint of not var
         # TODO: implementthis for OWL DL
 ##        avar = self.make_var(ClassDomain, var)
 ##        svar = self.make_var(ClassDomain, s)
             pass
 
-    def oneOf(self, s, p, var):
+    def oneOf(self, s, var):
         res = self.get_list(var)
         #prop = self.find_uriref(s)
         avar = self.make_var(None, s)
@@ -360,11 +346,11 @@
         else:
             self.variables[avar] = fd(res)
 
-    def unionOf(self,s, p, var):
+    def unionOf(self,s, var):
         res = self.get_list(var)
         return res #There might be doubles (but fd takes care of that)
 
-    def intersectionOf(self, s, p, var):
+    def intersectionOf(self, s, var):
         res = self.get_list(var)
         result = {}.fromkeys(res[0])
         for el in res:
@@ -375,33 +361,33 @@
 
 #---Property Axioms---#000000#FFFFFF--------------------------------------------
 
-    def range(self, s, p, var):
+    def range(self, s, var):
         avar = self.make_var(ClassDomain, var)
         svar = self.make_var(Property, s)
         cons = RangeConstraint(svar, avar)
         self.constraints.append(cons)
 
-    def domain(self, s, p, var):
+    def domain(self, s, var):
         # The classes that has this property (s) must belong to the class extension of var
         avar = self.make_var(ClassDomain, var)
         svar = self.make_var(Property, s)
         cons = DomainConstraint(svar, avar)
         self.constraints.append(cons)
 
-    def subPropertyOf(self, s, p, var):
+    def subPropertyOf(self, s, var):
         # s is a subproperty of var
         avar = self.make_var(Property, var)
         svar = self.make_var(Property, s)
         cons = SubPropertyConstraint( svar, avar)
         self.constraints.append(cons)
 
-    def equivalentProperty(self, s, p, var):
+    def equivalentProperty(self, s, var):
         avar = self.make_var(Property, var)
         svar = self.make_var(Property, s)
         cons = EquivalentPropertyConstraint( svar, avar)
         self.constraints.append(cons)
 
-    def inverseOf(self, s, p, var):
+    def inverseOf(self, s, var):
         avar = self.make_var(Property, var)
         svar = self.make_var(Property, s)
         con = InverseofConstraint(svar, avar)
@@ -409,21 +395,21 @@
 
 #---Property restrictions------------------------------------------------------
 
-    def maxCardinality(self, s, p, var):
+    def maxCardinality(self, s, var):
         """ Len of finite domain of the property shall be less than or equal to var"""
         avar = self.find_property(s)
         cls =self.make_var(None, self.find_cls(s))
         constrain = MaxCardinality(avar, cls, int(var))
         self.constraints.append(constrain) 
 
-    def minCardinality(self, s, p, var):
+    def minCardinality(self, s, var):
         """ Len of finite domain of the property shall be greater than or equal to var"""
         avar = self.find_property(s)
         cls =self.make_var(None, self.find_cls(s))
         constrain = MinCardinality(avar, cls, int(var))
         self.constraints.append(constrain) 
 
-    def cardinality(self, s, p, var):
+    def cardinality(self, s, var):
         """ Len of finite domain of the property shall be equal to var"""
         avar = self.find_property(s)
         cls =self.make_var(None, self.find_cls(s))
@@ -431,37 +417,43 @@
         constrain = Cardinality(avar, cls, int(var))
         self.constraints.append(constrain) 
 
-    def differentFrom(self, s, p, var):
+    def differentFrom(self, s, var):
         s_var = self.make_var(ClassDomain, s)
         var_var = self.make_var(Thing, var)
         constrain = BinaryExpression([s_var, var_var],"%s != %s" %(s_var,  var_var))
         self.constraints.append(constrain)
 
-    def distinctMembers(self, s, p, var):
+    def distinctMembers(self, s, var):
         res = self.get_list(var)
         self.constraints.append(AllDistinct([self.make_var(ClassDomain, y) for y in res]))
         return res
 
-    def sameAs(self, s, p, var):
+    def sameAs(self, s, var):
         s_var = self.make_var(None, s)
         var_var = self.make_var(None, var)
         constrain = BinaryExpression([s_var, var_var],
                "%s == %s" %(s_var, var_var))
         self.constraints.append(constrain)
 
-    def hasValue(self, s, p, var):
-        # TODO: implement this 
-        pass
-
-    def allValuesFrom(self, s, p, var):
+    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)
+        
+    def allValuesFrom(self, s, var):
         # TODO: implement this 
         pass
 
-    def someValuesFrom(self, s, p, var):
+    def someValuesFrom(self, s, var):
         # TODO: implement this 
         pass
 
-    def imports(self, s, p, var):
+    def imports(self, s, var):
         # PP TODO: implement this 
         pass
 
@@ -712,3 +704,6 @@
                 raise ConsistencyFailure("Inverseof failed") 
 ##            res.append((val, cls))
 ##        domains[self.variable].setValues(res)
+
+class HasvalueConstraint:
+    pass
\ No newline at end of file

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	Sat Jan  7 13:32:33 2006
@@ -28,8 +28,8 @@
     A = O.make_var(ClassDomain, a)
     C = O.make_var(ClassDomain, c)
     C = O.make_var(ClassDomain, b)
-    O.subClassOf(b, None, a)
-    O.subClassOf(c, None, b)
+    O.subClassOf(b, a)
+    O.subClassOf(c, b)
     for con in O.constraints:
         con.narrow(O.variables)
     assert len(O.variables) == 3
@@ -90,8 +90,8 @@
     b = URIRef('B')
     c = URIRef('C')
     O = Ontology()
-    O.equivalentClass(c, None, a)
-    O.equivalentClass(c, None, b)
+    O.equivalentClass(c, a)
+    O.equivalentClass(c, b)
     A = O.make_var(ClassDomain, a)
     B = O.make_var(ClassDomain, b)
     assert O.variables[A].values == O.variables[B].values
@@ -102,7 +102,7 @@
     obj = URIRef('o')
     O = Ontology()
     O.make_var(ClassDomain, obj)
-    O.type(sub, pred , obj)
+    O.type(sub, obj)
     
     assert O.variables[O.make_var(None, sub)].__class__  == ClassDomain
 
@@ -111,7 +111,7 @@
     pred = URIRef('type')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
     O = Ontology()
-    O.type(sub, pred , obj)
+    O.type(sub, obj)
     assert O.variables[O.make_var(None, sub)].__class__  == ObjectProperty
 
 def test_range():
@@ -119,11 +119,11 @@
     sub = URIRef('a')
     obj = URIRef('b')
     O.variables['b_'] = fd([1,2,3,4])
-    O.range(sub, None , obj)
+    O.range(sub, obj)
     sub = URIRef('a')
     pred = URIRef('type')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(sub, pred , obj)
+    O.type(sub, obj)
     assert len(O.constraints) == 1
     O.constraints[0].narrow(O.variables)
     assert O.variables['a_'].range == [1,2,3,4]
@@ -133,14 +133,14 @@
     sub = URIRef('a')
     obj = URIRef('b')
     O.variables['b_'] = fd([1,2,3,4])
-    O.range(sub, None , obj)
+    O.range(sub, obj)
     obj = URIRef('c')
     O.variables['c_'] = fd([3,4,5,6])
-    O.range(sub, None , obj)
+    O.range(sub, obj)
     sub = URIRef('a')
     pred = URIRef('type')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(sub, pred , obj)
+    O.type(sub, obj)
     assert len(O.constraints) == 2
     O.consistency()
     assert O.variables['a_'].range == [ 3,4]
@@ -150,11 +150,11 @@
     sub = URIRef('a')
     obj = URIRef('b')
     O.variables['b_'] = ClassDomain('b')
-    O.domain(sub, None , obj)
+    O.domain(sub, obj)
     sub = URIRef('a')
     pred = URIRef('type')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(sub, pred , obj)
+    O.type(sub, obj)
     assert len(O.constraints) == 1
     O.constraints[0].narrow(O.variables)
     assert O.variables['a_'].domain == ['b_']
@@ -164,12 +164,12 @@
     sub = URIRef('a')
     obj = URIRef('b')
     O.variables['b_'] = ClassDomain('b')
-    O.domain(sub, None , obj)
+    O.domain(sub, obj)
     obj = URIRef('c')
     O.variables['c_'] = ClassDomain('c')
-    O.domain(sub, None , obj)
+    O.domain(sub, obj)
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(sub, None , obj)
+    O.type(sub, obj)
     
     assert len(O.constraints) == 2
     for con in O.constraints:
@@ -180,10 +180,10 @@
     O = Ontology()
     sub = URIRef('a')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     b = URIRef('b')
-    O.type(b, None, obj)
-    O.subPropertyOf(sub, None, b)
+    O.type(b, obj)
+    O.subPropertyOf(sub, b)
     assert len(O.constraints) ==1
     O.variables['a_'].setValues([('individ_',42)])
     O.consistency()
@@ -196,15 +196,15 @@
     #Make functional property
     sub = URIRef('p')
     obj = URIRef(namespaces['owl']+'#FunctionalProperty')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make class
     sub = URIRef('c')
     obj = URIRef(namespaces['owl']+'#Class')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make individual with a value of the property
     sub = URIRef('individ')
     obj = URIRef('c')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     O.variables['p_'].setValues([('individ_',42)])
     #assert len(O.constraints) == 2
     #add another valueof the property
@@ -218,21 +218,21 @@
     #Make functional property
     sub = URIRef('p')
     obj = URIRef(namespaces['owl']+'#InverseFunctionalProperty')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make class
     sub = URIRef('c')
     obj = URIRef(namespaces['owl']+'#Class')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make individual with a value of the property
     sub = URIRef('individ')
     obj = URIRef('c')
-    O.type(sub, None, obj)
+    O.type(sub, 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.type(sub, obj)
     O.variables['p_'].setValues([('individ_',42),('individ2_',42)])
     #check that consistency raises
     py.test.raises(ConsistencyFailure, O.consistency)
@@ -243,19 +243,19 @@
     #Make functional property
     sub = URIRef('subRegionOf')
     obj = URIRef(namespaces['owl']+'#TransitiveProperty')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make class
     sub = URIRef('c')
     obj = URIRef(namespaces['owl']+'#Class')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make individual with a value of the property
     sub = URIRef('Italy')
     obj = URIRef('c')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     sub = URIRef('Tuscanny')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     sub = URIRef('Chianti')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     O.variables['subRegionOf_'].setValues([('Italy_','Tuscanny_'),('Tuscanny_','Chianti_')])
     O.consistency()
     assert ('Italy_', 'Chianti_') in O.variables['subRegionOf_'].getValues()
@@ -266,45 +266,54 @@
     #Make functional property
     sub = URIRef('friend')
     obj = URIRef(namespaces['owl']+'#SymmetricProperty')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     assert O.variables[O.make_var(None, sub)].__class__.__name__=='SymmetricProperty'
     #Make class
     sub = URIRef('c')
     obj = URIRef(namespaces['owl']+'#Class')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make individual with a value of the property
     sub = URIRef('Bob')
     obj = URIRef('c')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     sub = URIRef('Alice')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     O.variables['friend_'].setValues([('Bob_','Alice_')])
     O.consistency()
     assert ('Alice_', 'Bob_') in O.variables['friend_'].getValues()
 
 def test_inverseof():
-    
     O = Ontology()
     own = URIRef('owner')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(own, None, obj)
+    O.type(own, obj)
     owned = URIRef('ownedby')
     obj = URIRef(namespaces['owl']+'#ObjectProperty')
-    O.type(owned, None, obj)
-    O.inverseOf(own, None, owned)
+    O.type(owned, obj)
+    O.inverseOf(own, owned)
     #Make class
     sub = URIRef('c')
     obj = URIRef(namespaces['owl']+'#Class')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     #Make individual with a property value
     sub = URIRef('Bob')
     obj = URIRef('c')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     sub = URIRef('Fiat')
     obj = URIRef('car')
-    O.type(sub, None, obj)
+    O.type(sub, obj)
     O.variables['owner_'].setValues([('Bob_','Fiat_')])
     py.test.raises(ConsistencyFailure, O.consistency)
     O.variables['ownedby_'].setValues([('Fiat_','Bob_')])
     O.consistency()
-    assert not '_anon' in O.variables
\ No newline at end of file
+    
+def no_test_hasvalue():
+    O = Ontology()
+    own = URIRef('owner')
+    obj = URIRef(namespaces['owl']+'#ObjectProperty')
+    O.type(own, obj)
+    O.hasValue(own, 42)
+    py.test.raises(ConsistencyFailure, O.consistency)
+    #Make class
+    O.variables['owner_'].setValues([('Fiat_', 42)])
+    O.consistency()



More information about the Pypy-commit mailing list