# Path: siebenapp/domain.py
# class Graph:
#     """Base interface definition"""
# 
#     NO_VALUE = "no value"
# 
#     def __init__(self, goaltree=None):
#         self.goaltree: Graph = goaltree
# 
#     def __getattr__(self, item):
#         """When method is not found, ask nested goaltree for it"""
#         if self.goaltree != self:
#             return getattr(self.goaltree, item)
#         return None
# 
#     def accept(self, command: Command) -> None:
#         """React on the given command"""
#         method_name = "accept_" + command.__class__.__name__
#         if method := getattr(self, method_name):
#             method(command)
#         elif parent := self.goaltree:
#             parent.accept(command)
#         else:
#             raise NotImplementedError(f"Cannot find method {method_name}")
# 
#     def accept_all(self, *commands: Command) -> None:
#         """React on the command chain"""
#         for command in commands:
#             self.accept(command)
# 
#     def settings(self, key: str) -> Any:
#         """Returns given inner value by the key"""
#         if self.goaltree is not None:
#             return self.goaltree.settings(key)
#         return Graph.NO_VALUE
# 
#     def reconfigure_from(self, origin: "Graph") -> None:
#         """Synchronize *non-persistent* settings from the given origin"""
#         if self.goaltree:
#             self.goaltree.reconfigure_from(origin)
# 
#     def selections(self) -> Set[int]:
#         """Return ids of special 'selection' goals"""
#         if self.goaltree:
#             return self.goaltree.selections()
#         raise NotImplementedError
# 
#     def events(self) -> deque:
#         """Returns queue of applied actions.
#         Note: this queue is modifiable -- you may push new events into it. But this
#         behavior may be changed in future."""
#         if self.goaltree is not None:
#             return self.goaltree.events()
#         raise NotImplementedError
# 
#     def q(self, keys: str = "name") -> Dict[int, Any]:
#         """Run search query against content"""
#         raise NotImplementedError
# 
#     def error(self, message: str) -> None:
#         """Show error message"""
#         if self.goaltree is not None:
#             self.goaltree.error(message)
# 
#     def verify(self) -> bool:
#         """Check all inner data for correctness"""
#         if self.goaltree is not None:
#             return self.goaltree.verify()
#         return False
# 
# class EdgeType(IntEnum):
#     BLOCKER = 1
#     PARENT = 2
# 
# class Edge:
#     source: int
#     target: int
#     type: EdgeType
# 
# class HoldSelect(Command):
#     """Saves current selection into the "previous selection" state"""
# 
# class ToggleClose(Command):
#     """Close an open selected goal. Re-open a closed selected goal"""
# 
#     root: int = 0
# 
# class Delete(Command):
#     """Remove given or selected goal whether it exists. Do nothing in other case"""
# 
#     goal_id: int = 0
# 
# class ToggleLink(Command):
#     """Create or remove a link between two given goals, if possible"""
# 
#     lower: int = 0
#     upper: int = 0
#     edge_type: EdgeType = EdgeType.BLOCKER
# 
# class Add(Command):
#     """Add a new goal to the existing tree"""
# 
#     name: str
#     add_to: int = 0
#     edge_type: EdgeType = EdgeType.PARENT
# 
# class Select(Command):
#     """Select a goal by its id whether it exist. Do nothing in other case"""
# 
#     goal_id: int
# 
# class Insert(Command):
#     """Add an intermediate goal between two selected goals"""
# 
#     name: str
# 
# class Rename(Command):
#     """Change a name of the given goal"""
# 
#     new_name: str
#     goal_id: int = 0


from typing import Callable, Dict, Optional, List, Set, Any, Tuple
from collections import deque, defaultdict
from siebenapp.domain import (
    Graph,
    EdgeType,
    Edge,
    HoldSelect,
    ToggleClose,
    Delete,
    ToggleLink,
    Add,
    Select,
    Insert,
    Rename,
)
                self.settings("selection"),
                self.settings("previous_selection"),
            ]
            if k is not None
        )

    def events(self) -> deque:
        return self._events

    def accept_Add(self, command: Add) -> bool:
        add_to = command.add_to or self.selection
        if add_to in self.closed:
            self.error("A new subgoal cannot be added to the closed one")
            return False
        next_id = self._add_no_link(command.name)
        self.accept_ToggleLink(ToggleLink(add_to, next_id, command.edge_type))
        return True

    def _add_no_link(self, name: str) -> int:
        next_id = max(list(self.goals.keys()) + [0]) + 1
        self.goals[next_id] = name
        self._events.append(("add", next_id, name, True))
        return next_id

    def accept_Select(self, command: Select):
        goal_id = command.goal_id
        if goal_id in self.goals and self.goals[goal_id] is not None:
            self.selection = goal_id
            self._events.append(("select", goal_id))


