Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Nov 14, 2024
1 parent a5cbb7a commit 7204fa2
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 70 deletions.
42 changes: 19 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,9 @@ class ScAgentClassicTest(ScAgentClassic):

For the ScAgent initialization you should define the sc-element and the type of the ScEvent.

For the ScAgentClassic initialization
you should define the identifier of the action class node and arguments of the ScAgent.
`event_class` is set to the `action_initiated` keynode by default.
`event_type` is set to the `ScEventType.AFTER_GENERATE_OUTGOING_ARC` type by default.
For the ScAgentClassic initialization you should define the identifier of the action class node and arguments of the ScAgent. `subscription_element` is set to the `action_initiated` keynode by default. `event_class` is set to the `ScEventType.AFTER_GENERATE_OUTGOING_ARC` type by default.

**ClassicScAgent checks its action element automatically and doesn't run `on_event` method if checking fails.**
**ScAgentClassic checks its action element automatically and doesn't run `on_event` method if checking fails.**

```python
from sc_client.constants import sc_type
Expand All @@ -94,7 +91,7 @@ action_class = ScKeynodes.resolve("test_class", sc_type.CONST_NODE_CLASS)
agent = ScAgentTest(action_class, ScEventType.AFTER_GENERATE_OUTGOING_ARC)

classic_agent = ScAgentClassicTest("classic_test_class")
classic_agent_ingoing = ScAgentClassicTest("classic_test_class", ScEventType.AFTER_GENERATE_INCOMING_ARC)
classic_agent_incoming = ScAgentClassicTest("classic_test_class", ScEventType.AFTER_GENERATE_INCOMING_ARC)
```

### ScModule
Expand Down Expand Up @@ -376,7 +373,7 @@ assert numbered_set.elements_list == elements
![numbered_set_example](docs/schemes/png/numbered_set.png)

Class for handling sc-numbered-set construction.
Set-node is connectord with numerating each element with rrel node.
Set-node is set with numerating each element with rrel node.
Easy access to elements by index (index i is marked with rrel(i + 1))

```python
Expand Down Expand Up @@ -462,14 +459,14 @@ For generating links with string type content (by default) use these functions:
def generate_link(
content: Union[str, int],
content_type: ScLinkContentType = ScLinkContentType.STRING,
link_type: ScType = sc_type.LINK_CONST
link_type: ScType = sc_type.CONST_NODE_LINK
) -> ScAddr: ...


def generate_links(
*contents: Union[str, int],
content_type: ScLinkContentType = ScLinkContentType.STRING,
link_type: ScType = sc_type.LINK_CONST,
link_type: ScType = sc_type.CONST_NODE_LINK,
) -> List[ScAddr]: ...
```

Expand All @@ -482,7 +479,7 @@ from sc_kpm.utils import generate_link, generate_links

msg = generate_link("hello") # ScAddr(...)
four = generate_link(4, ScLinkContentType.INT) # ScAddr(...)
water = generate_link("water", link_type=sc_type.LINK_VAR) # ScAddr(...)
water = generate_link("water", link_type=sc_type.VAR_NODE_LINK) # ScAddr(...)
names = generate_links("Sam", "Pit") # [ScAddr(...), ScAddr(...)]
```

Expand All @@ -500,6 +497,8 @@ def generate_role_relation(src: ScAddr, trg: ScAddr, *rrel_nodes: ScAddr) -> ScA
def generate_non_role_relation(src: ScAddr, trg: ScAddr, *nrel_nodes: ScAddr) -> ScAddr: ...
```

These methods return ScAddr of sc-arc generated between `src` and `trg` sc-elements.

```python
from sc_client.constants import sc_type
from sc_kpm import ScKeynodes
Expand All @@ -514,9 +513,9 @@ rrel = generate_role_relation(src, trg, ScKeynodes.rrel_index(1)) # ScAddr(...)
nrel = generate_non_role_relation(src, trg, generate_node(sc_type.CONST_NODE_NON_ROLE)) # ScAddr(...)
```

### Deleting utils
### Erasing utils

If you want to remove all connectors between two nodes, which define by their type use
If you want to erase all connectors between two elements, which define by their type use

```python
def erase_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) -> bool: ...
Expand All @@ -536,7 +535,7 @@ erase_connectors(src, trg, sc_type.CONST_PERM_POS_ARC) # True

### Getting connectors

For getting connector or connectors between two nodes use:
For getting connector or connectors between two elements use:

```python
def get_connector(source: ScAddr, target: ScAddr, connector_type: ScType) -> ScAddr: ...
Expand Down Expand Up @@ -570,23 +569,23 @@ Get target element by source element and relation:
def get_element_by_role_relation(src: ScAddr, rrel_node: ScAddr) -> ScAddr: ...


