Class: Nashorn
- Inherits:
-
JRubyFX::Application
- Object
- JRubyFX::Application
- Nashorn
- Defined in:
- lib/nashorn/nashorn.rb
Overview
Instance Attribute Summary (collapse)
-
- (Object) browser
readonly
When view is true the following variables will be set.
-
- (Object) document
readonly
Returns the value of attribute document.
-
- (Object) engine
readonly
—————————————————————————————-.
-
- (Object) window
readonly
Returns the value of attribute window.
Instance Method Summary (collapse)
-
- (Object) assign(name, value)
—————————————————————————————-.
-
- (Object) build_vector(mdarray)
—————————————————————————————- Builds a Renjin vector from an MDArray.
-
- (Object) direct_eval(expression)
—————————————————————————————- Evaluates an expression but does not wrap the return in a RubySexp.
-
- (Object) eval(expression)
—————————————————————————————-.
-
- (Object) install__package(name)
—————————————————————————————-.
-
- (Object) library(package)
—————————————————————————————-.
-
- (Object) method_missing(symbol, *args)
—————————————————————————————-.
-
- (Object) parse(*args)
—————————————————————————————-.
-
- (Object) pull(name)
—————————————————————————————-.
- - (Object) start(stage)
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(symbol, *args)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/nashorn/nashorn.rb', line 83 def method_missing(symbol, *args) name = symbol.id2name if name =~ /(.*)=$/ super if args.length != 1 ret = assign($1,args[0]) else params = parse(*args) # p "#{name}(#{params})" # ret = eval("#{name}(#{params})") begin ret = @engine.invokeFunction(name, params) rescue Java::JavaLang::NoSuchMethodException => e p e. raise NoMethodError.new("Method #{name} is not defined.", name, params) end end ret end |
Instance Attribute Details
- (Object) browser (readonly)
When view is true the following variables will be set
39 40 41 |
# File 'lib/nashorn/nashorn.rb', line 39 def browser @browser end |
- (Object) document (readonly)
Returns the value of attribute document
41 42 43 |
# File 'lib/nashorn/nashorn.rb', line 41 def document @document end |
- (Object) engine (readonly)
36 37 38 |
# File 'lib/nashorn/nashorn.rb', line 36 def engine @engine end |
- (Object) window (readonly)
Returns the value of attribute window
40 41 42 |
# File 'lib/nashorn/nashorn.rb', line 40 def window @window end |
Instance Method Details
- (Object) assign(name, value)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/nashorn/nashorn.rb', line 198 def assign(name, value) original_value = value if ((value.is_a? MDArray) || (value.is_a? RubySexp)) if (value.sexp != nil) # MDArray already represented in R value = value.sexp else value = build_vector(value) end elsif (value == nil) value = NULL end @engine.put(name, value) original_value end |
- (Object) build_vector(mdarray)
Builds a Renjin vector from an MDArray. Should be private, but public for testing.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/nashorn/nashorn.rb', line 261 def build_vector(mdarray) shape = mdarray.shape # index = mdarray.nc_array.getIndex() # index = MDArray.index_factory(shape) # representation of shape in R is different from shape in MDArray. Convert MDArray # shape to R shape. if (shape.size > 2) shape.reverse! shape[0], shape[1] = shape[1], shape[0] end # AttributeMap attributes = AttributeMap.builder().setDim(new IntVector(dim)).build(); attributes = Java::OrgRenjinSexp::AttributeMap.builder() .setDim(Java::OrgRenjinSexp::IntArrayVector.new(*(shape))).build() # vector = Java::RbScicom::MDDoubleVector.new(mdarray.nc_array, attributes, index, # index.stride) case mdarray.type when "int" vector = Java::RbScicom::MDIntVector.factory(mdarray.nc_array, attributes) when "double" vector = Java::RbScicom::MDDoubleVector.factory(mdarray.nc_array, attributes) when "byte" vector = Java::RbScicom::MDLogicalVector.factory(mdarray.nc_array, attributes) when "string" vector = Java::RbScicom::MDStringVector.factory(mdarray.nc_array, attributes) when "boolean" raise "Boolean vectors cannot be converted to R vectors. If you are trying to \ convert to an R Logical object, use a :byte MDArray" else raise "Cannot convert MDArray #{mdarray.type} to R vector" end end |
- (Object) direct_eval(expression)
Evaluates an expression but does not wrap the return in a RubySexp. Needed for intermediate evaluation done by internal methods. In principle, should not be called by users.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/nashorn/nashorn.rb', line 131 def direct_eval(expression) begin ret = @engine.eval(expression) rescue Java::OrgRenjinEval::EvalException => e p e. ensure =begin Renjin.stack.each do |sexp| sexp.destroy end =end end ret end |
- (Object) eval(expression)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/nashorn/nashorn.rb', line 110 def eval(expression) begin ret = @engine.eval(expression) rescue Java::JavaxScript::ScriptException => e p e. =begin rescue Java::OrgRenjinParser::ParseException => e p e.message =end end ret end |
- (Object) install__package(name)
230 231 232 233 234 235 |
# File 'lib/nashorn/nashorn.rb', line 230 def install__package(name) pm = PackageManager.new pm.load_package(name) end |
- (Object) library(package)
241 242 243 244 245 246 247 248 249 |
# File 'lib/nashorn/nashorn.rb', line 241 def library(package) Dir.chdir(SciCom.cran_dir) filename = SciCom.cran_dir + "/#{package}.jar" require filename eval("library(#{package})") end |
- (Object) parse(*args)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/nashorn/nashorn.rb', line 152 def parse(*args) params = Array.new args.each do |arg| if (arg.is_a? Numeric) params << arg elsif(arg.is_a? String) params << "\"#{arg}\"" elsif (arg.is_a? Symbol) var = eval("#{arg.to_s}") params << var.r elsif (arg.is_a? TrueClass) params << "TRUE" elsif (arg.is_a? FalseClass) params << "FALSE" elsif (arg == nil) params << "NULL" elsif (arg.is_a? NegRange) final_value = (arg.exclude_end?)? (arg.end - 1) : arg.end params << "-(#{arg.begin}:#{final_value})" elsif (arg.is_a? Range) final_value = (arg.exclude_end?)? (arg.end - 1) : arg.end params << "(#{arg.begin}:#{final_value})" elsif (arg.is_a? Hash) arg.each_pair do |key, value| params << "#{key.to_s} = #{parse(value)}" end elsif ((arg.is_a? Renjin::RubySexp) || (arg.is_a? Array) || (arg.is_a? MDArray)) params << arg.r # elsif # params << arg.inspect else raise "Unknown parameter type for R: #{arg}" end end params.join(",") end |
- (Object) pull(name)
222 223 224 |
# File 'lib/nashorn/nashorn.rb', line 222 def pull(name) eval(name) end |
- (Object) start(stage)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nashorn/nashorn.rb', line 43 def start(stage) # Create a WebView and get a web_engine @browser = WebView.new @window = browser.engine.executeScript("window") @web_engine = browser.getEngine() @web_engine.setJavaScriptEnabled(true) # Create a Nashorn engine factory = Java::JavaxScript.ScriptEngineManager.new() @engine = factory.getEngineByName("nashorn") # JSAdapter.new end |