如何基于javasci v2
编写java应用程序,下面就一步一步来实现
1 编码
javasci v2
提供了java
接口来操作scilab
引擎
javasci v2
提供了响应的对象和方法来执行对应的操作,主要的类是Scilab
,这个对象支持:
- 从scialb引擎 发送/接收数据
- error管理
- 执行scialb脚本
javasci v2
的类只要集中在两个包中:
org.scilab.modules.javasci.*; // Scilab 类所在的包
org.scilab.modules.types.*; // 包含 Scilab <=> Java 数据类型映射
下面是一个典型的例子
/*
*
* This file is released under the 3-clause BSD license. See COPYING-BSD.
*
*/
import org.scilab.modules.javasci.Scilab;
import org.scilab.modules.types.ScilabType;
import org.scilab.modules.types.ScilabDouble;
class Example1 {
public static void main(String[] args) {
try {
Scilab sci = new Scilab();
if (sci.open()) {
/* Send a Scilab instruction */
sci.exec("foo = [ 2, 4, 6; 4, 0, 10; 6, 10, 12 ];");
/* Retrieve the variable foo */
ScilabType foo = sci.get("foo");
/* Display the variable */
System.out.println("Representation of : "+foo);
/* Get the data and retrieve the 2,2 value */
double[][] aReal = ((ScilabDouble)foo).getRealPart();
System.out.println("foo[1,1] = " + aReal[1][1]);
/* Change the value of 2,2 */
aReal[1][1] = Math.PI;
/* Create a new variable */
ScilabDouble bar = new ScilabDouble(aReal);
/* Send it to Scilab */
sci.put("bar",bar);
/* Display it through Scilab */
sci.exec("disp(bar)");
sci.close();
} else {
System.out.println("Could not start Scilab ");
}
/* Can be improved by other exceptions: AlreadyRunningException,
* InitializationException, UndefinedVariableException,
* UnknownTypeException, etc
*/
} catch (org.scilab.modules.javasci.JavasciException e) {
System.err.println("An exception occurred: " + e.getLocalizedMessage());
}
}
}
这个例子在D:\Program Files\scilab-5.5.2\modules\javasci\examples\v2
路径下
运行结果为:
可以查看the documentation of Javasci v2.了解更多的方法和对象
Build
GNU/Linux and Mac OS X下,需要设置Scilab变量的路径(windows下会自动检测)
# Variable SCI
# Under GNU/Linux with the Scilab binary, the path is:
/path/to/scilab-xxx/share/scilab/
# Under GNU/Linux and Mac OS X with the source tree, the path is the base of the source tree
/path/to/scilab/sources/
# Under Mac OS X, the path is:
/Applications/scilab-xxx/Contents/MacOS/share/scilab/
# On Windows, the path is:
C:\Program Files\Scilab-5.3.0\
build 一个代码,需要在classpath中加入对应的两个jar文件:
# For example, create a variable CLASSPATH
$(SCI)/modules/javasci/jar/org.scilab.modules.javasci.jar
$(SCI)/modules/types/jar/org.scilab.modules.types.jar
3 执行
这一步中,javasciv2 依赖两个native 类库,所以路径需要设置一下,就是bin文件夹的路径
# For example, create a variable LIBPATH
# Under GNU/Linux with the Scilab binary, the path is:
/path/to/scilab-xxx/lib/scilab/
# Under GNU/Linux and Mac OS X with the source tree, the path is:
$(SCI)/modules/javasci/.libs/:$(SCI)/modules/.libs/
# Under Mac OS X, the path is:
/Applications/scilab-xxx/Contents/MacOS/lib/scilab/
# Under Windows, the path is:
set LIBPATH="C:\Program Files\Scilab-5.5.2\bin"
运行程序的命令行如下:
java -cp $CLASSPATH:. -DSCI=$SCI -Djava.library.path=$LIBPATH Example1
A sample Makefile
SCI = /path/to/scilab/share/scilab/
CLASSPATH = $(SCI)/modules/javasci/jar/org.scilab.modules.javasci.jar:$(SCI)/modules/types/jar/org.scilab.modules.types.jar
LIB_PATH = /path/to/scilab/lib/scilab
all:
javac -cp $(CLASSPATH) Example1.java
java -cp $(CLASSPATH):. -DSCI=$(SCI) -Djava.library.path=$(LIB_PATH) Example1
可以在路径 D:\Program Files\scilab-5.5.2\modules\javasci\examples\v2
下看到 build.bat