def get_element_by_non_role_relation(src: ScAddr, nrel_node: ScAddr) -> ScAddr: ...
def search_element_by_non_role_relation(src: ScAddr, nrel_node: ScAddr) -> ScAddr: ...
```

```python
from sc_client.constants import sc_type
from sc_kpm import ScKeynodes
from sc_kpm.identifiers import CommonIdentifiers
from sc_kpm.utils import generate_nodes, generate_role_relation, generate_non_role_relation
from sc_kpm.utils import get_element_by_role_relation, get_element_by_non_role_relation
from sc_kpm.utils import get_element_by_role_relation, search_element_by_non_role_relation

src, trg_rrel, trg_nrel = generate_nodes(*[sc_type.CONST_NODE] * 3)
rrel = generate_role_relation(src, trg_rrel, ScKeynodes.rrel_index(1)) # ScAddr(...)
nrel = generate_non_role_relation(src, trg_nrel, ScKeynodes[CommonIdentifiers.NREL_SYSTEM_IDENTIFIER]) # ScAddr(...)

result_rrel = get_element_by_role_relation(src, ScKeynodes.rrel_index(1)) # ScAddr(...)
assert result_rrel == trg_rrel
result_nrel = get_element_by_non_role_relation(src, ScKeynodes[CommonIdentifiers.NREL_SYSTEM_IDENTIFIER]) # ScAddr(...)
result_nrel = search_element_by_non_role_relation(src, ScKeynodes[CommonIdentifiers.NREL_SYSTEM_IDENTIFIER]) # ScAddr(...)
assert result_nrel == trg_nrel
```

Expand All @@ -608,7 +607,7 @@ content = get_link_content_data(water) # "water"

### Getting element system identifier

For getting element system identifier of keynode use:
For getting system identifier of keynode use:

```python
def get_element_system_identifier(addr: ScAddr) -> str: ...
Expand All @@ -617,12 +616,9 @@ def get_element_system_identifier(addr: ScAddr) -> str: ...
```python
from sc_client.constants import sc_type
from sc_kpm import ScKeynodes
from sc_kpm.utils import generate_node
from sc_kpm.utils import get_element_system_identifier

lang_en = generate_node(sc_type.CONST_NODE_CLASS) # ScAddr(...)
idtf = get_element_system_identifier(lang_en) # "lang_en"
assert ScKeynodes[idtf] == lang_en
```

## Action utils
Expand Down Expand Up @@ -713,8 +709,8 @@ from sc_kpm.utils import generate_node
from sc_kpm.utils.action_utils import generate_action_result, get_action_result
from sc_kpm.sc_sets import ScStructure

action_node = generate_node(sc_type.CONST_NODE_STRUCTURE)
result_element = generate_node(sc_type.CONST_NODE_STRUCTURE)
action_node = generate_node(sc_type.CONST_NODE)
result_element = generate_node(sc_type.CONST_NODE)
generate_action_result(action_node, result_element)
result = get_action_result(action_node)
result_elements = ScStructure(result).elements_set
Expand All @@ -734,7 +730,7 @@ def call_agent(
) -> ScAddr: ...
```

Agent wait function: Waits for generation of arc to reaction node for some seconds.
Agent wait function: Waits for generation of arc from reaction node for some seconds.
Default reaction_node is `action_finished`.

```python
Expand Down
4 changes: 2 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
| get_edges | get_connectors |
| get_system_idtf | get_element_system_identifier |
| search_norole_relation_template | search_non_role_relation_template |
| get_element_by_norole_relation | get_element_by_non_role_relation |
| get_element_by_norole_relation | search_element_by_non_role_relation |
| delete_edges | erase_connectors |
| delete | erase |

### Added
- ScKeynodes method `erase`
- Common utils methods: `generate_nodes`, `generate_node`, `generate_links`, `generate_link`, `generate_connectors`, `generate_connector`, `generate_binary_relation`, `generate_role_relation`, `generate_non_role_relation`, `check_connector`, `get_connector`, `get_connectors`, `get_element_system_identifier`, `search_non_role_relation_template`, `get_element_by_non_role_relation`, `erase_connectors`
- Common utils methods: `generate_nodes`, `generate_node`, `generate_links`, `generate_link`, `generate_connectors`, `generate_connector`, `generate_binary_relation`, `generate_role_relation`, `generate_non_role_relation`, `check_connector`, `get_connector`, `get_connectors`, `get_element_system_identifier`, `search_non_role_relation_template`, `search_element_by_non_role_relation`, `erase_connectors`
- Action utils methods: `generate_action_result`, `generate_action`

### Deprecated
Expand Down
4 changes: 2 additions & 2 deletions src/sc_kpm/sc_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sc_client.constants import sc_type
from sc_client.constants.common import ScEventType
from sc_client.constants.exceptions import InvalidValueError
from sc_client.models import ScAddr, ScEvent, ScEventSubscriptionParams
from sc_client.models import ScAddr, ScEventSubscription, ScEventSubscriptionParams

