Exemplo n.º 1
0
  // G.Sturr 2010-7-5
  // added 'allowErrorDialog' flag to handle the case of unquoted text
  // entries in the spreadsheet
  public GeoElement[] processAlgebraCommandNoExceptionHandling(
      String cmd, boolean storeUndo, boolean allowErrorDialog, boolean throwMyError)
      throws Exception {
    ValidExpression ve;
    try {
      ve = parser.parseGeoGebraExpression(cmd);
    } catch (ParseException e) {
      // e.printStackTrace();
      if (allowErrorDialog) {
        app.showError(app.getError("InvalidInput") + ":\n" + cmd);
        return null;
      }
      throw new MyException(app.getError("InvalidInput") + ":\n" + cmd, MyException.INVALID_INPUT);
    } catch (MyError e) {
      // e.printStackTrace();
      if (allowErrorDialog) {
        app.showError(e.getLocalizedMessage());
        return null;
      }
      throw new Exception(e.getLocalizedMessage());
    } catch (Error e) {
      // e.printStackTrace();
      if (allowErrorDialog) {
        app.showError(app.getError("InvalidInput") + ":\n" + cmd);
        return null;
      }
      throw new Exception(app.getError("InvalidInput") + ":\n" + cmd);
    }

    // process ValidExpression (built by parser)
    GeoElement[] geoElements = null;
    try {
      geoElements = processValidExpression(ve);
      if (storeUndo && geoElements != null) app.storeUndoInfo();
    } catch (MyError e) {
      e.printStackTrace();
      // throw new Exception(e.getLocalizedMessage());

      // show error with nice "Show Online Help" box
      if (allowErrorDialog) { // G.Sturr 2010-7-5
        app.showError(e);
        e.printStackTrace();
      } else if (throwMyError) throw new MyError(app, e.getLocalizedMessage(), e.getcommandName());

      return null;
    } catch (CircularDefinitionException e) {
      Application.debug("CircularDefinition");
      throw e;
    } catch (Exception ex) {
      Application.debug("Exception");
      ex.printStackTrace();
      throw new Exception(app.getError("Error") + ":\n" + ex.getLocalizedMessage());
    }
    return geoElements;
  }
Exemplo n.º 2
0
  /**
   * Parses given String str and tries to evaluate it to a GeoImplicitPoly object. Returns null if
   * something went wrong.
   *
   * @param str
   * @boolean showErrors if false, only stacktraces are printed
   * @return implicit polygon or null
   */
  public GeoElement evaluateToGeoElement(String str, boolean showErrors) {
    boolean oldMacroMode = cons.isSuppressLabelsActive();
    cons.setSuppressLabelCreation(true);

    GeoElement geo = null;
    try {
      ValidExpression ve = parser.parseGeoGebraExpression(str);
      GeoElement[] temp = processValidExpression(ve);
      geo = temp[0];
    } catch (CircularDefinitionException e) {
      Application.debug("CircularDefinition");
      app.showError("CircularDefinition");
    } catch (Exception e) {
      e.printStackTrace();
      if (showErrors) app.showError("InvalidInput", str);
    } catch (MyError e) {
      e.printStackTrace();
      if (showErrors) app.showError(e);
    } catch (Error e) {
      e.printStackTrace();
      if (showErrors) app.showError("InvalidInput", str);
    }

    cons.setSuppressLabelCreation(oldMacroMode);
    return geo;
  }
Exemplo n.º 3
0
  /**
   * Parses given String str and tries to evaluate it to a GeoFunction Returns null if something
   * went wrong. Michael Borcherds 2008-04-04
   */
  public GeoFunction evaluateToFunction(String str, boolean suppressErrors) {
    boolean oldMacroMode = cons.isSuppressLabelsActive();
    cons.setSuppressLabelCreation(true);

    GeoFunction func = null;
    try {
      ValidExpression ve = parser.parseGeoGebraExpression(str);
      GeoElement[] temp = processValidExpression(ve);

      if (temp[0].isGeoFunctionable()) {
        GeoFunctionable f = (GeoFunctionable) temp[0];
        func = f.getGeoFunction();
      } else if (!suppressErrors) app.showError("InvalidInput", str);

    } catch (CircularDefinitionException e) {
      Application.debug("CircularDefinition");
      if (!suppressErrors) app.showError("CircularDefinition");
    } catch (Exception e) {
      e.printStackTrace();
      if (!suppressErrors) app.showError("InvalidInput", str);
    } catch (MyError e) {
      e.printStackTrace();
      if (!suppressErrors) app.showError(e);
    } catch (Error e) {
      e.printStackTrace();
      if (!suppressErrors) app.showError("InvalidInput", str);
    }

    cons.setSuppressLabelCreation(oldMacroMode);
    return func;
  }
Exemplo n.º 4
0
  @Override
  public GeoElement[] process(Command c) throws MyError {
    int n = c.getArgumentNumber();

    GeoElement[] arg;
    arg = resArgs(c);

    switch (n) {
      case 1:
        if (arg[0].isGeoFunctionable()) {

          GeoNumeric var = new GeoNumeric(cons, 0.0);

          arg[0].setEuclidianVisible(false);
          arg[0].update();

          var.setLabel(null); // set label to next available
          var.setEuclidianVisible(true);
          var.setIntervalMin(0.0);
          var.setIntervalMax(1.0);
          var.setAnimating(true);
          var.setAnimationStep(0.01);
          var.setAnimationType(GeoElement.ANIMATION_INCREASING);
          var.update();
          StringTemplate tpl = StringTemplate.maxPrecision;
          StringBuilder sb = new StringBuilder();
          sb.append("Function[");
          sb.append(arg[0].getLabel(tpl));
          sb.append(",x(Corner[1]), x(Corner[1]) (1-");
          sb.append(var.getLabel(tpl));
          sb.append(") + x(Corner(2)) ");
          sb.append(var.getLabel(tpl));
          sb.append("]");

          kernelA.getAnimatonManager().startAnimation();
          try {
            return kernelA
                .getAlgebraProcessor()
                .processAlgebraCommandNoExceptionHandling(sb.toString(), true, false, true);
          } catch (Exception e) {
            e.printStackTrace();
            throw argErr(app, c.getName(), arg[0]);
          } catch (MyError e) {
            e.printStackTrace();
            throw argErr(app, c.getName(), arg[0]);
          }
        }
        throw argErr(app, c.getName(), arg[0]);

      default:
        throw argNumErr(app, c.getName(), n);
    }
  }
Exemplo n.º 5
0
  /**
   * for AlgebraView changes in the tree selection and redefine dialog
   *
   * @return changed geo
   */
  public GeoElement changeGeoElementNoExceptionHandling(
      GeoElement geo, ValidExpression newValue, boolean redefineIndependent, boolean storeUndoInfo)
      throws Exception {
    String oldLabel, newLabel;
    GeoElement[] result;

    try {
      oldLabel = geo.getLabel();
      newLabel = newValue.getLabel();

      if (newLabel == null) {
        newLabel = oldLabel;
        newValue.setLabel(newLabel);
      }

      // make sure that points stay points and vectors stay vectors
      if (newValue instanceof ExpressionNode) {
        ExpressionNode n = (ExpressionNode) newValue;
        if (geo.isGeoPoint()) n.setForcePoint();
        else if (geo.isGeoVector()) n.setForceVector();
        else if (geo.isGeoFunction()) n.setForceFunction();
      }

      if (newLabel.equals(oldLabel)) {
        // try to overwrite
        result = processValidExpression(newValue, redefineIndependent);
        if (result != null && storeUndoInfo) app.storeUndoInfo();
        return result[0];
      } else if (cons.isFreeLabel(newLabel)) {
        newValue.setLabel(oldLabel);
        // rename to oldLabel to enable overwriting
        result = processValidExpression(newValue, redefineIndependent);
        result[0].setLabel(newLabel); // now we rename
        if (storeUndoInfo) app.storeUndoInfo();
        return result[0];
      } else {
        String str[] = {"NameUsed", newLabel};
        throw new MyError(app, str);
      }
    } catch (CircularDefinitionException e) {
      Application.debug("CircularDefinition");
      throw e;
    } catch (Exception e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    } catch (MyError e) {
      e.printStackTrace();
      throw new Exception(e.getLocalizedMessage());
    } catch (Error e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    }
  }
