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

ale at codespeak.net ale at codespeak.net
Sat Jul 8 16:19:16 CEST 2006


Author: ale
Date: Sat Jul  8 16:19:13 2006
New Revision: 29836

Modified:
   pypy/dist/pypy/lib/pyontology/constraint_classes.py
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Fixed a misunderstanding of the semantics of rdf:type and owl:oneof. Added a test

Modified: pypy/dist/pypy/lib/pyontology/constraint_classes.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/constraint_classes.py	(original)
+++ pypy/dist/pypy/lib/pyontology/constraint_classes.py	Sat Jul  8 16:19:13 2006
@@ -1,4 +1,5 @@
-from logilab.constraint.propagation import AbstractDomain, AbstractConstraint, ConsistencyFailure
+from logilab.constraint.propagation import AbstractDomain, AbstractConstraint,\
+       ConsistencyFailure
 from rdflib import URIRef
 
 class OwlConstraint(AbstractConstraint):

Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Sat Jul  8 16:19:13 2006
@@ -1,7 +1,8 @@
 from rdflib import Graph, URIRef, BNode, Literal
 from logilab.constraint import  Repository, Solver
 from logilab.constraint.fd import  Expression, FiniteDomain as fd
-from logilab.constraint.propagation import AbstractDomain, AbstractConstraint, ConsistencyFailure
+from logilab.constraint.propagation import AbstractDomain, AbstractConstraint,\
+       ConsistencyFailure
 from constraint_classes import *
 import sys, py
 import time
@@ -70,12 +71,20 @@
         self.bases = [] 
         self.finished = False
         self.value = None
+        self.type = []
 
     def finish(self, variables, glob_constraints):
         # The finish method constructs the constraints
         if not self.finished:
             log("FINISH %s" % self.name)
         # Try to initialise constraints for this class
+            if len(self.type) > 1:
+                #try to merge the domains of the types 
+                expr = []
+                for typ in self.type:
+                    expr +=[ "%s == %s" % (self.name, typ)]
+                expr = ' and '.join(expr)
+                self.in_constraint.append(Expression([self.name]+self.type, expr))
             prop = getattr(self, 'property')
             val = getattr(self, 'value')
             if prop:
@@ -123,7 +132,9 @@
     
     def removeValues(self, values):
         for val in values:
-            self.values.pop(val) 
+            self.values.pop(val)
+        if not self.values:
+            raise ConsistencyFailure 
     
     def getBases(self):
         return self._bases
@@ -145,6 +156,17 @@
     def setValues(self, values):
         self.values = dict.fromkeys(values)
 
+class FixedClassDomain(ClassDomain):
+
+    def removeValues(self, values):
+        pass #raise ConsistencyFailure
+
+    def setValues(self, values):
+        if not self.values:
+            self.values = dict.fromkeys(values)
+        #else:
+            #raise ConsistencyFailure
+
 class Thing(ClassDomain):
     pass
 
@@ -154,7 +176,10 @@
         self.uri = uri
         self.sameas = [] 
         self.differentfrom = []
-       
+    
+    def __repr__(self):
+        return "<%s( %s, %s)>"%(self.__class__.__name__, self.name, self.uri)
+
     def __hash__(self):
         return hash(self.uri) 
     def __eq__(self, other):
@@ -509,8 +534,9 @@
         if not var in builtin_voc :
             # var is not one of the builtin classes -> it is a Thing
             self.type(s, Thing_uri)
-            svar = self.make_var(None,s) 
-            self.constraints.append(MemberConstraint(Individual(svar,s), avar))
+            svar = self.make_var(None,s)
+            self.variables[svar].type.append(avar) 
+            #self.constraints.append(MemberConstraint(Individual(svar,s), avar))
         else:
             # var is a builtin class
             cls = builtin_voc[var]
@@ -579,10 +605,14 @@
         self.constraints.append(ComplementOfConstraint(svar, avar))       
     
     def oneOf(self, s, var):
+        # Oneof is used to generate a fixed class. The elements of the class
+        # are exactly the ones in the list.
+        # Can be used to define an enumerated datatype as well
         var = self.flatten_rdf_list(var)
-        svar = self.make_var(ClassDomain, s)
+        svar = self.make_var(FixedClassDomain, s)
         res = self.variables[var].getValues()
-        self.variables[svar].setValues([Individual(self.make_var(None,x),x) for x in res])
+        self.variables[svar].setValues([
+             Individual(self.make_var(None,x),x) for x in res])
     
     def unionOf(self,s, var):
         var = self.flatten_rdf_list(var)

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 Jul  8 16:19:13 2006
@@ -369,6 +369,24 @@
     assert len(O.rep._domains[restrict].getValues()) == 3
     assert set(O.rep._domains[restrict].getValues()) == set(own)
 
+def test_unification_of_two_oneofclassenumeration():
+    O = Ontology()
+    restrict = BNode('anon')
+    own = [UR('first'), UR('second'), UR('third')]
+    for i in own:
+        O.type(i,UR(namespaces['owl']+'#Thing'))
+    O.oneOf(restrict, own)
+    restrict1 = BNode('anon1')
+    own = [UR('second'), UR('third'), UR('first')]
+    O.oneOf(restrict1, own)
+    O.type(UR('test'), UR(namespaces['owl']+'#Thing'))
+    O.type(UR('test'), restrict)
+    O.type(UR('test'), restrict1)
+    O.consistency()
+    assert len(O.rep._domains[restrict].getValues()) == 3
+    assert set(O.rep._domains[restrict].getValues()) == set(own)
+
+
 def test_oneofdatarange():
     O = Ontology()
     restrict = BNode('anon')
@@ -616,7 +634,10 @@
         O.type(URIRef(i), URIRef(namespaces['owl']+'#Thing'))
     O.type(URIRef('i5'), URIRef(namespaces['owl']+'#Thing'))
     O.type(URIRef('i4'), b_cls)
+    O.type(URIRef('i4'), a_cls)
     O.complementOf(b_cls, a_cls)
+    # The above ontology states that 'b' is complement of 'a'. But that leads
+    # to an inconsistency as 'i4' is of type 'a' and 'b'
     raises(ConsistencyFailure, O.consistency)
 
 def test_class_promotion():



More information about the Pypy-commit mailing list