使用软件包 SDK 创建迭代器
- Updated: 2022/07/22
在 Automation 360 中,您可以使用 SDK 软件包创建迭代器。 迭代器用于循环处理多个相似类型的对象,并与 循环 包一起使用。 它包含两种方法,即 next
和 hasNext
,它们返回实际对象和布尔值。
所需注释
使用迭代器时,next
方法返回下一个可用值。 如果没有更多值可用,则会显示异常。 hasNext
方法检查其他值,如果没有更多可用值,则返回 false
。 在 Automation 360 中,当循环被执行时,先调用 hasNext()
,仅当 hasNext()
返回 true
值时才调用 next()
。
要创建迭代器,需要以下注释。
注释 | 使用情况 |
---|---|
BotCommand
|
使用 BotCommand 注释,iterator 作为 commandType 。 这确保了普通 Java 对象 (POJO) 符合创建 Automation 360 迭代器的条件。 |
CommandPkg
|
创建软件包时,请提供注释的名称、标签和描述。 |
Idx
|
注释所有必需的参数和成员变量,并帮助验证检查,也可以在输入界面中显示它们。 提供 Idx 和类型。 |
Pkg
|
注释将在界面中显示的所有参数和成员变量。 如果没有 Idx ,将忽略此注释。 |
HasNext
|
如果迭代器具有更多元素,则必须调用该方法进行测试(如果 next() 返回元素,则返回 true ,而不是抛出异常)。 必须返回布尔值。如果该方法接受参数,则必须使用 |
Next
|
方法返回迭代器中的下一个可用值。 返回类型是一个值。 如果没有更多值可用,则显示异常。 |
这个示例场景循环多次,例如,用于 (int i=0; I<n; i++
) Java 构造。 它接受 机器人 创建时来自 机器人创造程序 的 n
值。
- 确保已导入以下软件包,以保证所有功能都能按照示例中的描述运行。
import com.automationanywhere.botcommand.data.Value; import com.automationanywhere.botcommand.data.impl.NumberValue; import com.automationanywhere.commandsdk.annotations.BotCommand; import com.automationanywhere.commandsdk.annotations.CommandPkg; import com.automationanywhere.commandsdk.annotations.HasNext; import com.automationanywhere.commandsdk.annotations.Idx; import com.automationanywhere.commandsdk.annotations.Inject; import com.automationanywhere.commandsdk.annotations.Next; import com.automationanywhere.commandsdk.annotations.Pkg; import com.automationanywhere.commandsdk.annotations.BotCommand.CommandType; import com.automationanywhere.commandsdk.annotations.rules.GreaterThanEqualTo; import com.automationanywhere.commandsdk.annotations.rules.NotEmpty; import com.automationanywhere.commandsdk.annotations.rules.NumberInteger; import com.automationanywhere.commandsdk.model.AttributeType; import com.automationanywhere.commandsdk.model.DataType;
- 使用业务逻辑创建 POJO 类。
确保 POJO 具有方法并返回布尔值。 这将作为比较方法。
@BotCommand(commandType=CommandType.Iterator) @CommandPkg(return_label = "Return the value in variable", node_label = ": {{times}} times", label = "Iterator demo", description = "Iterate number of times", name = "iteratorTypeDemo", return_type = DataType.NUMBER) public class IteratorTypeDemo { @Idx(index = "1", type = AttributeType.NUMBER) @Pkg(label = "times", default_value = "10", default_value_type = DataType.NUMBER) @GreaterThanEqualTo("0") @NumberInteger @NotEmpty @Inject private Double times = 10d; private Double counter = 0d; @HasNext public boolean hasNext() { return counter < times; } @Next public Value<Double> next() throws Exception{ if (counter >= times) throw new Exception("Counter '"+ counter +"' is exceed the times limit '"+times+"'"); counter++; NumberValue result = new NumberValue(); result.set(counter); return result; } public void setTimes(Double times) { this.times = times; } }
- 对 POJO 类进行注释,以便 Automation 360 迭代器启用它并创建软件包。
@BotCommand(commandType = CommandType.Iterator) @CommandPkg(return_label = "Return the value in variable", node_label = ": {{times}} times", label = "Iterator demo", description = "Iterate number of times", name = "iteratorTypeDemo", return_type = DataType.NUMBER) public class IteratorTypeDemo { private Double times = 10 d; private Double counter = 0 d; public boolean hasNext() { return counter < times; }
- 对
hasNext()
和next()
方法进行适当注释。@HasNext public boolean hasNext() { return counter < times; } @Next public Value < Double > next() throws Exception { if (counter >= times) throw new Exception("Counter '" + counter + "' is exceed the times limit '" + times + "'"); counter++; NumberValue result = new NumberValue(); result.set(counter); return result; }
这些方法以易于使用的方式命名,并与 Java 迭代器界面并行完成。 命名方式时,SDK 端没有限制。
在迭代器中,不要使用参数方法,而是使用
setter injection
。 - 用
Idx
和Pkg
来注释变量。添加
@NotEmpty
以确保值不为空,添加@GreaterThanEqualTo
以确保值始终大于0
。@Idx(index = "1", type = AttributeType.NUMBER) @Pkg(label = "times", default_value = "10", default_value_type = DataType.NUMBER) @GreaterThanEqualTo("0") @NotEmpty @Inject private Double times = 10 d;
属性类型编号将返回
Double
。