Command set

How to create and use a CommandSet component.

Key takeaways

CommandSet Component used to group a set of commands.
Command Component used to call a method or a function.
Event Component used to send a notification to one or more listeners.

Introduction

The CommandSet component allows us to group a set of commands and events, which can be called and executed at a later stage.

In this tutorial, you will learn how to create a CommandSet component, add commands and events to it, and perform some operations, such as executing or finding a specific command from a command set.

In order to do this, we divide the work into two sections: the first explains how to operate with commands and the second how to work with events in a CommandSet.

Pre-requisites

In order to use the CommandSet component, we need to import it first. The following command shows how to do this:

import { CommandSet } from "pip-services3-commons-nodex";
using PipServices3.Commons.Commands;
import (
    ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
)
import 'package:pip_services3_commons/pip_services3_commons.dart';

from pip_services3_commons.commands import CommandSet
Not available

Defining a CommandSet

In order to create a CommandSet component, we need to either create an instance of this class:

let myCommandSetA = new CommandSet();

var myCommandSetA = new CommandSet();
import (
	ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
)

myCommandSetA := ccmd.NewCommandSet()
var myCommandSetA = CommandSet();
my_command_setA = CommandSet()
Not available

or define a subclass of it:

export class MyCommandSet extends CommandSet {
    public constructor() {
        super();
    }
}
public class MyCommandSet: CommandSet
{
    public MyCommandSet() : base() { }
}
import (
	ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
)

type MyCommandSet struct {
	*ccmd.CommandSet
}
class MyCommandSet extends CommandSet {
  MyCommandSet() : super();
}
class MyCommandSet(CommandSet):
    _controller = None
    def __init__(self, controller):
        super(MyCommandSet, self).__init__()
Not available

Using a CommandSet with Commands

In this section, we will learn how to add commands to a command set, and how to retrieve these commands and execute them

Adding a command

We can add a command to the set with the add_command() method. The following example shows how to add a command that, once executed, prints ‘command 1’.

import { Command, CommandSet, ICommand, Parameters } from "pip-services3-commons-nodex";


export class MyCommandSet extends CommandSet {
    public constructor() {
        super();
        this.addCommand(this.command1());
    }

    private command1(): ICommand {
        return new Command("command1",
                null,
                (correlationId: string, args: Parameters) => {
                console.log("command 1");
                return null;
            }
        );
    } 
}
public class MyCommandSet: CommandSet
{
    public MyCommandSet() : base() {
        AddCommand(Command1());
    }

    private ICommand Command1()
    {
        return new Command("command1", 
            null, 
            async (string correlationId, Parameters args) => { 
                Console.WriteLine("command 1"); 
                return null; 
            }
        );
    }
}
import (
	"fmt"

	ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
	crun "github.com/pip-services3-gox/pip-services3-commons-gox/run"
)

type MyCommandSet struct {
	*ccmd.CommandSet
}

func NewMyCommandSet() *MyCommandSet {
	c := &MyCommandSet{
		CommandSet: ccmd.NewCommandSet(),
	}

	c.AddCommand(c.command1())
	return c
}

func (c *MyCommandSet) command1() ccmd.ICommand {
	return ccmd.NewCommand(
		"command1",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 1")
			return
		},
	)
}
import 'package:pip_services3_commons/pip_services3_commons.dart';


class MyCommandSet extends CommandSet {
  MyCommandSet() : super() {
    addCommand(command1());
  }

  ICommand command1() {
    return Command('command1', null,
        (String? correlationId, Parameters args) async => print('command 1'));
  }
}
class MyCommandSet(CommandSet):
    _controller = None

    def __init__(self, controller):
        super(MyCommandSet, self).__init__()
        self.add_command(self._command1())
        
    def _command1(self):
        def handler(correlation_id, args):
            return print("command 1")
        return Command("command1",None,handler)
Not available

Adding several commands at once

We can also add several commands at once via the addCommands() method, which accepts as input a list with the names of the methods to be added. In the example below, we add two commands to the set.

export class MyCommandSet extends CommandSet {
    public constructor() {
        super();
        this.addCommands([this.command2(), this.command3()]);
    }

    private command2(): ICommand {
        return new Command("command2",
            null,
            (correlationId: string, args: Parameters) => {
                console.log("command 2");
                return null;
            }
        );
    }

    private command3(): ICommand {
        return new Command("command3",
            null,
            (correlationId: string, args: Parameters) => {
                console.log("command 2");
                return null;
            }
        );
    }
}