from sc_kpm.identifiers import ActionStatus
from sc_kpm.sc_keynodes import Idtf, ScKeynodes
Expand All @@ -24,7 +24,7 @@ class ScAgentAbstract(ABC):
def __init__(self, event_element: ScAddr, event_type: ScEventType) -> None:
self._event_element = event_element
self._event_type = event_type
self._event: Optional[ScEvent] = None
self._event: Optional[ScEventSubscription] = None
self.logger = getLogger(f"{self.__module__}.{self.__class__.__name__}")

@abstractmethod
Expand Down
5 changes: 4 additions & 1 deletion src/sc_kpm/sc_keynodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(cls, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
cls._dict: Dict[Idtf, ScAddr] = {}
cls._logger: Logger = getLogger(f"{__name__}.{cls.__name__}")
cls._min_rrel_index: int = 1
cls._max_rrel_index: int = 10

def __call__(cls, *args, **kwargs) -> None:
Expand Down Expand Up @@ -71,11 +72,13 @@ def resolve(cls, identifier: Idtf, sc_type: Optional[ScType]) -> ScAddr:
return addr

def rrel_index(cls, index: int) -> ScAddr:
"""Get rrel_i node. Max rrel index is 10"""
"""Get rrel_i node. Max rrel index is 10. Min rrel is 1."""
if not isinstance(index, int):
raise TypeError("Index of rrel node must be int")
if index > cls._max_rrel_index:
raise KeyError(f"You cannot use rrel more than {cls._max_rrel_index}")
if index < 1:
raise KeyError(f"You cannot use rrel less than {cls._min_rrel_index}")
return cls.resolve(f"rrel_{index}", CONST_NODE_ROLE) # pylint: disable=no-value-for-parameter


Expand Down
2 changes: 1 addition & 1 deletion src/sc_kpm/sc_sets/sc_numbered_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ScNumberedSet(ScSet):
ScNumberedSet is a class for handling numbered set structure in kb.
It has main set_node and arc elements:
Edge to each element marked with 'rrel_1', 'rrel_2', and so on nodes.
Arcs to each element marked with 'rrel_1', 'rrel_2', and so on nodes.
"""

def add(self, *elements: ScAddr) -> None:
Expand Down
44 changes: 22 additions & 22 deletions src/sc_kpm/sc_sets/sc_oriented_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@ class ScOrientedSet(ScSet):
ScOrientedSet is a class for handling oriented set structure in kb.
It has main set_node and arc elements:
Edge to the first element marked with 'rrel_1' node.
Arc to the first element marked with 'rrel_1' node.
The other have arcs between arcs from set_node marked with 'nrel_basic_sequence'.
"""

def add(self, *elements: ScAddr) -> None:
"""Add elements to ScOrientedSet"""
if elements:
elements_iterator = iter(elements)
current_connector = (
current_arc = (
self._generate_first_element_arc(next(elements_iterator))
if self.is_empty()
else self._get_last_arc_and_erase_rrel_last()
)
for element in elements_iterator:
current_connector = self._generate_next_arc(current_connector, element)
self._mark_connector_with_rrel_last(current_connector)
current_arc = self._generate_next_arc(current_arc, element)
self._mark_arc_with_rrel_last(current_arc)

def __iter__(self) -> Iterator[ScAddr]:
"""Iterate by ScOrientedSet elements"""
start_template = search_role_relation_template(self._set_node, ScKeynodes[CommonIdentifiers.RREL_ONE])
if not start_template:
return
yield start_template.get(ScAlias.ELEMENT)
next_connector = start_template.get(ScAlias.MEMBERSHIP_ARC)
next_arc = start_template.get(ScAlias.MEMBERSHIP_ARC)
while True:
elem_search_result = self._search_next_element_template(next_connector)
elem_search_result = self._search_next_element_template(next_arc)
if elem_search_result is None:
return
yield elem_search_result.get(ScAlias.ELEMENT)
next_connector = elem_search_result.get(ScAlias.MEMBERSHIP_ARC)
next_arc = elem_search_result.get(ScAlias.MEMBERSHIP_ARC)

@property
def elements_list(self) -> List[ScAddr]:
Expand All @@ -59,12 +59,12 @@ def remove(self, *elements: ScAddr) -> None:
self.add(*elements_new)

def _generate_first_element_arc(self, element: ScAddr) -> ScAddr:
"""Create marked with rrel_1 arc to first element"""
"""Generate marked with rrel_1 arc to first element"""
return generate_role_relation(self._set_node, element, ScKeynodes[CommonIdentifiers.RREL_ONE])

def _get_last_arc_and_erase_rrel_last(self) -> Optional[ScAddr]:
"""Search last connector of ScOrientedSet is it exists"""
# Search marked last connector
"""Search last arc of ScOrientedSet if it exists"""
# Search marked last arc
template = ScTemplate()
template.quintuple(
self._set_node,
Expand All @@ -76,27 +76,27 @@ def _get_last_arc_and_erase_rrel_last(self) -> Optional[ScAddr]:
last_elem_templates = search_by_template(template)
if last_elem_templates:
last_elem_template = last_elem_templates[0]
erase_elements(last_elem_template.get(ScAlias.RELATION_ARC)) # Erase arc between rrel_last and connector
erase_elements(last_elem_template.get(ScAlias.RELATION_ARC)) # Erase arc between rrel_last and arc
return last_elem_template.get(ScAlias.MEMBERSHIP_ARC)

# Search unmarked last connector
# Search unmarked last arc
next_elem_result = search_role_relation_template(self._set_node, ScKeynodes[CommonIdentifiers.RREL_ONE])
while True:
next_connector = next_elem_result.get(ScAlias.MEMBERSHIP_ARC)
next_elem_result = self._search_next_element_template(next_connector)
next_arc = next_elem_result.get(ScAlias.MEMBERSHIP_ARC)
next_elem_result = self._search_next_element_template(next_arc)
if next_elem_result is None:
return next_connector
return next_arc

def _generate_next_arc(self, previous_connector: ScAddr, element: ScAddr) -> ScAddr:
"""Create connector to element and connect with previous connector"""
def _generate_next_arc(self, previous_arc: ScAddr, element: ScAddr) -> ScAddr:
"""Generate arc to element and connect with previous arc"""
template = ScTemplate()
template.triple(
self._set_node,
sc_type.VAR_PERM_POS_ARC >> ScAlias.MEMBERSHIP_ARC,
element,
)
template.quintuple(
previous_connector,
previous_arc,
sc_type.VAR_COMMON_ARC,
ScAlias.MEMBERSHIP_ARC,
sc_type.VAR_PERM_POS_ARC,
Expand All @@ -105,17 +105,17 @@ def _generate_next_arc(self, previous_connector: ScAddr, element: ScAddr) -> ScA
return generate_by_template(template).get(ScAlias.MEMBERSHIP_ARC)

@staticmethod
def _mark_connector_with_rrel_last(last_connector: ScAddr) -> None:
def _mark_arc_with_rrel_last(last_arc: ScAddr) -> None:
generate_connector(
sc_type.CONST_PERM_POS_ARC,
ScKeynodes[CommonIdentifiers.RREL_LAST],
last_connector,
last_arc,
)

def _search_next_element_template(self, cur_element_connector: ScAddr) -> Optional[ScTemplateResult]:
def _search_next_element_template(self, cur_element_arc: ScAddr) -> Optional[ScTemplateResult]:
templ = ScTemplate()
templ.quintuple(
cur_element_connector,
cur_element_arc,
sc_type.VAR_COMMON_ARC,
sc_type.VAR_PERM_POS_ARC >> ScAlias.MEMBERSHIP_ARC,
sc_type.VAR_PERM_POS_ARC,
Expand Down
10 changes: 5 additions & 5 deletions src/sc_kpm/sc_sets/sc_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class ScSet:
"""
ScSet is a class for handling set construction in kb.
It has main set_node and arc elements.
It has main set_node and elements.
"""

def __init__(self, *elements: ScAddr, set_node: ScAddr = None, set_node_type: ScType = None) -> None:
"""
Initialize ScSet.
Receive or create set_node and add elements.
Receive or generate set_node and add elements.
:param elements: Elements of a set to initialize it.
:param set_node: Set node has connectors to each element.
Optional parameter: it will be created if it doesn't exist.
:param set_node: Set node has arcs to each element.
Optional parameter: it will be generated if it doesn't exist.
:param set_node_type: ScType for creating set node.
"""

Expand Down Expand Up @@ -79,7 +79,7 @@ def __contains__(self, element: ScAddr) -> bool:
return element in self.elements_set

def remove(self, *elements: ScAddr) -> None:
"""Remove the connections between set_node and elements"""
"""Erase the connections between set_node and elements"""
templ = ScTemplate()
for element in elements:
templ.triple(self._set_node, sc_type.VAR_PERM_POS_ARC, element)
Expand Down
2 changes: 1 addition & 1 deletion src/sc_kpm/sc_sets/sc_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ScStructure(ScSet):
"""
ScStructure is a class for handling structure construction in kb.
It has main set_node with type CONST_NODE_STRUCTURE and arc elements.
It has main set_node with type CONST_NODE_STRUCTURE and elements.
"""

def __init__(self, *elements: ScAddr, set_node: ScAddr = None, set_node_type: ScType = None) -> None:
Expand Down
Loading

0 comments on commit 7204fa2

Please sign in to comment.