📄 evaluatorjep.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.exact.ld.eval;
import jmt.common.exception.ExpressionParseException;
import org.cheffo.jeplite.JEP;
import org.cheffo.jeplite.ParseException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author alyf (Andrea Conti)
* Date: 14-set-2003
* Time: 16.26.42
*/
/**
* an Evaluator using JEP by Nathan Funk
* <a href="http://www.singularsys.com/jep">http://www.singularsys.com/jep</a>.
*/
public final class EvaluatorJep implements Evaluator {
private JEP parser;
private static final String X = "n";
private String helpString;
public EvaluatorJep() {
parser = new JEP();
parser.addStandardConstants();
parser.addStandardFunctions();
//custom functions can be added here with JEP.addFunction()
}
/**
* Creates the help string, retrieving it from a html file.
*/
private void loadHelp() {
try {
/*
//OLD
String helpFile = EvaluatorJep.class.getResource("EvaluatorJep.html").getPath();
FileReader fr = new FileReader(new File(helpFile));
char[] buf = new char[1024];
StringBuffer s = new StringBuffer();
int read;
read = fr.read(buf);
while (read > 0) {
s.append(buf, 0, read);
read = fr.read(buf);
}
fr.close();
helpString = s.toString();
*/
//String helpFile = EvaluatorJep.class.getResource("EvaluatorJep.html").getPath();
URL url = EvaluatorJep.class.getResource("EvaluatorJep.html");
InputStream inputStream = url.openStream();
//FileReader fr = new FileReader(new File(helpFile));
InputStreamReader fr = new InputStreamReader(inputStream);
char[] buf = new char[1024];
StringBuffer s = new StringBuffer();
int read;
read = fr.read(buf);
while (read > 0) {
s.append(buf, 0, read);
read = fr.read(buf);
}
fr.close();
helpString = s.toString();
} catch (MalformedURLException e) {
helpString = "JEP home page is at http://www.singularsys.com/jep";
} catch (IOException e) {
helpString = "JEP home page is at http://www.singularsys.com/jep";
}
}
public double evaluate(String expression, double x) throws ExpressionParseException {
parser.addVariable(X, x);
parser.parseExpression(expression);
if (parser.hasError()) {
throw new ExpressionParseException(parser.getErrorInfo());
}
double val = 0;
try {
val = parser.getValue();
} catch (ParseException e) {
throw new ExpressionParseException(e.getMessage());
}
return val;
}
public double[] evaluate(String expression, double[] x) throws ExpressionParseException {
double[] y = new double[x.length];
evaluate(expression, x, y);
return y;
}
public void evaluate(String expression, double[] x, double[] y) throws ExpressionParseException {
if (x.length != y.length) throw new IllegalArgumentException("x and y must be the same length");
for (int i = 0; i < x.length; i++) {
y[i] = evaluate(expression, x[i]);
}
}
//NEW
//@author Stefano Omini
public String getHelpText() {
//load help from html
loadHelp();
return helpString;
}
//end NEW
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -