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
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.
Groovy Science can take a lot of different directions. Here are some of the bigger sub-projects it might encompass:
- A toolkit of wrappers that make it easier to use SymbolicExpressions with a particular scientific Java library.
- A comprehensive or near-comprehensive palette of operators for use with a particular domain, such as expressions involving real numbers, assertions in set theory, or chemical reactions.
- A new, expressive way to generate SymbolicExpressions "literally" in code, such as by parsing a string or by using AST macros to interpret actual Groovy source code.
- A new way of visualizing SymbolicExpressions, such as a plain text, TeX, or HTML prettyprinter---or even a way to more easily create such prettyprinters.
In case you don't have a big idea like that or a lot of time to do it in, here are some of the more menial things that could still really help:
- Write a build script.
- Generate the Javadocs. (Javadoc comments are already maintained, but actual HTML hasn't been generated yet.)
- Write more test cases.
- Identify code that is in severe need of commenting. If you can, comment it yourself.