public static void computeOpenGoalOrdering(Proposition andproposition, List opengoals, List goalsequence) {
    { List opengoalclusters = List.newList();
      List clusterpropositions = Stella.NIL_LIST;
      List optimizervariables = List.newList();
      Cons initialgoalsequence = null;
      Cons initialresiduegoals = null;

      if (Logic.$QUERY_OPTIMIZATION_STRATEGY$ == Logic.KWD_STATIC_WITH_CLUSTERING) {
        Logic.extractOpenGoalClusters(opengoals, opengoalclusters);
      }
      if (opengoalclusters.nonEmptyP()) {
        clusterpropositions = List.newList();
        { Object old$Evaluationmode$000 = Logic.$EVALUATIONMODE$.get();

          try {
            Native.setSpecial(Logic.$EVALUATIONMODE$, Logic.KWD_DESCRIPTION);
            { List cluster = null;
              Cons iter000 = opengoalclusters.theConsList;
              Cons collect000 = null;

              for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
                cluster = ((List)(iter000.value));
                if (collect000 == null) {
                  {
                    collect000 = Cons.cons(((cluster.length() > 1) ? Proposition.fastenDownOneProposition(Logic.conjoinPropositions(cluster.theConsList), true) : ((Proposition)(cluster.first()))), Stella.NIL);
                    if (clusterpropositions.theConsList == Stella.NIL) {
                      clusterpropositions.theConsList = collect000;
                    }
                    else {
                      Cons.addConsToEndOfConsList(clusterpropositions.theConsList, collect000);
                    }
                  }
                }
                else {
                  {
                    collect000.rest = Cons.cons(((cluster.length() > 1) ? Proposition.fastenDownOneProposition(Logic.conjoinPropositions(cluster.theConsList), true) : ((Proposition)(cluster.first()))), Stella.NIL);
                    collect000 = collect000.rest;
                  }
                }
              }
            }

          } finally {
            Logic.$EVALUATIONMODE$.set(old$Evaluationmode$000);
          }
        }
        { Proposition prop = null;
          Cons iter001 = clusterpropositions.theConsList;
          Cons collect001 = null;

          for (;!(iter001 == Stella.NIL); iter001 = iter001.rest) {
            prop = ((Proposition)(iter001.value));
            if (!(prop.kind == Logic.KWD_AND)) {
              if (collect001 == null) {
                {
                  collect001 = Cons.cons(prop, Stella.NIL);
                  if (opengoals.theConsList == Stella.NIL) {
                    opengoals.theConsList = collect001;
                  }
                  else {
                    Cons.addConsToEndOfConsList(opengoals.theConsList, collect001);
                  }
                }
              }
              else {
                {
                  collect001.rest = Cons.cons(prop, Stella.NIL);
                  collect001 = collect001.rest;
                }
              }
            }
          }
        }
        { ListIterator it = ((ListIterator)(goalsequence.allocateIterator()));

          while (it.nextP()) {
            if (!opengoals.memberP(((Proposition)(it.value)))) {
              it.valueSetter(null);
            }
          }
        }
        goalsequence.remove(null);
      }
      opengoalclusters.free();
      initialgoalsequence = goalsequence.theConsList;
      initialresiduegoals = ((QueryIterator)(Logic.$QUERYITERATOR$.get())).residueGoals().theConsList;
      { Proposition goal = null;
        Cons iter002 = opengoals.theConsList;

        for (;!(iter002 == Stella.NIL); iter002 = iter002.rest) {
          goal = ((Proposition)(iter002.value));
          Proposition.collectOptimizerVariables(goal, optimizervariables);
        }
      }
      { Object old$Optimalgoalorderingrecursions$000 = Logic.$OPTIMALGOALORDERINGRECURSIONS$.get();

        try {
          Native.setIntSpecial(Logic.$OPTIMALGOALORDERINGRECURSIONS$, 0);
          Logic.computeOptimalOpenGoalOrdering(optimizervariables, opengoals.length(), goalsequence, 1.0, 1.0);
          if (((Integer)(Logic.$OPTIMALGOALORDERINGRECURSIONS$.get())).intValue() >= Logic.$OPTIMAL_GOAL_ORDERING_CUTOFF$) {
            goalsequence.theConsList = initialgoalsequence;
            ((QueryIterator)(Logic.$QUERYITERATOR$.get())).residueGoals().theConsList = initialresiduegoals;
            ((QueryIterator)(Logic.$QUERYITERATOR$.get())).bestGoalSequence().clear();
            KeyValueList.setDynamicSlotValue(((QueryIterator)(Logic.$QUERYITERATOR$.get())).dynamicSlots, Logic.SYM_LOGIC_BEST_COST, FloatWrapper.wrapFloat(Stella.NULL_FLOAT), Stella.NULL_FLOAT_WRAPPER);
            Logic.computeGreedyOpenGoalOrdering(optimizervariables, opengoals.length(), goalsequence, 1.0, 1.0);
          }

        } finally {
          Logic.$OPTIMALGOALORDERINGRECURSIONS$.set(old$Optimalgoalorderingrecursions$000);
        }
      }
      if (((QueryIterator)(Logic.$QUERYITERATOR$.get())).bestGoalSequence().emptyP() &&
          (!opengoals.emptyP())) {
        Logic.computeBackupOpenGoalOrdering(opengoals.reverse());
        if ((Stella.$TRACED_KEYWORDS$ != null) &&
            Stella.$TRACED_KEYWORDS$.membP(Logic.KWD_OPTIMIZER)) {
          {
            System.out.println("Using backup goal ordering:");
            System.out.println("Original Goal: " + andproposition);
          }
;
        }
      }
      { Proposition prop = null;
        Cons iter003 = clusterpropositions.sort(Native.find_java_method("edu.isi.powerloom.logic.Proposition", "biggerConjunctionP", new java.lang.Class [] {Native.find_java_class("edu.isi.powerloom.logic.Proposition"), Native.find_java_class("edu.isi.powerloom.logic.Proposition")})).theConsList;

        for (;!(iter003 == Stella.NIL); iter003 = iter003.rest) {
          prop = ((Proposition)(iter003.value));
          if (prop.kind == Logic.KWD_AND) {
            ((QueryIterator)(Logic.$QUERYITERATOR$.get())).bestGoalSequence().push(prop);
          }
        }
      }
    }
  }