Exemplo n.º 6
0
  /**
   * for AlgebraView changes in the tree selection and redefine dialog
   *
   * @return changed geo
   */
  public GeoElement changeGeoElementNoExceptionHandling(
      GeoElement geo, String newValue, boolean redefineIndependent, boolean storeUndoInfo)
      throws Exception {

    try {
      ValidExpression ve = parser.parseGeoGebraExpression(newValue);
      return changeGeoElementNoExceptionHandling(geo, ve, redefineIndependent, storeUndoInfo);
    } catch (Exception e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    } catch (MyError e) {
      e.printStackTrace();
      throw new Exception(e.getLocalizedMessage());
    } catch (Error e) {
      e.printStackTrace();
      throw new Exception(app.getError("InvalidInput") + ":\n" + newValue);
    }
  }
Exemplo n.º 7
0
  /**
   * Parses given String str and tries to evaluate it to a GeoPoint. Returns null if something went
   * wrong.
   */
  public GeoPointND evaluateToPoint(String str, boolean showErrors) {
    boolean oldMacroMode = cons.isSuppressLabelsActive();
    cons.setSuppressLabelCreation(true);

    GeoPointND p = null;
    GeoElement[] temp = null;
    ;
    try {
      ValidExpression ve = parser.parseGeoGebraExpression(str);
      if (ve instanceof ExpressionNode) {
        ExpressionNode en = (ExpressionNode) ve;
        en.setForcePoint();
      }

      temp = processValidExpression(ve);
      p = (GeoPointND) temp[0];
    } catch (CircularDefinitionException e) {
      if (showErrors) {
        Application.debug("CircularDefinition");
        app.showError("CircularDefinition");
      }
    } catch (Exception e) {
      if (showErrors) {
        e.printStackTrace();
        app.showError("InvalidInput", str);
      }
    } catch (MyError e) {
      if (showErrors) {
        e.printStackTrace();
        app.showError(e);
      }
    } catch (Error e) {
      if (showErrors) {
        e.printStackTrace();
        app.showError("InvalidInput", str);
      }
    }

    cons.setSuppressLabelCreation(oldMacroMode);
    return p;
  }
Exemplo n.º 8
0
 /**
  * Parses given String str and tries to evaluate it to a double. Returns Double.NaN if something
  * went wrong.
  */
 public double evaluateToDouble(String str, boolean suppressErrors) {
   try {
     ValidExpression ve = parser.parseExpression(str);
     ExpressionNode en = (ExpressionNode) ve;
     en.resolveVariables();
     NumberValue nv = (NumberValue) en.evaluate();
     return nv.getDouble();
   } catch (Exception e) {
     e.printStackTrace();
     if (!suppressErrors) app.showError("InvalidInput", str);
     return Double.NaN;
   } catch (MyError e) {
     e.printStackTrace();
     if (!suppressErrors) app.showError(e);
     return Double.NaN;
   } catch (Error e) {
     e.printStackTrace();
     if (!suppressErrors) app.showError("InvalidInput", str);
     return Double.NaN;
   }
 }
Exemplo n.º 9
0
  public GeoElement[] processEquation(Equation equ) throws MyError {
    //		Application.debug("EQUATION: " + equ);
    //		Application.debug("NORMALFORM POLYNOMIAL: " + equ.getNormalForm());

    try {
      equ.initEquation();
      // Application.debug("EQUATION: " + equ.getNormalForm());
      // check no terms in z
      checkNoTermsInZ(equ);

      if (equ.isFunctionDependent()) {
        return processImplicitPoly(equ);
      }

      // consider algebraic degree of equation
      // check not equation of eg plane
      switch (equ.degree()) {
          // linear equation -> LINE
        case 1:
          return processLine(equ, false);

          // quadratic equation -> CONIC
        case 2:
          return processConic(equ);

        default:
          // test for "y= <rhs>" here as well
          if (equ.getLHS().toString().trim().equals("y")) {
            Function fun = new Function(equ.getRHS());
            // try to use label of equation
            fun.setLabel(equ.getLabel());
            return processFunction(null, fun);
          }
          return processImplicitPoly(equ);
      }
    } catch (MyError eqnError) {
      eqnError.printStackTrace();

      // invalid equation: maybe a function of form "y = <rhs>"?
      String lhsStr = equ.getLHS().toString().trim();
      if (lhsStr.equals("y")) {
        try {
          // try to create function from right hand side
          Function fun = new Function(equ.getRHS());

          // try to use label of equation
          fun.setLabel(equ.getLabel());
          return processFunction(null, fun);
        } catch (MyError funError) {
          funError.printStackTrace();
        }
      }

      // throw invalid equation error if we get here
      if (eqnError.getMessage() == "InvalidEquation") throw eqnError;
      else {
        String[] errors = {"InvalidEquation", eqnError.getLocalizedMessage()};
        throw new MyError(app, errors);
      }
    }
  }
