//
// FormulaManager.java
//
/*
VisAD system for interactive analysis and visualization of numerical
data. Copyright (C) 1996 - 2017 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
package visad.formula;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.util.Vector;
import visad.*;
/** The FormulaManager class is the gateway into the visad.formula package,
a general-purpose formula parser and evaluator. Variables update
automatically when the variables upon which they depend change.
For an example of usage, see the FormulaUtil.createStandardManager()
method. */
public class FormulaManager {
/** binary operators */
String[] bOps;
/** binary operator precedences */
int[] bPrec;
/** binary method signatures */
Method[] bMethods;
/** unary operators */
String[] uOps;
/** unary operator precedences */
int[] uPrec;
/** unary method signatures */
Method[] uMethods;
/** function names */
String[] funcs;
/** function method signatures */
Method[] fMethods;
/** implicit function precedence */
int iPrec;
/** implicit function methods */
Method[] iMethods;
/** formula text pre-parsing method. For an application to support unusual
non-standard syntaxes, such as brackets (e.g., VAR[0]), that application
should supply a "pre-parsing" method in the preParseMethod
argument, which converts non-standard syntax to standard syntax (e.g.,
VAR[0] could become getElement(VAR, 0)). The supplied method should be
a static method taking a String argument (the formula) and a
FormulaManager argument (this FormulaManager). If the application does
not need such functionality, the preParseMethod argument can be null. */
Method ppMethod;
/** construct a new FormulaManager object */
public FormulaManager(String[] binOps, int[] binPrec, String[] binMethods,
String[] unaryOps, int[] unaryPrec, String[] unaryMethods,
String[] functions, String[] funcMethods, int implicitPrec,
String[] implicitMethods, String preParseMethod) throws FormulaException
{
bOps = binOps;
bPrec = binPrec;
bMethods = FormulaUtil.stringsToMethods(binMethods);
uOps = unaryOps;
uPrec = unaryPrec;
uMethods = FormulaUtil.stringsToMethods(unaryMethods);
funcs = functions;
fMethods = FormulaUtil.stringsToMethods(funcMethods);
iPrec = implicitPrec;
iMethods = FormulaUtil.stringsToMethods(implicitMethods);
String[] s = new String[1];
if (preParseMethod == null) ppMethod = null;
else {
String[] pps = new String[] {preParseMethod};
Method[] ppm = FormulaUtil.stringsToMethods(pps);
ppMethod = ppm[0];
}
// check that parallel arrays are really parallel
int l1 = bOps.length;
int l2 = bPrec.length;
int l3 = bMethods.length;
if (l1 != l2 || l1 != l3) {
throw new FormulaException("Binary arrays must have equal lengths");
}
l1 = uOps.length;
l2 = uPrec.length;
l3 = uMethods.length;
if (l1 != l2 || l1 != l3) {
throw new FormulaException("Unary arrays must have equal lengths");
}
l1 = funcs.length;
l2 = fMethods.length;
if (l1 != l2) {
throw new FormulaException("Function arrays must have equal lengths");
}
// check that all operators are one character in length, all operators are
// legal, and all duplicate operators have equal operator precedences
for (int i=0; i 1) {
throw new FormulaException("All operators must be one character " +
"in length");
}
char c = bOps[i].charAt(0);
if (c == '(' || c == ')' || c == ',' || (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z')) {
throw new FormulaException("The character \"" + c + "\" cannot be " +
"used as an operator");
}
for (int j=i+1; j 1) {
throw new FormulaException("All operators must be one character " +
"in length");
}
char c = uOps[i].charAt(0);
if (c == '(' || c == ')' || c == ',' || (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z')) {
throw new FormulaException("The character \"" + c + "\" cannot be " +
"used as an operator");
}
for (int j=i+1; j 'Z') && (c < 'a' || c > 'z')) {
throw new FormulaException("All functions must begin with a letter");
}
}
// check that all methods are legal
for (int i=0; i
Ads help maintain this website.