code: public InternalSparqlQuery translate(ParsedQuery pq) throws OntopUnsupportedInputQueryException, OntopInvalidInputQueryException {
  
          if (predicateIdx != 0 || !program.getRules().isEmpty())
              throw new IllegalStateException("SparqlAlgebraToDatalogTranslator.translate can only be called once.");
  
  		TupleExpr te = pq.getTupleExpr();
  		log.debug("SPARQL algebra: \n{}", te);
          //System.out.println("SPARQL algebra: \n" + te);
  
          TranslationResult body = translate(te);
  
          List<Variable> answerVariables;
  		if (pq instanceof ParsedTupleQuery || pq instanceof ParsedGraphQuery) {
              // order elements of the set in some way by converting it into the list
              answerVariables = new ArrayList<>(body.variables);
          }
  		else {
              // ASK queries have no answer variables
              answerVariables = Collections.emptyList();
          }
  
          AtomPredicate pred = atomFactory.getRDFAnswerPredicate(answerVariables.size());
          Function head = termFactory.getFunction(pred, (List<Term>)(List<?>)answerVariables);
          appendRule(head, body.atoms);
  
          //System.out.println("PROGRAM\n" + program.program);
  		return new InternalSparqlQuery(program, ImmutableList.copyOf(answerVariables));
  	}
docs: Translate a given SPARQL query object to datalog program.
  
  IMPORTANT: this method should be called only once on each instance of the class
  
  @pq     SPARQL query object
  @return our representation of the SPARQL query (as a datalog program)
  
  TODO: return an IntermediateQuery instead!