public class MyCommandSet: CommandSet
{
    public MyCommandSet() : base() {
        AddCommands(new List<ICommand> { Command2(), Command3() });
    }

    private ICommand Command2()
    {
        return new Command("command2",
            null,
            async (string correlationId, Parameters args) => {
                Console.WriteLine("command 2");
                return null;
            }
        );
    }

    private ICommand Command3()
    {
        return new Command("command3",
            null,
            async (string correlationId, Parameters args) => {
                Console.WriteLine("command 3");
                return null;
            }
        );
    }
}

import (
	"fmt"

	ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
	crun "github.com/pip-services3-gox/pip-services3-commons-gox/run"
)

type MyCommandSet struct {
	ccmd.CommandSet
}

func NewMyCommandSet() *MyCommandSet {
	c := &MyCommandSet{
		CommandSet: *ccmd.NewCommandSet(),
	}

	c.AddCommands([]ccmd.ICommand{c.command2(), c.command3()})
	return c
}

func (c *MyCommandSet) command2() ccmd.ICommand {
	return ccmd.NewCommand(
		"command2",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 2")
			return
		},
	)
}

func (c *MyCommandSet) command3() ccmd.ICommand {
	return ccmd.NewCommand(
		"command3",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 3")
			return
		},
	)
}

class MyCommandSet extends CommandSet {
  MyCommandSet() : super() {
    addCommands([command2(), command3()]);
  }

  ICommand command2() {
    return Command('command2', null,
        (String? correlationId, Parameters args) async => print('command 2'));
  }

  ICommand command3() {
    return Command('command3', null,
        (String? correlationId, Parameters args) async => print('command 3'));
  }
}

class MyCommandSet(CommandSet):
    _controller = None

    def __init__(self, controller):
        super(MyCommandSet, self).__init__()
        self.add_commands([self._command2(),self._command3()])
    
    def _command2(self):
        def handler(correlation_id, args):
            return print("command 2")
        return Command("command2",None,handler)
   
    def _command3(self):
        def handler(correlation_id, args):
            return print("command 3")
        return Command("command3",None,handler) 
Not available

Adding a command set

Alternatively, we can add a CommandSet containing one or more commands. This can be done with the addCommandSet() method. The following example shows how to do this:

export class MyCommandSetB extends CommandSet {
    public constructor() {
        super();
        this.addCommand(this.command1B());
    }

    private command1B(): ICommand {
        return new Command(
            "command1B",
            null, 
            async (correlationId: string, args: Parameters) => {
                console.log("command 1B");
            }
        );
    }
}
    
export class MyCommandSet extends CommandSet {
    private _commandSet = new MyCommandSetB();

    public constructor() {
        super();
        this.addCommandSet(this._commandSet);
    }
}
public class MyCommandSetB: CommandSet
{
    public MyCommandSetB() : base()
    {
        AddCommand(Command1B());
    }

    private ICommand Command1B()
    {
        return new Command("command1B",
            null,
            async (string correlationId, Parameters args) => {
                Console.WriteLine("command 1B");
                return null;
            }
        );
    }
}
    
public class MyCommandSet: CommandSet
{
    private CommandSet _commandSet = new MyCommandSetB();
    public MyCommandSet() : base() {
        AddCommandSet(_commandSet);
    }
}
type MyCommandSetB struct {
	*ccmd.CommandSet
}

func NewMyCommandSetB() *MyCommandSetB {
	c := &MyCommandSetB{
		CommandSet: ccmd.NewCommandSet(),
	}

	c.AddCommand(c.command1B())
	return c
}

func (c *MyCommandSetB) command1B() ccmd.ICommand {
	return ccmd.NewCommand(
		"command1B",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 1B")
			return
		},
	)
}

type MyCommandSet struct {
	*ccmd.CommandSet
}

func NewMyCommandSet() *MyCommandSet {
	c := &MyCommandSet{
		CommandSet: ccmd.NewCommandSet(),
	}

	c.AddCommandSet(NewMyCommandSetB().CommandSet)
	return c
}
class MyCommandSetB extends CommandSet {
  MyCommandSetB() : super() {
    addCommand(command1B());
  }

  ICommand command1B() {
    return Command('command1B', null,
        (String? correlationId, Parameters args) async => print('command 1B'));
  }
}
    
