Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Nov 26, 2024
1 parent d4d8c11 commit 40e9bf7
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 52 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,40 +533,40 @@ connector = generate_connector(sc_type.CONST_PERM_POS_ARC, src, trg)
erase_connectors(src, trg, sc_type.CONST_PERM_POS_ARC) # True
```

### Getting connectors
### Searching connectors

For getting connector or connectors between two elements use:

```python
def get_connector(source: ScAddr, target: ScAddr, connector_type: ScType) -> ScAddr: ...
def search_connector(source: ScAddr, target: ScAddr, connector_type: ScType) -> ScAddr: ...


def get_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) -> List[ScAddr]: ...
def search_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) -> List[ScAddr]: ...
```

_**NOTE: Use VAR type instead of CONST in getting utils**_

```python
from sc_client.constants import sc_type
from sc_kpm.utils import generate_nodes, generate_connector
from sc_kpm.utils import get_connector, get_connectors
from sc_kpm.utils import search_connector, search_connectors

src, trg = generate_nodes(*[sc_type.CONST_NODE] * 2)
connector1 = generate_connector(sc_type.CONST_PERM_POS_ARC, src, trg)
connector2 = generate_connector(sc_type.CONST_COMMON_ARC, src, trg)

class_connector = get_connector(src, trg, sc_type.VAR_PERM_POS_ARC) # ScAddr(...)
class_connector = search_connector(src, trg, sc_type.VAR_PERM_POS_ARC) # ScAddr(...)
assert class_connector == connector1
connectors = get_connectors(src, trg, sc_type.VAR_PERM_POS_ARC, sc_type.VAR_COMMON_ARC) # [ScAddr(...), ScAddr(...)]
connectors = search_connectors(src, trg, sc_type.VAR_PERM_POS_ARC, sc_type.VAR_COMMON_ARC) # [ScAddr(...), ScAddr(...)]
assert connectors == [connector1, connector2]
```

### Getting elements by relation
### Searching elements by relation

Get target element by source element and relation:
Search target element by source element and relation:

```python
def get_element_by_role_relation(src: ScAddr, rrel_node: ScAddr) -> ScAddr: ...
def search_element_by_role_relation(src: ScAddr, rrel_node: ScAddr) -> ScAddr: ...


