Interactive Use of SmartArrays

Several technologies exist that enable you to experiment with SmartArrays operations in an interactive environment.

The SmartArrays Interactive Compiler

The SmartArrays Interactive Compiler (SAIC) is a Java application that allows you to experiment with Java statements that use SmartArrays arrays and SmData toolkit objects.  Because of the similarities in syntax, the SAIC is also useful for C# and C++ developers.  It is included in all versions of the SmartArrays SDK and requires a Java runtime environment to work.  The classes for the SAIC are included in SmartArraysV4.jar.

SAIC Setup

Under Windows, the SAIC is launched by the batch file saic.bat, found in the bin subdirectory of the SmartArrays installation directory.  As provided, this batch file assumes the presence of two environment variables:

The batch file launches the SAIC with the following command line:

%JAVA_HOME%\bin\java -classpath %SMARTARRAYS_HOME%\bin\dynacode;%SMARTARRAYS_HOME%\lib\SmartArraysV4.jar;%JAVA_HOME%\jre\lib\rt.jar;%JAVA_HOME%\lib\tools.jar com.smartarrays.saic.SAICViewer

You may need to adjust the paths in this command to work with your own directory layout.  The directory bin\dynacode is the location that the SAIC uses to hold the dynamically generated Java classes created from user inputs.

Using SAIC

The SAIC session is shown a window containing an Input pane and Output pane.  Using it is a simple matter of typing statements into the input pane and clicking the Execute button to run them.  Output produced by the SmArray methods show() and showDebug() is shown in the output window.

Here is an example of the SAIC in use, showing user inputs and the output produced.  The Execute... button runs all the statements at once.  The Step button steps through the statements one at a time, which is useful for seeing which output comes from each statement.

If your statements cannot be compiled because of an error, the SAIC shows an error message, like that shown below, and attempts to highlight the faulty statement.  If the statements compile successfully but produce an exception when executed, the exception is trapped and displayed in the output window.

How SAIC Works

When you click Execute button, the SAIC creates a Java source file containing a class SAICImpl with an execute() method whose body consists of the statements you entered in the input window.  The execute() method redirects output produced by the SmArray show() and showDebug() methods into an HTML generator that writes the formatted output in the SAIC's Output pane.  After generating the class, the SAIC compiles it with the Java compiler and runs it to show the output. 

 

The following is the text of the generated class corresponding to the screen capture above


package com.smartarrays.saic; import com.smartarrays.*;
import com.smartarrays.toolkit.smdata.*;
import com.smartarrays.toolkit.common.*;
import java.util.*;

public class SAICImpl implements ISAIC {
  public SAICImpl(){}

  public void execute() throws Exception {
    SAIC.SAICShow show = new SAIC.SAICShow();
    SmArray.setShowHandler(show);
    try {
      SmArray.empty();
      SmArray x = SmArray.scalar(100).reshapeBy(3,3).roll();
      x.show();
      SmArray xinv = x.matrixInverse().show();
      x.inner( Sm.plus, Sm.times, xinv ).round().show();
    }
    catch (SmException e) {
      throw new SAICExecutionException(e.getMessage());
    }
  }
};

Single-stepping through the code is simulated by creating the dynamic class using a subset of the lines in the input window.  Each "step" actually runs all the lines of code prior to the current step.

Saving Input Statements

You can save the contents of the input pane as a text file using File>Save As menu.  You can then use the File>Open menu to read saved statements back into the input pane.  

Limitations of the SAIC 

The SAIC is not an interpreter, so its functionality as an interactive platform is limited.  It does not know how to display other than what is produced by the SmArray class or the SmData toolkit classes.  For true interactive use, you may prefer to use an interpreted language such as Python that calls SmartArrays (see below).

(which Java classes are imported; not a Java interpreter. 

Customizing SAIC

Using SmartArrays with Jython

Click here for instructions.

Using SmartArrays with Iron Python

Iron Python is an implementation of the Python language for the .NET environment. It can be used with .Net libraries, including SmartArrays for .NET. At this writing (July 2006), the Version 1.0 Release Candidate version of Iron Python is available for download at http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython.

After Iron Python is installed, the easiest way to add SmartArrays is to copy the .NET wrapper assembly SmartArraysV4.dll to the same directory as ipy.exe (the Python interpreter).

Sample Iron Python Session

>>> import clr

>>> clr.AddReference( "SmartArraysV4" )

>>> from SmartArrays import *

>>> from SmartArrays.SmData import *

>>> x = SmArray.vector( "Alice", "Barbara", "Claire" )

>>> x.ds()

'S[3]"Alice" "Barbara" "Claire"\n'

>>> mytable = SmTable( "mytable" )

>>> mytable.addColumn( "name", x )

>>> mytable.show()

name
Alice
Barbara
Claire