public static Keyword querySpecialist(ControlFrame frame, Keyword lastmove) {
    { QueryIterator subqueryiterator = ((QueryIterator)(((Iterator)(KeyValueList.dynamicSlotValue(frame.dynamicSlots, Logic.SYM_STELLA_ITERATOR, null)))));
      boolean truefalsequeryP = false;
      boolean partialqueryP = false;
      boolean sortedqueryP = false;
      boolean successP = false;
      ControlFrame result = null;

      lastmove = lastmove;
      if (subqueryiterator == null) {
        { Object [] caller_MV_returnarray = new Object[2];

          subqueryiterator = ControlFrame.createQuerySpecialistIterator(frame, caller_MV_returnarray);
          truefalsequeryP = ((boolean)(((BooleanWrapper)(caller_MV_returnarray[0])).wrapperValue));
          partialqueryP = ((boolean)(((BooleanWrapper)(caller_MV_returnarray[1])).wrapperValue));
        }
        if (subqueryiterator == null) {
          return (Logic.KWD_TERMINAL_FAILURE);
        }
        else {
          KeyValueList.setDynamicSlotValue(frame.dynamicSlots, Logic.SYM_STELLA_ITERATOR, subqueryiterator, null);
        }
        if (partialqueryP) {
          if (truefalsequeryP) {
            Logic.callAskPartial(subqueryiterator);
          }
          else {
            Logic.callRetrievePartial(subqueryiterator);
          }
        }
        else if ((!truefalsequeryP) &&
            (Logic.lookupQueryOption(subqueryiterator, Logic.KWD_SORT_BY) != null)) {
          sortedqueryP = true;
          Logic.callRetrieve(subqueryiterator);
        }
      }
      else {
        partialqueryP = subqueryiterator.partialMatchStrategy != null;
        sortedqueryP = Logic.lookupQueryOption(subqueryiterator, Logic.KWD_SORT_BY) != null;
      }
      if ((Stella.$TRACED_KEYWORDS$ != null) &&
          Stella.$TRACED_KEYWORDS$.membP(Logic.KWD_GOAL_TREE)) {
        System.out.println();
      }
      loop000 : for (;;) {
        if (partialqueryP ||
            sortedqueryP) {
          successP = subqueryiterator.querySucceededP();
          if (successP) {
            { QuerySolution solution = subqueryiterator.solutions.pop();

              result = subqueryiterator.baseControlFrame;
              if (!(truefalsequeryP)) {
                subqueryiterator.value = solution;
              }
              result.truthValue = solution.truthValue;
              if (((Boolean)(Logic.$RECORD_JUSTIFICATIONSp$.get())).booleanValue()) {
                KeyValueList.setDynamicSlotValue(result.dynamicSlots, Logic.SYM_LOGIC_JUSTIFICATION, solution.justification, null);
              }
              if (partialqueryP) {
                result.partialMatchFrame.positiveScore = solution.matchScore;
              }
            }
          }
        }
        else {
          { Object old$Queryiterator$000 = Logic.$QUERYITERATOR$.get();
            Object old$ReversepolarityP$000 = Logic.$REVERSEPOLARITYp$.get();
            Object old$Inferencelevel$000 = Logic.$INFERENCELEVEL$.get();
            Object old$GenerateAllProofsP$000 = Logic.$GENERATE_ALL_PROOFSp$.get();

            try {
              Native.setSpecial(Logic.$QUERYITERATOR$, subqueryiterator);
              Native.setBooleanSpecial(Logic.$REVERSEPOLARITYp$, frame.reversePolarityP);
              Native.setSpecial(Logic.$INFERENCELEVEL$, Logic.currentInferenceLevel());
              Native.setBooleanSpecial(Logic.$GENERATE_ALL_PROOFSp$, false);
              { int howmany = Logic.lookupHowManySolutions(subqueryiterator);

                if ((howmany == Stella.NULL_INTEGER) ||
                    (howmany >= 1)) {
                  successP = subqueryiterator.nextP();
                }
                if (howmany == Stella.NULL_INTEGER) {
                }
                else if (howmany <= 1) {
                  subqueryiterator.exhaustedP = true;
                }
                else {
                  subqueryiterator.options.insertAt(Logic.KWD_HOW_MANY, IntegerWrapper.wrapInteger(howmany - 1));
                }
                if (successP) {
                  result = subqueryiterator.baseControlFrame;
                }
              }

            } finally {
              Logic.$GENERATE_ALL_PROOFSp$.set(old$GenerateAllProofsP$000);
              Logic.$INFERENCELEVEL$.set(old$Inferencelevel$000);
              Logic.$REVERSEPOLARITYp$.set(old$ReversepolarityP$000);
              Logic.$QUERYITERATOR$.set(old$Queryiterator$000);
            }
          }
        }
        if (successP) {
          { Stella_Object var = null;
            Cons iter000 = frame.querySpecialistIoVariables().theConsList;
            Stella_Object value = null;
            Vector vector000 = ((QuerySolution)(subqueryiterator.value)).bindings;
            int index000 = 0;
            int length000 = vector000.length();

            loop001 : for (;(!(iter000 == Stella.NIL)) &&
                      (index000 < length000); iter000 = iter000.rest, index000 = index000 + 1) {
              var = iter000.value;
              value = (vector000.theArray)[index000];
              if (!(PatternVariable.bindVariableToValueP(((PatternVariable)(var)), value, true))) {
                successP = false;
                break loop001;
              }
            }
          }
          if ((!successP) &&
              (!truefalsequeryP)) {
            continue loop000;
          }
        }
        break loop000;
      }
      if ((Stella.$TRACED_KEYWORDS$ != null) &&
          Stella.$TRACED_KEYWORDS$.membP(Logic.KWD_GOAL_TREE)) {
        System.out.println();
      }
      if (successP) {
        ControlFrame.propagateFrameTruthValue(result, frame);
        if (((((QueryIterator)(Logic.$QUERYITERATOR$.get())) != null) &&
            (((QueryIterator)(Logic.$QUERYITERATOR$.get())).partialMatchStrategy != null)) &&
            partialqueryP) {
          result.partialMatchFrame.propagateFramePartialTruth(frame);
        }
        if (((Boolean)(Logic.$RECORD_JUSTIFICATIONSp$.get())).booleanValue()) {
          ControlFrame.recordGoalJustification(frame, ((Justification)(KeyValueList.dynamicSlotValue(result.dynamicSlots, Logic.SYM_LOGIC_JUSTIFICATION, null))));
        }
        if (truefalsequeryP) {
          return (Logic.KWD_FINAL_SUCCESS);
        }
        else {
          return (Logic.KWD_CONTINUING_SUCCESS);
        }
      }
      else {
        return (Logic.KWD_TERMINAL_FAILURE);
      }
    }
  }