diff options
Diffstat (limited to 'modules/javasci/src/java')
6 files changed, 1221 insertions, 0 deletions
diff --git a/modules/javasci/src/java/org/scilab/modules/javasci/Call_Scilab.java b/modules/javasci/src/java/org/scilab/modules/javasci/Call_Scilab.java new file mode 100755 index 000000000..f9ee451ba --- /dev/null +++ b/modules/javasci/src/java/org/scilab/modules/javasci/Call_Scilab.java @@ -0,0 +1,137 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.7 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package org.scilab.modules.javasci; + + +import org.scilab.modules.types.ScilabTypeEnum; +import org.scilab.modules.types.ScilabIntegerTypeEnum; + + /** + * Connector for Javasci v2. + * This class should <b>not</b> be used directly + * @see org.scilab.modules.javasci.Scilab + * @author DIGITEO - Sylvestre LEDRU + */ +public class Call_Scilab { + + /** + * Constructor + */ + protected Call_Scilab() { + throw new UnsupportedOperationException(); + } + public static int Call_ScilabOpen(String SCIpath, boolean advancedMode, String ScilabStartup, int Stacksize) { + return Call_ScilabJNI.Call_ScilabOpen(SCIpath, advancedMode, ScilabStartup, Stacksize); + } + + public static boolean TerminateScilab(String ScilabQuit) { + return Call_ScilabJNI.TerminateScilab(ScilabQuit); + } + + public static int SendScilabJob(String job) { + return Call_ScilabJNI.SendScilabJob(job); + } + + public static int SendScilabJobs(String[] jobs, int numberjobs) { + return Call_ScilabJNI.SendScilabJobs(jobs, numberjobs); + } + + public static void SetFromJavaToON() { + Call_ScilabJNI.SetFromJavaToON(); + } + + public static boolean isGraphicOpened() { + return Call_ScilabJNI.isGraphicOpened(); + } + + public static int GetLastErrorCode() { + return Call_ScilabJNI.GetLastErrorCode(); + } + + public static boolean isExistingVariable(String varname) { + return Call_ScilabJNI.isExistingVariable(varname); + } + + public static String getLastErrorMessage() { + return Call_ScilabJNI.getLastErrorMessage(); + } + + public static ScilabTypeEnum getVariableType(String varname) { + return ScilabTypeEnum.swigToEnum(Call_ScilabJNI.getVariableType(varname)); + } + + public static ScilabIntegerTypeEnum getIntegerPrecision(String varname) { + return ScilabIntegerTypeEnum.swigToEnum(Call_ScilabJNI.getIntegerPrecision(varname)); + } + + public static boolean isComplex(String varname) { + return Call_ScilabJNI.isComplex(varname); + } + + public static int putString(String variableName, String[][] variable) { + return Call_ScilabJNI.putString(variableName, variable); + } + + public static int putDoubleComplex(String variableName, double[][] variable, double[][] imag) { + return Call_ScilabJNI.putDoubleComplex(variableName, variable, imag); + } + + public static int putDouble(String variableName, double[][] variable) { + return Call_ScilabJNI.putDouble(variableName, variable); + } + + public static int putBoolean(String variableName, boolean[][] variable) { + return Call_ScilabJNI.putBoolean(variableName, variable); + } + + public static int putByte(String variableName, byte[][] variable) { + return Call_ScilabJNI.putByte(variableName, variable); + } + + public static int putUnsignedByte(String variableName, byte[][] variable) { + return Call_ScilabJNI.putUnsignedByte(variableName, variable); + } + + public static int putShort(String variableName, short[][] variable) { + return Call_ScilabJNI.putShort(variableName, variable); + } + + public static int putUnsignedShort(String variableName, short[][] variable) { + return Call_ScilabJNI.putUnsignedShort(variableName, variable); + } + + public static int putInt(String variableName, int[][] variable) { + return Call_ScilabJNI.putInt(variableName, variable); + } + + public static int putUnsignedInt(String variableName, int[][] variable) { + return Call_ScilabJNI.putUnsignedInt(variableName, variable); + } + + public static int putSparse(String variableName, int nbRow, int nbCol, int[] nbRowItem, int[] colPos, double[] data) { + return Call_ScilabJNI.putSparse(variableName, nbRow, nbCol, nbRowItem, colPos, data); + } + + public static int putComplexSparse(String variableName, int nbRow, int nbCol, int[] nbRowItem, int[] colPos, double[] data, double[] imag) { + return Call_ScilabJNI.putComplexSparse(variableName, nbRow, nbCol, nbRowItem, colPos, data, imag); + } + + public static int putBooleanSparse(String variableName, int nbRow, int nbCol, int[] nbRowItem, int[] colPos) { + return Call_ScilabJNI.putBooleanSparse(variableName, nbRow, nbCol, nbRowItem, colPos); + } + + public static int putPolynomial(String variableName, String polyVarName, double[][][] data) { + return Call_ScilabJNI.putPolynomial(variableName, polyVarName, data); + } + + public static int putComplexPolynomial(String variableName, String polyVarName, double[][][] data, double[][][] imag) { + return Call_ScilabJNI.putComplexPolynomial(variableName, polyVarName, data, imag); + } + +} diff --git a/modules/javasci/src/java/org/scilab/modules/javasci/Call_ScilabJNI.java b/modules/javasci/src/java/org/scilab/modules/javasci/Call_ScilabJNI.java new file mode 100755 index 000000000..d157c450c --- /dev/null +++ b/modules/javasci/src/java/org/scilab/modules/javasci/Call_ScilabJNI.java @@ -0,0 +1,74 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.7 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package org.scilab.modules.javasci; + + +/* It is generated code. Disable checkstyle */ +//CHECKSTYLE:OFF + /** + * Connector for Javasci v2. + * This class should <b>not</b> be used directly + * @see org.scilab.modules.javasci.Scilab + * @author DIGITEO - Sylvestre LEDRU + */ +class Call_ScilabJNI { + + /** + * Constructor + */ + protected Call_ScilabJNI() { + throw new UnsupportedOperationException(); + } + + static { + try { + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + System.loadLibrary("javasci"); + } else { + System.loadLibrary("javasci2"); + } + } catch (SecurityException e) { + System.err.println("A security manager exists and does not allow the loading of the specified dynamic library :"); + e.printStackTrace(System.err); + } catch (UnsatisfiedLinkError e) { + System.err.println("The native library javasci does not exist or cannot be found."); + e.printStackTrace(System.err); + } + } + + public final static native int putList(String varNmae, Object list, char type); + + public final static native int Call_ScilabOpen(String jarg1, boolean jarg2, String jarg3, int jarg4); + public final static native boolean TerminateScilab(String jarg1); + public final static native int SendScilabJob(String jarg1); + public final static native int SendScilabJobs(String[] jarg1, int jarg2); + public final static native void SetFromJavaToON(); + public final static native boolean isGraphicOpened(); + public final static native int GetLastErrorCode(); + public final static native boolean isExistingVariable(String jarg1); + public final static native String getLastErrorMessage(); + public final static native int getVariableType(String jarg1); + public final static native int getIntegerPrecision(String jarg1); + public final static native boolean isComplex(String jarg1); + public final static native int putString(String jarg1, String[][] jarg2); + public final static native int putDoubleComplex(String jarg1, double[][] jarg2, double[][] jarg5); + public final static native int putDouble(String jarg1, double[][] jarg2); + public final static native int putBoolean(String jarg1, boolean[][] jarg2); + public final static native int putByte(String jarg1, byte[][] jarg2); + public final static native int putUnsignedByte(String jarg1, byte[][] jarg2); + public final static native int putShort(String jarg1, short[][] jarg2); + public final static native int putUnsignedShort(String jarg1, short[][] jarg2); + public final static native int putInt(String jarg1, int[][] jarg2); + public final static native int putUnsignedInt(String jarg1, int[][] jarg2); + public final static native int putSparse(String jarg1, int jarg2, int jarg3, int[] jarg4, int[] jarg6, double[] jarg8); + public final static native int putComplexSparse(String jarg1, int jarg2, int jarg3, int[] jarg4, int[] jarg6, double[] jarg8, double[] jarg10); + public final static native int putBooleanSparse(String jarg1, int jarg2, int jarg3, int[] jarg4, int[] jarg6); + public final static native int putPolynomial(String jarg1, String jarg2, double[][][] jarg3); + public final static native int putComplexPolynomial(String jarg1, String jarg2, double[][][] jarg3, double[][][] jarg7); +} diff --git a/modules/javasci/src/java/org/scilab/modules/javasci/JavasciException.java b/modules/javasci/src/java/org/scilab/modules/javasci/JavasciException.java new file mode 100755 index 000000000..329fa630d --- /dev/null +++ b/modules/javasci/src/java/org/scilab/modules/javasci/JavasciException.java @@ -0,0 +1,132 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2010 - INRIA - Sylvestre LEDRU + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + * + */ + +package org.scilab.modules.javasci; + +/** + * This class provides a general exception class for Javasci v2 API. + */ +public abstract class JavasciException extends Exception { + + + /** + * This class provides a exception class when an error occurs on the + * initialization + */ + public static class InitializationException extends JavasciException { + + public InitializationException(String message) { + super(message); + } + + public InitializationException(String message, Throwable cause) { + super(message, cause); + } + + } + + + /** + * This class provides a exception class when an unsupported type is used + */ + public static class UnsupportedTypeException extends JavasciException { + + public UnsupportedTypeException(String message) { + super(message); + } + } + + + /** + * This class provides a exception class when a type is unknown + * This should be very very rare. + */ + public static class UnknownTypeException extends JavasciException { + + public UnknownTypeException(String message) { + super(message); + } + + } + + /** + * This class provides a exception class when Scilab is already running + */ + public static class AlreadyRunningException extends JavasciException { + + public AlreadyRunningException(String message) { + super(message); + } + } + + + /** + * This class provides a exception class when an internal error occurs + */ + public static class ScilabInternalException extends JavasciException { + + public ScilabInternalException(String message) { + super(message); + } + + } + + /** + * This class provides a exception class when a Scilab error occurs + * @since 5.4.0 + */ + public static class ScilabErrorException extends JavasciException { + + public ScilabErrorException(String message, int errorCode) { + super(message + "\nCode: " + errorCode); + } + + } + + + /** + * This class provides a exception class when an non-existing variable is + * accessed. + */ + public static class UndefinedVariableException extends JavasciException { + + public UndefinedVariableException(String message) { + super(message); + } + + } + + /** + * Default constructor + */ + public JavasciException() { + super(); + } + + /** + * Default constructor + */ + public JavasciException(String message) { + super(message); + } + + /** + * + * @param message the message to be printed + * @param cause the cause + */ + public JavasciException(String message, Throwable cause) { + super(message, cause); + } + + +} diff --git a/modules/javasci/src/java/org/scilab/modules/javasci/Scilab.java b/modules/javasci/src/java/org/scilab/modules/javasci/Scilab.java new file mode 100755 index 000000000..207b8f1cd --- /dev/null +++ b/modules/javasci/src/java/org/scilab/modules/javasci/Scilab.java @@ -0,0 +1,770 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2010 - INRIA - Sylvestre LEDRU + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + * + */ +package org.scilab.modules.javasci; + +import java.io.File; +import java.io.FileNotFoundException; + +import org.scilab.modules.types.ScilabType; +import org.scilab.modules.types.ScilabTypeEnum; +import org.scilab.modules.types.ScilabIntegerTypeEnum; +import org.scilab.modules.types.ScilabDouble; +import org.scilab.modules.types.ScilabList; +import org.scilab.modules.types.ScilabMList; +import org.scilab.modules.types.ScilabTList; +import org.scilab.modules.types.ScilabString; +import org.scilab.modules.types.ScilabBoolean; +import org.scilab.modules.types.ScilabBooleanSparse; +import org.scilab.modules.types.ScilabInteger; +import org.scilab.modules.types.ScilabPolynomial; +import org.scilab.modules.types.ScilabSparse; +import org.scilab.modules.javasci.Call_Scilab; +import org.scilab.modules.javasci.JavasciException.AlreadyRunningException; +import org.scilab.modules.javasci.JavasciException.InitializationException; +import org.scilab.modules.javasci.JavasciException.UnsupportedTypeException; +import org.scilab.modules.javasci.JavasciException.UndefinedVariableException; +import org.scilab.modules.javasci.JavasciException.UnknownTypeException; +import org.scilab.modules.javasci.JavasciException.ScilabInternalException; +import org.scilab.modules.javasci.JavasciException.ScilabErrorException; + +/** + * This class provides the capability to access to the Scilab engine from + * a Java application.<br /> + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab();<br /> + * if (sci.open()) {<br /> + * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br /> + * ScilabDouble aOriginal = new ScilabDouble(a);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabDouble aFromScilab = (ScilabDouble)sci.get("a");<br /> + * }<br /> + * </code> + * @see org.scilab.modules.types + */ +public class Scilab { + + private static int notHandledError = -999; + + private String SCI = null; + private boolean advancedMode = false; + + /** + * Creator of the Scilab Javasci object. <br /> + * Scilab data path is autodetected and advanced features disabled + */ + public Scilab() throws InitializationException { + this(null, false); + } + + /** + * Creator of the Scilab Javasci object with a specific Scilab path.<br /> + * Advanced features are disabled (faster) + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab("/path/to/Scilab/data/dir/");<br /> + * <br /> + * </code> + * @param SCI provide the path to Scilab data + */ + public Scilab(String SCI) throws InitializationException { + this(SCI, false); + } + + /** + * Creator of the Scilab Javasci object in advanced mode<br /> + * Scilab data path is autodetected + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab(true); // Starts in advanced mode<br /> + * <br /> + * </code> + * @param advancedMode true enables the advanced mode (GUI, graphics, Tcl/Tk, sciNotes...). Smaller. + */ + public Scilab(boolean advancedMode) throws InitializationException { + this(null, advancedMode); + } + + /** + * Creator of the Scilab Javasci object. <br /> + * Under GNU/Linux / Mac OS X, try to detect Scilab base path<br /> + * if the property SCI is set, use it<br /> + * if not, try with the global variable SCI<br /> + * if not, throws a new exception<br /> + * Under Windows, use also the registery<br /> + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab("/path/to/Scilab/data/dir/",true); // Starts in advanced mode<br /> + * <br /> + * </code> + * @param SCIPath the path to Scilab data + * @param advancedMode true enables the advanced mode (GUI, graphics, Tcl/Tk, sciNotes...). Smaller. + */ + public Scilab(String SCIPath, boolean advancedMode) throws InitializationException { + String SCI = SCIPath; + if (!System.getProperty("os.name").toLowerCase().contains("windows")) { + if (SCI == null) { + // Auto detect + try { + SCI = System.getProperty("SCI"); + if (SCI == null || SCI.length() == 0) { + SCI = System.getenv("SCI"); + if (SCI == null || SCI.length() == 0) { + throw new InitializationException("Auto detection of SCI failed.\nSCI empty."); + } + } + } catch (Exception e) { + throw new InitializationException("Auto detection of SCI failed.\nCould not retrieve the variable SCI.", e); + } + } + } + this.advancedMode = advancedMode; + this.initScilab(SCI); + + } + + + private void initScilab(String SCI) throws InitializationException { + /* Let Scilab engine knows that he is run through the Javasci API */ + Call_Scilab.SetFromJavaToON(); + if (!System.getProperty("os.name").toLowerCase().contains("windows")) { + File f = new File(SCI); + if (!f.isDirectory()) { + throw new InitializationException("Could not find directory " + f.getAbsolutePath()); + } + this.SCI = SCI; + } + } + + /** + * Open a connection to the Scilab engine<br /> + * This function is based on Call_ScilabOpen from call_scilab<br /> + * Note: For now, only one instance of Scilab can be launched<br /> + * A second launch will return FALSE<br /> + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab();<br /> + * sci.open();<br /> + * <br /> + * </code> + * @return if the operation is successful + * @throws AlreadyRunningException Scilab is already running + * @throws InitializationException Cannot start Scilab + */ + public boolean open() throws JavasciException { + int res = Call_Scilab.Call_ScilabOpen(this.SCI, this.advancedMode, null, -1); + switch (res) { + case 0: /* Success */ + return true; + case -1: + throw new AlreadyRunningException("Javasci already running."); + case -2: + /* Should not occurd (processed before) */ + throw new InitializationException("Could not find SCI."); + case -3: + throw new InitializationException("No existing directory."); + case 10001: + throw new InitializationException("Stacksize failed (not enought memory ?)."); + default: + throw new InitializationException("Unknown startup error: " + res); + } + } + + /** + * Open a connection to the Scilab engine and run the command job<br /> + * This function is based on Call_ScilabOpen from call_scilab<br /> + * Note: For now, only one instance of Scilab can be launched<br /> + * A second launch will return FALSE<br /> + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab();<br /> + * sci.open("a=%pi;");<br /> + * <br /> + * </code> + * @param job The job to run on startup + * @return if the operation is successful + */ + public boolean open(String job) throws JavasciException { + if (!this.open()) { + return false; + } + + return this.exec(job); + } + + /** + * Open a connection to the Scilab engine and run commands job<br /> + * This function is based on Call_ScilabOpen from call_scilab<br /> + * Note: For now, only one instance of Scilab can be launched<br /> + * A second launch will return FALSE<br /> + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab();<br /> + * sci.open(new String[]{"a=42*2;","b=44*2", "c=(a==b)"});<br /> + * <br /> + * </code> + * @param jobs The serie of jobs to run on startup + * @return if the operation is successful + */ + public boolean open(String jobs[]) throws JavasciException { + if (!this.open()) { + return false; + } + return this.exec(jobs); + } + + + /** + * Open a connection to the Scilab engine and run thefile scriptFilename<br /> + * This function is based on Call_ScilabOpen from call_scilab<br /> + * Note: For now, only one instance of Scilab can be launched<br /> + * A second launch will return FALSE<br /> + * <br /> + * Example:<br /> + * <code> + * Scilab sci = new Scilab();<br /> + * sci.open(new File("/tmp/myscript.sce"));<br /> + * <br /> + * </code> + * @param scriptFilename The script to execute on startup + * @return if the operation is successful + */ + public boolean open(File scriptFilename) throws JavasciException, FileNotFoundException { + if (!this.open()) { + return false; + } + + return this.exec(scriptFilename); + } + + + /** + * Execute a single command in Scilab<br /> + * This function is based on SendScilabJob from call_scilab + * <br /> + * Example:<br /> + * <code> + * sci.exec("a=2*%pi");<br /> + * <br /> + * </code> + * @param job the job to execute + * @return if the operation is successful + */ + public boolean exec(String job) { + try { + this.execException(job); + return true; + } catch (Exception e) { + return false; + } + } + + + /** + * Execute a single command in Scilab<br /> + * Returns a ScilabErrorException in case of Scilab problem<br /> + * This function is based on SendScilabJob from call_scilab + * <br /> + * Example:<br /> + * <code> + * sci.exec("a=2*%pi");<br /> + * <br /> + * </code> + * @param job the job to execute + * @since 5.4.0 + */ + public void execException(String job) throws ScilabErrorException { + int result = Call_Scilab.SendScilabJob(job); + if (result != 0) { + throw new ScilabErrorException("A Scilab error occurred: " + this.getLastErrorMessage(), this.getLastErrorCode()); + } + } + + + /** + * Execute several commands in Scilab<br /> + * This function is based on SendScilabJob from call_scilab + * <br /> + * Example:<br /> + * <code> + * sci.exec(new String[]{"a=42*2;","b=44*2", "c=(a==b)"});<br /> + * <br /> + * </code> + * @param jobs the serie of job to execute + * @return if the operation is successful + */ + public boolean exec(String jobs[]) { + try { + this.execException(jobs); + return true; + } catch (Exception e) { + return false; + } + } + + /** + * Execute several commands in Scilab<br /> + * Returns a ScilabErrorException in case of Scilab problem<br /> + * This function is based on SendScilabJob from call_scilab + * <br /> + * Example:<br /> + * <code> + * sci.exec(new String[]{"a=42*2;","b=44*2", "c=(a==b)"});<br /> + * <br /> + * </code> + * @param jobs the serie of job to execute + * @since 5.4.0 + */ + public void execException(String jobs[]) throws ScilabErrorException { + int result = Call_Scilab.SendScilabJobs(jobs, jobs.length); + if (result != 0) { + throw new ScilabErrorException("A Scilab error occurred: " + this.getLastErrorMessage(), this.getLastErrorCode()); + } + } + + /** + * Execute a Scilab script .sce/.sci and throws an exception in case<br /> + * of a Scilab error<br /> + * Returns a ScilabErrorException in case of Scilab problem<br /> + * This function is based on SendScilabJob from call_scilab<br /> + * Note that this function is a direct call on the Scilab function exec: + * <code> this.exec("exec('" + scriptFilename + "');");</code> + * <br /> + * Example:<br /> + * <code> + * sci.exec(new File("/tmp/myscript.sci"));<br /> + * <br /> + * </code> + * @param scriptFilename the script to execute + * @since 5.4.0 + */ + public void execException(File scriptFilename) throws FileNotFoundException, ScilabErrorException { + if (!scriptFilename.exists()) { + throw new FileNotFoundException("Could not find " + scriptFilename); + } + this.execException("exec('" + scriptFilename + "');"); + } + + /** + * Execute a Scilab script .sce/.sci and throws an exception in case<br /> + * the file is not found<br /> + * This function is based on SendScilabJob from call_scilab<br /> + * Note that this function is a direct call on the Scilab function exec: + * <code> this.exec("exec('" + scriptFilename + "');");</code> + * <br /> + * Example:<br /> + * <code> + * sci.exec(new File("/tmp/myscript.sci"));<br /> + * <br /> + * </code> + * @param scriptFilename the script to execute + * @return if the operation is successful + */ + public boolean exec(File scriptFilename) throws FileNotFoundException { + if (!scriptFilename.exists()) { + throw new FileNotFoundException("Could not find " + scriptFilename); + } + return this.exec("exec('" + scriptFilename + "');"); + } + + + /** + * Detect if a variable (varname) exists in Scilab + * <br /> + * Example:<br /> + * <code> + * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br /> + * ScilabDouble aOriginal = new ScilabDouble(a);<br /> + * sci.put("a",aOriginal);<br /> + * assert sci.isExistingVariable("a") == true;<br /> + * <br /> + * </code> + * @param varname the variable to check + * @return if the variable exists or not + */ + public boolean isExistingVariable(String varname) { + return Call_Scilab.isExistingVariable(varname); + } + + + /** + * Shutdown Scilab<br /> + * This function is based on TerminateScilab from call_scilab + * <br /> + * Example:<br /> + * <code> + * sci.close();<br /> + * <br /> + * </code> + * @return if the operation is successful + */ + public boolean close() { + return Call_Scilab.TerminateScilab(null); + } + + + /** + * Return the last error code + * <br /> + * Example:<br /> + * <code> + * sci.open("a=1+"); // Wrong operation<br /> + * sci.getLastErrorCode() // Returns 2<br /> + * <br /> + * </code> + * @return the error code + */ + public int getLastErrorCode() { + return Call_Scilab.GetLastErrorCode(); + } + + + /** + * Return the last error message + * <br /> + * Example:<br /> + * <code> + * sci.open("a=1+");<br /> + * System.err.println(sci.getLastErrorMessage());<br /> + * <br /> + * </code> + * @return the error message itself + */ + public String getLastErrorMessage() { + return Call_Scilab.getLastErrorMessage(); + } + + + /** + * Detect if a Scilab graphic window is still opened<br /> + * This function is based on ScilabHaveAGraph from call_scilab + * <br /> + * Example:<br /> + * <code> + * sci.exec("plot3d();");<br /> + * sci.isGraphicOpened();<br /> + * <br /> + * </code> + * @return if the graphic is open or not + */ + public boolean isGraphicOpened() { + return Call_Scilab.isGraphicOpened(); + } + + /** + * Return the code type of a variable varname + * <br /> + * Example:<br /> + * <code> + * sci.exec("a = 2*%pi");<br /> + * if (sci.getVariableType("a") == ScilabTypeEnum.sci_matrix) {<br /> + * System.out.println("a is a double matrix");<br /> + * }<br /> + * <br /> + * </code> + * @param varName the name of the variable + * @return the type of the variable + * @throws UndefinedVariableException The variable does not exist + * @throws UnknownTypeException Cannot find the type + */ + public ScilabTypeEnum getVariableType(String varName) throws JavasciException { + return getVariableTypeInCurrentScilabSession(varName); + } + + /** + * Return the code type of a variable varname in the current Scilab session + * <br /> + * Example:<br /> + * <code> + * sci.exec("a = 2*%pi");<br /> + * if (sci.getVariableType("a") == ScilabTypeEnum.sci_matrix) {<br /> + * System.out.println("a is a double matrix");<br /> + * }<br /> + * <br /> + * </code> + * @param varName the name of the variable + * @return the type of the variable + * @throws UndefinedVariableException The variable does not exist + * @throws UnknownTypeException Cannot find the type + */ + public static ScilabTypeEnum getVariableTypeInCurrentScilabSession(String varName) throws JavasciException { + ScilabTypeEnum variableType = null; + try { + variableType = Call_Scilab.getVariableType(varName); + if (variableType == null ) { + throw new UndefinedVariableException("Could not find the type of the variable '" + varName + "'"); + } + } catch (IllegalArgumentException e) { + String lastWord = e.getMessage().substring(e.getMessage().lastIndexOf(' ') + 1); + if (lastWord.equals("-2")) { /* Crappy workaround. Parse the exception */ + throw new UndefinedVariableException("Could not find variable '" + varName + "'"); + } + throw new UnknownTypeException("Type of " + varName + " unknown"); + + } + return variableType; + } + + /** + * Returns a variable named varname<br /> + * Throws an exception if the datatype is not managed or if the variable is not available + * <br /> + * Example:<br /> + * <code> + * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br /> + * double [][]aImg={{212.2, 221.0, 423.0, 393.0},{234.2, 244.0, 441.0, 407.0}};<br /> + * ScilabDouble aOriginal = new ScilabDouble(a, aImg);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabDouble aFromScilab = (ScilabDouble)sci.get("a");<br /> + * <br /> + * </code> + * @param varname the name of the variable + * @return return the variable + * @throws UnsupportedTypeException Type not managed yet. + */ + public ScilabType get(String varname) throws JavasciException { + return getInCurrentScilabSession(varname); + } + + /** + * Returns a reference variable named varname<br /> + * Throws an exception if the datatype is not managed or if the variable is not available + * <br /> + * Example:<br /> + * <code> + * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br /> + * double [][]aImg={{212.2, 221.0, 423.0, 393.0},{234.2, 244.0, 441.0, 407.0}};<br /> + * ScilabDouble aOriginal = new ScilabDouble(a, aImg);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabDouble aFromScilab = (ScilabDouble)sci.get("a");<br /> + * <br /> + * </code> + * @param varname the name of the variable + * @return return the variable + * @throws UnsupportedTypeException Type not managed yet. + */ + public ScilabType getByReference(String varname) throws JavasciException { + return getInCurrentScilabSession(varname, true); + } + + /** + * Returns a variable named varname in the current Scilab session<br /> + * Throws an exception if the datatype is not managed or if the variable is not available + * <br /> + * Example:<br /> + * <code> + * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br /> + * double [][]aImg={{212.2, 221.0, 423.0, 393.0},{234.2, 244.0, 441.0, 407.0}};<br /> + * ScilabDouble aOriginal = new ScilabDouble(a, aImg);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabDouble aFromScilab = (ScilabDouble)sci.get("a");<br /> + * <br /> + * </code> + * @param varname the name of the variable + * @return return the variable + * @throws UnsupportedTypeException Type not managed yet. + */ + public static ScilabType getInCurrentScilabSession(String varname) throws JavasciException { + return getInCurrentScilabSession(varname, false); + } + + /** + * Returns a variable named varname in the current Scilab session<br /> + * Throws an exception if the datatype is not managed or if the variable is not available + * <br /> + * Example:<br /> + * <code> + * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br /> + * double [][]aImg={{212.2, 221.0, 423.0, 393.0},{234.2, 244.0, 441.0, 407.0}};<br /> + * ScilabDouble aOriginal = new ScilabDouble(a, aImg);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabDouble aFromScilab = (ScilabDouble)sci.get("a");<br /> + * <br /> + * </code> + * @param varname the name of the variable + * @return return the variable + * @throws UnsupportedTypeException Type not managed yet. + */ + public static ScilabType getInCurrentScilabSession(String varname, boolean byref) throws JavasciException { + ScilabTypeEnum sciType = getVariableTypeInCurrentScilabSession(varname); + switch (sciType) { + case sci_matrix: + case sci_boolean: + case sci_strings: + + case sci_poly: + case sci_sparse: + case sci_boolean_sparse: + case sci_list: + case sci_tlist: + case sci_mlist: + return ScilabVariablesJavasci.getScilabVariable(varname, true, byref); + case sci_ints: + ScilabIntegerTypeEnum typeInt = Call_Scilab.getIntegerPrecision(varname); + + switch (typeInt) { + case sci_int8: + case sci_uint8: + case sci_int16: + case sci_uint16: + case sci_int32: + case sci_uint32: + return ScilabVariablesJavasci.getScilabVariable(varname, true, byref); + case sci_int64: + case sci_uint64: + // will be available in Scilab 6 + throw new UnsupportedTypeException("64 bit (signed and unsigned) integer types not managed in Scilab 5.X"); + + } + + default: + throw new UnsupportedTypeException("Type not managed: " + sciType); + } + } + + /** + * Send to Scilab a variable theVariable named varname<br /> + * Throws an exception if the datatype is not managed or if the variable is not available + * <br /> + * Example:<br /> + * <code> + * boolean [][]a={{true, true, false, false},{true, false, true, false}};<br /> + * ScilabBoolean aOriginal = new ScilabBoolean(a);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabBoolean aFromScilab = (ScilabBoolean)sci.get("a");<br /> + * <br /> + * </code> + * @param varname the name of the variable + * @param theVariable the variable itself + * @return true if the operation is successful + * @throws UnsupportedTypeException Type not managed yet. + */ + public boolean put(String varname, ScilabType theVariable) throws JavasciException { + return putInCurrentScilabSession(varname, theVariable); + } + + /** + * Send to the current Scilab session a variable theVariable named varname<br /> + * Throws an exception if the datatype is not managed or if the variable is not available + * <br /> + * Example:<br /> + * <code> + * boolean [][]a={{true, true, false, false},{true, false, true, false}};<br /> + * ScilabBoolean aOriginal = new ScilabBoolean(a);<br /> + * sci.put("a",aOriginal);<br /> + * ScilabBoolean aFromScilab = (ScilabBoolean)sci.get("a");<br /> + * <br /> + * </code> + * @param varname the name of the variable + * @param theVariable the variable itself + * @return true if the operation is successful + * @throws UnsupportedTypeException Type not managed yet. + */ + public static boolean putInCurrentScilabSession(String varname, ScilabType theVariable) throws JavasciException { + int err = notHandledError; /* -999: if the type is not handled */ + + switch (theVariable.getType()) { + case sci_matrix : + ScilabDouble sciDouble = (ScilabDouble) theVariable; + if (sciDouble.isReal()) { + err = Call_Scilab.putDouble(varname, sciDouble.getRealPart()); + } else { + err = Call_Scilab.putDoubleComplex(varname, sciDouble.getRealPart(), sciDouble.getImaginaryPart()); + } + break; + case sci_poly : + ScilabPolynomial sciPoly = (ScilabPolynomial) theVariable; + if (sciPoly.isReal()) { + err = Call_Scilab.putPolynomial(varname, sciPoly.getPolyVarName(), sciPoly.getRealPart()); + } else { + err = Call_Scilab.putComplexPolynomial(varname, sciPoly.getPolyVarName(), sciPoly.getRealPart(), sciPoly.getImaginaryPart()); + } + break; + case sci_boolean : + ScilabBoolean sciBoolean = (ScilabBoolean) theVariable; + err = Call_Scilab.putBoolean(varname, sciBoolean.getData()); + break; + case sci_sparse : + ScilabSparse sciSparse = (ScilabSparse) theVariable; + if (sciSparse.isReal()) { + err = Call_Scilab.putSparse(varname, sciSparse.getHeight(), sciSparse.getWidth(), sciSparse.getNbItemRow(), sciSparse.getScilabColPos(), sciSparse.getRealPart()); + } else { + err = Call_Scilab.putComplexSparse(varname, sciSparse.getHeight(), sciSparse.getWidth(), sciSparse.getNbItemRow(), sciSparse.getScilabColPos(), sciSparse.getRealPart(), sciSparse.getImaginaryPart()); + } + break; + case sci_boolean_sparse : + ScilabBooleanSparse sciBooleanSparse = (ScilabBooleanSparse) theVariable; + err = Call_Scilab.putBooleanSparse(varname, sciBooleanSparse.getHeight(), sciBooleanSparse.getWidth(), sciBooleanSparse.getNbItemRow(), sciBooleanSparse.getScilabColPos()); + break; + case sci_ints : + ScilabInteger sciInteger = (ScilabInteger) theVariable; + switch (sciInteger.getPrec()) { + case sci_uint8: + err = Call_Scilab.putUnsignedByte(varname, sciInteger.getDataAsByte()); + break; + case sci_int8: + err = Call_Scilab.putByte(varname, sciInteger.getDataAsByte()); + break; + case sci_uint16: + err = Call_Scilab.putUnsignedShort(varname, sciInteger.getDataAsShort()); + break; + case sci_int16: + err = Call_Scilab.putShort(varname, sciInteger.getDataAsShort()); + break; + case sci_uint32: + err = Call_Scilab.putUnsignedInt(varname, sciInteger.getDataAsInt()); + break; + case sci_int32: + err = Call_Scilab.putInt(varname, sciInteger.getDataAsInt()); + break; + case sci_uint64: + // err = Call_Scilab.putUnsignedLong(varname, sciInteger.getData_()); + case sci_int64: + // err = Call_Scilab.putLong(varname, sciInteger.getData_()); + break; + } + break; + case sci_strings : + ScilabString sciString = (ScilabString) theVariable; + err = Call_Scilab.putString(varname, sciString.getData()); + break; + case sci_list : + ScilabList sciList = (ScilabList) theVariable; + err = Call_ScilabJNI.putList(varname, sciList.getSerializedObject(), 'l'); + break; + case sci_tlist : + ScilabTList sciTList = (ScilabTList) theVariable; + err = Call_ScilabJNI.putList(varname, sciTList.getSerializedObject(), 't'); + break; + case sci_mlist : + ScilabMList sciMList = (ScilabMList) theVariable; + err = Call_ScilabJNI.putList(varname, sciMList.getSerializedObject(), 'm'); + break; + } + + if (err == notHandledError) { + throw new UnsupportedTypeException("Type not managed: " + theVariable.getClass()); + } else { + if (err != 0) { + throw new ScilabInternalException("Storage of the variable '" + varname + "' (" + theVariable.getClass() + ") failed."); + } + } + return true; + } +} diff --git a/modules/javasci/src/java/org/scilab/modules/javasci/ScilabVariablesJavasci.java b/modules/javasci/src/java/org/scilab/modules/javasci/ScilabVariablesJavasci.java new file mode 100755 index 000000000..ed01e1df0 --- /dev/null +++ b/modules/javasci/src/java/org/scilab/modules/javasci/ScilabVariablesJavasci.java @@ -0,0 +1,90 @@ +/* + * Scilab (http://www.scilab.org/) - This file is part of Scilab + * Copyright (C) 2011 - DIGITEO - Calixte DENIZET + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt + * + */ + +package org.scilab.modules.javasci; + +import java.util.HashMap; +import java.util.Map; + +import org.scilab.modules.types.GetScilabVariable; +import org.scilab.modules.types.ScilabVariables; +import org.scilab.modules.types.ScilabVariablesHandler; +import org.scilab.modules.types.ScilabType; + +/** + * Class to get Scilab data from a Java call + */ +public final class ScilabVariablesJavasci implements ScilabVariablesHandler { + + private static final Map<Thread, ScilabType> map = new HashMap<Thread, ScilabType>(); + + private static int id = -1; + + /** + * Constructor + */ + private ScilabVariablesJavasci() { } + + /** + * {@inheritDoc} + */ + public void handle(ScilabType var) { + map.put(Thread.currentThread(), var); + } + + /** + * Get a Scilab variable with a given name + * @param name the variable name + * @param swapRowCol if true the returned data will be stored row by row + * @param byref if true the variable is passed by reference if it is possible + * @return the corresponding ScilabType object + */ + public static final ScilabType getScilabVariable(String name, boolean swapRowCol, boolean byref) { + if (id == -1) { + id = ScilabVariables.addScilabVariablesHandler(new ScilabVariablesJavasci()); + } + + if (name != null && !name.isEmpty()) { + if (byref) { + GetScilabVariable.getScilabVariableAsReference(name, id); + } else { + GetScilabVariable.getScilabVariable(name, swapRowCol ? 1 : 0, id); + } + Thread t = Thread.currentThread(); + ScilabType var = map.get(t); + map.remove(t); + + return var; + } + + return null; + } + + /** + * Get a Scilab variable with a given name + * @param name the variable name + * @param swapRowCol if true the returned data will be stored row by row + * @return the corresponding ScilabType object + */ + public static final ScilabType getScilabVariable(String name, boolean swapRowCol) { + return getScilabVariable(name, swapRowCol, false); + } + + /** + * Get a Scilab variable with a given name + * @param name the variable name + * @return the corresponding ScilabType object where the data are stored row by row + */ + public static final ScilabType getScilabVariable(String name) { + return getScilabVariable(name, true); + } +} diff --git a/modules/javasci/src/java/org/scilab/modules/javasci/package.html b/modules/javasci/src/java/org/scilab/modules/javasci/package.html new file mode 100755 index 000000000..d65eeb7f4 --- /dev/null +++ b/modules/javasci/src/java/org/scilab/modules/javasci/package.html @@ -0,0 +1,18 @@ +<html> +<body> + +This package provides a way to use and call Scilab engine from a Java +application or library.<br /> +<br /> +Example:<br /> +<code> +Scilab sci = new Scilab();<br /> +sci.open();<br /> +sci.exec("b = matrix(1:100,10,10)");<br /> +ScilabType b = sci.get("b");<br /> +b.getHeight(); // 10 <br /> +b.getWidth(); // 10<br /> +ScilabDouble b2 = (ScilabDouble)sci.get("b");<br /> +b2.equals(b); // true<br /> +</body> +</html>
\ No newline at end of file |