author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 Jan 2010 20:28:56 +0100 | |
changeset 4380 | 5613d7c06339 |
parent 4287 | 15499a46c009 |
child 4467 | 0e73d299730a |
permissions | -rw-r--r-- |
0 | 1 |
"""Functions to add additional annotations on a rql syntax tree to ease later |
2 |
code generation. |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3815
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from logilab.common.compat import any |
|
12 |
||
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
13 |
from rql import BadRQLQuery |
1132 | 14 |
from rql.nodes import Relation, VariableRef, Constant, Variable, Or |
0 | 15 |
from rql.utils import common_parent |
16 |
||
17 |
def _annotate_select(annotator, rqlst): |
|
18 |
for subquery in rqlst.with_: |
|
19 |
annotator._annotate_union(subquery.query) |
|
20 |
#if server.DEBUG: |
|
21 |
# print '-------- sql annotate', repr(rqlst) |
|
22 |
getrschema = annotator.schema.rschema |
|
438 | 23 |
has_text_query = False |
0 | 24 |
need_distinct = rqlst.distinct |
25 |
for rel in rqlst.iget_nodes(Relation): |
|
2353
b11f1068a0d3
fix regression w/ NOT symetric relation (distinct not needed)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2199
diff
changeset
|
26 |
if getrschema(rel.r_type).symetric and not rel.neged(strict=True): |
0 | 27 |
for vref in rel.iget_nodes(VariableRef): |
28 |
stinfo = vref.variable.stinfo |
|
29 |
if not stinfo['constnode'] and stinfo['selected']: |
|
30 |
need_distinct = True |
|
31 |
# XXX could mark as not invariant |
|
32 |
break |
|
33 |
for name, var in rqlst.defined_vars.items(): |
|
34 |
stinfo = var.stinfo |
|
35 |
if stinfo.get('ftirels'): |
|
36 |
has_text_query = True |
|
37 |
if stinfo['attrvar']: |
|
38 |
stinfo['invariant'] = False |
|
39 |
stinfo['principal'] = _select_main_var(stinfo['rhsrelations']) |
|
40 |
continue |
|
41 |
if not stinfo['relations'] and not stinfo['typerels']: |
|
42 |
# Any X, Any MAX(X)... |
|
43 |
# those particular queries should be executed using the system |
|
44 |
# entities table unless there is some type restriction |
|
45 |
stinfo['invariant'] = True |
|
46 |
stinfo['principal'] = None |
|
47 |
continue |
|
48 |
if any(rel for rel in stinfo['relations'] if rel.r_type == 'eid' and rel.operator() != '=') and \ |
|
49 |
not any(r for r in var.stinfo['relations'] - var.stinfo['rhsrelations'] |
|
50 |
if r.r_type != 'eid' and (getrschema(r.r_type).inlined or getrschema(r.r_type).final)): |
|
51 |
# Any X WHERE X eid > 2 |
|
52 |
# those particular queries should be executed using the system entities table |
|
53 |
stinfo['invariant'] = True |
|
54 |
stinfo['principal'] = None |
|
55 |
continue |
|
56 |
if stinfo['selected'] and var.valuable_references() == 1+bool(stinfo['constnode']): |
|
57 |
# "Any X", "Any X, Y WHERE X attr Y" |
|
58 |
stinfo['invariant'] = False |
|
59 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
60 |
joins = set() |
0 | 61 |
invariant = False |
62 |
for ref in var.references(): |
|
63 |
rel = ref.relation() |
|
64 |
if rel is None or rel.is_types_restriction(): |
|
65 |
continue |
|
66 |
lhs, rhs = rel.get_parts() |
|
67 |
onlhs = ref is lhs |
|
68 |
if rel.r_type == 'eid': |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
69 |
if not (onlhs and len(stinfo['relations']) > 1): |
0 | 70 |
break |
71 |
if not stinfo['constnode']: |
|
72 |
joins.add(rel) |
|
73 |
continue |
|
74 |
elif rel.r_type == 'identity': |
|
75 |
# identity can't be used as principal, so check other relation are used |
|
76 |
# XXX explain rhs.operator == '=' |
|
77 |
if rhs.operator != '=' or len(stinfo['relations']) <= 1: #(stinfo['constnode'] and rhs.operator == '='): |
|
78 |
break |
|
79 |
joins.add(rel) |
|
80 |
continue |
|
81 |
rschema = getrschema(rel.r_type) |
|
82 |
if rel.optional: |
|
83 |
if rel in stinfo['optrelations']: |
|
84 |
# optional variable can't be invariant if this is the lhs |
|
85 |
# variable of an inlined relation |
|
86 |
if not rel in stinfo['rhsrelations'] and rschema.inlined: |
|
87 |
break |
|
88 |
else: |
|
89 |
# variable used as main variable of an optional relation |
|
90 |
# can't be invariant |
|
91 |
break |
|
92 |
if rschema.final or (onlhs and rschema.inlined): |
|
93 |
if rschema.type != 'has_text': |
|
94 |
# need join anyway if the variable appears in a final or |
|
95 |
# inlined relation |
|
96 |
break |
|
97 |
joins.add(rel) |
|
98 |
continue |
|
99 |
if not stinfo['constnode']: |
|
100 |
if rschema.inlined and rel.neged(strict=True): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
101 |
# if relation is inlined, can't be invariant if that |
0 | 102 |
# variable is used anywhere else. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
103 |
# see 'Any P WHERE NOT N ecrit_par P, N eid 512': |
0 | 104 |
# sql for 'NOT N ecrit_par P' is 'N.ecrit_par is NULL' so P |
105 |
# can use N.ecrit_par as principal |
|
106 |
if (stinfo['selected'] or len(stinfo['relations']) > 1): |
|
107 |
break |
|
108 |
elif rschema.symetric and stinfo['selected']: |
|
109 |
break |
|
110 |
joins.add(rel) |
|
111 |
else: |
|
112 |
# if there is at least one ambigous relation and no other to |
|
113 |
# restrict types, can't be invariant since we need to filter out |
|
114 |
# other types |
|
115 |
if not annotator.is_ambiguous(var): |
|
116 |
invariant = True |
|
117 |
stinfo['invariant'] = invariant |
|
118 |
if invariant and joins: |
|
119 |
# remember rqlst/solutions analyze information |
|
120 |
# we have to select a kindof "main" relation which will "extrajoins" |
|
121 |
# the other |
|
122 |
# priority should be given to relation which are not in inner queries |
|
123 |
# (eg exists) |
|
124 |
try: |
|
125 |
stinfo['principal'] = _select_principal(var.sqlscope, joins) |
|
126 |
except CantSelectPrincipal: |
|
127 |
stinfo['invariant'] = False |
|
128 |
rqlst.need_distinct = need_distinct |
|
129 |
return has_text_query |
|
130 |
||
131 |
||
132 |
||
133 |
class CantSelectPrincipal(Exception): pass |
|
134 |
||
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
135 |
def _select_principal(sqlscope, relations, _sort=lambda x:x): |
0 | 136 |
"""given a list of rqlst relations, select one which will be used to |
137 |
represent an invariant variable (e.g. using on extremity of the relation |
|
138 |
instead of the variable's type table |
|
139 |
""" |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
140 |
# _sort argument is there for test |
0 | 141 |
diffscope_rels = {} |
142 |
has_same_scope_rel = False |
|
143 |
ored_rels = set() |
|
144 |
diffscope_rels = set() |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
145 |
for rel in _sort(relations): |
0 | 146 |
# note: only eid and has_text among all final relations may be there |
147 |
if rel.r_type in ('eid', 'identity'): |
|
148 |
has_same_scope_rel = rel.sqlscope is sqlscope |
|
149 |
continue |
|
150 |
if rel.ored(traverse_scope=True): |
|
151 |
ored_rels.add(rel) |
|
152 |
elif rel.sqlscope is sqlscope: |
|
153 |
return rel |
|
154 |
elif not rel.neged(traverse_scope=True): |
|
155 |
diffscope_rels.add(rel) |
|
156 |
if len(ored_rels) > 1: |
|
157 |
ored_rels_copy = tuple(ored_rels) |
|
158 |
for rel1 in ored_rels_copy: |
|
159 |
for rel2 in ored_rels_copy: |
|
160 |
if rel1 is rel2: |
|
161 |
continue |
|
162 |
if isinstance(common_parent(rel1, rel2), Or): |
|
163 |
ored_rels.discard(rel1) |
|
164 |
ored_rels.discard(rel2) |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
165 |
for rel in _sort(ored_rels): |
0 | 166 |
if rel.sqlscope is sqlscope: |
167 |
return rel |
|
168 |
diffscope_rels.add(rel) |
|
169 |
# if DISTINCT query, can use variable from a different scope as principal |
|
170 |
# since introduced duplicates will be removed |
|
171 |
if sqlscope.stmt.distinct and diffscope_rels: |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
438
diff
changeset
|
172 |
return iter(_sort(diffscope_rels)).next() |
0 | 173 |
# XXX could use a relation for a different scope if it can't generate |
174 |
# duplicates, so we would have to check cardinality |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
175 |
raise CantSelectPrincipal() |
0 | 176 |
|
177 |
def _select_main_var(relations): |
|
178 |
"""given a list of rqlst relations, select one which will be used as main |
|
179 |
relation for the rhs variable |
|
180 |
""" |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
181 |
principal = None |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
182 |
# sort for test predictability |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
183 |
for rel in sorted(relations, key=lambda x: (x.children[0].name, x.r_type)): |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
184 |
# only equality relation with a variable as rhs may be principal |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
185 |
if rel.operator() not in ('=', 'IS') \ |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
186 |
or not isinstance(rel.children[1].children[0], VariableRef): |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
187 |
continue |
0 | 188 |
if rel.sqlscope is rel.stmt: |
189 |
return rel |
|
190 |
principal = rel |
|
3815
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
191 |
if principal is None: |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
192 |
raise BadRQLQuery('unable to find principal in %s' % ', '.join( |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
193 |
r.as_string() for r in relations)) |
0 | 194 |
return principal |
195 |
||
196 |
||
438 | 197 |
def set_qdata(getrschema, union, noinvariant): |
0 | 198 |
"""recursive function to set querier data on variables in the syntax tree |
199 |
""" |
|
200 |
for select in union.children: |
|
201 |
for subquery in select.with_: |
|
438 | 202 |
set_qdata(getrschema, subquery.query, noinvariant) |
0 | 203 |
for var in select.defined_vars.itervalues(): |
204 |
if var.stinfo['invariant']: |
|
205 |
if var in noinvariant and not var.stinfo['principal'].r_type == 'has_text': |
|
206 |
var._q_invariant = False |
|
207 |
else: |
|
208 |
var._q_invariant = True |
|
209 |
else: |
|
210 |
var._q_invariant = False |
|
438 | 211 |
for rel in select.iget_nodes(Relation): |
212 |
if rel.neged(strict=True) and not rel.is_types_restriction(): |
|
213 |
rschema = getrschema(rel.r_type) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
214 |
if not rschema.final: |
438 | 215 |
# if one of the relation's variable is ambiguous but not |
216 |
# invariant, an intersection will be necessary |
|
217 |
for vref in rel.get_nodes(VariableRef): |
|
218 |
var = vref.variable |
|
219 |
if (not var._q_invariant and var.valuable_references() == 1 |
|
220 |
and len(var.stinfo['possibletypes']) > 1): |
|
221 |
select.need_intersect = True |
|
222 |
break |
|
223 |
else: |
|
224 |
continue |
|
225 |
break |
|
226 |
else: |
|
227 |
select.need_intersect = False |
|
0 | 228 |
|
229 |
||
230 |
class SQLGenAnnotator(object): |
|
231 |
def __init__(self, schema): |
|
232 |
self.schema = schema |
|
233 |
self.nfdomain = frozenset(eschema.type for eschema in schema.entities() |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
234 |
if not eschema.final) |
0 | 235 |
|
236 |
def annotate(self, rqlst): |
|
237 |
"""add information to the rql syntax tree to help sources to do their |
|
238 |
job (read sql generation) |
|
239 |
||
240 |
a variable is tagged as invariant if: |
|
241 |
* it's a non final variable |
|
242 |
* it's not used as lhs in any final or inlined relation |
|
243 |
* there is no type restriction on this variable (either explicit in the |
|
244 |
syntax tree or because a solution for this variable has been removed |
|
245 |
due to security filtering) |
|
246 |
""" |
|
3694 | 247 |
#assert rqlst.TYPE == 'select', rqlst |
0 | 248 |
rqlst.has_text_query = self._annotate_union(rqlst) |
249 |
||
250 |
def _annotate_union(self, union): |
|
251 |
has_text_query = False |
|
252 |
for select in union.children: |
|
253 |
htq = _annotate_select(self, select) |
|
254 |
if htq: |
|
255 |
has_text_query = True |
|
256 |
return has_text_query |
|
257 |
||
258 |
def is_ambiguous(self, var): |
|
259 |
# ignore has_text relation |
|
260 |
if len([rel for rel in var.stinfo['relations'] |
|
261 |
if rel.sqlscope is var.sqlscope and rel.r_type == 'has_text']) == 1: |
|
262 |
return False |
|
263 |
try: |
|
264 |
data = var.stmt._deamb_data |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
265 |
except AttributeError: |
0 | 266 |
data = var.stmt._deamb_data = IsAmbData(self.schema, self.nfdomain) |
267 |
data.compute(var.stmt) |
|
268 |
return data.is_ambiguous(var) |
|
269 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
270 |
|
0 | 271 |
class IsAmbData(object): |
272 |
def __init__(self, schema, nfdomain): |
|
273 |
self.schema = schema |
|
274 |
# shortcuts |
|
275 |
self.rschema = schema.rschema |
|
276 |
self.eschema = schema.eschema |
|
277 |
# domain for non final variables |
|
278 |
self.nfdomain = nfdomain |
|
279 |
# {var: possible solutions set} |
|
280 |
self.varsols = {} |
|
281 |
# set of ambiguous variables |
|
282 |
self.ambiguousvars = set() |
|
283 |
# remember if a variable has been deambiguified by another to avoid |
|
284 |
# doing the opposite |
|
285 |
self.deambification_map = {} |
|
286 |
# not invariant variables (access to final.inlined relation) |
|
287 |
self.not_invariants = set() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
288 |
|
0 | 289 |
def is_ambiguous(self, var): |
290 |
return var in self.ambiguousvars |
|
291 |
||
292 |
def restrict(self, var, restricted_domain): |
|
293 |
self.varsols[var] &= restricted_domain |
|
294 |
if var in self.ambiguousvars and self.varsols[var] == var.stinfo['possibletypes']: |
|
295 |
self.ambiguousvars.remove(var) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
296 |
|
0 | 297 |
def compute(self, rqlst): |
298 |
# set domains for each variable |
|
299 |
for varname, var in rqlst.defined_vars.iteritems(): |
|
300 |
if var.stinfo['uidrels'] or \ |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
301 |
self.eschema(rqlst.solutions[0][varname]).final: |
0 | 302 |
ptypes = var.stinfo['possibletypes'] |
303 |
else: |
|
304 |
ptypes = set(self.nfdomain) |
|
305 |
self.ambiguousvars.add(var) |
|
306 |
self.varsols[var] = ptypes |
|
307 |
if not self.ambiguousvars: |
|
308 |
return |
|
309 |
# apply relation restriction |
|
310 |
self.maydeambrels = maydeambrels = {} |
|
311 |
for rel in rqlst.iget_nodes(Relation): |
|
4287 | 312 |
if rel.r_type == 'eid' or rel.is_types_restriction(): |
0 | 313 |
continue |
314 |
lhs, rhs = rel.get_variable_parts() |
|
315 |
if isinstance(lhs, VariableRef) or isinstance(rhs, VariableRef): |
|
316 |
rschema = self.rschema(rel.r_type) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3249
diff
changeset
|
317 |
if rschema.inlined or rschema.final: |
0 | 318 |
self.not_invariants.add(lhs.variable) |
319 |
self.set_rel_constraint(lhs, rel, rschema.subjects) |
|
320 |
self.set_rel_constraint(rhs, rel, rschema.objects) |
|
321 |
# try to deambiguify more variables by considering other variables'type |
|
322 |
modified = True |
|
323 |
while modified and self.ambiguousvars: |
|
324 |
modified = False |
|
325 |
for var in self.ambiguousvars.copy(): |
|
326 |
try: |
|
327 |
for rel in (var.stinfo['relations'] & maydeambrels[var]): |
|
328 |
if self.deambiguifying_relation(var, rel): |
|
329 |
modified = True |
|
330 |
break |
|
331 |
except KeyError: |
|
332 |
# no relation to deambiguify |
|
333 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
334 |
|
0 | 335 |
def _debug_print(self): |
336 |
print 'varsols', dict((x, sorted(str(v) for v in values)) |
|
337 |
for x, values in self.varsols.iteritems()) |
|
338 |
print 'ambiguous vars', sorted(self.ambiguousvars) |
|
339 |
||
340 |
def set_rel_constraint(self, term, rel, etypes_func): |
|
341 |
if isinstance(term, VariableRef) and self.is_ambiguous(term.variable): |
|
342 |
var = term.variable |
|
343 |
if len(var.stinfo['relations'] - var.stinfo['typerels']) == 1 \ |
|
3249
280080eadb22
fix rqlannotation bug w/ identity relation in subscope
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2353
diff
changeset
|
344 |
or rel.sqlscope is var.sqlscope or rel.r_type == 'identity': |
0 | 345 |
self.restrict(var, frozenset(etypes_func())) |
346 |
try: |
|
347 |
self.maydeambrels[var].add(rel) |
|
348 |
except KeyError: |
|
349 |
self.maydeambrels[var] = set((rel,)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
350 |
|
0 | 351 |
def deambiguifying_relation(self, var, rel): |
352 |
lhs, rhs = rel.get_variable_parts() |
|
353 |
onlhs = var is getattr(lhs, 'variable', None) |
|
354 |
other = onlhs and rhs or lhs |
|
355 |
otheretypes = None |
|
356 |
# XXX isinstance(other.variable, Variable) to skip column alias |
|
357 |
if isinstance(other, VariableRef) and isinstance(other.variable, Variable): |
|
358 |
deambiguifier = other.variable |
|
359 |
if not var is self.deambification_map.get(deambiguifier): |
|
360 |
if not var.stinfo['typerels']: |
|
361 |
otheretypes = deambiguifier.stinfo['possibletypes'] |
|
362 |
elif not self.is_ambiguous(deambiguifier): |
|
363 |
otheretypes = self.varsols[deambiguifier] |
|
364 |
elif deambiguifier in self.not_invariants: |
|
365 |
# we know variable won't be invariant, try to use |
|
366 |
# it to deambguify the current variable |
|
367 |
otheretypes = self.varsols[deambiguifier] |
|
4285
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
368 |
if not deambiguifier.stinfo['typerels']: |
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
369 |
# if deambiguifier has no type restriction using 'is', |
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
370 |
# don't record it |
ea590101691c
don't record deambiguifier when it has no type restriction using 'is', so we don't miss later some available constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
371 |
deambiguifier = None |
0 | 372 |
elif isinstance(other, Constant) and other.uidtype: |
373 |
otheretypes = (other.uidtype,) |
|
374 |
deambiguifier = None |
|
375 |
if otheretypes is not None: |
|
967 | 376 |
# to restrict, we must check that for all type in othertypes, |
377 |
# possible types on the other end of the relation are matching |
|
378 |
# variable's possible types |
|
0 | 379 |
rschema = self.rschema(rel.r_type) |
380 |
if onlhs: |
|
381 |
rtypefunc = rschema.subjects |
|
382 |
else: |
|
383 |
rtypefunc = rschema.objects |
|
384 |
for otheretype in otheretypes: |
|
385 |
reltypes = frozenset(rtypefunc(otheretype)) |
|
386 |
if var.stinfo['possibletypes'] != reltypes: |
|
4287 | 387 |
return False |
388 |
self.restrict(var, var.stinfo['possibletypes']) |
|
389 |
self.deambification_map[var] = deambiguifier |
|
390 |
return True |
|
0 | 391 |
return False |