You could try running that with RDFlib and see what it does…. How is substitute() different to just string template substitution? Is it just a convenience function so substitution can be done on a Jena SPARQL object (perhaps algebra) rather than a string?
Is that your goal for an RDFLib equivalent? SPARQL string to/from SPARQL grammar object is handles in RDFLib fine so I guess as I think the code says, an RDFLib substitute() could work on either or both. Nick On Thu, Feb 6, 2025 at 10:05 am, Martynas Jusevičius <[marty...@atomgraph.com](mailto:On Thu, Feb 6, 2025 at 10:05 am, Martynas Jusevičius <<a href=)> wrote: > OK I simply fed Jena's source to ChatGPT and asked it to produce an > RDFLIb version :) > No idea if it's correct but I'll find out I guess. > > from rdflib import Variable, BNode, Literal > from rdflib.plugins.sparql.algebra import BGP, Filter, Join, LeftJoin, > Union, Graph, Extend, Minus, OrderBy, Project > > def substitute(algebra, binding): > if isinstance(algebra, BGP): > new_triples = [] > for s, p, o in algebra.triples: > s = binding.get(s, s) > p = binding.get(p, p) > o = binding.get(o, o) > new_triples.append((s, p, o)) > return BGP(new_triples) > elif isinstance(algebra, Filter): > new_p = substitute(algebra.p, binding) > new_expr = algebra.expr > for var, val in binding.items(): > new_expr = new_expr.substitute(var, val) > return Filter(new_expr, new_p) > elif isinstance(algebra, Join): > return Join(substitute(algebra.p1, binding), > substitute(algebra.p2, binding)) > elif isinstance(algebra, LeftJoin): > return LeftJoin(substitute(algebra.p1, binding), > substitute(algebra.p2, binding), algebra.expr) > elif isinstance(algebra, Union): > return Union(substitute(algebra.p1, binding), > substitute(algebra.p2, binding)) > elif isinstance(algebra, Graph): > return Graph(algebra.term, substitute(algebra.p, binding)) > elif isinstance(algebra, Extend): > new_p = substitute(algebra.p, binding) > new_expr = algebra.expr > for var, val in binding.items(): > new_expr = new_expr.substitute(var, val) > return Extend(new_p, new_expr, algebra.var) > elif isinstance(algebra, Minus): > return Minus(substitute(algebra.p1, binding), > substitute(algebra.p2, binding)) > elif isinstance(algebra, OrderBy): > return OrderBy(substitute(algebra.p, binding), algebra.expr) > elif isinstance(algebra, Project): > return Project(substitute(algebra.p, binding), algebra.PV) > else: > return algebra > > On Thu, Feb 6, 2025 at 12:54 AM Martynas Jusevičius > <marty...@atomgraph.com> wrote: >> >> Hi, >> >> I was looking to implement something like substitute() using RDFLib. >> >> I checked the note: >> https://afs.github.io/substitute.htm >> But I couldn't find a clear example. >> >> For example, how would this query string (the template) >> >> PREFIX dbo: <http://dbpedia.org/ontology/> >> CONSTRUCT >> WHERE { ?city dbo:populationTotal ?population } >> >> would look like after substituting (?city, >> <http://dbpedia.org/resource/Copenhagen>)? >> >> >> Martynas