Source for org.jfree.formula.Formula

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: Formula.java 3515 2007-10-16 09:22:24Z tmorgner $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula;
  32: 
  33: import java.io.Serializable;
  34: 
  35: import org.jfree.formula.lvalues.LValue;
  36: import org.jfree.formula.lvalues.TypeValuePair;
  37: import org.jfree.formula.parser.FormulaParser;
  38: import org.jfree.formula.parser.ParseException;
  39: import org.jfree.formula.parser.TokenMgrError;
  40: import org.jfree.formula.typing.Type;
  41: import org.jfree.formula.typing.coretypes.ErrorType;
  42: import org.jfree.util.Log;
  43: 
  44: /**
  45:  * Creation-Date: 31.10.2006, 14:43:05
  46:  *
  47:  * @author Thomas Morgner
  48:  */
  49: public class Formula implements Serializable, Cloneable
  50: {
  51:   private LValue rootReference;
  52: 
  53:   public Formula(final String formulaText) throws ParseException
  54:   {
  55:     try
  56:     {
  57:       final FormulaParser parser = new FormulaParser();
  58:       this.rootReference = parser.parse(formulaText);
  59:     }
  60:     catch(TokenMgrError tokenMgrError)
  61:     {
  62:       // This is ugly.
  63:       throw new ParseException(tokenMgrError.getMessage());
  64:     }
  65:   }
  66: 
  67:   public Formula(final LValue rootReference)
  68:   {
  69:     this.rootReference = rootReference;
  70:   }
  71: 
  72:   public void initialize (final FormulaContext context) throws EvaluationException
  73:   {
  74:     rootReference.initialize(context);
  75:   }
  76: 
  77:   /**
  78:    * Returns the root reference for this formula. This allows external programms
  79:    * to modify the formula directly.
  80:    *
  81:    * @return
  82:    */
  83:   public LValue getRootReference()
  84:   {
  85:     return rootReference;
  86:   }
  87: 
  88:   public TypeValuePair evaluateTyped ()
  89:   {
  90:     try
  91:     {
  92:       final TypeValuePair typeValuePair = rootReference.evaluate();
  93:       if (typeValuePair == null)
  94:       {
  95:         // Should no longer happen..
  96:         return new TypeValuePair
  97:             (ErrorType.TYPE, LibFormulaErrorValue.ERROR_NA_VALUE);
  98:       }
  99:       if(typeValuePair.getType().isFlagSet(Type.ERROR_TYPE))
 100:       {
 101:         Log.debug ("Error: " + typeValuePair.getValue());
 102:       }
 103:       return typeValuePair;
 104:     }
 105:     catch(EvaluationException ee)
 106:     {
 107:       return new TypeValuePair(ErrorType.TYPE, ee.getErrorValue());
 108:     }
 109:     catch (Exception e)
 110:     {
 111:       Log.warn ("Evaluation failed unexpectedly: ", e);
 112:       return new TypeValuePair(ErrorType.TYPE, LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE);
 113:     }
 114:   }
 115:   
 116:   public Object evaluate ()
 117:   {
 118:     return evaluateTyped().getValue();
 119:   }
 120: 
 121:   public Object clone () throws CloneNotSupportedException
 122:   {
 123:     final Formula o = (Formula) super.clone();
 124:     o.rootReference = (LValue) rootReference.clone();
 125:     return o;
 126:   }
 127: }