class MyCommandSet extends CommandSet {
  MyCommandSet() : super() {
    addCommandSet(MyCommandSetB());
  }
}
class MyCommandSetB(CommandSet):
    _controller = None
    def __init__(self, controller):
        super(MyCommandSetB, self).__init__()
        self.add_command(self._command1B())
        
    def _command1B(self):
        def handler(correlation_id, args):
            return print("command 1B")
        return Command("command1B",None,handler)
    
class MyCommandSet(CommandSet):
    _controller = None
    _command_set = MyCommandSetB(None)
    def __init__(self, controller):
        super(MyCommandSet, self).__init__()
        self.add_command_set(self._command_set)
Not available

Executing a command

Once our command set is ready, we can create an instance of it and execute any of the commands it contains with the execute() method. This method accepts as inputs the correlation id, the name of the command to be executed, and the parameters that this command requires.

let mySet = new MyCommandSet();

await mySet.execute(null, "command1", null); // Returns command 1

var mySet = new MyCommandSet();

await mySet.ExecuteAsync(null, "command1", null); // Returns command 1

mySet := NewMyCommandSet()

mySet.Execute(context.Background(), "", "command1", nil) // Returns command 1

var mySet = MyCommandSet();

await mySet.execute(null, 'command1', Parameters()); // Returns command 1

my_set = MyCommandSet(None)

my_set.execute(None, "command1B", None) # Returns Command 1B
Not available

Finding a command

We can find a command via the findCommand() method, which accepts the name of the command as input. The next example shows how to use it.

let result = mySet.findCommand("command1");
    
console.log(result.getName());
var result = mySet.FindCommand("command1");

Console.WriteLine(result.Name); // Returns 'command1'
result := mySet.FindCommand("command1")

fmt.Println(result.Name()) // Returns 'command1'
var result = mySet.findCommand('command1');

print(result!.getName());
result = my_set.find_command("command1B")

result.get_name() # Returns 'command1B'
Not available

Getting all commands

We can get all the commands available in our command set via the getCommands() method, which returns the results in the form of a list. In the following example, we obtain a list with the commands and then we print their names.

let result2 = mySet.getCommands();

for (let command of result2) {
    console.log(command.getName());
}

// Returns    
// command1
// command2
// command3
// command1B
var result2 = mySet.Commands;

foreach (var command in result2)
{
    Console.WriteLine(command.Name);
}

// Returns    
// command1
// command2
// command3
// command1B
result2 := mySet.Commands()

for _, command := range result2 {
	fmt.Println(command.Name())
}

// Returns    
// command1
// command2
// command3
// command1B
var result2 = mySet.getCommands();

for (var command in result2) {
  print(command.getName());
}

// Returns    
// command1
// command2
// command3
// command1B
result2 = my_set.get_commands()

for comm in result2:
    print(comm.get_name())
# Returns    
# command1
# command2
# command3
# command1B
Not available

Using a CommandSet with Events

We can also use our command set to store events.

Adding an event

Adding an event is very similar to adding a command and can be done with the addEvent() method. This method accepts the event to be added as input.

export class MyEventSet extends CommandSet {
    public constructor() {
        super();
        this.addEvent(this.event1());
    }

    private event1(): IEvent {
        return new Event("event1");
    }
}

public class MyEventSet: CommandSet
{
    public MyEventSet(): base()
    {
        AddEvent(Event1());
    }

    private IEvent Event1()
    {
        return new Event("event1");
    }
}

type MyEventSet struct {
	*ccmd.CommandSet
}

func NewMyEventSet() *MyEventSet {
	c := &MyEventSet{
		CommandSet: ccmd.NewCommandSet(),
	}

	c.AddEvent(c.event1())
	return c
}

func (c *MyEventSet) event1() ccmd.IEvent {
	return ccmd.NewEvent("event1")
}

class MyEventSet extends CommandSet {
  MyEventSet() : super() {
    addEvent(event1());
  }

  IEvent event1() {
    return Event('event1');
  }
}

class MyEventSet(CommandSet):
    _controller = None
    _event = None
    def __init__(self, controller):
        super(MyEventSet, self).__init__()
        self.add_event(self._event1())

    def _event1(self):
        return Event("event1")
Not available

Adding several events at once

We can also add several commands together with the addEvents() method, which accepts as input a list containing the events to be added. This example explains how to do this:

export class MyEventSet extends CommandSet {
    public constructor() {
        super();
        this.addEvents([this.event2(), this.event3()]);
    }

