Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for mqtt 2 #25

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.8.3
- Fix bug in mqtt disconnect callback

## 0.8.2
- Realtime Environment always has a clock to prevent agents that define callbacks only from terminating
- Environment time in real time is now based on system time, decoupling it from the simpy process
Expand Down
7 changes: 2 additions & 5 deletions agentlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
Besides import of submodules, nothing happens here.
"""

from . import core
from . import utils
from . import models
from . import modules
from . import core, utils, models, modules
from .core import Agent, BaseModule, BaseModuleConfig, Environment, Model, ModelConfig
from .core.datamodels import *
from .utils.multi_agent_system import (
Expand All @@ -15,7 +12,7 @@
LocalCloneMAPAgency,
)

__version__ = "0.8.2"
__version__ = "0.8.3"

__all__ = [
"core",
Expand Down
14 changes: 13 additions & 1 deletion agentlib/core/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class AgentConfig(BaseModel):
description="Maximal number of waiting items in data-broker queues. "
"Set to -1 for infinity"
)
log_level: Optional[str] = Field(
default=None,
description="The log level for this Agent and its Modules. "
"Default uses the root-loggers level."
"Will not override custom log-levels specified in Modules."
"Options: DEBUG; INFO; WARNING; ERROR; CRITICAL",
)

@field_validator("modules")
@classmethod
Expand Down Expand Up @@ -92,6 +99,8 @@ def __init__(self, *, config, env: Environment):
data_broker_logger = agentlib_logging.create_logger(
env=self.env, name=f"{config.id}/DataBroker"
)
if config.log_level is not None:
data_broker_logger.setLevel(config.log_level)
if env.config.rt:
self._data_broker = RTDataBroker(
env=env, logger=data_broker_logger,
Expand All @@ -108,7 +117,6 @@ def __init__(self, *, config, env: Environment):
# Setup logger
self.logger = agentlib_logging.create_logger(env=self.env, name=self.id)


# Register the thread monitoring if configured
if env.config.rt:
self.env.process(self._monitor_threads())
Expand Down Expand Up @@ -256,6 +264,10 @@ def _register_modules(self):
# Insert default module id if it did not exist:
module_config.update({"module_id": _module_id})

# Insert agent log-level if no log_level is specified:
if "log_level" not in module_config:
module_config["log_level"] = self.config.log_level

if _module_id in updated_modules:
raise KeyError(
f"Module with module_id '{_module_id}' "
Expand Down
4 changes: 2 additions & 2 deletions agentlib/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic import ConfigDict, BaseModel, Field, field_validator
import numpy as np
from pydantic.fields import PrivateAttr
from pydantic_core.core_schema import FieldValidationInfo
from pydantic_core.core_schema import ValidationInfo

from agentlib.core.datamodels import (
ModelVariable,
Expand Down Expand Up @@ -80,7 +80,7 @@ def check_name(cls, name):
@field_validator("parameters", "inputs", "outputs", "states", mode="after")
@classmethod
def include_default_model_variables(
cls, _: List[ModelVariable], info: FieldValidationInfo
cls, _: List[ModelVariable], info: ValidationInfo
):
"""
Validator building block to merge default variables with config variables in a standard validator.
Expand Down
45 changes: 30 additions & 15 deletions agentlib/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BaseModuleConfig(BaseModel):
log_level: Optional[str] = Field(
default=None,
description="The log level for this Module. "
"Default uses the root-loggers level."
"Default uses the agent or root-loggers level."
"Options: DEBUG; INFO; WARNING; ERROR; CRITICAL",
)
shared_variable_fields: List[str] = Field(
Expand Down Expand Up @@ -209,7 +209,7 @@ def merge_variables(
This function:

- Collects all variables
- Checks if duplicate names (will cause errors in the get() function.
- Checks if duplicate names (will cause errors in the get() function).
"""
_vars = []
# Extract all variables from fields
Expand All @@ -226,25 +226,40 @@ def merge_variables(
update_var_with = user_config.get(field_name, {})

make_shared = field_name in shared_variable_fields

var = update_default_agent_variable(
default_var=field.default,
user_data=update_var_with,
make_shared=make_shared,
agent_id=agent_id,
field_name=field_name,
)
try:
var = update_default_agent_variable(
default_var=field.default,
user_data=update_var_with,
make_shared=make_shared,
agent_id=agent_id,
field_name=field_name,
)
except Exception as err:
logger.error(
"Could not update the default config of AgentVariable field '%s'. "
"You most probably used some validator on this field. "
"Error message: %s. User config: %s",
field_name, err, update_var_with
)
var = pre_merged_attr
_vars.append(var)
pre_validated_instance.__setattr__(field_name, var)

elif is_list_of_agent_variables(pre_merged_attr):
user_config_var_dicts = user_config.get(field_name, [])
type_ = pre_merged_attr[0].__class__
update_vars_with = [
conf
for conf in user_config_var_dicts
if is_valid_agent_var_config(conf, field_name, type_)
]
update_vars_with = []
for conf in user_config_var_dicts:
try:
is_valid_agent_var_config(conf, field_name, type_)
update_vars_with.append(conf)
except Exception as err:
logger.error(
"Could not update the default config of AgentVariables '%s'."
"You most probably used some validator on this field. "
"Error message: %s. User config: %s",
field_name, err, conf
)

make_shared = field_name in shared_variable_fields
variables = include_defaults_in_root(
Expand Down
Loading