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

ale at codespeak.net ale at codespeak.net
Tue Nov 21 15:53:49 CET 2006


Author: ale
Date: Tue Nov 21 15:53:48 2006
New Revision: 34827

Modified:
   pypy/dist/pypy/lib/pyontology/constraint_classes.py
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_sparql.py
Log:
Make a test pass, adding a generator aware Expression (from logilab.constraint.fd)


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 15:53:48 2006
@@ -1,6 +1,7 @@
 from logilab.constraint.propagation import AbstractDomain, AbstractConstraint,\
        ConsistencyFailure, Solver
 from logilab.constraint.distributors import DichotomyDistributor, SplitDistributor
+from logilab.constraint.fd import Expression
 from rdflib import URIRef
 import autopath
 import py
@@ -48,6 +49,35 @@
             print strftime('%H:%M:%S'),
             print 'Maximum recursion depth = ', self.max_depth
 
+class MyExpression(Expression):
+
+    def _assign_values(self, domains):
+        variables = []
+        kwargs = {}
+        for variable in self._variables:
+            domain = domains[variable]
+            values = list(domain.getValues())
+            variables.append((domain.size(), [variable, values, 0, len(values)]))
+            kwargs[variable] = values[0]
+        # sort variables to instanciate those with fewer possible values first
+        variables.sort()
+
+        go_on = 1
+        while go_on:
+            yield kwargs
+            # try to instanciate the next variable
+            for size, curr in variables:
+                if (curr[2] + 1) < curr[-1]:
+                    curr[2] += 1
+                    kwargs[curr[0]] = curr[1][curr[2]]
+                    break
+                else:
+                    curr[2] = 0
+                    kwargs[curr[0]] = curr[1][0]
+            else:
+                # it's over
+                go_on = 0
+ 
 
 class MyDistributor(SplitDistributor):
 

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 15:53:48 2006
@@ -1,12 +1,13 @@
 import autopath
 from rdflib import Graph, URIRef, BNode, Literal as rdflib_literal
 from logilab.constraint import  Repository
-from logilab.constraint.fd import  Expression, FiniteDomain as fd
+from logilab.constraint.fd import  FiniteDomain as fd
 from logilab.constraint.propagation import AbstractDomain, AbstractConstraint,\
        ConsistencyFailure
 from pypy.lib.pyontology.sparql_grammar import SPARQLGrammar as SP
 from constraint_classes import *
 Solver = MySolver
+Expression = MyExpression
 import sys, py
 import datetime, time
 from urllib2 import URLError

Modified: pypy/dist/pypy/lib/pyontology/test/test_sparql.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/test_sparql.py	(original)
+++ pypy/dist/pypy/lib/pyontology/test/test_sparql.py	Tue Nov 21 15:53:48 2006
@@ -91,6 +91,7 @@
     O = Ontology()
     O.add_file("testont.rdf")
     O.attach_fd()
+    O.finish()
     res = O.sparql(query)
     assert res[0]['x'] == u'http://example.org/ns#sub' 
 
@@ -102,6 +103,7 @@
     O.add_file("testont.rdf")
     O.attach_fd()
 
+    O.finish()
     res = O.sparql(query)
     assert list(O.variables['query_x_'].getValues())[0] == 'ns_p' 
     assert res[0]['x'] == 'ns_p'
@@ -114,8 +116,7 @@
     O.add_file("testont.rdf")
 
     O.attach_fd()
-#    import pdb
-#    pdb.set_trace()
+    O.finish()
     res = O.sparql(query)
     assert res[0]['x'] == '123'
 
@@ -126,6 +127,7 @@
     O = Ontology()
     O.add_file("testont.rdf")
     O.attach_fd()
+    O.finish()
 
     res = O.sparql(query)
     assert res[0]['x'] == u'http://example.org/ns#sub' 
@@ -139,6 +141,7 @@
     O = Ontology()
     O.add_file("testont.rdf")
     O.attach_fd()
+    O.finish()
 
     res = O.sparql(query)
     assert res[0]['x'] == u'http://example.org/ns#sub' 
@@ -153,6 +156,7 @@
     O = Ontology()
     O.add_file("testont.rdf")
     O.attach_fd()
+    O.finish()
 
     res = O.sparql(query)
     assert list(O.variables['x'].getValues())[0].uri == u'http://example.org/ns#sub' 
@@ -160,13 +164,13 @@
 
 def test_case_7():
     """ for all p's return p[1] if p[0]==s """
-    py.test.skip("Doesn't work yet due to changed generatorinterface")
+    #py.test.skip("Doesn't work yet due to changed generatorinterface")
 
     query = qt_proto % ('?x ?y ?z', '?x ?y ?z .')
     O = Ontology()
     O.add_file("testont.rdf")
     O.attach_fd()
-
+    O.finish()
     res = O.sparql(query)
     assert list(O.variables['query_x_'].getValues())[0].uri == u'http://example.org/ns#sub' 
     assert res[0]['x'] == u'http://example.org/ns#sub' 



More information about the Pypy-commit mailing list