public static int computeMaximumCardinality(NamedDescription relation, Stella_Object instance) {
    { Object old$ReversepolarityP$000 = Logic.$REVERSEPOLARITYp$.get();

      try {
        Native.setBooleanSpecial(Logic.$REVERSEPOLARITYp$, false);
        { Surrogate relationref = relation.surrogateValueInverse;
          boolean singlevaluedP = Logic.singleValuedTermP(relation);

          if (singlevaluedP &&
              (Logic.accessBinaryValue(instance, relationref) != null)) {
            return (1);
          }
          { Cons maxcards = Cons.consList(Cons.cons(IntegerWrapper.wrapInteger(NamedDescription.computeStoredBoundOnRoleset(relation, instance, Logic.KWD_UPPER)), Stella.NIL));

            if (singlevaluedP) {
              maxcards = Cons.cons(IntegerWrapper.wrapInteger(1), maxcards);
            }
            { Skolem roleset = NamedDescription.getRolesetOf(relation, instance);

              if ((roleset != null) &&
                  Logic.emptyTermP(roleset)) {
                return (0);
              }
            }
            if (Logic.closedTermP(relation)) {
              maxcards = Cons.cons(IntegerWrapper.wrapInteger(NamedDescription.computeMinimumCardinality(relation, instance)), maxcards);
            }
            { NamedDescription superr = null;
              Cons iter000 = NamedDescription.allSuperrelations(relation, false);

              for (;!(iter000 == Stella.NIL); iter000 = iter000.rest) {
                superr = ((NamedDescription)(iter000.value));
                maxcards = Cons.cons(IntegerWrapper.wrapInteger(NamedDescription.computeStoredBoundOnRoleset(superr, instance, Logic.KWD_UPPER)), maxcards);
              }
            }
            { int minmax = ((IntegerWrapper)(maxcards.value)).wrapperValue;

              { IntegerWrapper ub = null;
                Cons iter001 = maxcards.rest;

                for (;!(iter001 == Stella.NIL); iter001 = iter001.rest) {
                  ub = ((IntegerWrapper)(iter001.value));
                  if (ub != null) {
                    minmax = Stella.integer_min(minmax, ub.wrapperValue);
                  }
                }
              }
              return (minmax);
            }
          }
        }

      } finally {
        Logic.$REVERSEPOLARITYp$.set(old$ReversepolarityP$000);
      }
    }
  }