gpt4 book ai didi

java - 如何将多个参数从 Jolie 传递到嵌入式 Java 服务?

转载 作者:行者123 更新时间:2023-11-30 03:55:24 26 4
gpt4 key购买 nike

我想将多个参数从 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.jarjolie.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com