    private event2(): IEvent {
        return new Event("event2");
    }

    private event3(): IEvent {
        return new Event("event3");
    }
}

public class MyEventSet: CommandSet
{
    public MyEventSet(): base()
    {
        AddEvents(new List<IEvent> { Event2(), Event3() });
    }

    private IEvent Event2()
    {
        return new Event("event2");
    }

    private IEvent Event3()
    {
        return new Event("event3");
    }
}

type MyEventSet struct {
	*ccmd.CommandSet
}

func NewMyEventSet() *MyEventSet {
	c := &MyEventSet{
		CommandSet: ccmd.NewCommandSet(),
	}

	c.AddEvents([]ccmd.IEvent{c.event2(), c.event3()})
	return c
}

func (c *MyEventSet) event2() ccmd.IEvent {
	return ccmd.NewEvent("event2")
}

func (c *MyEventSet) event3() ccmd.IEvent {
	return ccmd.NewEvent("event3")
}

class MyEventSet extends CommandSet {
  MyEventSet() : super() {
    addEvents([event2(), event3()]);
  }

  IEvent event2() {
    return Event('event2');
  }

  IEvent event3() {
    return Event('event3');
  }
}

class MyEventSet(CommandSet):
    _controller = None
    _event = None
    def __init__(self, controller):
        super(MyEventSet, self).__init__()
        self.add_events([self._event2(),self._event3()])
    
    def _event2(self):
        return Event("event2")
        
    def _event3(self):
        return Event("event3")
Not available

Adding a listener

Additionally, we can add a listener to our command set by using the addListener() method. This method takes the listener as input and adds a listener that is connected to all the events in the command set. In the following example, we add a listener that is connected to event2 and event3.

import { 
    Command, CommandSet, ICommand, 
    IEvent, Parameters, Event, IEventListener 

} from "pip-services3-commons-nodex";

export class MyListener implements IEventListener {
    onEvent(correlationId: string, event: IEvent, args: Parameters): void {
        console.log("Fired event name " + event.getName());
    }
} 

export class MyEventSet extends CommandSet {
    public constructor() {
        super();
        
        this.addEvents([this.event2(), this.event3()]);
        this.addListener(this.listener1());
    }

    private event2(): IEvent {
        return new Event("event2");
    }

    private event3(): IEvent {
        return new Event("event3");
    }

    private listener1(): IEventListener {
        return new MyListener();
    }
}

public class MyListener : IEventListener
{
    public void OnEvent(string correlationId, IEvent e, Parameters value)
    {
        Console.WriteLine("Fired event name " + e.Name);
    }
}

public class MyEventSet: CommandSet
{
    public MyEventSet(): base()
    {
        AddEvents(new List<IEvent> { Event2(), Event3() });
        AddListener(Listener1());
    }

    private IEvent Event2()
    {
        return new Event("event2");
    }

    private IEvent Event3()
    {
        return new Event("event3");
    }

    private IEventListener Listener1()
    {
        return new MyListener();
    }
}

type MyEventSet struct {
	*ccmd.CommandSet
}

func NewMyEventSet() *MyEventSet {
	c := &MyEventSet{
		CommandSet: ccmd.NewCommandSet(),
	}

	c.AddEvents([]ccmd.IEvent{c.event2(), c.event3()})
	c.AddListener(c.listener1())
	return c
}

func (c *MyEventSet) event2() ccmd.IEvent {
	return ccmd.NewEvent("event2")
}

func (c *MyEventSet) event3() ccmd.IEvent {
	return ccmd.NewEvent("event3")
}

func (c *MyEventSet) listener1() ccmd.IEventListener {
	return NewMyListener()
}

type MyListener struct{}

func (c *MyListener) OnEvent(correlationId string, event ccmd.IEvent, value *crun.Parameters) {
	fmt.Println("Fired event " + event.Name())
}

func NewMyListener() *MyListener {
	return &MyListener{}
}

class MyListener implements IEventListener {
  @override
  void onEvent(String? correlationId, IEvent event, Parameters args) {
    print('Fired event name ' + event.getName());
  }
}

class MyEventSet extends CommandSet {
  MyEventSet() : super() {
    addEvents([event2(), event3()]);
    addListener(listener1());
  }

  IEvent event2() {
    return Event('event2');
  }

  IEvent event3() {
    return Event('event3');
  }

  IEventListener listener1() {
    return MyListener();
  }
}

