Environment configuration
| 229 | ) |
| 230 | |
| 231 | class EnvironmentConfig(BaseModel): |
| 232 | """Environment configuration""" |
| 233 | model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow") |
| 234 | |
| 235 | name: str = Field(description="The name of the environment") |
| 236 | description: str = Field(description="The description of the environment") |
| 237 | metadata: Optional[Dict[str, Any]] = Field(default_factory=dict, description="The metadata of the environment") |
| 238 | rules: str = Field(description="The rules of the environment") |
| 239 | version: str = Field(default="1.0.0", description="Version of the environment") |
| 240 | require_grad: bool = Field(default=False, description="Whether the environment requires gradients") |
| 241 | |
| 242 | cls: Optional[Type[Environment]] = Field(default=None, description="The class of the environment") |
| 243 | config: Optional[Dict[str, Any]] = Field(default={}, description="The initialization configuration of the environment") |
| 244 | instance: Optional[Any] = Field(default=None, description="The instance of the environment") |
| 245 | code: Optional[str] = Field(default=None, description="Source code for dynamically generated environment classes (used when cls cannot be imported from a module)") |
| 246 | |
| 247 | actions: Dict[str, ActionConfig] = Field(default_factory=dict, description="Dictionary of actions available in this environment") |
| 248 | |
| 249 | def model_dump(self, **kwargs) -> Dict[str, Any]: |
| 250 | """Dump the model to a dictionary, recursively serializing nested Pydantic models.""" |
| 251 | result = { |
| 252 | "name": self.name, |
| 253 | "description": self.description, |
| 254 | "metadata": self.metadata, |
| 255 | "rules": self.rules, |
| 256 | "version": self.version, |
| 257 | "require_grad": self.require_grad, |
| 258 | |
| 259 | "cls": dynamic_manager.get_class_string(self.cls) if self.cls else None, |
| 260 | "config": self.config, |
| 261 | "instance": None, |
| 262 | "code": self.code, |
| 263 | |
| 264 | "actions": {name: action_config.model_dump() for name, action_config in self.actions.items()}, |
| 265 | } |
| 266 | |
| 267 | return result |
| 268 | |
| 269 | @classmethod |
| 270 | def model_validate(cls, data: Dict[str, Any]) -> 'EnvironmentConfig': |
| 271 | """Validate the model from a dictionary.""" |
| 272 | |
| 273 | name = data.get("name") |
| 274 | description = data.get("description") |
| 275 | metadata = data.get("metadata") |
| 276 | rules = data.get("rules") |
| 277 | version = data.get("version") |
| 278 | require_grad = data.get("require_grad", False) |
| 279 | |
| 280 | cls_ = None |
| 281 | code = data.get("code") |
| 282 | if code: |
| 283 | class_name = dynamic_manager.extract_class_name_from_code(code) |
| 284 | if class_name: |
| 285 | try: |
| 286 | cls_ = dynamic_manager.load_class( |
| 287 | code, |
| 288 | class_name=class_name, |
no outgoing calls
no test coverage detected