Groovy Science is a symbolic manipulation library for Groovy that is intended to be easy to "glue" to existing scientific Java (and Groovy) libraries.
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.
Groovy Science has been successfully built and used under Java 1.6.0 Update 7 and Groovy 1.5.1.
(to be written)
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.
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:
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:
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:
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 );
- Ross Angle [rokitna at hotmail]
Please contact the team members by e-mail.