Forum: JRuby I'm in trouble to use JRuby by Java's ScriptEngine

8f29d55cb88c94a1b375646b3d297e5a?d=identicon&s=25 "クラモトケンスケ" <cike12001@g.nihon-u.ac.jp> (Guest)
on 2013-07-30 15:48
(Received via mailing list)
Hello, everyone.

I'm student and I'm studying multi-agent simulation.
So, I expect to use JRuby to describe each agent's rules.
But I am in trouble using JRuby by Java's ScriptEngine.

I am trying to use some ScriptEngines that are independent each other.
I expect them to have independent variables, independent methods and
independent constants.
However, I don't know the way to use them in such situation.
The code that follows to the text shows a sample for my problem.
The code has two ScriptEngines and I expect that they are independent
each other.
But unfortunately, it seems that they share variables and methods.
I use JRuby Ver.1.7.4 and JDK 1.7.0.
Would you please tell me the solution of the problem.
Thank you.

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/** Test code for ScriptEngine scope*/
public class TestScopeKai {
    public static void main(String[] argv){
        // Create First ScriptEngine
        ScriptEngineManager man=new ScriptEngineManager();
        ScriptEngine scriptEngine=man.getEngineByName("jruby");
        ScriptContext thisContext = scriptEngine.getContext();
        Bindings engineScope =
thisContext.getBindings(ScriptContext.ENGINE_SCOPE);
        String script=
                "CONSTANT=1 \n" + //set 1 to this constant
                "def get_constant \n" +
                "  print 'in 1st engine, constant value is
',CONSTANT,'\n' \n"+
                "  return CONSTANT \n"+
                "end\n";
        try{
            scriptEngine.eval(script, engineScope);
            scriptEngine.eval("get_constant", engineScope); // It
displays "in 1st engine, constant value is 1" and it's my expected
result.
        }catch(Exception e){
            e.printStackTrace();
        }
        // I hope that second ScriptEngine is independent from the
first one. So, I use new objects.
        ScriptEngineManager man2=new ScriptEngineManager();
        ScriptEngine scriptEngine2=man2.getEngineByName("jruby");
        ScriptContext thisContext2 = scriptEngine2.getContext();
        Bindings engineScope2 =
thisContext2.getBindings(ScriptContext.ENGINE_SCOPE);
        String script2=
                "CONSTANT=2 \n" +   //set 2 to this constant
                "def get_constant2 \n" +
                "  print 'in 2nd engine, constant value is
',CONSTANT,'\n' \n"+
                "  return CONSTANT \n"+
                "end\n";
        try{
            scriptEngine2.eval(script2, engineScope2); // Warning
"<script>:2 warning: already initialized constant CONSTANT" is appear.
            scriptEngine2.eval("get_constant2", engineScope2); // It
displays "in 2nd engine, constant value is 2". Although the result
itself is OK, but the constant has been overwritten.
        }catch(Exception e){
            e.printStackTrace();
        }
        // Use the first ScriptEngine again.
        try{
            scriptEngine.eval("get_constant", engineScope); // It
displays "in 1st engine, constant value is 2". I expected "1", but it
returns "2".
            scriptEngine.eval("get_constant2", engineScope); // It
feels strange that the first engine can call the method that belongs
to the second one.
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

<<<Result of the Code>>>
in 1st engine, constant value is 1
<script>:2 warning: already initialized constant CONSTANT
in 2nd engine, constant value is 2
in 1st engine, constant value is 2
in 2nd engine, constant value is 2

--
////////////////////////////////////////////////////////////////
Kensuke KURAMOTO
Ph.D. Student
Furuichi-lab
Graduate School of Mathematical Information Engineering,
Nihon University
E-mail: cike12001@g.nihon-u.ac.jp
TEL    : +81-47-474-2663
////////////////////////////////////////////////////////////////
13313ac2ec7ba7c43b1b952db034ff3b?d=identicon&s=25 Thomas E Enebo (Guest)
on 2013-07-30 15:55
(Received via mailing list)
Try adding the following line before you acquire any scripting engines:

System.setProperty("org.jruby.embed.localcontext.scope",
"singlethread");

Check out our embedding documentation for more options:

https://github.com/jruby/jruby/wiki/RedBridge


This documentation explains our internal embedding API (which is very
nice if you do not need javax.scripting.*) but it also has JSR 223 api
info in it.
8f29d55cb88c94a1b375646b3d297e5a?d=identicon&s=25 "クラモトケンスケ" <cike12001@g.nihon-u.ac.jp> (Guest)
on 2013-07-30 16:04
(Received via mailing list)
Hi,  Thomas

Thank you for your great and quick reply !
I've tried your code and it solved my problem !!

Great thanks, Domo Arigato !!


2013/7/30 Thomas E Enebo <tom.enebo@gmail.com>:
>
>>
>> each other.
>> /** Test code for ScriptEngine scope*/
>>                 "def get_constant \n" +
>>             e.printStackTrace();
>>                 "def get_constant2 \n" +
>>         }catch(Exception e){
>>         }catch(Exception e){
>> in 2nd engine, constant value is 2
>> ////////////////////////////////////////////////////////////////
> --
> blog: http://blog.enebo.com       twitter: tom_enebo
> mail: tom.enebo@gmail.com



--
////////////////////////////////////////////////////////////////
$B!!F|K\Bg3X@8;:9)3X8&5f2J(B
$B!!!!?tM}>pJs9)3X@l96(B
$B!!!!!!8E;T8&5f<<(B $BGn;N8e4|2]Dx(B2$BG/(B
$B!!!!!!!!!!ARK\!!7r2p(B
    E-mail$B!'(B cike12001@g.nihon-u.ac.jp
    TEL   $B!'(B 047-474-2663
////////////////////////////////////////////////////////////////
13313ac2ec7ba7c43b1b952db034ff3b?d=identicon&s=25 Thomas E Enebo (Guest)
on 2013-07-30 16:17
(Received via mailing list)
$B$I$&$$$?$7$^$7$F!#(B

-Tom
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.