author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 12 Nov 2009 09:33:55 +0100 | |
branch | stable |
changeset 3822 | b61c7b065a66 |
parent 3815 | 50b87f759b5d |
child 4212 | ab6573088b4a |
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 |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 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 |
print iter(relations).next().root |
50b87f759b5d
test and fix http://www.logilab.org/ticket/499838
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3694
diff
changeset
|
193 |
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
|
194 |
r.as_string() for r in relations)) |
0 | 195 |
return principal |
196 |
||
197 |
||
438 | 198 |
def set_qdata(getrschema, union, noinvariant): |
0 | 199 |
"""recursive function to set querier data on variables in the syntax tree |
200 |
""" |
|
201 |
for select in union.children: |
|
202 |
for subquery in select.with_: |
|
438 | 203 |
set_qdata(getrschema, subquery.query, noinvariant) |
0 | 204 |
for var in select.defined_vars.itervalues(): |
205 |
if var.stinfo['invariant']: |
|
206 |
if var in noinvariant and not var.stinfo['principal'].r_type == 'has_text': |
|
207 |
var._q_invariant = False |
|
208 |
else: |
|
209 |
var._q_invariant = True |
|
210 |
else: |
|
211 |
var._q_invariant = False |
|
438 | 212 |
for rel in select.iget_nodes(Relation): |
213 |
if rel.neged(strict=True) and not rel.is_types_restriction(): |
|
214 |
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
|
215 |
if not rschema.final: |
438 | 216 |
# if one of the relation's variable is ambiguous but not |
217 |
# invariant, an intersection will be necessary |
|
218 |
for vref in rel.get_nodes(VariableRef): |
|
219 |
var = vref.variable |
|
220 |
if (not var._q_invariant and var.valuable_references() == 1 |
|
221 |
and len(var.stinfo['possibletypes']) > 1): |
|
222 |
select.need_intersect = True |
|
223 |
break |
|
224 |
else: |
|
225 |
continue |
|
226 |
break |
|
227 |
else: |
|
228 |
select.need_intersect = False |
|
0 | 229 |
|
230 |
||
231 |
class SQLGenAnnotator(object): |
|
232 |
def __init__(self, schema): |
|
233 |
self.schema = schema |
|
234 |
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
|
235 |
if not eschema.final) |
0 | 236 |
|
237 |
def annotate(self, rqlst): |
|
238 |
"""add information to the rql syntax tree to help sources to do their |
|
239 |
job (read sql generation) |
|
240 |
||
241 |
a variable is tagged as invariant if: |
|
242 |
* it's a non final variable |
|
243 |
* it's not used as lhs in any final or inlined relation |
|
244 |
* there is no type restriction on this variable (either explicit in the |
|
245 |
syntax tree or because a solution for this variable has been removed |
|
246 |
due to security filtering) |
|
247 |
""" |
|
3694 | 248 |
#assert rqlst.TYPE == 'select', rqlst |
0 | 249 |
rqlst.has_text_query = self._annotate_union(rqlst) |
250 |
||
251 |
def _annotate_union(self, union): |
|
252 |
has_text_query = False |
|
253 |
for select in union.children: |
|
254 |
htq = _annotate_select(self, select) |
|
255 |
if htq: |
|
256 |
has_text_query = True |
|
257 |
return has_text_query |
|
258 |
||
259 |
def is_ambiguous(self, var): |
|
260 |
# ignore has_text relation |
|
261 |
if len([rel for rel in var.stinfo['relations'] |
|
262 |
if rel.sqlscope is var.sqlscope and rel.r_type == 'has_text']) == 1: |
|
263 |
return False |
|
264 |
try: |
|
265 |
data = var.stmt._deamb_data |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
266 |
except AttributeError: |
0 | 267 |
data = var.stmt._deamb_data = IsAmbData(self.schema, self.nfdomain) |
268 |
data.compute(var.stmt) |
|
269 |
return data.is_ambiguous(var) |
|
270 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
271 |
|
0 | 272 |
class IsAmbData(object): |
273 |
def __init__(self, schema, nfdomain): |
|
274 |
self.schema = schema |
|
275 |
# shortcuts |
|
276 |
self.rschema = schema.rschema |
|
277 |
self.eschema = schema.eschema |
|
278 |
# domain for non final variables |
|
279 |
self.nfdomain = nfdomain |
|
280 |
# {var: possible solutions set} |
|
281 |
self.varsols = {} |
|
282 |
# set of ambiguous variables |
|
283 |
self.ambiguousvars = set() |
|
284 |
# remember if a variable has been deambiguified by another to avoid |
|
285 |
# doing the opposite |
|
286 |
self.deambification_map = {} |
|
287 |
# not invariant variables (access to final.inlined relation) |
|
288 |
self.not_invariants = set() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
289 |
|
0 | 290 |
def is_ambiguous(self, var): |
291 |
return var in self.ambiguousvars |
|
292 |
||
293 |
def restrict(self, var, restricted_domain): |
|
294 |
self.varsols[var] &= restricted_domain |
|
295 |
if var in self.ambiguousvars and self.varsols[var] == var.stinfo['possibletypes']: |
|
296 |
self.ambiguousvars.remove(var) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
297 |
|
0 | 298 |
def compute(self, rqlst): |
299 |
# set domains for each variable |
|
300 |
for varname, var in rqlst.defined_vars.iteritems(): |
|
301 |
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
|
302 |
self.eschema(rqlst.solutions[0][varname]).final: |
0 | 303 |
ptypes = var.stinfo['possibletypes'] |
304 |
else: |
|
305 |
ptypes = set(self.nfdomain) |
|
306 |
self.ambiguousvars.add(var) |
|
307 |
self.varsols[var] = ptypes |
|
308 |
if not self.ambiguousvars: |
|
309 |
return |
|
310 |
# apply relation restriction |
|
311 |
self.maydeambrels = maydeambrels = {} |
|
312 |
for rel in rqlst.iget_nodes(Relation): |
|
313 |
if rel.is_types_restriction() or rel.r_type == 'eid': |
|
314 |
continue |
|
315 |
lhs, rhs = rel.get_variable_parts() |
|
316 |
if isinstance(lhs, VariableRef) or isinstance(rhs, VariableRef): |
|
317 |
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
|
318 |
if rschema.inlined or rschema.final: |
0 | 319 |
self.not_invariants.add(lhs.variable) |
320 |
self.set_rel_constraint(lhs, rel, rschema.subjects) |
|
321 |
self.set_rel_constraint(rhs, rel, rschema.objects) |
|
322 |
# try to deambiguify more variables by considering other variables'type |
|
323 |
modified = True |
|
324 |
while modified and self.ambiguousvars: |
|
325 |
modified = False |
|
326 |
for var in self.ambiguousvars.copy(): |
|
327 |
try: |
|
328 |
for rel in (var.stinfo['relations'] & maydeambrels[var]): |
|
329 |
if self.deambiguifying_relation(var, rel): |
|
330 |
modified = True |
|
331 |
break |
|
332 |
except KeyError: |
|
333 |
# no relation to deambiguify |
|
334 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
335 |
|
0 | 336 |
def _debug_print(self): |
337 |
print 'varsols', dict((x, sorted(str(v) for v in values)) |
|
338 |
for x, values in self.varsols.iteritems()) |
|
339 |
print 'ambiguous vars', sorted(self.ambiguousvars) |
|
340 |
||
341 |
def set_rel_constraint(self, term, rel, etypes_func): |
|
342 |
if isinstance(term, VariableRef) and self.is_ambiguous(term.variable): |
|
343 |
var = term.variable |
|
344 |
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
|
345 |
or rel.sqlscope is var.sqlscope or rel.r_type == 'identity': |
0 | 346 |
self.restrict(var, frozenset(etypes_func())) |
347 |
try: |
|
348 |
self.maydeambrels[var].add(rel) |
|
349 |
except KeyError: |
|
350 |
self.maydeambrels[var] = set((rel,)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
351 |
|
0 | 352 |
def deambiguifying_relation(self, var, rel): |
353 |
lhs, rhs = rel.get_variable_parts() |
|
354 |
onlhs = var is getattr(lhs, 'variable', None) |
|
355 |
other = onlhs and rhs or lhs |
|
356 |
otheretypes = None |
|
357 |
# XXX isinstance(other.variable, Variable) to skip column alias |
|
358 |
if isinstance(other, VariableRef) and isinstance(other.variable, Variable): |
|
359 |
deambiguifier = other.variable |
|
360 |
if not var is self.deambification_map.get(deambiguifier): |
|
361 |
if not var.stinfo['typerels']: |
|
362 |
otheretypes = deambiguifier.stinfo['possibletypes'] |
|
363 |
elif not self.is_ambiguous(deambiguifier): |
|
364 |
otheretypes = self.varsols[deambiguifier] |
|
365 |
elif deambiguifier in self.not_invariants: |
|
366 |
# we know variable won't be invariant, try to use |
|
367 |
# it to deambguify the current variable |
|
368 |
otheretypes = self.varsols[deambiguifier] |
|
369 |
elif isinstance(other, Constant) and other.uidtype: |
|
370 |
otheretypes = (other.uidtype,) |
|
371 |
deambiguifier = None |
|
372 |
if otheretypes is not None: |
|
967 | 373 |
# to restrict, we must check that for all type in othertypes, |
374 |
# possible types on the other end of the relation are matching |
|
375 |
# variable's possible types |
|
0 | 376 |
rschema = self.rschema(rel.r_type) |
377 |
if onlhs: |
|
378 |
rtypefunc = rschema.subjects |
|
379 |
else: |
|
380 |
rtypefunc = rschema.objects |
|
381 |
for otheretype in otheretypes: |
|
382 |
reltypes = frozenset(rtypefunc(otheretype)) |
|
383 |
if var.stinfo['possibletypes'] != reltypes: |
|
384 |
break |
|
967 | 385 |
else: |
386 |
self.restrict(var, var.stinfo['possibletypes']) |
|
0 | 387 |
self.deambification_map[var] = deambiguifier |
388 |
return True |
|
389 |
return False |