class MyListener(IEventListener):
    def on_event(self, correlation_id, event, args):
        print("Fired event name " + event.get_name())

class MyEventSet(CommandSet):
    _controller = None
    _event = None
    def __init__(self, controller):
        super(MyEventSet, self).__init__()
        self.add_events([self._event2(),self._event3()])
        self.add_listener(self._listener1())
    
    def _event2(self):
        return Event("event2")
        
    def _event3(self):
        return Event("event3")
    
    def _listener1(self):
        return MyListener()
Not available

Finding an event

We can use the findEvent() method to search for a specific event. This method asks for the event name as input, and if found, returns the event object.

let result4 = myEvents.findEvent("event1");

console.log(result4.getName());  // Returns 'event1'
var result4 = myEvents.FindEvent("event1");

Console.WriteLine(result4.Name);  // Returns 'event1'
result4 := myEvents.FindEvent("event1")

fmt.Println(result4.Name())  // Returns 'event1'
var result4 = myEvents.findEvent('event1');

print(result4!.getName());  // Returns 'event1'
result4 = my_events.find_event("event2")

result4.get_name()  # Returns 'event2'
Not available

Getting all the events

Finally, similar to the previous method, the getEvents() method allows us to obtain all the events available in our command set in the form of a list. In the example below, we obtain a list with the events, and then, we print their names.

let myEvents = new MyEventSet();

let result3 = myEvents.getEvents();

for (let event of result3)
{
    console.log(event.getName());
}

// Returns:    
// event1
// event2
// event3
var myEvents = new MyEventSet();

var result3 = myEvents.getEvents();

for (let event of result3)
{
    console.log(event.getName());
}

// Returns:    
// event1
// event2
// event3
myEvents := NewMyEventSet()

result3 := myEvents.Events()

for _, event := range result3 {
	fmt.Println(event.Name())
}

// Returns:    
// event1
// event2
// event3
var myEvents = MyEventSet();

var result3 = myEvents.getEvents();

for (var event in result3) {
  print(event.getName());
}

// Returns:    
// event1
// event2
// event3
my_events = MyEventSet(None)

result3 = my_events.get_events()

for event in result3:
    print(event.get_name())
# Returns:    
# event1
# event2
# event3
Not available

Examples

In this section, we have two examples that show how to work with command sets. The first focus on commands and the second on events.

Example 1

import { Command, CommandSet, ICommand, Parameters } from "pip-services3-commons-nodex";


export async function main() { 
    var mySet = new MyCommandSet();

    await mySet.execute(null, "command1", null);
    await mySet.execute(null, "command2", null);
    await mySet.execute(null, "command3", null);
    await mySet.execute(null, "command1B", null);
}


export class MyCommandSetB extends CommandSet {
    public constructor() {
        super();

        this.addCommand(this.command1B());
    }

    private command1B(): ICommand {
        return new Command(
            "command1B",
            null, 
            async (correlationId: string, args: Parameters) => {
                console.log("command 1B");
            }
        );
    }
}

export class MyCommandSet extends CommandSet {
    private _commandSet = new MyCommandSetB();

    public constructor() {
        super();
        this.addCommandSet(this._commandSet);
        this.addCommand(this.command1());
        this.addCommands([this.command2(), this.command3()]);
    }

    private command1(): ICommand {
        return new Command("command1",
                null,
                (correlationId: string, args: Parameters) => {
                console.log("command 1");
                return null;
            }
        );
    } 

    private command2(): ICommand {
        return new Command("command2",
            null,
            (correlationId: string, args: Parameters) => {
                console.log("command 2");
                return null;
            }
        );
    }

    private command3(): ICommand {
        return new Command("command3",
            null,
            (correlationId: string, args: Parameters) => {
                console.log("command 2");
                return null;
            }
        );
    }
}

After running it, this code produces the following output:

figure 1

using System;
using System.Collections.Generic;

using PipServices3.Commons.Commands;
using PipServices3.Commons.Run;

namespace ExampleApp
{
    class Program
    {

        static void Main(string[] args)
        {
            var mySet = new MyCommandSet();

            mySet.ExecuteAsync(null, "command1", null).Wait();
            mySet.ExecuteAsync(null, "command2", null).Wait();
            mySet.ExecuteAsync(null, "command3", null).Wait();
            mySet.ExecuteAsync(null, "command1B", null).Wait();

        }
    }