# Path: siebenapp/domain.py
# class Graph:
#     """Base interface definition"""
# 
#     NO_VALUE = "no value"
# 
#     def __init__(self, goaltree=None):
#         self.goaltree: Graph = goaltree
# 
#     def __getattr__(self, item):
#         """When method is not found, ask nested goaltree for it"""
#         if self.goaltree != self:
#             return getattr(self.goaltree, item)
#         return None
# 
#     def accept(self, command: Command) -> None:
#         """React on the given command"""
#         method_name = "accept_" + command.__class__.__name__
#         if method := getattr(self, method_name):
#             method(command)
#         elif parent := self.goaltree:
#             parent.accept(command)
#         else:
#             raise NotImplementedError(f"Cannot find method {method_name}")
# 
#     def accept_all(self, *commands: Command) -> None:
#         """React on the command chain"""
#         for command in commands:
#             self.accept(command)
# 
#     def settings(self, key: str) -> Any:
#         """Returns given inner value by the key"""
#         if self.goaltree is not None:
#             return self.goaltree.settings(key)
#         return Graph.NO_VALUE
# 
#     def reconfigure_from(self, origin: "Graph") -> None:
#         """Synchronize *non-persistent* settings from the given origin"""
#         if self.goaltree:
#             self.goaltree.reconfigure_from(origin)
# 
#     def selections(self) -> Set[int]:
#         """Return ids of special 'selection' goals"""
#         if self.goaltree:
#             return self.goaltree.selections()
#         raise NotImplementedError
# 
#     def events(self) -> deque:
#         """Returns queue of applied actions.
#         Note: this queue is modifiable -- you may push new events into it. But this
#         behavior may be changed in future."""
#         if self.goaltree is not None:
#             return self.goaltree.events()
#         raise NotImplementedError
# 
#     def q(self, keys: str = "name") -> Dict[int, Any]:
#         """Run search query against content"""
#         raise NotImplementedError
# 
#     def error(self, message: str) -> None:
#         """Show error message"""
#         if self.goaltree is not None:
#             self.goaltree.error(message)
# 
#     def verify(self) -> bool:
#         """Check all inner data for correctness"""
#         if self.goaltree is not None:
#             return self.goaltree.verify()
#         return False
# 
# class EdgeType(IntEnum):
#     BLOCKER = 1
#     PARENT = 2
# 
# class Edge:
#     source: int
#     target: int
#     type: EdgeType
# 
# class HoldSelect(Command):
#     """Saves current selection into the "previous selection" state"""
# 
# class ToggleClose(Command):
#     """Close an open selected goal. Re-open a closed selected goal"""
# 
#     root: int = 0
# 
# class Delete(Command):
#     """Remove given or selected goal whether it exists. Do nothing in other case"""
# 
#     goal_id: int = 0
# 
# class ToggleLink(Command):
#     """Create or remove a link between two given goals, if possible"""
# 
#     lower: int = 0
#     upper: int = 0
#     edge_type: EdgeType = EdgeType.BLOCKER
# 
# class Add(Command):
#     """Add a new goal to the existing tree"""
# 
#     name: str
#     add_to: int = 0
#     edge_type: EdgeType = EdgeType.PARENT
# 
# class Select(Command):
#     """Select a goal by its id whether it exist. Do nothing in other case"""
# 
#     goal_id: int
# 
# class Insert(Command):
#     """Add an intermediate goal between two selected goals"""
# 
#     name: str
# 
# class Rename(Command):
#     """Change a name of the given goal"""
# 
#     new_name: str
#     goal_id: int = 0


# Path: siebenapp/goaltree.py
from typing import Callable, Dict, Optional, List, Set, Any, Tuple
from collections import deque, defaultdict
from siebenapp.domain import (
    Graph,
    EdgeType,
    Edge,
    HoldSelect,
    ToggleClose,
    Delete,
    ToggleLink,
    Add,
    Select,
    Insert,
    Rename,
)

                self.settings("selection"),
                self.settings("previous_selection"),
            ]
            if k is not None
        )

    def events(self) -> deque:
        return self._events

    def accept_Add(self, command: Add) -> bool:
        add_to = command.add_to or self.selection
        if add_to in self.closed:
            self.error("A new subgoal cannot be added to the closed one")
            return False
        next_id = self._add_no_link(command.name)
        self.accept_ToggleLink(ToggleLink(add_to, next_id, command.edge_type))
        return True

    def _add_no_link(self, name: str) -> int:
        next_id = max(list(self.goals.keys()) + [0]) + 1
        self.goals[next_id] = name
        self._events.append(("add", next_id, name, True))
        return next_id

    def accept_Select(self, command: Select):
        goal_id = command.goal_id
        if goal_id in self.goals and self.goals[goal_id] is not None:
            self.selection = goal_id
            self._events.append(("select", goal_id))


    def accept_HoldSelect(self, command: HoldSelect):