code: public GroovyExpression optimize(GroovyExpression source) {
          LOGGER.debug("Optimizing gremlin query: " + source);
          OptimizationContext context = new OptimizationContext();
          GroovyExpression updatedExpression = source;
          for (GremlinOptimization opt : optimizations) {
              updatedExpression = optimize(updatedExpression, opt, context);
              LOGGER.debug("After "+ opt.getClass().getSimpleName() + ", query = " + updatedExpression);
          }
  
          StatementListExpression result = new StatementListExpression();
          result.addStatements(context.getInitialStatements());
          result.addStatement(updatedExpression);
          LOGGER.debug("Final optimized query:  " + result.toString());
          return result;
      }
docs: Optimizes the provided groovy expression.  Note that the optimization
  is a <i>destructive</i> process.  The source GroovyExpression will be
  modified as part of the optimization process.  This is done to avoid
  expensive copying operations where possible.
  
  @param source what to optimize
  @return the optimized query