    public class MyCommandSetB : CommandSet
    {
        public MyCommandSetB() : base()
        {
            AddCommand(Command1B());
        }

        private ICommand Command1B()
        {
            return new Command("command1B",
                null,
                async (string correlationId, Parameters args) =>
                {
                    Console.WriteLine("command 1B");
                    return null;
                }
            );
        }
    }

    public class MyCommandSet : CommandSet
    {
        private CommandSet _commandSet = new MyCommandSetB();
        public MyCommandSet() : base()
        {
            AddCommand(Command1());
            AddCommandSet(_commandSet);
            AddCommands(new List<ICommand> { Command2(), Command3() });
        }

        private ICommand Command1()
        {
            return new Command("command1",
                null,
                async (string correlationId, Parameters args) =>
                {
                    Console.WriteLine("command 1");
                    return null;
                }
            );
        }

        private ICommand Command2()
        {
            return new Command("command2",
                null,
                async (string correlationId, Parameters args) =>
                {
                    Console.WriteLine("command 2");
                    return null;
                }
            );
        }

        private ICommand Command3()
        {
            return new Command("command3",
                null,
                async (string correlationId, Parameters args) =>
                {
                    Console.WriteLine("command 3");
                    return null;
                }
            );
        }
    }
}

After running it, this code produces the following output:

figure 1

package main

import (
	"context"
	"fmt"

	ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
	crun "github.com/pip-services3-gox/pip-services3-commons-gox/run"
)

func main() {

	mySet := NewMyCommandSet()

	mySet.Execute(context.Background(), "", "command1", nil)
	mySet.Execute(context.Background(), "", "command2", nil)
	mySet.Execute(context.Background(), "", "command3", nil)
	mySet.Execute(context.Background(), "", "command1B", nil)
}

type MyCommandSetB struct {
	ccmd.CommandSet
}

func NewMyCommandSetB() *MyCommandSetB {
	c := &MyCommandSetB{
		CommandSet: *ccmd.NewCommandSet(),
	}

	c.AddCommand(c.command1B())
	return c
}

func (c *MyCommandSetB) command1B() ccmd.ICommand {
	return ccmd.NewCommand(
		"command1B",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 1B")
			return
		},
	)
}

type MyCommandSet struct {
	ccmd.CommandSet
}

func NewMyCommandSet() *MyCommandSet {
	c := &MyCommandSet{
		CommandSet: *ccmd.NewCommandSet(),
	}

	c.AddCommand(c.command1())
	c.AddCommandSet(&NewMyCommandSetB().CommandSet)
	c.AddCommands([]ccmd.ICommand{c.command2(), c.command3()})
	return c
}

func (c *MyCommandSet) command1() ccmd.ICommand {
	return ccmd.NewCommand(
		"command1",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 1")
			return
		},
	)
}

func (c *MyCommandSet) command2() ccmd.ICommand {
	return ccmd.NewCommand(
		"command2",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 2")
			return
		},
	)
}

func (c *MyCommandSet) command3() ccmd.ICommand {
	return ccmd.NewCommand(
		"command3",
		nil,
		func(ctx context.Context, correlationId string, args *crun.Parameters) (result interface{}, err error) {
			fmt.Println("command 3")
			return
		},
	)
}

After running it, this code produces the following output:

figure 1

import 'package:pip_services3_commons/pip_services3_commons.dart';

void main(List<String> argument) async {
  var mySet = MyCommandSet();

  await mySet.execute(null, 'command1', Parameters());
  await mySet.execute(null, 'command2', Parameters());
  await mySet.execute(null, 'command3', Parameters());
  await mySet.execute(null, 'command1B', Parameters());
}

class MyCommandSetB extends CommandSet {
  MyCommandSetB() : super() {
    addCommand(command1B());
  }

  ICommand command1B() {
    return Command('command1B', null,
        (String? correlationId, Parameters args) async => print('command 1B'));
  }
}

class MyCommandSet extends CommandSet {
  MyCommandSet() : super() {
    addCommandSet(MyCommandSetB());
    addCommand(command1());
    addCommands([command2(), command3()]);
  }

  ICommand command1() {
    return Command('command1', null,
        (String? correlationId, Parameters args) async => print('command 1'));
  }

  ICommand command2() {
    return Command('command2', null,
        (String? correlationId, Parameters args) async => print('command 2'));
  }

  ICommand command3() {
    return Command('command3', null,
        (String? correlationId, Parameters args) async => print('command 3'));
  }
}

