public static Stella_Object groundValueOf(Stella_Object self) {
    { Surrogate testValue000 = Stella_Object.safePrimaryType(self);

      if (Surrogate.subtypeOfP(testValue000, Logic.SGT_LOGIC_PATTERN_VARIABLE)) {
        { PatternVariable self000 = ((PatternVariable)(self));

          { Stella_Object value = null;

            if ((self000.boundToOffset != Stella.NULL_INTEGER) &&
                ((Boolean)(Logic.$COMPUTEDQUERYp$.get())).booleanValue()) {
              value = (((QueryIterator)(Logic.$QUERYITERATOR$.get())).currentPatternRecord.variableBindings.theArray)[(self000.boundToOffset)];
            }
            if (value == null) {
              value = ((Stella_Object)(Stella_Object.accessInContext(self000.variableValue, self000.homeContext, false)));
            }
            if (value != null) {
              return (Logic.groundValueOf(value));
            }
            else {
              return (null);
            }
          }
        }
      }
      else if (Surrogate.subtypeOfP(testValue000, Logic.SGT_LOGIC_SKOLEM)) {
        { Skolem self000 = ((Skolem)(self));

          { Stella_Object value = ((Stella_Object)(Stella_Object.accessInContext(self000.variableValue, self000.homeContext, false)));

            if (value != null) {
              return (Logic.groundValueOf(value));
            }
            else {
              return (null);
            }
          }
        }
      }
      else if (Surrogate.subtypeOfSurrogateP(testValue000)) {
        { Surrogate self000 = ((Surrogate)(self));

          { Stella_Object value = self000.surrogateValue;

            if (value != null) {
              return (Logic.groundValueOf(value));
            }
            else {
              return (null);
            }
          }
        }
      }
      else if (Surrogate.subtypeOfP(testValue000, Logic.SGT_STELLA_RELATION)) {
        { Relation self000 = ((Relation)(self));

          return (((NamedDescription)(KeyValueList.dynamicSlotValue(self000.dynamicSlots, Logic.SYM_LOGIC_DESCRIPTION, null))));
        }
      }
      else {
        return (self);
      }
    }
  }