パッケージ SDK を使用した共有セッションの設定

パッケージ SDK は、SDK ユーザーが共有セッションを作成し、使用するための設定を提供します。セッションは異なる Bot 間でシームレスに共有され、Bot 間のリソースを共有することができます。これは、セッションが作成されている場合、他のTaskBot がセッションを読み取って更新できることを意味し、その逆も可能です。

親子でセッションを共有

次のセクションでは、共有セッションの作成について説明します。親の GlobalSessionDemoUpperCaseParent は、セッション値をグローバル セッションに返し、子 Bot GlobalSessionDemoUpperCaseChild によって使用されます。
注: 以下の 3 つのクラスを、以下の場所に配置します。<PackageSDK>\src\main\java\com\automationanywhere\botcommand\samples\commands\basic
  1. CloseableSessionObject の次の POJO クラス DemoForSession を作成します。

    package com.automationanywhere.botcommand.samples.commands.basic.GlobalSessionSampleProject;
    
    import com.automationanywhere.toolchain.runtime.session.CloseableSessionObject;
    
    import java.io.IOException;
    
    public class DemoForSession implements CloseableSessionObject
    {
        public void setClose(boolean close) {
            this.close = close;
        }
    
        boolean close=false;
        public String getDemo() {
            return demo;
        }
    
        public void setDemo(String demo) {
            this.demo = demo;
        }
        public DemoForSession(String demo){
            this.demo=demo;
        }
        String demo;
    
        @Override
        public boolean isClosed() {
            return close;
        }
    
        @Override
        public void close() throws IOException {
    
        }
    }
    
  2. @BotCommand 注釈を追加して、クラスを Control Room のアクションとして作成します。注釈の詳細については、「注釈」をご覧ください。
    @BotCommand
  3. @CommandPkg 注釈を追加して、UI 関連のすべてのコンポーネント (ラベル説明アイコンリターンタイプ名前) を定義します。@CommandPkg に属する要素 return_labelreturn_settingsreturn_type、および return_required は、SESSION が親 Bot によって返されることを保証します。
    @CommandPkg(
    		//Unique name inside a package and label to display.
    		name = "GlobalSession", label = "Global session uppercase parent",
    		node_label = "Shared session parent node label", description = "Shared session parent description", icon = "pkg.svg",
    		
    		//Return type information. return_type ensures only the right kind of variable is provided on the UI. 
    		return_label = "Return the parent session",
    		return_settings = {ReturnSettingsType.SESSION_TARGET},
    		return_type = SESSION,
    		return_required = true)
  4. GlobalSessionDemoUpperCaseParent オブジェクトを作成します。GlobalSessionDemoUpperCaseParent は、アクションの入力から小文字の文字列入力を取得し、それを大文字の文字列に変換して、結果を SessionValue に格納します。

    • コンパイル中にメソッドを実行するには、@Execute でメソッドに注釈を付けます。

      package com.automationanywhere.botcommand.samples.commands.basic.GlobalSessionSampleProject;
      
      import com.automationanywhere.botcommand.data.impl.SessionValue;
      import com.automationanywhere.commandsdk.annotations.*;
      import com.automationanywhere.commandsdk.annotations.rules.NotEmpty;
      import com.automationanywhere.commandsdk.model.ReturnSettingsType;
      import org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;
      
      import java.util.Map;
      
      import static com.automationanywhere.commandsdk.model.AttributeType.TEXT;
      import static com.automationanywhere.commandsdk.model.DataType.SESSION;
      
      
      //BotCommand makes a class eligible for being considered as an action.
      @BotCommand
      
      //CommandPks adds required information to be dispalable on GUI.
      @CommandPkg(
      		//Unique name inside a package and label to display.
      		name = "GlobalSession", label = "Global session uppercase parent",
      		node_label = "Shared session parent node label", description = "Shared session parent description", icon = "pkg.svg",
      		
      		//Return type information. return_type ensures only the right kind of variable is provided on the UI. 
      		return_label = "Return the parent session",
      		return_settings = {ReturnSettingsType.SESSION_TARGET},
      		return_type = SESSION,
      		return_required = true)
      public class GlobalSessionDemoForStringUpperCaseParent {
      	private static Logger logger = LogManager.getLogger(GlobalSessionDemoForStringUpperCaseParent.class);
      
      	//Identify the entry point for the action. Returns a SessionValue because the return_type is SESSION. 
      	@Execute
      	public SessionValue action(
      			//Idx 1 would be displayed first, with a text box for entering the value.
      			@Idx(index = "1", type = TEXT) 
      			//UI labels.
      			@Pkg(label = "Enter string in lower case")
      			//Ensure that a validation error is thrown when the value is null.
      			@NotEmpty 
      			String firstString) {
      		String result = firstString.toUpperCase();
      		DemoForSession demoForSession= new DemoForSession(result);
      		return SessionValue
      				.builder()
      				.withSessionObject(demoForSession)
      				.build();
      	}
      }
  5. GlobalSessionDemoUpperCaseChild オブジェクトを作成します。GlobalSessionDemoUpperCaseChild は、アクションの入力から小文字の文字列入力を取得し、それを大文字に変換して、親 Bot から受け取った SessionValue と連結します。

    package com.automationanywhere.botcommand.samples.commands.basic.GlobalSessionSampleProject;
    
    import com.automationanywhere.botcommand.data.Value;
    import com.automationanywhere.botcommand.data.impl.StringValue;
    import com.automationanywhere.botcommand.samples.commands.basic.Uppercase;
    import com.automationanywhere.commandsdk.annotations.*;
    import com.automationanywhere.commandsdk.annotations.rules.NotEmpty;
    import com.automationanywhere.commandsdk.annotations.rules.SessionObject;
    import com.automationanywhere.commandsdk.model.AttributeType;
    import com.automationanywhere.commandsdk.model.DataType;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    import static com.automationanywhere.commandsdk.model.AttributeType.TEXT;
    import static com.automationanywhere.commandsdk.model.DataType.STRING;
    
    
    //BotCommand makes a class eligible for being considered as an action.
    @BotCommand
    
    //CommandPks adds required information to be dispalable on GUI.
    @CommandPkg(
    		//Unique name inside a package and label to display.
    		name = "GlobalSessionChild", label = "Global session upper case child",
    		node_label = "Global session child node label",
    		description = "Global session child description", icon = "pkg.svg",
    		
    		//Return type information. return_type ensures only the right kind of variable is provided on the UI. 
    		return_label = "Return the concatenated string to", return_type = STRING, return_required = true)
    public class GlobalSessionDemoUpperCaseChild {
    	private static Logger logger = LogManager.getLogger(Uppercase.class);
    	
    	@Execute
    	public Value<String> action(
    			//Idx 1 would be displayed first, with a text box for entering the value.
    			@Idx(index = "1", type = TEXT) 
    			//UI labels.
    			@Pkg(label = "Enter a sting in lower case to concatenate")
    			//Ensure that a validation error is thrown when the value is null.
    			@NotEmpty 
    			String sourceString,
    			@Idx(index = "2", type = AttributeType.SESSION)
    			@Pkg(label = "sharedSession", description = "sharedSession",
    					default_value = "Default", default_value_type = DataType.SESSION)
    			//Using the sessionObject annotation here as its a consumer class
    					@SessionObject
    					DemoForSession session) {
    
    		//Business logic
    		String result = sourceString.toUpperCase();
    		logger.info("session: {}",session);
    		return new StringValue(result+session.getDemo().toUpperCase());
    	}
    }
  6. カスタム パッケージをビルドし、Control Room にアップロードします。パッケージのアップロードの詳細については、「パッケージ SDK の使用」をご覧ください。
  7. グローバル セッションの大文字の親アクションを使用して Bot を作成します。
    1. 小文字の文字列を入力します (例: parentbot 文字列)
    2. セッションの名前を入力します。親 Bot は値をグローバル セッションに返します (例: グローバル)
  8. [グローバル セッションの大文字] アクションを使用して TaskBot を作成します。小文字の文字列を入力し、共有の親セッションから文字列を取得することもできます。親と子の文字列を連結します。
  9. [メッセージボックス] を使用して出力を表示します。

    画像は、Variable SDK デモを示しています

  10. 次に、グローバル セッションの大文字の子を親の中に呼び出します。画像は、Variable SDK デモを示しています
  11. 親 Bot を実行すると、共有グローバル セッションを使用して取得した子 Bot の値が表示されたメッセージ ボックスが表示されます。