You are a guest. Restricted access. Read more.
SCaVis manual

Using BeanShell syntax

You can write your program using BenShell scripting language and run it using the “run” button. If your code contains the “import” statement (usually always does for BeanShell scripts), the program will be recognized as a BeanShell code and it will execute it. Look at the output using the “Output” button. If these will be no “import” statement, the program will interpret your code as jMathLab.

Learning BeanShell

BeanShell is a simple scripting language for the Java platform. You can learn about BeanShell using jLearn interactive tutorial (Java is required for interactive examples)

Outputs from BeanShell programs

Text outputs can be viewed by using the button Output. If you use the BeanShell command “print(object)” or “println(object)”, the output will be printed in the text filed which can be opened with the “Output” button. Note that the print message simply calls object.toString() method.

The same Output button is used to see error messages. If you see that your program does not run, click on the “Output” to see what is wrong.

Graphics from BeanShell programs

You can view the graphs by pressing the Graph button. You can zoom in into the graphs etc. Make a screenshoot to generate an image file.

Getting started. Calculations

Use the standard BeanShell statements to evaluate Java expression. To print the outputs, use the print statement. Here is an example:

import java.lang.*;
print(30*30);

Press “Run” and then “Output”. You will see the answer 900. Note the semicolon after each line.

In principle, the “import” statement is not needed in this example. It has the purpose to tell to AWork that this is a BeanShell code and it must be executed as a BeanShell code when you press the “run” button.

The “print(object)” method works for any object which has “toString()” method. One can access any Java class from java.lang.*, including the “Math” library. BeanShell imports java.lang and java.utils. automatically, thus you do not need to import such packages. Here is an example:

import jhplot.*;
print(Math.sqrt(2));

which will print the answer in the output window. Here we import external graphic/numeric library which will be needed for next examples.

It is more convenient to import all Math functions as:

import java.lang.Math.*;
print(sqrt(2));

One can also use the complete jhplot package with the numerical libraries. For example, let us create a 1D array and fill with with random numbers:

import jhplot.*;
p= new P0D("test");
a=p.randomNormal(100,0,1); // 100 Normally-distributed numbers with mu= and sigma=1
print(a); // print random numbers.

One can also evaluate functions using jhplot package:

import jhplot.*;
p= new F1D("x*x",0,10);
print(p.eval(0.4)); // evaluate at x=0.4

Read more about functions in the Section functions (but you should use the BeanShell syntax instead of Python).

Graphical canvaces

Unlike the full version of SCaVis which supports many Java-based canvaces Java-based canvaces, the graphical representation of functions, array, histograms in AWork is based on the HChart canvas Currently, no support for 3D canvaces exits. Also, you cannot plot several drawing pads as it can be usually done using the Java version of the HChart canvas (and this does not make too much sense anyway due to small sizes of screens of Android devices).

The examples for the android platform can be run on the Java platform as well. Only replace and.awt library by java.awt (for colors, fonts etc). On the Java platform, you should also call “visible()” for HChart canvas to bring up swing frame with the image.

Plotting functions

This is a small example to evaluate a function and show it on the canvas. Note that colors are taken from the “and.awt” package.

import jhplot.*;
import and.awt.*;
p= new F1D("x*x",0,10);
g= new F1D("x*x*cos(x)",-10,10);
print(p.eval(0.4));
c=new HChart();
p.setColor(Color.red);
p.setPenWidth(4);
c.add(p);
c.add(g);
c.update();

Save this code in a file “example.bsh”, load it in the AWork editor and press “run”. You will see two functions shown on top of each other. You can zoom in and zoom out such functions. Make a snapshot of the screen to get a image file.

The output on an android tablet screen is:

Showing data points

This example shows how to show a scatter XY plot of data points. We make 2 P1D containers and fill randomly points using a Gaussian distribution from java.util package. The we print one container on the screen.

import jhplot.*;
import java.util.Random;
import and.awt.*;
 
p= new P1D("test1");
g= new P1D("test2");
r=new Random();
for (i=0; i<100; i++) p.add(r.nextGaussian(),r.nextGaussian());
for (i=0; i<1000; i++) g.add(0.5*r.nextGaussian(),r.nextGaussian());
c=new HChart();
p.setColor(Color.red);
p.setSymbolSize(16);
p.setSymbol(5);
g.setColor(Color.blue);
c.add(p);
c.add(g);
c.update();
print(p);

You may notice that we used the “print(p)”. It print the object (P1D) to the console which can be opened by pressing the button “Output”.

Showing data points and functions

In this example we draw data points with statistical errors and a function. We use “add” method to add these points.

import jhplot.*;
import java.util.Random;
import java.awt.*;
 
p= new P1D("test1");
g= new P1D("test2");
c=new HChart("Data points and function");
p.setColor(Color.red);
p.setSymbolSize(16);
p.setSymbol(5);
p.add(10,20,4,4,5,5);
p.add(20,30,4,4,6,6);
 
g.setColor(Color.blue);
g.setSymbolSize(10);
g.setSymbol(0);
g.add(9,10,4,4,5,5);
g.add(25,14,3,3,5,6);
g.add(32,40,4,4,6,15);
 
c.add(p);
c.add(g);
f=new F1D("x+5*sin(x)",0,40);
f.setColor(Color.green);
f.setPenWidth(4);
c.add(f);
c.update();

