"any" 类型如何与字符串等基本类型一起使用?

How does "any" type work with basic types like string?

我正在尝试使用 switch 子句来根据 "any" 类型变量的实际类型定义要执行的操作,我的代码崩溃了。代码是下一个:

handleResponse(any.parseType("string",respValuesRaw[type]), currEPIFace);
...
...
...
action handleResponse(any response, CurrentExtraParamsInteface currEPIFace){

switch(response){
    case string:
        {}

我得到的错误是:"ParseException - Error in ParseType() method: Unable to parse string: missing opening quote"

但是,respValuesRaw 变量是 <string,string>

类型的字典

这是在 Apama 10.1 上。

知道哪里出了问题吗?

我发现这种解析不适用于基本类型,所以我更改了调用 handleResponse 操作的方式:

handleResponse("string", currEPIFace);

实际上,任何字符串值都适合。

根据 any.parseType, this is equivalent to calling type.parse, so this is equivalent to string.parse 的文档,其中指出:

The parse method takes a string in the form used for event files. String arguments must be enclosed in double quotes. All escape characters will be converted to the natural character.

如果你只想使用字典条目的值,你可能只想写:

handleResponse(respValuesRaw[type], currEPIFace);

字典条目的值是一个字符串,将任何类型传递给 'any' 参数都是合法的。

any类型分配一个像string这样的基本类型是绝对合法的。问题在别的地方。

由于您没有以用于事件文件的形式传递字符串,因此会出错。一旦您查看一个使用 parseType 方法的示例,解码错误消息就会变得非常简单。这给出了一些提示,为什么它真的在参数中寻找 开场引号


你的问题简单的说:

package com.apama.test;

event Evt{}
monitor Foo {
    action onload() {
        Evt e1;
        // handleResponse(any.parseType("string", "World!")); // #1 Invalid argument. Doesn't work
        handleResponse(any.parseType("com.apama.test.Evt", "com.apama.test.Evt()")); // #2
        handleResponse("World!"); // #3
    }
    action handleResponse(any response){
        log "Hello " + response.toString() ;
    }
}

打印:

com.apama.test.Foo [1] Hello any(com.apama.test.Evt,com.apama.test.Evt())
com.apama.test.Foo [1] Hello any(string,"World!")

取消注释 #1 时会出现如下所示的错误:

ParseException - Error in parseType() method: Unable to parse string: missing opening quote 

此外,如果您将一个格式正确但不存在的事件传递给 parseType 方法,它将抛出一个错误,指出找不到该类型。

ParseException - Error in parseType() method: Unable to find type 'com.apama.test.Evt2'