Forum: JRuby Howto call static java method from Jruby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Diamantini Maurice (Guest)
on 2009-01-17 19:24
(Received via mailing list)
Bonjour à tous,

Choco is a bsd licenced java constraint programming framework.
I use it from some (litle) project and for (french) teaching.
The course is finished, but I consider to switch from java
to JRuby next year. So I have to convert all java code to JRuby
and see if it is possible (all java syntax, performances for callback
methods, ...).

But I've currently problem while calling static method
from the toplevel class "choco.Choco".
Il also like to import all static methods as in java:
     import static choco.Choco.*;


I'm trying to port the following java code to jruby

import choco.cp.model.CPModel;
import choco.kernel.model.Model;
import choco.kernel.model.variables.integer.IntegerVariable;
...

// import static choco.Choco.*;
import choco.Choco;

public class Mini {
     public static void main(String[] args) {

         Model m = new CPModel();

         // IntegerVariable v1 = makeIntVar("v1", 1, 5, "cp:enum");
         IntegerVariable v1 = Choco.makeIntVar("v1", 1, 5,"cp:enum");
         m.addVariable(v1);

         ...
     }
}

I write the following JRuby code:

#!/usr/bin/env jruby

require ENV['CHOCO_TEST_JAR_NEW']
require "java"

include_class "choco.cp.model.CPModel"
include_class "choco.kernel.model.Model"
include_class "choco.kernel.model.variables.integer.IntegerVariable"
...
include_class "choco.Choco"
import choco.Choco
# How to import static methodss from the choco.Choco class?

class Mini
     def initialize args
         model = CPModel.new;

         # v1 = Choco.makeIntVar "v1", 1, 5, "cp:enum"
         # # v1 = Choco::makeIntVar "v1", 1, 5, "cp:enum"
         # # v2 = Choco::makeIntVar "v2", 1, 5, "cp:bound"
         v1 = Choco::makeIntVar "v1", 1, 5, "cp:enum"
         model.addVariable v1
         ...
     end
end

But the following error occurs:

/../jruby/lib/ruby/site_ruby/1.8/builtin/javasupport/core_ext/
object.rb:116:
      in `initialize': no makeIntVar with arguments matching
      [class org.jruby.RubyString, class org.jruby.RubyFixnum,
         class org.jruby.RubyFixnum, class org.jruby.RubyString]
      on object Java::Choco::Choco (NameError)
  from ./Mini.new_api_bad.rb:169

Thank you for your work on Jruby!
-- Maurice


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Marcin M. (Guest)
on 2009-01-17 20:17
(Received via mailing list)
Hi,

include Java
require "choco-2.0.1.0-beta.jar"
CPModel = Java::choco.cp.model.CPModel
Choco = Java::choco.Choco
model = CPModel.new

Choco.makIntVar takes a String, two ints and an array of Strings

Indeed, Java Integration has some problems matching:
Choco.make_int_var("v2", 1, 5, ["cp:bound"])

so, you'll have to convert it to java String array explicitly:
Choco.make_int_var("v2", 1, 5, ["cp:bound"].to_java(:String))

Marcin.


Diamantini Maurice pisze:
> from the toplevel class "choco.Choco".
>
>         m.addVariable(v1);
> require "java"
>     def initialize args
>
> Thank you for your work on Jruby!
> -- Maurice
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Maurice Diamantini (Guest)
on 2009-01-22 20:29
(Received via mailing list)
2009/1/17 Marcin Miel?y?ski <removed_email_address@domain.invalid>:
> Hi,
>
> include Java
> require "choco-2.0.1.0-beta.jar"
> CPModel = Java::choco.cp.model.CPModel
> Choco = Java::choco.Choco
> model = CPModel.new
>
> Choco.makIntVar takes a String, two ints and an array of Strings

Thanks you very much Marcin!

Yes I didn't see that make_int_var() expects a variable list of
arguments,
so the method receive an Array (never use this feature yet in java)!


> Indeed, Java Integration has some problems matching:
> Choco.make_int_var("v2", 1, 5, ["cp:bound"])

I'd even expect (for a futur jruby release ;-):
     Choco.make_int_var("v2", 1, 5, "cp:bound", ...

> so, you'll have to convert it to java String array explicitly:
> Choco.make_int_var("v2", 1, 5, ["cp:bound"].to_java(:String))

That work, thank you very much !
But the to_java(:String) doesn't work for not java class,
I had then (solved) problem and here is som result:

    java code:
       m.addVariables(v1, v2); // variable number of args

    jruby code which doesn't work:

       # as the previous exemple "xxx.to_java(:String)" worked:
       m.addVariables [v1, v2].to_java(:IntegerVariable) # BAD !
       m.addVariables [v1, v2].to_java("IntegerVariable") # BAD
       m.addVariables [v1, v2].to_java(choco.xxxxx.IntegerVariable)  #
BAD

    jruby code which works:

       m.addVariables [v1, v2].to_java("choco.xxxxx.IntegerVariable")
# OK
       m.addVariables [v1,
v2].to_java(Java::choco.xxxxx.IntegerVariable)  # OK
       # if previous import is done
       m.addVariables [v1, v2].to_java(IntegerVariable)  # OK !!

Another question
I'm trying to
see if all java feature are available in jruby, then to show that its
easy to convert java code from java to jruby (it is simpler and less
verbose) But choco provide a top class with many static methods for
building model variables and various model constraints (such eq, leq,
neq, scalar, plus (for an array of variable), ifThenElse, ..and also
higher level constraints...).

With java1.5 one can use these methods directly.

The question is:
- Is it possible to import static methods in Jruby as in java:
    import static choco.Choco.*;

- if no, is is expected in some future release or should we consider
  writing ruby methods?

- Could we make a simple ruby method that dynamicaly define
  à jruby method for all static java method in a class
  (e.g; "choco.Choco class") ?

Think you very much for you help.

-- Maurice Diamantini

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
This topic is locked and can not be replied to.