[Scilab-Dev] call function in java language

Yung-Jang Lee yjlee123 at gmail.com
Sun Nov 11 17:13:55 CET 2012


Scilab 目前提供在 Java 環境 call scilab 函數的功能
你可依照以下程序使用

      1. 先以 Scilab 語言設計未來需要被 Java  操作的計算功能
      2. 利用 Scilab 所提供的 Java 介面, 透過 Java 環境操作 Scilab

         help javasci 可查出有哪些介面可以使用

      3. 上一步計算完的結果以變數的形式存在 Scilab 工作空間 (workspace),可透過 Scilab -Java 介面傳到
Java 環境轉成Java 變數



你必須將 SCI\bin 加入到你的 PATH 環境參數內, 另外將幾個路徑加入倒  CLASSPATH 中 , 詳細說明可
以 help compile_and_run_javasci_v2 <compile_and_run_javasci_v2.html>  查到


以 scilab-5.4.0\modules\javasci\examples\v2 內之範例說明, 首先修改其中所附的 build.bat 如下


--------------build.bat-------------

echo offrem Path to Scilab dataset
SCI=C:\ADE\programs\scilab-5.4.0<--------- 這行要記得用你自己了 Scilab 路徑名稱set
oPath=%PATH%set PATH=%SCI%\bin;%oPath%rem Java APIset
CLASSPATH=%SCI%/modules/javasci/jar/org.scilab.modules.javasci.jar;%SCI%/modules/types/jar/org.scilab.modules.types.jar;%SCI%/jarrem
Path to native libsjavac -cp %CLASSPATH% Example1.javajava -cp
%CLASSPATH%;.  Example1
set PATH=%oPath%
-----  end of build.bat----------------------------------


開啟一個 DOS 視窗, cd 到 scilab-5.4.0\modules\javasci\examples\v2 內執行

   build

應該可看到輸出

......\scilab-5.4.0\modules\javasci\examples\v2>echo off

    2.    4.           6.
    4.    3.1415927    10.
    6.    10.          12.

我將 Java code 簡單註解說明如下 :

---------------Example1.java 說明---------------


// 以下三行之 Java package 由 Scilab 環境提供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(); // 取得一個 Scilab 物件

            if (sci.open()) {
                /* Send a Scilab instruction */
                sci.exec("foo = [ 2, 4, 6; 4, 0, 10; 6, 10, 12 ];");
 //以字串方式送出給 Scilab 執行

                /* Retrieve the variable foo */
                ScilabType foo = sci.get("foo"); // 取得 scilab 變數 foo

                /* Display the variable */
                System.out.println("Representation of  : " + foo); //
顯示 Scilab 變數 foo

                /* Get the data and retrieve the 2,2 value */
                double[][] aReal = ((ScilabDouble)foo).getRealPart();
// 取得  foo(2,2) 之實部存成 Java 變數 aReal
                System.out.println("foo[1,1] = " + aReal[1][1]);  // 列印 aReal

                /* Change the value of 2,2 */
                aReal[1][1] = Math.PI;    //  改變 aReal(1,1) 內容

                /* Create a new variable */
                ScilabDouble bar = new ScilabDouble(aReal); // 以 aReal
內容設定一和 Scilab 交換資料的 Java 變數 bar

                /* Send it to Scilab */
                sci.put("bar", bar); // 將 Java 變數 bar 送到 Scilab 存成 Scilab 變數 bar

                /* Display it through Scilab */
                sci.exec("disp(bar)"); // 以 Scilab 語言顯示 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());
        }

    }}
----------------------------------------------------


不過我發現目前這個版本 (Scilab-5.4 ) 在我的環境 執行這個程式有 bug, 幾個列印指令並未正常輸出


如果你和我的輸出結果一樣 那麼試試 5.3 版

這類 bug  最近幾版會較多  因為 Scilab 似乎正在進行由核心改變的大變動



2012/11/11 杜宽 <du.k at neusoft.com>

> Hi,****
>
>          I want to call a function created by myself in using java
> language, so how can do ? thanks very much!****
>
> ** **
>
> ***************************
>
> 杜宽 du.k at neusoft.com****
>
> Mobile:13464181474****
>
> 东软集团股份有限公司****
>
> 政府事业部****
>
> 国土与住建开发部****
>
> ***************************
>
> ** **
>
>
> ---------------------------------------------------------------------------------------------------
> Confidentiality Notice: The information contained in this e-mail and any
> accompanying attachment(s)
> is intended only for the use of the intended recipient and may be
> confidential and/or privileged of
> Neusoft Corporation, its subsidiaries and/or its affiliates. If any reader
> of this communication is
> not the intended recipient, unauthorized use, forwarding, printing,
> storing, disclosure or copying
> is strictly prohibited, and may be unlawful.If you have received this
> communication in error,please
> immediately notify the sender by return e-mail, and delete the original
> message and all copies from
> your system. Thank you.
>
> ---------------------------------------------------------------------------------------------------
>
> _______________________________________________
> dev mailing list
> dev at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/dev/attachments/20121112/5a85e665/attachment.htm>


More information about the dev mailing list