Skip to content

Commit

Permalink
simplify env structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Oct 28, 2024
1 parent d1ae655 commit 72aaa72
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 54 deletions.
2 changes: 0 additions & 2 deletions examples/environments/chatroom/envs/immutable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ def __init__(
value: Any,
listeners: dict[str, List[EventListener]] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
super().__init__(
name=name,
listeners=listeners,
children=children,
parent=parent,
)
self._value = value

Expand Down
3 changes: 0 additions & 3 deletions examples/environments/chatroom/envs/map2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ def __init__(
self,
name: str,
children: List[Env] = None,
parent: Env = None,
) -> None:
"""Initialize a Map2D env.
Args:
name (`str`): The name of the env.
children (`List[envibute]`): The children of the env. Note
that all children must be Movable2D.
parent (`envibute`): The parent of the env.
"""
for child in children if children else []:
if not isinstance(child, Movable2D):
Expand All @@ -60,7 +58,6 @@ def __init__(
super().__init__(
name=name,
children=children,
parent=parent,
)

@event_func
Expand Down
2 changes: 0 additions & 2 deletions examples/environments/chatroom/envs/mutable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ def __init__(
value: Any,
listeners: dict[str, List[EventListener]] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
super().__init__(
name=name,
listeners=listeners,
children=children,
parent=parent,
)
self._value = value

Expand Down
4 changes: 0 additions & 4 deletions examples/environments/chatroom/envs/point2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ def __init__(
y: float,
listeners: dict[str, List[EventListener]] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
super().__init__(
name=name,
listeners=listeners,
children=children,
parent=parent,
)
self.x = x
self.y = y
Expand Down Expand Up @@ -75,14 +73,12 @@ def __init__(
y: float,
listeners: dict[str, List[EventListener]] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
super().__init__(
name=name,
value=value,
listeners=listeners,
children=children,
parent=parent,
)
self.add_child(Point2D("position", x, y))

Expand Down
2 changes: 0 additions & 2 deletions examples/environments/chatroom/envs/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ def __init__(
unit: int = 1,
end: Optional[int] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
super().__init__(
name=name,
children=children,
parent=parent,
)
self.cur_time = start
self.unit = unit
Expand Down
42 changes: 1 addition & 41 deletions src/agentscope/environment/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Env(ABC, metaclass=RpcMeta):
Each env has its own name and value, and multiple envs can
be organized into a tree structure, where each env can have
multiple children envs and one parent env.
multiple children envs.
Different implementations of envs may have different event
functions, which are marked by `@event_func`.
Expand All @@ -115,22 +115,6 @@ def name(self) -> str:
`str`: The name of the env.
"""

@abstractmethod
def get_parent(self) -> Env:
"""Get the parent env of the current env.
Returns:
`Env`: The parent env.
"""

@abstractmethod
def set_parent(self, parent: Env) -> None:
"""Set the parent env of the current env.
Args:
parent (`Env`): The parent env.
"""

@abstractmethod
def get_children(self) -> dict[str, Env]:
"""Get the children envs of the current env.
Expand Down Expand Up @@ -225,7 +209,6 @@ def __init__(
name: str,
listeners: dict[str, List[EventListener]] = None,
children: List[Env] = None,
parent: Env = None,
) -> None:
"""Init an BasicEnv instance.
Expand All @@ -235,14 +218,11 @@ def __init__(
listener dict. Defaults to None.
children (`List[Env]`, optional): A list of children
envs. Defaults to None.
parent (`Env`, optional): The parent env. Defaults
to None.
"""
self._name = name
self.children = {
child.name: child for child in (children if children else [])
}
self.parent = parent
self.event_listeners = {}
if listeners:
for target_func, listener in listeners.items():
Expand All @@ -257,24 +237,6 @@ def name(self) -> str:
"""Name of the env"""
return self._name

def get_parent(self) -> Env:
"""Get the parent env of the current env.
Returns:
`Env`: The parent env.
"""
return self.parent

def set_parent(self, parent: Env) -> None:
"""Set the parent env of the current env.
Args:
parent (`Env`): The parent env.
"""
if self.parent is not None:
self.parent.remove_child(self.name)
self.parent = parent

def get_children(self) -> dict[str, Env]:
"""Get the children envs of the current env.
Expand All @@ -296,7 +258,6 @@ def add_child(self, child: Env) -> bool:
if child.name in self.children:
return False
self.children[child.name] = child
child.set_parent(self)
return True

def remove_child(self, children_name: str) -> bool:
Expand Down Expand Up @@ -392,7 +353,6 @@ def __setitem__(self, env_name: str, env: Env) -> None:
raise TypeError("Only Env can be set")
if env_name not in self.children:
self.children[env_name] = env
env.set_parent(self)
logger.debug(f"Set Env[{env_name}] as child of Env[{self.name}]")
else:
raise EnvAlreadyExistError(env_name)

0 comments on commit 72aaa72

Please sign in to comment.