program context in GroovyShell

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

program context in GroovyShell

Sam Huang
Hi,
I use GroovyShell for running embedded Java code inside my program.

GroovyShell shell = new GroovyShell();

res = shell.evaluate("import net.math.Combination;");
               
res = shell.evaluate("Combination c = new Combination(5, 3);");

And it fails at the second statement, and here is the exception:
groovy.lang.MissingPropertyException: No such property: c for class: Script2

How to I correct it? Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: program context in GroovyShell

Guillaume Laforge
Administrator
On 10/12/05, Sam Hwang <[hidden email]> wrote:

> Hi,
> I use GroovyShell for running embedded Java code inside my program.
>
> GroovyShell shell = new GroovyShell();
>
> res = shell.evaluate("import net.math.Combination;");
>
> res = shell.evaluate("Combination c = new Combination(5, 3);");
>
> And it fails at the second statement, and here is the exception:
> groovy.lang.MissingPropertyException: No such property: c for class: Script2
>
> How to I correct it? Thanks!

The error message is not very clear unfortunately, but I suspect
you'll have to evaluate the whols combined script rather than evaluate
each line after the other. You have to evaluate everythin in a row.

GroovyShell shell = new GroovyShell()
res = shell.evaluate("import net.math.Combination \n Combination c =
new Combination(5, 3)");

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: program context in GroovyShell

Jochen Theodorou
In reply to this post by Sam Huang
Sam Hwang wrote:

> Hi,
> I use GroovyShell for running embedded Java code inside my program.
>
> GroovyShell shell = new GroovyShell();
>
> res = shell.evaluate("import net.math.Combination;");
>
> res = shell.evaluate("Combination c = new Combination(5, 3);");
>
> And it fails at the second statement, and here is the exception:
> groovy.lang.MissingPropertyException: No such property: c for class: Script2
>
> How to I correct it? Thanks!

you executed two independent scripts. Evaluate creates a new enviroment
every time. If you want the imports you have to put them into the  next
String too.

res = shell.evaluate("import net.math.Combination;\nCombination c = new
Combination(5, 3);");

bye blackdrag
Reply | Threaded
Open this post in threaded view
|

Re: program context in GroovyShell

Sam Huang
Hi,
Honestly, the input statements are not executed sequently. They are
user input statements acutally, so I cannot expect what are inside. In
my attempt, my code should like this:

String stmt;
if(the current line is a statement){
stmt = input line;
shell.evaluate(stmt);
}
// some other process
// and continue read on the file
if(the current line is another statement){
stmt = input line;
shell.evaluate(stmt);
}

So I cannot combine statements into a single line at design time. I
wonder if there is a Jexl-like context to hold an conversation for
GroovyShell. I tried Jexl but it seems to be limited functional to
reach my expection, at least it dosen't support importing Java
classes. What I want is to use Java packages inside my program
dynamically, I am going to try some different solutions if possible,
anyone got better suggestions? Thanks!


On 12/10/05, Jochen Theodorou <[hidden email]> wrote:

> Sam Hwang wrote:
> > Hi,
> > I use GroovyShell for running embedded Java code inside my program.
> >
> > GroovyShell shell = new GroovyShell();
> >
> > res = shell.evaluate("import net.math.Combination;");
> >
> > res = shell.evaluate("Combination c = new Combination(5, 3);");
> >
> > And it fails at the second statement, and here is the exception:
> > groovy.lang.MissingPropertyException: No such property: c for class: Script2
> >
> > How to I correct it? Thanks!
>
> you executed two independent scripts. Evaluate creates a new enviroment
> every time. If you want the imports you have to put them into the  next
> String too.
>
> res = shell.evaluate("import net.math.Combination;\nCombination c = new
> Combination(5, 3);");
>
> bye blackdrag
>
Reply | Threaded
Open this post in threaded view
|

Re: program context in GroovyShell

Sam Huang
In reply to this post by Guillaume Laforge
Hi,
Honestly, the input statements are not executed sequently. They are
user input statements acutally, so I cannot expect what are inside. In
my attempt, my code should like this:

String stmt;
if(the current line is a statement){
stmt = input line;
shell.evaluate(stmt);
}
// some other process
// and continue read on the file
if(the current line is another statement){
stmt = input line;
shell.evaluate(stmt);
}

So I cannot combine statements into a single line at design time. I
wonder if there is a Jexl-like context to hold an conversation for
GroovyShell. I tried Jexl but it seems to be limited functional to
reach my expection, at least it dosen't support importing Java
classes. What I want is to use Java packages inside my program
dynamically, I am going to try some different solutions if possible,
anyone got better suggestions? Thanks!


On 12/10/05, Guillaume Laforge <[hidden email]> wrote:

> On 10/12/05, Sam Hwang <[hidden email]> wrote:
> > Hi,
> > I use GroovyShell for running embedded Java code inside my program.
> >
> > GroovyShell shell = new GroovyShell();
> >
> > res = shell.evaluate("import net.math.Combination;");
> >
> > res = shell.evaluate("Combination c = new Combination(5, 3);");
> >
> > And it fails at the second statement, and here is the exception:
> > groovy.lang.MissingPropertyException: No such property: c for class: Script2
> >
> > How to I correct it? Thanks!
>
> The error message is not very clear unfortunately, but I suspect
> you'll have to evaluate the whols combined script rather than evaluate
> each line after the other. You have to evaluate everythin in a row.
>
> GroovyShell shell = new GroovyShell()
> res = shell.evaluate("import net.math.Combination \n Combination c =
> new Combination(5, 3)");
>
> --
> Guillaume Laforge
> Groovy Project Manager
> http://glaforge.free.fr/blog/groovy
>
Reply | Threaded
Open this post in threaded view
|

Re: program context in GroovyShell

Guillaume Laforge
Administrator
On 13/12/05, Sam Hwang <[hidden email]> wrote:
> Hi,
> Honestly, the input statements are not executed sequently. They are
> user input statements acutally, so I cannot expect what are inside. In
> my attempt, my code should like this:

But your users do know when they want to execute something? a set of statements?
Why not providing them a way to actually execute a bunch of statements
you'll have gathered?
The Groovy shell uses this technique and requires a "go" or "execute"
command to execute things.
Couldn't that be an approach here?

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy