作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将多个参数从 Jolie 传递到嵌入式 Java 服务。这是我的 Jolie 程序和 Java 程序的代码:
朱莉节目:
include "console.iol"
type NandRequest:void {
.number[2,*]: bool
}
interface NANDInterface {
RequestResponse: test(NandRequest)(bool)
}
outputPort NAND {
Interfaces: NANDInterface
}
embedded {
Java: "example.NAND" in NAND
}
main
{
request.number[0] = true;
request.number[1] = true;
test@NAND(request)(response);
println@Console( response )()
}
Java 程序:封装示例;
import jolie.runtime.JavaService;
public class NAND extends JavaService{
public java.lang.Boolean test(final java.lang.Boolean x , final java.lang.Boolean y) {
java.lang.Boolean r = !(x&&y);
return r;
}
}
但是当我运行 Jolie 服务时,它给出错误“jolie.runtime.InvalidException:无效标识符:测试”
传递如此多个参数的正确方法是什么?
最佳答案
最简单的方法是使用 jolie.jar
中 jolie.runtime
包中的 Value
类,它允许您导航从 Jolie 作为 map 传递的数据树:
package example;
import jolie.runtime.JavaService;
import jolie.runtime.Value;
import jolie.runtime.ValueVector;
public class NAND extends JavaService
{
public Boolean test( Value request )
{
ValueVector vec = request.getChildren( "number" );
for( Value v : vec ) {
if ( !v.boolValue() ) return false;
}
return true;
}
}
我冒昧地将您的代码改编为通用数组。
如果您希望自动将 Jolie 值转换为自定义 Java 类的 Java 对象,可以使用 ValueConverter
(来自 jolie.runtime.JavaService
)。例如,Jolie 标准库中的 FileService
就这样使用它:
public static class StartsWithRequest implements ValueConverter
{
private String self, prefix;
private StartsWithRequest() {}
public static StartsWithRequest fromValue( Value value )
{
StartsWithRequest ret = new StartsWithRequest();
ret.self = value.strValue();
ret.prefix = value.getFirstChild( "prefix" ).strValue();
return ret;
}
public static Value toValue( StartsWithRequest p )
{
Value ret = Value.create();
ret.setValue( p.self );
ret.getFirstChild( "prefix" ).setValue( p.prefix );
return ret;
}
}
public Boolean startsWith( StartsWithRequest request )
{
return request.self.startsWith( request.prefix );
}
请注意,ValueConverter
仍在进行中,并且不能保证该 API 在 Jolie 的 future 版本中保持稳定,我们仍在努力完善它(尽管它不应该改变)或者至少没有改变太多)。
关于java - 如何将多个参数从 Jolie 传递到嵌入式 Java 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23341907/
我是一名优秀的程序员,十分优秀!