Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/rdflib/plugins/sparql/evalutils.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:32:28 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:d30785e31577 | 1:56ad4e20f292 |
|---|---|
| 1 import collections | |
| 2 | |
| 3 from rdflib.term import Variable, Literal, BNode, URIRef | |
| 4 | |
| 5 from rdflib.plugins.sparql.operators import EBV | |
| 6 from rdflib.plugins.sparql.parserutils import Expr, CompValue, value | |
| 7 from rdflib.plugins.sparql.sparql import SPARQLError, NotBoundError | |
| 8 | |
| 9 | |
| 10 def _diff(a, b, expr): | |
| 11 res = set() | |
| 12 | |
| 13 for x in a: | |
| 14 if all(not x.compatible(y) or not _ebv(expr, x.merge(y)) for y in b): | |
| 15 res.add(x) | |
| 16 | |
| 17 return res | |
| 18 | |
| 19 | |
| 20 def _minus(a, b): | |
| 21 for x in a: | |
| 22 if all((not x.compatible(y)) or x.disjointDomain(y) for y in b): | |
| 23 yield x | |
| 24 | |
| 25 | |
| 26 | |
| 27 def _join(a, b): | |
| 28 for x in a: | |
| 29 for y in b: | |
| 30 if x.compatible(y): | |
| 31 yield x.merge(y) | |
| 32 | |
| 33 | |
| 34 def _ebv(expr, ctx): | |
| 35 | |
| 36 """ | |
| 37 Return true/false for the given expr | |
| 38 Either the expr is itself true/false | |
| 39 or evaluates to something, with the given ctx | |
| 40 | |
| 41 an error is false | |
| 42 """ | |
| 43 | |
| 44 try: | |
| 45 return EBV(expr) | |
| 46 except SPARQLError: | |
| 47 pass | |
| 48 if isinstance(expr, Expr): | |
| 49 try: | |
| 50 return EBV(expr.eval(ctx)) | |
| 51 except SPARQLError: | |
| 52 return False # filter error == False | |
| 53 elif isinstance(expr, CompValue): | |
| 54 raise Exception( | |
| 55 "Weird - filter got a CompValue without evalfn! %r" % expr) | |
| 56 elif isinstance(expr, Variable): | |
| 57 try: | |
| 58 return EBV(ctx[expr]) | |
| 59 except: | |
| 60 return False | |
| 61 return False | |
| 62 | |
| 63 | |
| 64 def _eval(expr, ctx, raise_not_bound_error=True): | |
| 65 if isinstance(expr, (Literal, URIRef)): | |
| 66 return expr | |
| 67 if isinstance(expr, Expr): | |
| 68 return expr.eval(ctx) | |
| 69 elif isinstance(expr, Variable): | |
| 70 try: | |
| 71 return ctx[expr] | |
| 72 except KeyError: | |
| 73 if raise_not_bound_error: | |
| 74 raise NotBoundError("Variable %s is not bound" % expr) | |
| 75 else: | |
| 76 return None | |
| 77 elif isinstance(expr, CompValue): | |
| 78 raise Exception( | |
| 79 "Weird - _eval got a CompValue without evalfn! %r" % expr) | |
| 80 else: | |
| 81 raise Exception("Cannot eval thing: %s (%s)" % (expr, type(expr))) | |
| 82 | |
| 83 | |
| 84 def _filter(a, expr): | |
| 85 for c in a: | |
| 86 if _ebv(expr, c): | |
| 87 yield c | |
| 88 | |
| 89 | |
| 90 def _fillTemplate(template, solution): | |
| 91 | |
| 92 """ | |
| 93 For construct/deleteWhere and friends | |
| 94 | |
| 95 Fill a triple template with instantiated variables | |
| 96 """ | |
| 97 | |
| 98 bnodeMap = collections.defaultdict(BNode) | |
| 99 for t in template: | |
| 100 s, p, o = t | |
| 101 | |
| 102 _s = solution.get(s) | |
| 103 _p = solution.get(p) | |
| 104 _o = solution.get(o) | |
| 105 | |
| 106 # instantiate new bnodes for each solution | |
| 107 _s, _p, _o = [bnodeMap[x] if isinstance( | |
| 108 x, BNode) else y for x, y in zip(t, (_s, _p, _o))] | |
| 109 | |
| 110 if _s is not None and \ | |
| 111 _p is not None and \ | |
| 112 _o is not None: | |
| 113 | |
| 114 yield (_s, _p, _o) | |
| 115 | |
| 116 def _val(v): | |
| 117 """ utilitity for ordering things""" | |
| 118 if isinstance(v, Variable): | |
| 119 return (0, v) | |
| 120 elif isinstance(v, BNode): | |
| 121 return (1, v) | |
| 122 elif isinstance(v, URIRef): | |
| 123 return (2, v) | |
| 124 elif isinstance(v, Literal): | |
| 125 return (3, v) |
