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

ale at codespeak.net ale at codespeak.net
Wed Jan 11 18:22:49 CET 2006


Author: ale
Date: Wed Jan 11 18:22:48 2006
New Revision: 21964

Modified:
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Added tests for unionof/intersectionof. 
Refactored the subclassing of rdflib.Graph
removed get_list


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 11 18:22:48 2006
@@ -189,15 +189,18 @@
                getUriref('rdf', 'List') : List
               }
   
-class Ontology(Graph):
+class Ontology:
 
     def __init__(self):
-        Graph.__init__(self)
+        self.graph = Graph()
         self.variables = {}
         self.constraints = []
         self.seen = {}
         self.var2ns ={}
 
+    def add(self, triples):
+        self.graph.add(triples)
+        
     def add_file(self, f):
         tmp = Graph()
         tmp.load(f)
@@ -205,7 +208,7 @@
             self.add(i)
 
     def attach_fd(self):
-        for (s, p, o) in (self.triples((None, None, None))):
+        for (s, p, o) in (self.graph.triples((None,)*3)):
             if p.find('#') != -1:
                 ns, func = p.split('#')
             else:
@@ -238,21 +241,6 @@
         self.rep = Repository(self.variables.keys(), self.variables, self.constraints)
         self.rep.consistency(verbose)
  
-    def get_list(self, subject):
-        res = []
-        first = list(self.objects(subject, rdf_first)) 
-        assert len(first) == 1
-        if type(first[0]) == URIRef:
-            var = self.make_var(ClassDomain, first[0])
-        res += first
-        
-        rest = list(self.objects(subject, rdf_rest)) 
-        if "#nil" in rest[0] :
-           return res
-        else:
-           res += self.get_list(rest[0])
-        return res
-
     def make_var(self, cls=fd, a=''):
         if type(a) == URIRef:
             if a.find('#') != -1:
@@ -349,17 +337,16 @@
         self.constraints.append(cons)
 
     def unionOf(self,s, var):
-        res = self.get_list(var)
-        return res #There might be doubles (but fd takes care of that)
+        avar = self.make_var(List, var)
+        svar = self.make_var(ClassDomain, s)
+        cons = UnionofConstraint(svar, avar)
+        self.constraints.append(cons)
 
     def intersectionOf(self, s, var):
-        res = self.get_list(var)
-        result = {}.fromkeys(res[0])
-        for el in res:
-            for cls in result.keys():
-                if cls not in el:
-                   result.pop(cls)
-        return result.keys()
+        avar = self.make_var(List, var)
+        svar = self.make_var(ClassDomain, s)
+        cons = IntersectionofConstraint(svar, avar)
+        self.constraints.append(cons)
 
 #---Property Axioms---#000000#FFFFFF--------------------------------------------
 
@@ -424,10 +411,11 @@
         constrain = BinaryExpression([s_var, var_var],"%s != %s" %(s_var,  var_var))
         self.constraints.append(constrain)
 
-    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
+#XXX need to change this
+##    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, var):
         s_var = self.make_var(None, s)
@@ -780,6 +768,49 @@
             domains[self.variable].setValues(val)
             return 1
 
+class UnionofConstraint(AbstractConstraint):
+
+    def __init__(self, variable, List):
+        AbstractConstraint.__init__(self, [variable, List])
+        self.variable = variable
+        self.List = List
+
+    def estimateCost(self, domains):
+        return 200
+        
+    def narrow(self, domains):
+        val = domains[self.List].getValues()
+        union = []
+        for v in val:
+            for u in domains[v].getValues():
+                if not u in union:
+                    union.append(u)
+        cls = domains[self.variable].setValues(union)
+        
+class IntersectionofConstraint(AbstractConstraint):
+
+    def __init__(self, variable, List):
+        AbstractConstraint.__init__(self, [variable, List])
+        self.variable = variable
+        self.List = List
+
+    def estimateCost(self, domains):
+        return 200
+        
+    def narrow(self, domains):
+        val = domains[self.List].getValues()
+        intersection = domains[val[0]].getValues()
+        for v in val[1:]:
+            vals= domains[v].getValues()
+            remove = []
+            for u in intersection:
+                if not u in vals:
+                    remove.append(u)
+            for u in remove:
+                intersection.remove(u)
+        cls = domains[self.variable].setValues(intersection)
+        
+
 class SomeValueConstraint(AbstractConstraint):
 
     def __init__(self, variable, List):

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 11 18:22:48 2006
@@ -17,12 +17,12 @@
     ont.type(owllist, obj)
     own =owllist
     for i,dat in enumerate(data[:-1]):
-        next = URIRef( 'anon'+str(i))
+        next = URIRef( name + str(i))
         print next,i,dat,own
-        ont.first(own, URIRef(dat))
+        ont.first(own, dat)
         ont.rest(own,  next)
         own = next
-    ont.first(own, URIRef(data[-1]))
+    ont.first(own, data[-1])
     ont.rest(own,  URIRef(namespaces['rdf']+'#nil'))
     return owllist
 
@@ -434,31 +434,32 @@
     O.variables['p_'].setValues([(cls,1)])
     py.test.raises(ConsistencyFailure, O.consistency)
 
-def no_test_unionof():
+def test_unionof():
     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.unionOf(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)
+    cls = BNode('anon')
+    own1 = BNode('liist1')
+    own2 = BNode('liist2')
+    list1 = rdf_list(O, 'favlist1', ['1', '2', '3'])
+    list2 = rdf_list(O, 'favlist2', ['3', '4', '5'])
+    own = rdf_list(O, 'favlist', [own1, own2])
+    O.oneOf( own1, list1)
+    O.oneOf( own2, list2)
+    O.unionOf(cls, own)
+    O.type(cls, namespaces['owl']+'#Class')
+    O.consistency(3)
+    assert O.rep._domains[cls].getValues() == ['1', '2', '3', '4', '5']
+
+def test_intersectionof():
+    O = Ontology()
+    cls = BNode('anon')
+    own1 = BNode('liist1')
+    own2 = BNode('liist2')
+    list1 = rdf_list(O, 'favlist1', ['1', '2', '3'])
+    list2 = rdf_list(O, 'favlist2', ['3', '4', '5'])
+    own = rdf_list(O, 'favlist', [own1, own2])
+    O.oneOf( own1, list1)
+    O.oneOf( own2, list2)
+    O.intersectionOf(cls, own)
+    O.type(cls, namespaces['owl']+'#Class')
+    O.consistency(3)
+    assert O.rep._domains[cls].getValues() == ['3']



More information about the Pypy-commit mailing list