in assets/lambda_helper_neptune/python/rdflib/paths.py [0:0]
def eval(self, graph, subj=None, obj=None, first=True):
if self.zero and first:
if subj and obj:
if subj == obj:
yield (subj, obj)
elif subj:
yield (subj, subj)
elif obj:
yield (obj, obj)
def _fwd(subj=None, obj=None, seen=None):
seen.add(subj)
for s, o in evalPath(graph, (subj, self.path, None)):
if not obj or o == obj:
yield s, o
if self.more:
if o in seen:
continue
for s2, o2 in _fwd(o, obj, seen):
yield s, o2
def _bwd(subj=None, obj=None, seen=None):
seen.add(obj)
for s, o in evalPath(graph, (None, self.path, obj)):
if not subj or subj == s:
yield s, o
if self.more:
if s in seen:
continue
for s2, o2 in _bwd(None, s, seen):
yield s2, o
def _fwdbwd():
if self.zero:
seen1 = set()
# According to the spec, ALL nodes are possible solutions
# (even literals)
# we cannot do this without going through ALL triples
# unless we keep an index of all terms somehow
# but lets just hope this query doesnt happen very often...
for s, o in graph.subject_objects(None):
if s not in seen1:
seen1.add(s)
yield s, s
if o not in seen1:
seen1.add(o)
yield o, o
for s, o in evalPath(graph, (None, self.path, None)):
if not self.more:
yield s, o
else:
seen = set()
f = list(_fwd(s, None, seen)) # cache or recompute?
for s3, o3 in _bwd(None, o, seen):
for s2, o2 in f:
yield s3, o2 # ?
done = set() # the spec does by defn. not allow duplicates
if subj:
for x in _fwd(subj, obj, set()):
if x not in done:
done.add(x)
yield x
elif obj:
for x in _bwd(subj, obj, set()):
if x not in done:
done.add(x)
yield x
else:
for x in _fwdbwd():
if x not in done:
done.add(x)
yield x