1

我已经构建了一个agent将在环境中实例化多次的泛型:

unit agent_u {
    monitor : monitor_u is instance;
};

监视器打印一些消息,例如:

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

我想在监视器的消息中添加哪个代理实例打印了它们。例如,对于环境:

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

理想的输出将是这样的:

first_agent: Hello monitor!
second_agent: Hello monitor!

我在反射 API 中找不到与实例名称相关的任何内容...有没有办法在 e 中打印实例名称?

谢谢您的帮助

4

2 回答 2

2

使用 message() 打印将已经包含实例指针,在您的情况下类似于:

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

你可以通过这些@NUM来区分。

或在您的消息中包含“me.e_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!
于 2018-06-04T12:05:56.883 回答
1

不确定“实例名称”到底指的是什么,但有几点需要注意。

有预定义e_path()的方法any_unit

struct 和 unit 实例有一个唯一标识符,由类型名称和唯一编号组成,由to_string()方法返回。此唯一标识符已作为消息的一部分打印。

此外,您可以使用预定义的 hook 方法create_formatted_message(),最好与定义您自己的 new 一起使用message_format,来自定义消息的打印方式。例如e_path(),如果您希望它自动出现在所有消息中,您可以将结果附加到格式化的消息字符串中。

您还可以使用该short_name()钩子为单元提供您自己的符号名称,这些名称将出现在消息中,而不是to_string().

原则上,您也可以覆盖to_string()自身,但这会产生比消息打印输出更多的效果。

于 2018-06-04T14:17:23.547 回答