1

I've built a generic agent that will be instantiated several times in the environment:

unit agent_u {
    monitor : monitor_u is instance;
};

The monitor prints some messages, e.g.:

unit monitor_u {
    run() is also {
        message(LOW, "Hello monitor!");
    };
};

I would like to add to the monitor's messages which agent instance has printed them. For example, for environment:

extend sys {
    first_agent : agent_u is instance;
    second_agent : agent_u is instance;
};

the desirable output will be something like that:

first_agent: Hello monitor!
second_agent: Hello monitor!

I could not find anything related to instance name in the reflection API... Is there some way to print an instance name in e?

Thank you for your help

Halona
  • 1,475
  • 1
  • 15
  • 26
  • 1
    The `e_path()` method of a unit returns a string representing the full instance hierarchy of the unit. May not be what you require though. – Thorsten Jun 04 '18 at 11:43
  • It's cool - it does shows the instance name. Thank you – Halona Jun 04 '18 at 14:07

2 Answers2

2

printing with message() will already include the instance pointer, in your case something like:

[0] agent_u-@1: Hello monitor!
[0] agent_u-@2: Hello monitor!

you can distinguish by these @NUM.

or include "me.e_path()" in your message, which will give the full instance path:

     message(LOW, me.e_path(), ": Hello monitor!");

[0] agent_u-@1: sys.first_agent.monitor: Hello monitor! 
[0] agent_u-@2: sys.second_agent.monitor: Hello monitor!
Nils
  • 131
  • 1
1

Not sure what exactly you refer to by "the instance name", but there are several points to be aware of.

There is the predefined e_path() method of any_unit.

There is a unique identifier for struct and unit instances, consisting of the type name and a unique number, which is returned by to_string() method. This unique identifier is already printed as part of a message.

Moreover, you can use the predefined hook method create_formatted_message(), preferably together with defining your own new message_format, to customize how messages are printed. For example, you might append the result of e_path() to the formatted message string, if you wish it to automatically appear in all messages.

You can also use the short_name() hook to provide your own symbolic names to units, and those will appear in messages instead of the result of to_string().

In principle, you can also override to_string() itself, but that would have more effects than just messages printout.

Yuri Tsoglin
  • 963
  • 4
  • 7