2014-04-28 31 views
0

我想將多個參數傳遞給來自Jolie的嵌入式Java服務。 這裏是我的朱莉程序和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; 
    [email protected](request)(response); 
    [email protected](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.runtime.InvalidException:無效的標識:測試

什麼是通過這種多參數的正確方法是什麼?

回答

1

做是jolie.jar使用類Value從包jolie.runtime最簡單的方法,它允許您導航到朱莉通過作爲地圖數據樹:

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)。例如,從朱莉標準庫中的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將在朱莉的未來版本將保持穩定,我們仍在努力提煉它(儘管它不應該改變或者至少不會改變太多)。