Module Overview
Groovy Science is a symbolic manipulation library for Groovy that is intended to be easy to "glue" to existing scientific Java (and Groovy) libraries.
Installing
There are no archive releases of Groovy Science yet, but the current source can be found at http://svn.codehaus.org/groovy-contrib/science/. To use it, you can do any of the following things:
- Build it in its own project, and have your project reference that project.
- Copy the source into your own project.
- Make a .jar file yourself, and use that.
Pre-requisites
Groovy Science has been successfully built and used under Java 1.6.0 Update 7 and Groovy 1.5.1.
Documentation
(to be written)
Building SymbolicExpressions
The centerpiece of the library is the SymbolicExpression class. A SymbolicExpression is a representation of the "application" of an operator object to a list of other SymbolicExpressions. This makes for a simple tree structure, and it is not unlike the way Lisp code is represented in s-expressions.
| Code Block |
|---|
import org.codehaus.groovy.science.SymbolicExpression
import static org.codehaus.groovy.science.SymbolicExpression.expr
Object plusOp = new Object();
Object leafOp = new Object();
SymbolicExpression leaf = expr( leafOp );
SymbolicExpression myExpression = expr( plusOp, leaf, leaf );
assert myExpression.operator == plusOp;
assert myExpression.argumentList == [ leaf, leaf ];
assert myExpression.argumentList[ 0 ].operator == leafOp;
assert myExpression.argumentList[ 0 ].argumentList == [];
|
The SymbolicExpression class overloads almost all of the operators that can be overloaded in Groovy. So, instead of building all expressions using expr, you can sometimes take advantage of Groovy's own syntax:
| Code Block |
|---|
import org.codehaus.groovy.science.SymbolicExpression
import static org.codehaus.groovy.science.SymbolicExpression.expr
import org.codehaus.groovy.science.OverloadableOperators
Object leafOp = new Object();
SymbolicExpression leaf = expr( leafOp );
assert leaf + leaf == expr( OverloadableOperators.Plus, leaf, leaf );
assert leaf[ leaf ] == expr( OverloadableOperators.GetAt, leaf, leaf );
|
If you wanted to represent an expression like "1 + 1", you could do so as follows:
| Code Block |
|---|
import org.codehaus.groovy.science.SymbolicExpression
import static org.codehaus.groovy.science.SymbolicExpression.expr
SymbolicExpression one = expr( 1 );
SymbolicExpression onePlusOne = one + one;
|
If you do that, though, you might run the risk of confusing your constants with your other operators. To help keep your constants clearly identified, you can use the ConstantOperator class:
| Code Block |
|---|
import org.codehaus.groovy.science.SymbolicExpression
import static org.codehaus.groovy.science.SymbolicExpression.expr
import org.codehaus.groovy.science.ConstantOperator
import static org.codehaus.groovy.science.ConstantOperator.*
// for con, unCon, and isCon
SymbolicExpression one = con( 1 );
SymbolicExpression onePlusOne = one + one;
assert one == expr( new ConstantOperator( 1 ) );
assert one.operator.value == 1;
assert unCon( one ) == 1;
assert isCon( one );
assert !isCon( onePlusOne );
|
Developers
- Ross Angle [rokitna at hotmail]
Source Control
http://svn.codehaus.org/groovy-contrib/science/
Contributing
Please contact the team members by e-mail.
...