def f_reversible_inverse(self, program):
    """Invert a SPARQL program from its IR back to its original representation."""
    prefix, conjuncts = self._get_program_parts(program)
    regex_binary = r"\( (.*?) \( (.*?) \) \( (.*?) \) \)"
    regex_unary = r"\( (.*?) a (.*?) \)"
    conjuncts_original_form = []
    for conjunct in conjuncts:
      if "FILTER" in conjunct:
        # Only remove brackets for FILTER conjuncts.
        conjuncts_original_form.append(conjunct[1:-1].strip())
      else:  # Handle unary and binary conjuncts.
        unary_parse = re.findall(regex_unary, conjunct)
        binary_parse = re.findall(regex_binary, conjunct)
        if unary_parse:  # Parse as an unary conjunct.
          conjuncts_original_form.append(
              self._invert_unary_conjunct(unary_parse[0][0], unary_parse[0][1]))
        elif binary_parse:  # Parse as a binary conjunct.
          conjuncts_original_form.extend(
              self._invert_binary_conjunct(binary_parse[0][0],
                                           binary_parse[0][1],
                                           binary_parse[0][2]))
        else:
          raise ValueError("Wrong prediction format.")
    return "{} lb {} rb".format(prefix, " . ".join(conjuncts_original_form))
Invert a SPARQL program from its IR back to its original representation.
language/compir/dataset_parsers/cfq_parser.py