001 /** 002 * JEP - Java Expression Parser JEP is a Java package for parsing and evaluating mathematical expressions. It currently supports user defined 003 * variables, constant, and functions. A number of common mathematical functions and constants are included. Author: Nathan Funk Copyright (C) 004 * 2000 Nathan Funk JEP is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as 005 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. JEP is distributed in the 006 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 007 * PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with 008 * JEP; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 009 */ 010 011 package jigcell.sbml2.jep.function; 012 013 import java.util.Stack; 014 import jigcell.sbml2.FunctionDefinition; 015 016 /** 017 * Function classes extend this class. It is an implementation of the PostfixMathCommandI interface. 018 * 019 * <p> 020 * It includes a numberOfParameters member, that is checked when parsing the expression. This member should be initialized to an appropriate 021 * value for all classes extending this class. If an arbitrary number of parameters should be allowed, initialize this member to -1. 022 * </p> 023 */ 024 025 public class PostfixMathCommand extends FunctionDefinition implements PostfixMathCommandI { 026 027 /** 028 * Number of parameters to be used for the next run() invocation. Applies only if the required umber of parameters is variable 029 * (numberOfParameters = -1). 030 */ 031 032 protected int curNumberOfParameters; 033 034 /** 035 * Number of parameters a the function requires. Initialize this value to -1 if any number of parameters should be allowed. 036 */ 037 038 protected int numberOfParameters; 039 040 /** 041 * Creates a new PostfixMathCommand class. 042 */ 043 044 public PostfixMathCommand (int numParameters) { 045 super (); 046 this.numberOfParameters = numParameters; 047 curNumberOfParameters = numParameters; 048 } 049 050 /** 051 * Return the required number of parameters. 052 */ 053 054 public int getNumberOfParameters () { 055 return numberOfParameters; 056 } 057 058 /** 059 * Throws an exception because this method should never be called under normal circumstances. Each function should use it's own run() method 060 * for evaluating the function. This includes popping off the parameters from the stack, and pushing the result back on the stack. 061 */ 062 063 public void run (Stack s) throws Exception { 064 throw new Exception ("run() method of PostfixMathCommand called"); 065 } 066 067 /** 068 * Sets the number of current number of parameters used in the next call of run(). This method is only called when the reqNumberOfParameters 069 * is -1. 070 */ 071 072 public void setCurNumberOfParameters (int n) { 073 curNumberOfParameters = n; 074 } 075 076 /** 077 * Convenience method for use with the parser to set the parameters if they have changed. 078 */ 079 080 public void setNumberOfParameters (int n) { 081 numberOfParameters = n; 082 curNumberOfParameters = n; 083 } 084 085 /** 086 * Check whether the stack is not null, throw an Exception if it is. 087 */ 088 089 protected void checkStack (Stack inStack) throws Exception { 090 091 /* Check if stack is null */ 092 if (null == inStack) 093 throw new Exception ("Stack argument null"); 094 } 095 }