The output of this example is overlaid data points and the function:

Showing histogram

This example shows how to draw a histogram:

import jhplot.*;
import java.util.Random;
import and.awt.*;
 
p=new H1D("test1",100,-1,1);
g=new H1D("test2",50,-1,1);
 
p.setFillColor(Color.red);
p.setColor(Color.green);
g.setColor(Color.blue);
 
r=new Random();
for (i=0; i<1000; i++) p.fill(r.nextGaussian());
for (i=0; i<100; i++) g.fill(0.5+0.3*r.nextGaussian());
 
print (p.getColor().toString());
 
c=new HChart();
c.add(p);
c.add(g);
c.update();
print(p);

The output on the tablet screen is:

Pie charts

Here is an example of the Pie chart:

import jhplot.*;
 
c1 = new HChart("Canvas");
c1.setChartPie();
c1.setName("Pie example");
c1.valuePie("Hamburg",1.0);
c1.valuePie("London",2.0);
c1.valuePie("Paris",1.0);
c1.valuePie("Bern",1.0);
c1.update();

The output on an Android tablet screen is:

Line charts

Similarly, one can draw the lines:

import jhplot.*;
 
c1 = new HChart("Canvas");
c1.setChartLine(12); // set line width here
c1.setName("Line example");
c1.valueLine(1.0, "First", "category1");
c1.valueLine(2.0, "First", "category2");
c1.valueLine(10.0, "First", "category3");
c1.valueLine(8.0, "First", "category3");
 
c1.valueLine(3.0, "Second", "category1");
c1.valueLine(2.8, "Second", "category2");
c1.valueLine(4.0, "Second", "category3");
c1.valueLine(1.0, "Second", "category3");
c1.update();

The output on an Android device is:

Bar charts

The bar chart style is shown here:

import jhplot.*;
 
c1 = new HChart("Canvas");
c1.setChartBar();
c1.valueBar(1.0, "First", "category1");
c1.valueBar(4.0, "Second", "category2");
c1.valueBar(3.0, "Third",   "category3");
c1.update();

The output on android device is:

Input and output

The Android version supports the same input output Java classes as the standard Java application. Read Section input_output about Java-implemented classes such as HFile (Java compressed serialization), HPlotXML (XML serialization), the class “Serialized()”, and PFile class, a platform-neutral Google protobuf (Protocol Buffers) IO. Also all Java-based databases are supported.

In this example we show how to create a several objects, write them into a compressed serialized file and then to restore them for plotting:

import jhplot.*;
import jhplot.io.*;
import java.util.Random;
import and.awt.*;
 
p= new P1D("test1");
g= new P1D("test2");
r=new Random();
for (i=0; i<100; i++) p.add(r.nextGaussian(),r.nextGaussian());
for (i=0; i<1000; i++) g.add(0.5*r.nextGaussian(),r.nextGaussian());
p.setColor(Color.red);
p.setSymbolSize(16);
p.setSymbol(5);
g.setColor(Color.blue);
 
f= new HFile("/mnt/sdcard/test.ser","w"); // write the data
f.write(p);
f.write(g);
f.close();
f= new HFile("/mnt/sdcard/test.ser"); // read the data
p=f.read();
g=f.read();
f.close();
 
c=new HChart(); # plot data
c.add(p);
c.add(g);
c.update();

Saving this into a file test.bsh and executing it, you will see a scatter plot with the data. You may notice that even graphical attributes of the objects are restored correctly. Be careful with the path to the file - if it will be wrong then you will not be able to write objects to such file. In this case we use a sdcard“ to store the data.

As usual, you cannot serialize graphical canvaces (like HChart in this example). But you can write essentially any Java class into a file and restore it later. One can also make collection of objects and write them at once using the “Serialized” class as discussed in Section input_output.

Symbolic calculations

You can use Octave-scripting language directly in Awork as explained in Symbolic calculations section. Yet, you can call symbolic calculations from the BeanShell.

You can use AWork for symbolic calculations in exactly the same way as it is described in the section symbolic calculations. You can integrate, differentiate, simplify, solve equations. Remember that you should use the BeanShell syntax.

import jhplot.math.*;
j=new Symbolic("jasymca");
s="syms x; trigrat(sin(x)^2+cos(x)^2)";
print(j.eval(s));

In the above program we used the Jasymca engine to evaluate the expression written in the MatLab/Octave mode. Read more about the jasymca engine here. It should be noted that plotting features are not available. The manual of Jasymca is available here

Similarly, you can use other symbolic engines, such as “jscl”. For example, let us integrate the function: <m> cos(x)+x^2 </m>

You BeanShell code will look as:

import jhplot.math.*;
j=new Symbolic("jscl");
s1="integral(cos(x)+x^2,x)";
print(j.eval(s));

The answer is

(x^3+3*sin(x))/3

Advanced topics

Not every package of SCaVis is support on Android. The following major core packages are available:

You should read the following section of this manual:

Credits

The AWork project is based on the two open-source packages: the 920 TextEditor and AFreeChart.

Sergei Chekanov 2012/05/26 21:23

man/android/beanshell.txt · Last modified: 2013/05/31 16:11 (external edit)
CC Attribution-Share Alike 3.0 Unported
Powered by PHP Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 Valid HTML5