13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. |
14 # details. |
15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """SPARQL -> RQL translator |
18 """SPARQL -> RQL translator""" |
19 |
19 |
20 """ |
|
21 from logilab.common import make_domains |
20 from logilab.common import make_domains |
22 from rql import TypeResolverException |
21 from rql import TypeResolverException |
23 from fyzz.yappsparser import parse |
22 from fyzz.yappsparser import parse |
24 from fyzz import ast |
23 from fyzz import ast |
25 |
24 |
96 if not '*' in svptypes: |
95 if not '*' in svptypes: |
97 if self.set_possible_types(subjvar, svptypes): |
96 if self.set_possible_types(subjvar, svptypes): |
98 modified = True |
97 modified = True |
99 # restrict predicates according to allowed subject var types |
98 # restrict predicates according to allowed subject var types |
100 if subjvar in self.possible_types: |
99 if subjvar in self.possible_types: |
101 yams_predicates = [(s, r, o) for s, r, o in yams_predicates |
100 yams_predicates[:] = [(s, r, o) for s, r, o in yams_predicates |
102 if s == '*' or s in self.possible_types[subjvar]] |
101 if s == '*' or s in self.possible_types[subjvar]] |
103 if isinstance(obj, ast.SparqlVar): |
102 if isinstance(obj, ast.SparqlVar): |
104 # make a valid rql var name |
103 # make a valid rql var name |
105 objvar = obj.name.upper() |
104 objvar = obj.name.upper() |
106 # get possible types for the object variable, according to |
105 # get possible types for the object variable, according to |
107 # the current predicate |
106 # the current predicate |
109 if not '*' in ovptypes: |
108 if not '*' in ovptypes: |
110 if self.set_possible_types(objvar, ovptypes): |
109 if self.set_possible_types(objvar, ovptypes): |
111 modified = True |
110 modified = True |
112 # restrict predicates according to allowed object var types |
111 # restrict predicates according to allowed object var types |
113 if objvar in self.possible_types: |
112 if objvar in self.possible_types: |
114 yams_predicates = [(s, r, o) for s, r, o in yams_predicates |
113 yams_predicates[:] = [(s, r, o) for s, r, o in yams_predicates |
115 if o == '*' or o in self.possible_types[objvar]] |
114 if o == '*' or o in self.possible_types[objvar]] |
116 # ensure this still make sense |
115 # ensure this still make sense |
117 if not yams_predicates: |
116 if not yams_predicates: |
118 raise TypeResolverException() |
117 raise TypeResolverException() |
119 if len(yams_predicates) != nbchoices: |
118 if len(yams_predicates) != nbchoices: |
120 modified = True |
119 modified = True |
195 for subj, predicate, obj in sparqlst.where: |
194 for subj, predicate, obj in sparqlst.where: |
196 if not isinstance(subj, ast.SparqlVar): |
195 if not isinstance(subj, ast.SparqlVar): |
197 raise UnsupportedQuery() |
196 raise UnsupportedQuery() |
198 # make a valid rql var name |
197 # make a valid rql var name |
199 subjvar = subj.name.upper() |
198 subjvar = subj.name.upper() |
200 if predicate == ('', 'a'): |
199 if predicate in [('', 'a'), |
|
200 ('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'type')]: |
201 # special 'is' relation |
201 # special 'is' relation |
202 if not isinstance(obj, tuple): |
202 if not isinstance(obj, tuple): |
203 raise UnsupportedQuery() |
203 raise UnsupportedQuery() |
204 # restrict possible types for the subject variable |
204 # restrict possible types for the subject variable |
205 qi.set_possible_types( |
205 qi.set_possible_types( |