def search_element_by_non_role_relation(src: ScAddr, nrel_node: ScAddr) -> ScAddr: ...
Expand All @@ -577,13 +577,13 @@ 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, search_element_by_non_role_relation
from sc_kpm.utils import search_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(...)
result_rrel = search_element_by_role_relation(src, ScKeynodes.rrel_index(1)) # ScAddr(...)
assert result_rrel == trg_rrel
result_nrel = search_element_by_non_role_relation(src, ScKeynodes[CommonIdentifiers.NREL_SYSTEM_IDENTIFIER]) # ScAddr(...)
assert result_nrel == trg_nrel
Expand Down
11 changes: 6 additions & 5 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
| create_role_relation | generate_role_relation |
| create_norole_relation | generate_non_role_relation |
| check_edge | check_connector |
| get_edge | get_connector |
| get_edges | get_connectors |
| get_edge | search_connector |
| get_edges | search_connectors |
| get_system_idtf | get_element_system_identifier |
| search_norole_relation_template | search_non_role_relation_template |
| get_element_by_norole_relation | search_element_by_non_role_relation |
| get_element_by_role_relation | search_element_by_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`, `search_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`, `search_connector`, `search_connectors`, `get_element_system_identifier`, `search_non_role_relation_template`, `search_element_by_role_relation`, `search_element_by_non_role_relation`, `erase_connectors`
- Action utils methods: `generate_action_result`, `generate_action`

### Deprecated
- ScKeynodes method `delete`
- Common utils methods: `create_nodes`, `create_node`, `create_links`, `create_link`, `create_edges`, `create_edge`, `create_binary_relation`, `create_role_relation`, `create_norole_relation`, `check_edge`, `get_edge`, `get_edges`, `get_system_idtf`, `search_norole_relation_template`, `get_element_by_norole_relation`, `delete_edges`
- Common utils methods: `create_nodes`, `create_node`, `create_links`, `create_link`, `create_edges`, `create_edge`, `create_binary_relation`, `create_role_relation`, `create_norole_relation`, `check_edge`, `get_edge`, `get_edges`, `get_system_idtf`, `search_norole_relation_template`, `get_element_by_role_relation`, `get_element_by_norole_relation`, `delete_edges`
- Action utils methods: `create_action_result`, `create_action`

### Changed
Expand Down
5 changes: 3 additions & 2 deletions src/sc_kpm/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@
generate_nodes,
generate_non_role_relation,
generate_role_relation,
get_connector,
get_connectors,
get_edge,
get_edges,
get_element_by_norole_relation,
get_element_by_role_relation,
get_element_system_identifier,
get_link_content_data,
get_system_idtf,
search_connector,
search_connectors,
search_element_by_non_role_relation,
search_element_by_role_relation,
search_role_relation_template,
)
4 changes: 2 additions & 2 deletions src/sc_kpm/utils/action_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
generate_node,
generate_non_role_relation,
generate_role_relation,
get_element_by_role_relation,
search_element_by_role_relation,
)

COMMON_WAIT_TIME: float = 5
Expand All @@ -42,7 +42,7 @@ def check_action_class(action_class: Union[ScAddr, Idtf], action_node: ScAddr) -
def get_action_arguments(action_node: ScAddr, count: int) -> List[ScAddr]:
arguments = []
for index in range(1, count + 1):
argument = get_element_by_role_relation(action_node, ScKeynodes.rrel_index(index))
argument = search_element_by_role_relation(action_node, ScKeynodes.rrel_index(index))
arguments.append(argument)
return arguments

Expand Down
28 changes: 18 additions & 10 deletions src/sc_kpm/utils/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def create_norole_relation(src: ScAddr, trg: ScAddr, *nrel_nodes: ScAddr) -> ScA


def check_connector(connector_type: ScType, source: ScAddr, target: ScAddr) -> bool:
return bool(get_connectors(source, target, connector_type))
return bool(search_connectors(source, target, connector_type))


def check_edge(connector_type: ScType, source: ScAddr, target: ScAddr) -> bool:
Expand All @@ -167,20 +167,20 @@ def check_edge(connector_type: ScType, source: ScAddr, target: ScAddr) -> bool:
return check_connector(connector_type, source, target)


def get_connector(source: ScAddr, target: ScAddr, connector_type: ScType) -> ScAddr:
connectors = get_connectors(source, target, connector_type)
def search_connector(source: ScAddr, target: ScAddr, connector_type: ScType) -> ScAddr:
connectors = search_connectors(source, target, connector_type)
return connectors[0] if connectors else ScAddr(0)


def get_edge(source: ScAddr, target: ScAddr, connector_type: ScType) -> ScAddr:
warnings.warn(
"Common utils 'get_edge' method is deprecated. Use `get_connector` method instead.",
"Common utils 'get_edge' method is deprecated. Use `search_connector` method instead.",
DeprecationWarning,
)
return get_connector(source, target, connector_type)
return search_connector(source, target, connector_type)


def get_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) -> List[ScAddr]:
def search_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) -> List[ScAddr]:
result_connectors = []
for connector_type in connector_types:
templ = ScTemplate()
Expand All @@ -192,10 +192,10 @@ def get_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) ->

def get_edges(source: ScAddr, target: ScAddr, *connector_types: ScType) -> List[ScAddr]:
warnings.warn(
"Common utils 'get_edges' method is deprecated. Use `get_connectors` method instead.",
"Common utils 'get_edges' method is deprecated. Use `search_connectors` method instead.",
DeprecationWarning,
)
return get_connectors(source, target, *connector_types)
return search_connectors(source, target, *connector_types)


def get_element_system_identifier(addr: ScAddr) -> Idtf:
Expand Down Expand Up @@ -252,11 +252,19 @@ def search_norole_relation_template(src: ScAddr, nrel_node: ScAddr) -> Optional[
return search_non_role_relation_template(src, nrel_node)


def get_element_by_role_relation(src: ScAddr, rrel_node: ScAddr) -> ScAddr:
def search_element_by_role_relation(src: ScAddr, rrel_node: ScAddr) -> ScAddr:
search_result = search_role_relation_template(src, rrel_node)
return search_result.get(ScAlias.ELEMENT) if search_result else ScAddr(0)


def get_element_by_role_relation(src: ScAddr, rrel_node: ScAddr) -> ScAddr:
warnings.warn(
"Common utils 'get_element_by_role_relation' method is deprecated. Use `search_element_by_role_relation` method instead.",
DeprecationWarning,
)
return search_element_by_role_relation(src, rrel_node)


def search_element_by_non_role_relation(src: ScAddr, nrel_node: ScAddr) -> ScAddr:
search_result = search_non_role_relation_template(src, nrel_node)
return search_result.get(ScAlias.ELEMENT) if search_result else ScAddr(0)
Expand All @@ -276,7 +284,7 @@ def get_link_content_data(link: ScAddr) -> ScLinkContentData:


def erase_connectors(source: ScAddr, target: ScAddr, *connector_types: ScType) -> bool:
return client.erase_elements(*get_connectors(source, target, *connector_types))
return client.erase_elements(*search_connectors(source, target, *connector_types))


def delete_edges(source: ScAddr, target: ScAddr, *connector_types: ScType) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_keynodes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from common_tests import BaseTestCase
from sc_client import client
from sc_client.client import get_elements_types, erase_elements
from sc_client.client import erase_elements, get_elements_types
from sc_client.constants import sc_type
from sc_client.constants.exceptions import InvalidValueError
from sc_client.models import ScAddr, ScIdtfResolveParams
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils/test_action_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
from sc_client.constants.common import ScEventType

from sc_kpm import ScAgent, ScKeynodes, ScModule
from sc_kpm.identifiers import CommonIdentifiers, ActionStatus
from sc_kpm.identifiers import ActionStatus, CommonIdentifiers
from sc_kpm.sc_result import ScResult
from sc_kpm.utils.action_utils import (
add_action_arguments,
call_action,
call_agent,
check_action_class,
generate_action,
execute_action,
execute_agent,
finish_action_with_status,
generate_action,
wait_agent,
)
from sc_kpm.utils.common_utils import check_connector, generate_connector, generate_node
Expand Down
34 changes: 17 additions & 17 deletions tests/test_utils/test_common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"""

import pytest

from sc_client import client
from sc_client.client import erase_elements
from sc_client.constants import sc_type, exceptions
from sc_client.constants import exceptions, sc_type

from sc_kpm import ScKeynodes
from sc_kpm.utils.common_utils import (
check_connector,
erase_connectors,
generate_binary_relation,
generate_connector,
generate_connectors,
Expand All @@ -22,18 +22,17 @@
generate_nodes,
generate_non_role_relation,
generate_role_relation,
erase_connectors,
get_connector,
get_connectors,
search_element_by_non_role_relation,
get_element_by_role_relation,
get_link_content_data,
get_element_system_identifier,
get_link_content_data,
search_connector,
search_connectors,
search_element_by_non_role_relation,
search_element_by_role_relation,
)
from tests.common_tests import BaseTestCase


class TestActionUtils(BaseTestCase):
class TestCommonUtils(BaseTestCase):
def test_node_utils(self):
node = generate_node(sc_type.VAR_NODE_ROLE)
node_2 = generate_node(sc_type.CONST_NODE_CLASS)
Expand Down Expand Up @@ -72,26 +71,26 @@ def test_link_utils(self):

def test_connector_utils(self):
source, target = generate_nodes(sc_type.CONST_NODE_CLASS, sc_type.CONST_NODE)
empty = get_connector(source, target, sc_type.VAR_PERM_POS_ARC)
empty = search_connector(source, target, sc_type.VAR_PERM_POS_ARC)
assert empty.is_valid() is False

connector = generate_connector(sc_type.CONST_PERM_POS_ARC, source, target)
assert connector.is_valid()
same_connector = get_connector(source, target, sc_type.VAR_PERM_POS_ARC)
same_connector = search_connector(source, target, sc_type.VAR_PERM_POS_ARC)
assert same_connector.is_valid() and same_connector.value == connector.value
assert check_connector(sc_type.VAR_PERM_POS_ARC, source, target)

source, target, target2 = generate_nodes(sc_type.CONST_NODE_CLASS, sc_type.CONST_NODE, sc_type.CONST_NODE)
connectors = generate_connectors(sc_type.CONST_PERM_POS_ARC, source, target, target2)
assert all(connector_.is_valid() for connector_ in connectors)
same_target1 = get_connector(source, target, sc_type.VAR_PERM_POS_ARC)
same_target2 = get_connector(source, target2, sc_type.VAR_PERM_POS_ARC)
same_target1 = search_connector(source, target, sc_type.VAR_PERM_POS_ARC)
same_target2 = search_connector(source, target2, sc_type.VAR_PERM_POS_ARC)
assert connectors == [same_target1, same_target2]

connector_counter = 10
for _ in range(connector_counter):
generate_connector(sc_type.CONST_PERM_POS_ARC, source, target)
result = get_connectors(source, target, sc_type.VAR_PERM_POS_ARC)
result = search_connectors(source, target, sc_type.VAR_PERM_POS_ARC)
assert len(result) == connector_counter + 1
for connector in result:
assert connector.is_valid()
Expand All @@ -115,8 +114,8 @@ def test_relation_utils(self):
assert result[0].is_pos() and result[1].is_pos()
assert result[2].is_pos() is False and result[3].is_pos() is False

expected_rrel_target = get_element_by_role_relation(src, rrel_node)
expected_empty = get_element_by_role_relation(src, nrel_node)
expected_rrel_target = search_element_by_role_relation(src, rrel_node)
expected_empty = search_element_by_role_relation(src, nrel_node)
assert expected_rrel_target.is_valid()
assert expected_rrel_target.value == rrel_trg.value
assert expected_empty.is_valid() is False
Expand All @@ -138,4 +137,5 @@ def test_deletion_utils(self):
nrel_connector = generate_non_role_relation(src, nrel_trg)
assert erase_connectors(src, rrel_trg, sc_type.VAR_PERM_POS_ARC)
assert erase_elements(nrel_connector, src, rrel_trg, nrel_trg)
self.assertRaises(exceptions.ServerError, client.check_elements(rrel_connector)[0])
with pytest.raises(exceptions.ServerError):
client.check_elements(rrel_connector)[0]
4 changes: 2 additions & 2 deletions tests/test_utils/test_iteration_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from common_tests import BaseTestCase
from sc_client.client import generate_elements, erase_elements
from sc_client.client import erase_elements, generate_elements
from sc_client.constants import sc_type
from sc_client.models import ScConstruction, ScLinkContent, ScLinkContentType

from sc_kpm.utils import generate_links
from sc_kpm.utils.iteration_utils import iter_link_contents_data, iter_links_data


class TestActionUtils(BaseTestCase):
class TestIterationUtils(BaseTestCase):
def test_iter_link_contents_data(self):
content1 = ScLinkContent(content1_data := "content1", ScLinkContentType.STRING)
content2 = ScLinkContent(content2_data := "content2", ScLinkContentType.STRING)
Expand Down

0 comments on commit 40e9bf7

Please sign in to comment.