Exemplo n.º 10
0
  /**
   * processes valid expression.
   *
   * @param ve
   * @param redefineIndependent == true: independent objects are redefined too
   * @throws MyError
   * @throws Exception
   * @return
   */
  public GeoElement[] processValidExpression(ValidExpression ve, boolean redefineIndependent)
      throws MyError, Exception {

    // check for existing labels
    String[] labels = ve.getLabels();
    GeoElement replaceable = null;
    if (labels != null && labels.length > 0) {
      boolean firstTime = true;
      for (int i = 0; i < labels.length; i++) {
        GeoElement geo = kernel.lookupLabel(labels[i]);
        if (geo != null) {
          if (geo.isFixed()) {
            String[] strs = {
              "IllegalAssignment", "AssignmentToFixed", ":\n", geo.getLongDescription()
            };
            throw new MyError(app, strs);
          } else {
            // replace (overwrite or redefine) geo
            if (firstTime) { // only one geo can be replaced
              replaceable = geo;
              firstTime = false;
            }
          }
        }
      }
    }

    GeoElement[] ret;
    boolean oldMacroMode = cons.isSuppressLabelsActive();
    if (replaceable != null) cons.setSuppressLabelCreation(true);

    // we have to make sure that the macro mode is
    // set back at the end
    try {
      ret = doProcessValidExpression(ve);

      if (ret == null) { // eg (1,2,3) running in 2D
        Application.debug("Unhandled ValidExpression : " + ve);
        throw new MyError(app, app.getError("InvalidInput") + ":\n" + ve);
      }
    } finally {
      cons.setSuppressLabelCreation(oldMacroMode);
    }

    //	try to replace replaceable geo by ret[0]
    if (replaceable != null && ret != null && ret.length > 0) {
      // a changeable replaceable is not redefined:
      // it gets the value of ret[0]
      // (note: texts are always redefined)
      if (!redefineIndependent && replaceable.isChangeable() && !(replaceable.isGeoText())) {
        try {
          replaceable.set(ret[0]);
          replaceable.updateRepaint();
          ret[0] = replaceable;
        } catch (Exception e) {
          String errStr =
              app.getError("IllegalAssignment")
                  + "\n"
                  + replaceable.getLongDescription()
                  + "     =     "
                  + ret[0].getLongDescription();
          throw new MyError(app, errStr);
        }
      }
      // redefine
      else {
        try {
          // SPECIAL CASE: set value
          // new and old object are both independent and have same type:
          // simply assign value and don't redefine
          if (replaceable.isIndependent()
              && ret[0].isIndependent()
              && replaceable.getGeoClassType() == ret[0].getGeoClassType()) {
            replaceable.set(ret[0]);
            replaceable.updateRepaint();
            ret[0] = replaceable;
          }

          // STANDARD CASE: REDFINED
          else {
            GeoElement newGeo = ret[0];
            cons.replace(replaceable, newGeo);

            // now all objects have changed
            // get the new object with same label as our result
            String newLabel = newGeo.isLabelSet() ? newGeo.getLabel() : replaceable.getLabel();
            ret[0] = kernel.lookupLabel(newLabel, false);
          }
        } catch (CircularDefinitionException e) {
          throw e;
        } catch (Exception e) {
          e.printStackTrace();
          throw new MyError(app, "ReplaceFailed");
        } catch (MyError e) {
          e.printStackTrace();
          throw new MyError(app, "ReplaceFailed");
        }
      }
    }

    return ret;
  }