After running it, this code produces the following output:

figure 1

class MyCommandSetB(CommandSet):

    def __init__(self, controller):
        super(MyCommandSetB, self).__init__()
        self.add_command(self._command1B())
        
    def _command1B(self):
        def handler(correlation_id, args):
            return print("command 1B")
        return Command("command1B",None,handler)

class MyCommandSet(CommandSet):

    _command_set = MyCommandSetB(None)
    def __init__(self, controller):
        super(MyCommandSet, self).__init__()
        self.add_command(self._command1())
        self.add_commands([self._command2(),self._command3()])
        self.add_command_set(self._command_set)
        
    def _command1(self):
        def handler(correlation_id, args):
            return print("command 1")
        return Command("command1",None,handler)
    
    def _command2(self):
        def handler(correlation_id, args):
            return print("command 2")
        return Command("command2",None,handler)
   
    def _command3(self):
        def handler(correlation_id, args):
            return print("command 3")
        return Command("command3",None,handler)  
    
mySet = MyCommandSet(None)

mySet.execute(None, "command1", None)
mySet.execute(None, "command2", None)
mySet.execute(None, "command3", None)
mySet.execute(None, "command1B", None)

After running it, this code produces the following output:

figure 1

Not available

Example 2

import { Command, CommandSet, ICommand, IEvent, Parameters, Event, IEventListener } from "pip-services3-commons-nodex";

// Step 1 - Create the command set with events
export class MyEventSet extends CommandSet {
    public constructor() {
        super();
        this.addEvent(this.event1());
        this.addEvents([this.event2(), this.event3()]);
        this.addListener(this.listener1());
    }

    private event1(): IEvent {
        return new Event("event1");
    }

    private event2(): IEvent {
        return new Event("event2");
    }

    private event3(): IEvent {
        return new Event("event3");
    }

    private listener1(): IEventListener {
        return new MyListener();
    }
}

// Step 2 - Create a listener
export class MyListener implements IEventListener {
    onEvent(correlationId: string, event: IEvent, args: Parameters): void {
        console.log("Fired event name " + event.getName());
    }
} 


// Step 3  - Create an instance of the command set
let myEvents = new MyEventSet();

// Step 4 - Obtain events
let event1 = myEvents.findEvent("event1");
let events = myEvents.getEvents();  // Returns a list with event1, event2 and event3

// Step 5 - Select event1 (first element in the list)
let event2 = events[1];  // Returns event1

// Step 6 - Notify the listener of an event occurrence
event1.notify("123", null);
event2.notify("123", null);
myEvents.notify("123", "event3", null);

After running, this code produces the following output:

figure 2

from pip_services3_commons.commands import Command, CommandSet, ICommand, IEvent, Event, IEventListener

// Step 1 - Create the command set with events
public class MyEventSet : CommandSet
{
    public MyEventSet() : base()
    {
        AddEvent(Event1());
        AddEvents(new List<IEvent> { Event2(), Event3() });
        AddListener(Listener1());
    }

    private IEvent Event1()
    {
        return new Event("event1");
    }

    private IEvent Event2()
    {
        return new Event("event2");
    }

    private IEvent Event3()
    {
        return new Event("event3");
    }

    private IEventListener Listener1()
    {
        return new MyListener();
    }
}

// Step 2 - Create a listener
public class MyListener : IEventListener
{
    public void OnEvent(string correlationId, IEvent e, Parameters value)
    {
        Console.WriteLine("Fired event name " + e.Name);
    }
}


// Step 3  - Create an instance of the command set
var myEvents = new MyEventSet();

// Step 4 - Obtain events
var event1 = myEvents.FindEvent("event1");
var events = myEvents.Events;  // Returns a list with event1, event2 and event3

// Step 5 - Select event1 (first element in the list)
var event2 = events[1];  // Returns event1

// Step 6 - Notify the listener of an event occurrence
await event1.NotifyAsync("123", null);
await event2.NotifyAsync("123", null);
await myEvents.NotifyAsync("123", "event3", null);

After running, this code produces the following output:

figure 2

import (
	"fmt"

	ccmd "github.com/pip-services3-gox/pip-services3-commons-gox/commands"
	crun "github.com/pip-services3-gox/pip-services3-commons-gox/run"
)

// Step 1 - Create the command set with events
type MyEventSet struct {
	ccmd.CommandSet
}

