Ejemplo n.º 1
0
  private GeoElement[] processList(ExpressionNode n, MyList evalList) {
    String label = n.getLabel();

    GeoElement[] ret = new GeoElement[1];

    // no operations or no variables are present, e.g.
    // { a, b, 7 } or  { 2, 3, 5 } + {1, 2, 4}
    if (!n.hasOperations() || n.isConstant()) {

      // PROCESS list items to generate a list of geoElements
      ArrayList<GeoElement> geoElements = new ArrayList<GeoElement>();
      boolean isIndependent = true;

      // make sure we don't create any labels for the list elements
      boolean oldMacroMode = cons.isSuppressLabelsActive();
      cons.setSuppressLabelCreation(true);

      int size = evalList.size();
      for (int i = 0; i < size; i++) {
        ExpressionNode en = (ExpressionNode) evalList.getListElement(i);
        // we only take one resulting object
        GeoElement[] results = processExpressionNode(en);
        GeoElement geo = results[0];

        // add to list
        geoElements.add(geo);
        if (geo.isLabelSet() || !geo.isIndependent()) isIndependent = false;
      }
      cons.setSuppressLabelCreation(oldMacroMode);

      // Create GeoList object
      ret[0] = kernel.List(label, geoElements, isIndependent);
    }

    // operations and variables are present
    // e.g. {3, 2, 1} + {a, b, 2}
    else {
      ret[0] = kernel.ListExpression(label, n);
    }

    return ret;
  }
  // evaluate the current value of the arithmetic tree
  protected final void compute() {
    // get resulting list of ExpressionNodes
    ExpressionValue evlist = root.evaluate();
    MyList myList = (evlist instanceof MyList) ? (MyList) evlist : ((GeoList) evlist).getMyList();

    int evalListSize = myList.size();
    int cachedListSize = list.getCacheSize();

    list.clear();
    for (int i = 0; i < evalListSize; i++) {
      ExpressionValue element = myList.getListElement(i).evaluate();
      GeoElement geo = null;

      // number result
      if (element.isNumberValue()) {
        double val = ((NumberValue) element).getDouble();

        // try to use cached element of same type
        if (i < cachedListSize) {
          GeoElement cachedGeo = list.getCached(i);

          // the cached element is a number: set value
          if (cachedGeo.isGeoNumeric()) {
            ((GeoNumeric) cachedGeo).setValue(val);
            geo = cachedGeo;
          }
        }

        // no cached number: create new one
        if (geo == null) {
          geo = new GeoNumeric(cons, val);
        }

        // add number to list
        list.add(geo);
      }

      // point
      else if (element.isVectorValue()) {
        GeoVec2D vec = ((VectorValue) element).getVector();

        // try to use cached element of same type
        if (i < cachedListSize) {
          GeoElement cachedGeo = list.getCached(i);

          // the cached element is a point: set value
          if (cachedGeo.isGeoPoint()) {
            ((GeoPoint) cachedGeo).setCoords(vec);
            geo = cachedGeo;
          }
        }

        // no cached point: create new one
        if (geo == null) {
          GeoPoint point = new GeoPoint(cons);
          point.setCoords(vec);
          geo = point;
        }

        // add point to list
        list.add(geo);
      }

      // needed for matrix multiplication
      // eg {{1,3,5},{2,4,6}}*{{11,14},{12,15},{13,a}}
      else if (element instanceof MyList) {
        MyList myList2 = (MyList) element;
        GeoList list2 = new GeoList(cons);
        list2.clear();

        /* removed Michael Borcherds 20080602
         * bug: 9PointCubic.ggb (matrix multiplication)
        // try to use cached element of  type GeoList
        GeoList list2 = null;
        if (i < cachedListSize) {
        	GeoElement cachedGeo = list.getCached(i);

        	// the cached element is a number: set value
        	if (cachedGeo.isGeoList()) {
        		list2 = (GeoList) cachedGeo;
        	}
        }

        if (list2 == null) {
        	list2 = new GeoList(cons);
        } */

        for (int j = 0; j < myList2.size(); j++) {
          ExpressionValue en = myList2.getListElement(j);
          ExpressionValue ev = en.evaluate();

          if (ev instanceof MyDouble) {
            GeoNumeric geo2 = new GeoNumeric(cons);
            geo2.setValue(((NumberValue) ev).getDouble());
            list2.add(geo2);
          }
        }

        list.add(list2);
      } else if (element instanceof MyStringBuffer) {
        MyStringBuffer str = (MyStringBuffer) element;
        // try to use cached element of same type
        if (i < cachedListSize) {
          GeoElement cachedGeo = list.getCached(i);

          // the cached element is a point: set value
          if (cachedGeo.isGeoText()) {
            ((GeoText) cachedGeo).setTextString(str.toValueString());
            geo = cachedGeo;
          }
        }

        // no cached point: create new one
        if (geo == null) {
          GeoText text = new GeoText(cons);
          text.setTextString(str.toValueString());
          geo = text;
        }

        // add point to list
        list.add(geo);
      } else if (element instanceof MyBoolean) {
        MyBoolean bool = (MyBoolean) element;
        // try to use cached element of same type
        if (i < cachedListSize) {
          GeoElement cachedGeo = list.getCached(i);

          // the cached element is a point: set value
          if (cachedGeo.isGeoBoolean()) {
            ((GeoBoolean) cachedGeo).setValue(bool.getBoolean());
            geo = cachedGeo;
          }
        }

        // no cached point: create new one
        if (geo == null) {
          GeoBoolean geoBool = new GeoBoolean(cons);
          geoBool.setValue(bool.getBoolean());
          geo = geoBool;
        }

        // add point to list
        list.add(geo);
      } else if (element instanceof GeoFunction) {
        GeoFunction fun = (GeoFunction) element;
        if (i < cachedListSize) {
          GeoElement cachedGeo = list.getCached(i);

          // the cached element is a point: set value
          if (cachedGeo.isGeoFunction()) {
            ((GeoFunction) cachedGeo).set(fun);
            geo = cachedGeo;
          }
        }

        // no cached point: create new one
        if (geo == null) {
          GeoFunction geoFun = new GeoFunction(cons);
          geoFun.set(fun);
          geo = geoFun;
        }
        list.add(geo);

      } else {
        Application.debug("unsupported list addition: " + element.getClass() + "");
      }
    }
  }