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

ale at codespeak.net ale at codespeak.net
Tue Nov 21 14:00:52 CET 2006


Author: ale
Date: Tue Nov 21 14:00:50 2006
New Revision: 34822

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:
Going back to generator interface + bugfixes


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	Tue Nov 21 14:00:50 2006
@@ -502,11 +502,12 @@
         property = dom.property
         indi = dom.getValues()
         prop = Linkeddict(domains[property].getValues())
+        remove = []
         for v in indi:
             if not v in prop:
-                dom.removeValue(v)
+                remove.append(v)
             else:
                 prop_val = prop[v] 
                 if not val in prop_val:
-                       dom.removeValue(v)
-
+                       remove.append(v)
+        dom.removeValues(remove)

Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Tue Nov 21 14:00:50 2006
@@ -170,9 +170,9 @@
         self.values[value] = True
 
     def getValues(self):
-        #for key in self.values:
-        #    yield key
-        return self.values.keys()
+        for key in self.values:
+            yield key
+        #return self.values.keys()
         
     def __iter__(self):
         return iter(self.values.keys())
@@ -228,9 +228,13 @@
     def __eq__(self, other):
         #log("CMP %r,%r %i"%(self.name,other, len(self.differentfrom)))
         #assert isinstance(other, ClassDomain)
-        if ((hasattr(other,'uri') and self.uri == other.uri) or
-            (not hasattr(other,'uri') and self.uri == other) or
-              other in self.sameas):
+        if hasattr(other,'uri'):
+            if self.uri == other.uri:
+                return True
+        elif not hasattr(other,'uri'):
+            if self.uri == other:
+                return True
+        elif other in self.sameas:
             return True
         if not other or other in self.differentfrom:
             return False
@@ -278,9 +282,7 @@
         res = []
         for k,vals in items:
             for v in vals:
-                #yield (k,v)
-                res.append((k,v))
-        return res
+                yield (k,v)
 
     def getValuesPrKey(self, key= None):
         if key:
@@ -516,8 +518,8 @@
                 constraint.narrow(self.variables)
 #                except ConsistencyFailure, e:
 #                    print "FAilure", e
-        things = self.variables['owl_Thing'].getValues()
-        things += self.variables['owl_Literal'].getValues()
+        things = list(self.variables['owl_Thing'].getValues())
+        things += list(self.variables['owl_Literal'].getValues())
         self.variables['owl_Thing'].setValues(things)
 
     def _sparql(self, query):
@@ -652,10 +654,17 @@
                 query_dom[prop] = self.variables[prop]
                 p_vals = self.variables[prop].getValues()
                 sub = self.make_var(Thing, trip[0])
-                self.variables[sub].setValues([v[0] for v in p_vals])
+                vals = set([v[0] for v in p_vals])
+                if self.variables[sub].size():
+                    vals &= set(self.variables[sub].getValues())
+                self.variables[sub].setValues(vals)
                 obj = self.make_var(Thing, trip[2])
-                self.variables[obj].setValues([v[1] for v in p_vals])
-                con = Expression([sub,prop,obj], "%s == (%s, %s)" %(prop, sub, obj))
+                vals = set([v[1] for v in p_vals])
+                if self.variables[obj].size():
+                    vals &= set(self.variables[obj].getValues())
+                self.variables[obj].setValues(vals)
+                con = PropertyConstrain3(prop, sub, obj)
+#                con = Expression([sub,prop,obj], "%s == (%s, %s)" %(prop, sub, obj))
                 query_constr.append(con)
 
             elif case == 6:
@@ -688,20 +697,22 @@
                      for v in vars])
 
         dom.update(query_dom)
-        print dom
         # solve the repository and return the solution
         rep = Repository(dom.keys(), dom, query_constr)
-        res_s = Solver().solve(rep, 3)
-        #res_s = self.solve()
+        res_s = Solver(MyDistributor()).solve(rep, verbose=0)
         res = []
+        query_vars = dict([('query_%s_'%name,name) for name in resvars])
         for d in res_s:
+           res_dict = {}
            for k,v in d.items():
                if hasattr(v,'uri'):
                    val = v.uri
                else:
                    val = v 
                d[k] = unicode(val)
-           res.append(d)
+               if k in query_vars:
+                   res_dict[query_vars[k]] = unicode(val)
+           res.append(res_dict)
         return res
     
     def consider_triple(self,(s, p, o)):

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	Tue Nov 21 14:00:50 2006
@@ -173,7 +173,7 @@
     O.type(sub, obj)
     O.type(obj, namespaces['owl']+"#Class")
     
-    assert O.variables[O.make_var(None, obj)].getValues()[0].__class__  == Individual 
+    assert list(O.variables[O.make_var(None, obj)].getValues())[0].__class__  == Individual 
 
 # test for multiple types
 # test for type hierarchy


More information about the pypy-svn mailing list