Experiments with BeanShell Scripting language and GeoGebra

Version: 01.12.07

26.03.08: A similar experiment with JavaScript is here.


Documentation and updates for BeanShell, © Patrick Niemeyer,can be found at http://www.beanshell.org/, BeanShell is distributed under the terms of the LGPL

Documentation and updates for Geogebra, ©Markus Hohenwarter at www.geogebra.org, GeoGebra is distributed under the terms of the GPL v2


My humble contribution is to make some glue to make these two wonderful programs cooperate,
which makes it possible to control GeoGebra and manipulate GeoGebra objects from an interactive scripting environment.

I have made two programs:

The application has to be downloaded and run locally, but a rough demonstration is here. (Bugs, just for ĺ quick test.)

The applet ScriptRunner is demonstrated and documented here.

This picture also gives a hint about how the local application looks:

To try out the local application:

(Might be neccessary to adjust the path to java.exe in sricpt.bat...)

(Source code: script.src.zip )

Help and information on the BeanShell script language:

Warning:

This is for the more advanced GeoGebra users, who have some experience with scripting and programming. Some knowledge about Java is also needed.

Personally I think gluing together BeanShell and GeoGebra is a good illustration of what can be done, when programmers, like the GeoGebra and BeanShell developers,


Some help and examples:

Syntax:

BeanShell uses java syntax, but loose typing is accepted.

	a=5; 				//define variable, loose typing
	double a=5.0d;			//strict typing
	sum(a,b) {return a+b;};		//define function, loose typing
	sum(2,3);			//use function
	sum("a","b");			//again ...
	double sum(double a,double b){...}	//strict typing
	frame=new JFrame("tittel"); 	//call java-library and make  objects
	frame.setVisible(true);		//The whole JDK can be used, one of the largest frameworks available...
	btn=new JButton("Press");
	f.add(btn);
	c (){				//define a BS class
	   i=5;				//(can also define a Java class with normal, strict  Java syntax,
	   s="I am c";			// BS is nearly completely Java compatible and "eats" most Java source code!)
	   method() {
	      print(s);
	   }
	   return this;	
	}
	o=c();				//make object from class
	print(o.i);			//use class
	o.m();
	for(i=0;i<5;i++){		//loop...
	   print(i);
	}
	if(i>0) {...}else{...}		// nothing new...
	//switch	is expanded to deal with objects! (See documentation BeanShell manual.)

Commands:

The BeanShell Interpreter has the following internal commands:

Commands for controlling GeoGebra:

I have myself added a ggb object reference for communication with GeoGebra,
in order to do the usual things: (See GeoGebra's JavaScript/Java Applet API.)

I have also added some other commands:

(source(), sourceGgb([fn]), editor() can also be run from the buttons in the graphical user interface.)

BeanShell script examples:

Put BeanShell scripts in files, and run them with:

source(String filename);	
or "Run as BS-file" in console (or "Eval as BS" in editor)
Set up a construction in Ggb:
	ggb.evalCommand("S=(3,3)");
	ggb.evalCommand("r=5");
	ggb.evalCommand("C=Circle[S,r]");
Make five point in Ggb:
	for(i=0;i<5;i++) {
	   obj="P_"+i; 				//P_1, P_2, ...
	   val="("+i+","+i*i+")"; 		// point (i,i*i)
	   ggb.evalCommand(obj+"="+val);	// Make point in ggb
	};
Animate a variable:
	evalCmd("t=0");
	ggb.setVisible("t",true);
	for(t=0;t<6.28;t+=0.1){
	   evalCmd("t="+t);
	   wait(200);
	};
A more involved example, start of a user interface:
	b=new JButton("ok");
	cp=scriptapplet.getContentPane();
	cp.add("South",b);
	scriptapplet.validate();
	al=new ActionListener() {
	      actionPerformed() {
	         print("Button pushed!");
	      };
	   };
	b.addActionListener(al);
Ggb commands:
f(x)=sin(x)
g(x)=cos(x)
a=5
P=(3,2)
...
can also be put in txt-files and run with:

I hope some of the nice people in the GeoGebra community will find what I have done useful.

I am open for suggestions.

H-P Ulven 20.11.07