Class TupleExprIRRenderer

java.lang.Object
org.eclipse.rdf4j.queryrender.sparql.TupleExprIRRenderer

@Experimental public class TupleExprIRRenderer extends Object
TupleExprIRRenderer: user-facing façade to convert RDF4J algebra back into SPARQL text.

Conversion of TupleExpr into a textual IR and expression rendering is delegated to TupleExprToIrConverter. This class orchestrates IR transforms and printing, and provides a small configuration surface and convenience entrypoints.

Features:
  • SELECT / ASK / DESCRIBE / CONSTRUCT forms
  • BGPs, OPTIONALs, UNIONs, MINUS, GRAPH, SERVICE, VALUES
  • Property paths, plus safe best-effort reassembly for simple cases
  • Aggregates, GROUP BY, HAVING (with _anon_having_* substitution)
  • Subselects in WHERE
  • ORDER BY, LIMIT, OFFSET
  • Prefix compaction and nice formatting
How it works (big picture):
  • Normalize the TupleExpr (peel Order/Slice/Distinct/etc., detect HAVING) into a lightweight Normalized carrier.
  • Build a textual Intermediate Representation (IR) that mirrors SPARQL’s shape: a header (projection), a list-like WHERE block (IrBGP), and trailing modifiers. The IR tries to be a straightforward, low-logic mirror of the TupleExpr tree.
  • Run a small, ordered pipeline of IR transforms (IrTransforms) that are deliberately side‑effect‑free and compositional. Each transform is narrowly scoped (e.g., property path fusions, negated property sets, collections) and uses simple heuristics like only fusing across parser‑generated bridge variables named with the _anon_path_ prefix.
  • Print the transformed IR using a tiny printer interface (
    invalid reference
    IrPrinter
    ) that centralizes indentation, IRI compaction, and child printing.
Policy/decisions:
  • Do not rewrite a single inequality ?p != <iri> into ?p NOT IN (<iri>). Only reconstruct NOT IN when multiple != terms share the same variable.
  • Do not fuse ?s ?p ?o . FILTER (?p != <iri>) into a negated path ?s !(<iri>) ?o.
  • Use a for rdf:type consistently, incl. inside property lists.
Naming hints from the RDF4J parser:
  • _anon_path_*: anonymous intermediate variables introduced when parsing property paths. Transforms only compose chains across these bridge variables to avoid altering user bindings.
  • _anon_having_*: marks variables synthesized for HAVING extraction.
  • _anon_bnode_*: placeholder variables for [] that should render as an empty blank node.
  • Constructor Details

    • TupleExprIRRenderer

      public TupleExprIRRenderer()
    • TupleExprIRRenderer

      public TupleExprIRRenderer(TupleExprIRRenderer.Config cfg)
  • Method Details

    • reset

      public void reset()
    • toIRSelect

      public IrSelect toIRSelect(TupleExpr tupleExpr)
      Build a best‑effort textual IR for a SELECT‑form query. Steps:
      1. Normalize the TupleExpr (gather LIMIT/OFFSET/ORDER, peel wrappers, detect HAVING candidates).
      2. Translate the remaining WHERE tree into an IR block (IrBGP) with simple, explicit nodes (statement patterns, path triples, filters, graphs, unions, etc.).
      3. Apply the ordered IR transform pipeline (IrTransforms.transformUsingChildren(IrSelect, TupleExprIRRenderer)) to perform purely-textual best‑effort fusions (paths, NPS, collections, property lists) while preserving user variable bindings.
      4. Populate IR header sections (projection, group by, having, order by) from normalized metadata.
      The method intentionally keeps TupleExpr → IR logic simple; most nontrivial decisions live in transform passes for clarity and testability.
    • toIRSelectRaw

      public IrSelect toIRSelectRaw(TupleExpr tupleExpr)
      Build IR without applying IR transforms (raw). Useful for tests and debugging.
    • dumpIRRaw

      public String dumpIRRaw(TupleExpr tupleExpr)
      Dump raw IR (JSON) for debugging/tests.
    • dumpIRTransformed

      public String dumpIRTransformed(TupleExpr tupleExpr)
      Dump transformed IR (JSON) for debugging/tests.
    • render

      public String render(IrSelect ir, TupleExprIRRenderer.DatasetView dataset, boolean subselect)
      Render a textual SELECT query from an IrSelect model.
    • render

      public String render(TupleExpr tupleExpr)
      Backward-compatible: render as SELECT query (no dataset).
    • render

      public String render(TupleExpr tupleExpr, TupleExprIRRenderer.DatasetView dataset)
      SELECT with dataset (FROM/FROM NAMED).
    • renderAsk

      public String renderAsk(TupleExpr tupleExpr, TupleExprIRRenderer.DatasetView dataset)
      ASK query (top-level).
    • convertValueToString

      public String convertValueToString(Value val)
    • convertIRIToString

      public String convertIRIToString(IRI iri)
    • convertVarIriToString

      public String convertVarIriToString(Var v)
      Convert a Var to a compact IRI string when it is bound to a constant IRI; otherwise return null. Centralizes a common pattern used by IR nodes and helpers to avoid duplicate null/instance checks.