def evalDeleteWhere(ctx, u): """ http://www.w3.org/TR/sparql11-update/#deleteWhere """ res = {} res["type"] = "DELETEWHERE" res["delta"] = {} _res = evalBGP(ctx, u.triples) for g in u.quads: cg = ctx.dataset.get_context(g) c = ctx.pushGraph(cg) _res = _join(_res, list(evalBGP(c, u.quads[g]))) for c in _res: g = ctx.graph filled, filled_delta = tee(_fillTemplate(u.triples, c)) _append(res["delta"], 'default', 'removals', list(filled_delta)) g -= filled for g in u.quads: cg = ctx.dataset.get_context(c.get(g)) filledq, filledq_delta = tee(_fillTemplate(u.quads[g], c)) _append(res["delta"], cg.identifier, 'removals', list(filledq_delta)) cg -= filledq return res
def evalDeleteWhere(ctx, u): """ http://www.w3.org/TR/sparql11-update/#deleteWhere """ res = {} res["type_"] = "DELETEWHERE" res["delta"] = {} _res = evalBGP(ctx, u.triples) for g in u.quads: cg = ctx.dataset.get_context(g) c = ctx.pushGraph(cg) _res = _join(_res, list(evalBGP(c, u.quads[g]))) for c in _res: g = ctx.graph filled, filled_delta = tee(_fillTemplate(u.triples, c)) _append(res["delta"], 'default', 'removals', list(filled_delta)) g -= filled for g in u.quads: cg = ctx.dataset.get_context(c.get(g)) filledq, filledq_delta = tee(_fillTemplate(u.quads[g], c)) _append(res["delta"], cg.identifier, 'removals', list(filledq_delta)) cg -= filledq return res
def evalModify(ctx, u): originalctx = ctx # Using replaces the dataset for evaluating the where-clause if u.using: otherDefault = False for d in u.using: if d.default: if not otherDefault: # replace current default graph dg = Graph() ctx = ctx.pushGraph(dg) otherDefault = True ctx.load(d.default, default=True) elif d.named: g = d.named ctx.load(g, default=False) # "The WITH clause provides a convenience for when an operation # primarily refers to a single graph. If a graph name is specified # in a WITH clause, then - for the purposes of evaluating the # WHERE clause - this will define an RDF Dataset containing a # default graph with the specified name, but only in the absence # of USING or USING NAMED clauses. In the presence of one or more # graphs referred to in USING clauses and/or USING NAMED clauses, # the WITH clause will be ignored while evaluating the WHERE # clause." if not u.using and u.withClause: g = ctx.dataset.get_context(u.withClause) ctx = ctx.pushGraph(g) res = evalPart(ctx, u.where) if u.using: if otherDefault: ctx = originalctx # restore original default graph if u.withClause: g = ctx.dataset.get_context(u.withClause) ctx = ctx.pushGraph(g) for c in res: dg = ctx.graph if u.delete: dg -= _fillTemplate(u.delete.triples, c) for g, q in u.delete.quads.items(): cg = ctx.dataset.get_context(c.get(g)) cg -= _fillTemplate(q, c) if u.insert: dg += _fillTemplate(u.insert.triples, c) for g, q in u.insert.quads.items(): cg = ctx.dataset.get_context(c.get(g)) cg += _fillTemplate(q, c)
def evalModify(ctx, u): originalctx = ctx # Using replaces the dataset for evaluating the where-clause if u.using: otherDefault = False for d in u.using: if d.default: if not otherDefault: # replace current default graph dg = Graph() ctx = ctx.pushGraph(dg) otherDefault = True ctx.load(d.default, default=True) elif d.named: g = d.named ctx.load(g, default=False) # "The WITH clause provides a convenience for when an operation # primarily refers to a single graph. If a graph name is specified # in a WITH clause, then - for the purposes of evaluating the # WHERE clause - this will define an RDF Dataset containing a # default graph with the specified name, but only in the absence # of USING or USING NAMED clauses. In the presence of one or more # graphs referred to in USING clauses and/or USING NAMED clauses, # the WITH clause will be ignored while evaluating the WHERE # clause." if not u.using and u.withClause: g = ctx.dataset.get_context(u.withClause) ctx = ctx.pushGraph(g) res = evalPart(ctx, u.where) if u.using: if otherDefault: ctx = originalctx # restore original default graph if u.withClause: g = ctx.dataset.get_context(u.withClause) ctx = ctx.pushGraph(g) for c in res: dg = ctx.graph if u.delete: dg -= _fillTemplate(u.delete.triples, c) for g, q in iteritems(u.delete.quads): cg = ctx.dataset.get_context(c.get(g)) cg -= _fillTemplate(q, c) if u.insert: dg += _fillTemplate(u.insert.triples, c) for g, q in iteritems(u.insert.quads): cg = ctx.dataset.get_context(c.get(g)) cg += _fillTemplate(q, c)
def evalDeleteWhere(ctx, u): """ http://www.w3.org/TR/sparql11-update/#deleteWhere """ res = evalBGP(ctx, u.triples) for g in u.quads: cg = ctx.dataset.get_context(g) c = ctx.pushGraph(cg) res = _join(res, list(evalBGP(c, u.quads[g]))) for c in res: g = ctx.graph g -= _fillTemplate(u.triples, c) for g in u.quads: cg = ctx.dataset.get_context(c.get(g)) cg -= _fillTemplate(u.quads[g], c)
def evalConstructQuery(ctx, query): template = query.template if not template: # a construct-where query template = query.p.p.triples # query->project->bgp ... graph = Graph() for c in evalPart(ctx, query.p): graph += _fillTemplate(template, c) res = {} res["type_"] = "CONSTRUCT" res["graph"] = graph return res
def evalModify(ctx, u): originalctx = ctx res = {} res["type_"] = "MODIFY" res["delta"] = {} # Using replaces the dataset for evaluating the where-clause if u.using: otherDefault = False for d in u.using: if d.default: if not otherDefault: # replace current default graph dg = Graph() ctx = ctx.pushGraph(dg) otherDefault = True ctx.load(d.default, default=True) elif d.named: g = d.named ctx.load(g, default=False) # "The WITH clause provides a convenience for when an operation # primarily refers to a single graph. If a graph name is specified # in a WITH clause, then - for the purposes of evaluating the # WHERE clause - this will define an RDF Dataset containing a # default graph with the specified name, but only in the absence # of USING or USING NAMED clauses. In the presence of one or more # graphs referred to in USING clauses and/or USING NAMED clauses, # the WITH clause will be ignored while evaluating the WHERE # clause." graphName = 'default' if not u.using and u.withClause: g = ctx.dataset.get_context(u.withClause) graphName = str(g.identifier) ctx = ctx.pushGraph(g) _res = evalPart(ctx, u.where) if u.using: if otherDefault: ctx = originalctx # restore original default graph if u.withClause: g = ctx.dataset.get_context(u.withClause) graphName = str(g.identifier) ctx = ctx.pushGraph(g) for c in _res: dg = ctx.graph if u.delete: filled, filled_delta = tee(_fillTemplate(u.delete.triples, c)) _append(res["delta"], graphName, 'removals', list(filled_delta)) dg -= filled for g, q in u.delete.quads.items(): cg = ctx.dataset.get_context(c.get(g)) filledq, filledq_delta = tee(_fillTemplate(q, c)) _append(res["delta"], cg.identifier, 'removals', list(filledq_delta)) cg -= filledq if u.insert: filled, filled_delta = tee(_fillTemplate(u.insert.triples, c)) _append(res["delta"], graphName, 'additions', list(filled_delta)) dg += filled for g, q in u.insert.quads.items(): cg = ctx.dataset.get_context(c.get(g)) filledq, filledq_delta = tee(_fillTemplate(q, c)) _append(res["delta"], cg.identifier, 'additions', list(filledq_delta)) cg += filledq return res
def evalModify(ctx, u): originalctx = ctx res = {} res["type"] = "MODIFY" res["delta"] = {} # Using replaces the dataset for evaluating the where-clause if u.using: otherDefault = False for d in u.using: if d.default: if not otherDefault: # replace current default graph dg = Graph() ctx = ctx.pushGraph(dg) otherDefault = True ctx.load(d.default, default=True) # TODO re-enable original behaviour if USING NAMED works with named graphs # https://github.com/AKSW/QuitStore/issues/144 elif d.named: if otherDefault: ctx = originalctx # restore original default graph raise UnSupportedQuery # g = d.named # ctx.load(g, default=False) # "The WITH clause provides a convenience for when an operation # primarily refers to a single graph. If a graph name is specified # in a WITH clause, then - for the purposes of evaluating the # WHERE clause - this will define an RDF Dataset containing a # default graph with the specified name, but only in the absence # of USING or USING NAMED clauses. In the presence of one or more # graphs referred to in USING clauses and/or USING NAMED clauses, # the WITH clause will be ignored while evaluating the WHERE # clause." graphName = 'default' if not u.using and u.withClause: g = ctx.dataset.get_context(u.withClause) graphName = str(g.identifier) ctx = ctx.pushGraph(g) _res = evalPart(ctx, u.where) if u.using: if otherDefault: ctx = originalctx # restore original default graph if u.withClause: g = ctx.dataset.get_context(u.withClause) graphName = str(g.identifier) ctx = ctx.pushGraph(g) for c in _res: dg = ctx.graph if u.delete: filled, filled_delta = tee(_fillTemplate(u.delete.triples, c)) _append(res["delta"], graphName, 'removals', list(filled_delta)) dg -= filled for g, q in u.delete.quads.items(): cg = ctx.dataset.get_context(c.get(g)) filledq, filledq_delta = tee(_fillTemplate(q, c)) _append(res["delta"], cg.identifier, 'removals', list(filledq_delta)) cg -= filledq if u.insert: filled, filled_delta = tee(_fillTemplate(u.insert.triples, c)) _append(res["delta"], graphName, 'additions', list(filled_delta)) dg += filled for g, q in u.insert.quads.items(): cg = ctx.dataset.get_context(c.get(g)) filledq, filledq_delta = tee(_fillTemplate(q, c)) _append(res["delta"], cg.identifier, 'additions', list(filledq_delta)) cg += filledq return res