版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、单位代码 01 学 号 分 类 号 tp312 密 级 文献翻译r,java和object c之间桥连接方法 院(系)名称信息工程学院 专业名称计算机科学与技术 学生姓名 指导教师2013年3月6日 黄河科技学院毕业设计(文献翻译) 第14页 英文译文r,java和object c之间桥连接方法西蒙,乌尔巴内克r是一个非常流行和通用的统计计算环境(r核心开发团队)。它为许多功不可没的包提供了平台,自己的功能也得到了推广,r为许多领域提供了前沿的工具。r的这种可扩展性从某种程度上让r和本地代码进行通信成为可能。其中一个扩展性手段就是在其他系统和语言中创建接口,这样,在这个系统中就能让r用户进入并
2、对提供的应用程序和库文件进行访问。现在r直接支持java和object c这两种系统。他们提供了一个大幅度重复使用已经存在的工具的机会,例如图形用户界面(guis),计算库或者通信设备。新增的实用性接口让基于这些接口的广泛用户得以部署工程。首先我们将详细讨论java接口,在一个简介之后我们将以用户角度举例高亮文本接口。下一个区域我们将逐渐由开发,书写软件包,嵌入到实现细节和当前方法的局限性。最后我们将聚集包开发焦点讨论object c 接口。首先,对java 的支持已经在r 2.2.0版已经有介绍,后来在实质上也得到增强。它的目的是让包的开发很容易访问java功能。这已经通过建立java虚拟机
3、(jvm)环境和通过提供java运行时刻位置信息得到解决。这个信息在编译时刻收集,但是可以随后通过任何安装过r的r cmd javareconf命令更新。它通常在java配置发生改变时起作用,例如,在java运行时刻版本更新后起作用。有两种方法链接r与java环境 :l 从一个运行的r中开启一个jvm在这种情形下用户用普通方法使用r(例如:通过命令行和一些gui),java通过执行运算或者提供其他一些服务来使用。典型的一个应用都是为java中执行运算包,例如:rweka(hornik et al 2007),提供用户接口元素,例如:iplots(urbanek 和 theus 2003)或者跨
4、平台访问资源,例如:rjdbc(rjdbc)。l 从java应用程序中嵌入r这里r被作为这个应用程序的一个线程运行,这样r就能向程序提供它的服务。一个典型的应用r guis, 例如:jgr(jgr)或者程序通过r来计算,例如:mondrian(theus 2002)或者myjavastat(harner et al. 2007)。这个普遍的差别同样适用于object c:基于线程的服务可以通过r中实例化r中的object c类来使用,反之,mac os x 中r gui嵌入了一个基于运行时刻环境的object c(cocoa)。接下来我们将集中于第一个部分,一个r用户想在他们的包和功能中连接j
5、ava功能的最令人感兴趣的部分。尽管r通过设置环境提供基本的java支撑,但是实际的接口是通过包提供。其中一个有效的包就是cran的rjava。它提供了一个非常快速的低水平的能和r中的.c,.call或.fortran相比拟的接口。让我们在演示怎么写一个基于java的包中去连接它的设备和往更深层次详细介绍。具体实现前先用一些简单的例子上阐述它的用法。想要使用java接口功能,开启jvm和加载rjava包是必需的。这是通过.jinit 函数提供一些与jvm初始化有关的可选参数。这样,一个简单的jvm初始化就可以如下操作: library(rjava) .jinit()一旦jvm初始化成功,我们可
6、以创建新的java对象或者调用java方法。让我们开始一个简单的例子吧: s s1 java-objecthello world! class(s)1 “jobref”使用.jnew 函数我们创建了一个新的java对象,也就是一个内容为“hello world!”的字符串这段代码与java代码s= new java.lang.string(“hello world !”)相符合.我们返回的是通过jobref(java对象参照)表示的s4类的一个参照。由于s是一个java对象我们可以调用方法和访问这个对象的域。让我们来调用length方法,如同java中的s.length(): .jcall(s
7、,”i”,”length”)112 s$length112这个.jcall接口运行在一个非常低的水平,这样它就不仅要求我们提供方法名字(“length”)而且还要提供一个返回类型(整型“int”)。返回类型.jcall中使用的返回类型的列表在表1中已经列出。第二种是一个用反射来决定返回类型,并以牺牲速度的方式方便性操作符$来实现调用。在内部,它使用了一些低水平的.jcall接口来实现必需的反射查询。一些速度要求高的包推荐.jcall的使用。$通常被保留在交互式的情况使用。一个外部接口的一个重要功能就是能够提供从一种语言向另一种语言的类型转换。在上个例子中我们用java.lang.string类
8、的构造方法接受另外一个字符串引用作为参数。现在,我们改为通过一个r字符载体“hello world!”解决问题。.jnew函数自动转换字符载体到一个作为构造方法的引用的java字符串里面。这种自动转换在r中的大部分载体类型都会执行。下面阐述的例子的返回值也是一样: .jcall(s,”s”,”split”,” “)1 “hello” “world!”split方法是一个string类对象的java数组,但是它会自动转换到字符下载。rjava提供了为寄存定制的那些能够转换任意r对象到java对象转换器函数的设备,反之亦然。当调用java方法时,牢记我们正在处理一个低水平的接口是很重要的。与.c接
9、口相似,我们必须确保那些需要参数类型一致的地方,.jcall中要去确保有相匹配的参数和类。有时候这就意味着我们必须抛出一个对象去使用.jcall到其父类,因为一个准确的匹配是期望出现的,如下:f - .jnew(java.awt.frame) # f = new frame()b - .jnew(java.awt.button,ok) # b = new button(ok).jcall(f, ljava.awt.component;, add, # f.add(b).jcast(b, java.awt.component) #.jcall(f,pack) # f.pack().jcall(f
10、,show) # f.show()在调用add方法时,我们必须将b转换为java.awt.component类,因为这是add方法要求的。用.jmethod可以列出所支持的方法和它们的参数类型,例如:.jmethod(f,”add”)。另外一个可能存在的缺陷,事实上,r不能区别矢量长度和标量值。默认的rjava接口假设一个矢量长对应一个标量类型,如.:jcall(o,”foo”,1.5),意思是应该存在一个java方法:void foo(double d)。当一个方法接收到一个数组时,用.jarray函数将参数准确的转换到数组是很有必要的。当一列的引用转换到java数组对象时它也可以与java
11、引用一起使用。反过来,转换数组到载体或者对象列表的就是.jevalarray了。rjava也提供了检索和为java对象域设置值的设备。$操作符提供了一个双重的服务,如果一个域名匹配,它就会用于访问域,否则它就用于引用一个方法。假设我们有一个定义为class apublic string s;的类。然后给这个类一个实例对象a。我们可以用a$s检索这个域的值,也可以设置值 a$s - “hello”。使用.jfield函数可以对结果有更多的控制。java中的r包通常包含java代码和r代码的连接。尽管通过r包构建过程能够编译java代码,但是java development kit(jdk)通常就
12、不要求这样做了,这样将源代码和编译过的类打包到java 文档(jar)是很有实用性的。所有操作必需的文档包必须移动到java 子目录包中,这样,它们能够自动与jvm一块加载。为了初始化jvm,包需要调用.onload或者.first.lib中的.jpackage函数。一个典型的用法就是将rjava增加到depends中:初始化java和注释语句如下:.onload library(rjava) .jinit()once the jvm is initialized, we can create new java objects or call java methods. let us star
13、t with a simple example: s s1 java-objecthello world! class(s)1 jobjrefusing the .jnew function we create a new java object, namely a string with the contents “hello world!”. this code corresponds to the java code s = new java. lang.string(hello world!). what we get back is a reference to that objec
14、t represented by the jobjref (java object reference) s4 class. since s is a java object, we can call methods and access fields of this object. let us call the length method, corresponding to s.length() in java: .jcall(s,i,length) 1 12 s$length() 1 12the .jcall interface works at a very low-level and
15、 therefore it requires us to provide not just the method name (“length”) but also the return type (i for integer).the second call shows a convenience operator $ which uses reflection to determine the return type automatically at the cost of speed.internally, it uses the low-level.jcall interface to
16、perform necessary reflection queries. the use of the .jcall is encouraged in packages where speed is crucial and $ is usually reserved for interactive use.one important feature of a foreign interface is its ability to provide a conversion of types from one language to another. in the above example w
17、e have used a constructor of the class java.lang.string which accepts another string reference as an argument. however, we have passed an r character vector hello world! instead. the.jnew function has automatically converted the character vector into a java stringwhich was then passed as a reference
18、 to the constructor.such automatic conversion is performed for most vector types in r. the same is true for return values as the following example illustrates: .jcall(s,s,split, ) 1 helloworld!the result of the split method is a java array of objects of the class string, but it is automatically conv
19、erted into a character vector. rjava provides facilities for registering custom convertor functions which can convert arbitrarily complex r objects into java objects and vice versa.when calling java methods, it is important to remember that we are dealing with a low-level interface. similarly to the
20、 .c interface, where we must make sure that the arguments are of the corresponding types, with .jcall we must make sure that the arguments have a matching type or class. sometimes it means that we have to explicitly cast an object using .jcast to one of its super-classes, because an exact match is e
21、xpected as the following example shows:f - .jnew(java.awt.frame) # f = new frame()b - .jnew(java.awt.button,ok) # b = new button(ok).jcall(f, ljava.awt.component;, add, # f.add(b).jcast(b, java.awt.component)#.jcall(f,pack) # f.pack().jcall(f,show) # f.show()in the call to the add method we have to
22、cast the button b into the java.awt. component class, because that is the class that the add method requires. it is possible to list the supported methods and their argument types using .jmethods such as.jmethods(f, add).another possible pitfall is the fact that r does not distinguish between a scal
23、ar value and a vector of length one. by default the rjava interface assumes that a vector of length one matches a scalar type, such as .jcall(o, foo, 1.5) is expected to find a java method void foo(double d).in cases where a method always accepts an array, it may be necessary to explicitlyconvert su
24、ch parameter into an array reference using .jarray function. this can also be used with java references where a list of references is converted into an array of objects in java. the inverse function which turns array references into vectors or lists of objects is .jevalarray.rjava also provides faci
25、lities for retrieving or setting values of java object fields. the $ operator serves a dual purpose, if a field name matches, then it is used to access that field, otherwise it refers to a method. let us assume we have a class defined as class a public string s;. then given an instance object a of t
26、his class we can retrieve the value of the field using a$s and set it with a$s-hello. more control over the result can be exhibited using the .jfield function.r packages that use java usually consist of java code (in compiled and/or source form) and the interfacing r code. although it is possible to
27、 use the r package building process to compile java code,the presence of a java development kit (jdk) is in general not required and thus it is more practical to package both the sources and compiled classes into a java archive (jar). all archives necessary for the operation of the package should be
28、 placed in the java subdirectory of the package such that they are automatically registered with the jvm.the package should call .jpackage function in .onload or .first.lib in order to initialize the jvm. a typical use is to add rjava to the depends: clause in the description file and initialize jav
29、a as follows:.onload - function (libname, pkgname) .jpackage(pkgname)# the java interface can be used from now on .note that .jinit should not be used in this case, because the registration of java classes and jvm initialization is handled automatically by .jpackage. this also ensures peaceful co-op
30、eration of multiple packages. packages are free to depend on other java-based packages. the above mechanism guarantees that their corresponding classes are available in the right order and thus packages can depend on each others classes if they desire so.although rjava is primarily an r package, it
31、also contains tools necessary to embed r into a java application. this part of rjava is often called jri (java/r interface) and can be found in the jri subdirectory of the rjava package. it consists of a set of java classes that constitutes an interface for accessing most parts of r from within java
32、. jri uses java native interface to link against r shared library and thus to directly embed r in a running java application.the heart of the jri interface is the rengine class which provides methods that closely reflect the c api in r, enriched with high-level functions. r objects are represented b
33、y the rexp class which also provides accessor methods that convert r values to java native types.the embedded r can be used either in an ad-hoc mode, utilizing mostly the parser and evaluator,or in a full event loop mode, which provides callbacks into java for console handling.in conjunction with th
34、e java graphics device provided by the javagd package.it is possible to map all aspects of the r user communication into java. this feature is used by the java gui for r (jgr package) to provide a graphics user interface written entirely in java.the rjava package is implemented as a layer between r
35、and java using java native interface (jni) and rs c api. the main goal of the rjava implementation is to be truly cross-platform, easy to install and highly efficient. this is achieved by keeping the native jni layer very thin in order to increase maintainability, and to add automatic java detection
36、 features for easy installation. it distinguishes rjava from other, more complex and feature-rich approaches such as rsjava which is part of the omegahat project (temple lang 2000). as a result rjava now works on many platforms out of the box (windows, mac os x and many unices) and supports both com
37、mercial (sun, ibm) and open-source java platforms (gcj/gij). in order to ensure proper memory management, rjava uses external pointers with finalizers to represent java references. before a java object is passed to r, a global reference is created and this reference is released once the correspondin
38、g r object is garbage-collected. this ensures that there are no memory leaks. instances of the fundamental s4 class jobjref hold the external pointer and a class name. this way java references in r are very light, can be arbitrarily copied and cast with very little overhead.objective c (objc) is a dynamic, reflective, object-oriented programming language (brad 1986) which is defined as a strict superset of c. due to its dynamic nature it is extremely flexible, because the behavior of objects can be determined and even changed at run-time.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度第二章国际货物买卖合同标的检验与认证3篇
- 二零二五年度雕塑项目投标采购合同范本3篇
- 2025年度旅游景区导游旅游纪念品销售合作合同4篇
- 二零二五版驾校教练员绩效考核及激励合同3篇
- 2025年度餐厅总经理数字化运营管理合同3篇
- 二零二五年度深部矿产资源勘查开采权转让合同2篇
- 二零二四事业单位借调人员临时工作期间劳动合同解除流程3篇
- 2024-2025学年高中政治第一单元文化与生活第一课第一框体味文化训练含解析新人教版必修3
- 二零二五版能源效率认证EMC合同能源管理合作协议3篇
- 二零二四年度专业演出服务合同-舞台剧制作合作协议3篇
- 带状疱疹护理查房课件整理
- 年月江西省南昌市某综合楼工程造价指标及
- 奥氏体型不锈钢-敏化处理
- 作物栽培学课件棉花
- 交通信号控制系统检验批质量验收记录表
- 弱电施工验收表模板
- 绝对成交课件
- 探究基坑PC工法组合钢管桩关键施工技术
- 国名、语言、人民、首都英文-及各地区国家英文名
- API SPEC 5DP-2020钻杆规范
- 组合式塔吊基础施工专项方案(117页)
评论
0/150
提交评论