使用軟件包 SDK 創建迭代器
在 Automation 360中,您可以使用 SDK 軟件包創建迭代器。迭代器用於循環遍歷類似類型的多個對象,並與 Loop 軟件包一起使用。 它由
兩種方法組成: NEXT 和 HasNext ,返回實際對象和布爾值。
所需的註釋
使用迭代器時, 下
一個方法返回下一個可用值。如果沒有更多的值可用,則會顯示異常。 hasNext
方法檢查其他值, 如果沒有更多值可用,則返回 false 。在 Automation 360中,執行循環時
,首先調用 hasNext() ,
而只
有 hasNext()
返回 一個 true
值時才調用 NEXT () 。
要創建迭代器,需要以下註釋。
註解 | 使用方式 |
---|---|
BotCommand |
將 BotCommand 註釋與 迭代器一起使用 作 爲命令類型 。這可確保普通舊 Java 對象 (POJO) 符合創建 Automation 360 迭代器的條件。 |
CommandPkg |
創建軟件包時,請爲註釋提供名稱、標籤和說明。 |
IDX |
註釋所有必需的參數和成員變量,並幫助進行驗證檢查,或者它們可以顯示在輸入的接口中。提供 IDX 和類型。 |
包裝 |
註釋界面中顯示的所有參數和成員變量。如果 沒有 IDX 附帶此註釋,則將忽略該註釋 。 |
HasNext |
如果迭代具有更多元素,則必須調用用於測試的方法( 如果 NEXT () 返回元素而不是拋出異常,則返回 true )。它必須返回布爾值。 如果方法接受參數,則必須用 |
下一步 |
該方法返回迭代中的下一個可用值。返回類型是一個值。如果沒有更多的值可用,則會顯示異常。 |
示例使用案例循環次數,例如,(int i=0 ; i
) Java 構造。 Bot Creator bot 當創建時,它接受來自的 n 值。
- 使用業務邏輯創建 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 注入
。- 使用
IDX 和
包註釋變量
。添加
@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;
。 attribute type number 返回
雙精度值
。