func NewMyEventSet() *MyEventSet {
	c := &MyEventSet{
		CommandSet: *ccmd.NewCommandSet(),
	}

	c.AddEvent(c.event1())
	c.AddEvents([]ccmd.IEvent{c.event2(), c.event3()})
	c.AddListener(c.listener1())
	return c
}

func (c *MyEventSet) event1() ccmd.IEvent {
	return ccmd.NewEvent("event1")
}

func (c *MyEventSet) event2() ccmd.IEvent {
	return ccmd.NewEvent("event2")
}

func (c *MyEventSet) event3() ccmd.IEvent {
	return ccmd.NewEvent("event3")
}

func (c *MyEventSet) listener1() ccmd.IEventListener {
	return NewMyListener()
}

// Step 2 - Create a listener
type MyListener struct{}

func (c *MyListener) OnEvent(ctx context.Context, correlationId string, event ccmd.IEvent, value *crun.Parameters) {
	fmt.Println("Fired event " + event.Name())
}

func NewMyListener() *MyListener {
	return &MyListener{}
}


// Step 3  - Create an instance of the command set
myEvents := NewMyEventSet()

// Step 4 - Obtain events
event1 := myEvents.FindEvent("event1")
events := myEvents.Events()  // Returns a list with event1, event2 and event3

// Step 5 - Select event1 (first element in the list)
event2 := events[1]  // Returns event1

// Step 6 - Notify the listener of an event occurrence
event1.Notify(context.Background(), "123", nil)
event2.Notify(context.Background(), "123", nil)
myEvents.Notify(context.Background(), "123", "event3", nil)

After running, this code produces the following output:

figure 2

import 'package:pip_services3_commons/pip_services3_commons.dart';

// Step 1 - Create the command set with events
class MyEventSet extends CommandSet {
  MyEventSet() : super() {
    addEvent(event1());
    addEvents([event2(), event3()]);
    addListener(listener1());
  }

  IEvent event1() {
    return Event('event1');
  }

  IEvent event2() {
    return Event('event2');
  }

  IEvent event3() {
    return Event('event3');
  }

  IEventListener listener1() {
    return MyListener();
  }
}

// Step 2 - Create a listener
class MyListener implements IEventListener {
  @override
  void onEvent(String? correlationId, IEvent event, Parameters args) {
    print('Fired event name ' + event.getName());
  }
}


// Step 3  - Create an instance of the command set
var myEvents = MyEventSet();

// Step 4 - Obtain events
var event1 = myEvents.findEvent('event1');
var events = myEvents.getEvents();  // Returns a list with event1, event2 and event3

// Step 5 - Select event1 (first element in the list)
var event2 = events[1];  // Returns event1

// Step 6 - Notify the listener of an event occurrence
event1!.notify('123', Parameters());
event2.notify('123', Parameters());
myEvents.notify('123', 'event3', Parameters());

After running, this code produces the following output:

figure 2

from pip_services3_commons.commands import Command, CommandSet, ICommand, IEvent, Event, IEventListener

# Step 1 - Create the command set with events

class my_eventset(CommandSet):

    def __init__(self, controller):
        super(my_eventset, self).__init__()
        self.add_event(self._event1())  
        self.add_events([self._event2(), self._event3()])  
        self.add_listener(self._listener1()) 
        

    def _event1(self):
        event = Event("event1")
        event.add_listener(MyListener())
        return Event("event1")

    def _event2(self):
        event = Event("event2")
        event.add_listener(MyListener())
        return Event("event2")

    def _event3(self):
        event = Event("event3")
        event.add_listener(MyListener())
        return Event("event3")

    def _listener1(self):
        return MyListener()

# Step 2 - Create a listener
class MyListener(IEventListener):
    def on_event(self, correlation_id, event, args):
        print("Fired event with name " + event.get_name())

# Step 3  - Create an instance of the command set
my_events = my_eventset(None)

# Step 4 - Obtain events
event1 = my_events.find_event("event1")
events = my_events.get_events()  # Returns a list with event1, event2 and event3

# Step 5 - Select event1 (first element in the list)
event2 = events[1]  # Returns event1

# Step 6 - Notify the listener of an event occurrence
event1.notify("123", None)
event2.notify("123", None)
my_events.notify("123", 'event3', None)

After running, this code produces the following output:

figure 2

Not available

Wrapping up

In this tutorial, we have seen how to create a CommandSet that contains commands and events. We have also seen how to extract and use those commands and events and perform some operations with them.