diff --git a/README.md b/README.md index c2f5ec5..06a6480 100644 --- a/README.md +++ b/README.md @@ -533,15 +533,15 @@ 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**_ @@ -549,24 +549,24 @@ _**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: ... @@ -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 diff --git a/docs/changelog.md b/docs/changelog.md index 2debc57..1192fa0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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 diff --git a/src/sc_kpm/utils/__init__.py b/src/sc_kpm/utils/__init__.py index 4c51aad..aa639ae 100644 --- a/src/sc_kpm/utils/__init__.py +++ b/src/sc_kpm/utils/__init__.py @@ -28,8 +28,6 @@ generate_nodes, generate_non_role_relation, generate_role_relation, - get_connector, - get_connectors, get_edge, get_edges, get_element_by_norole_relation, @@ -37,6 +35,9 @@ 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, ) diff --git a/src/sc_kpm/utils/action_utils.py b/src/sc_kpm/utils/action_utils.py index 899b3c3..1d73f35 100644 --- a/src/sc_kpm/utils/action_utils.py +++ b/src/sc_kpm/utils/action_utils.py @@ -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 @@ -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 diff --git a/src/sc_kpm/utils/common_utils.py b/src/sc_kpm/utils/common_utils.py index 8ff4082..625f553 100644 --- a/src/sc_kpm/utils/common_utils.py +++ b/src/sc_kpm/utils/common_utils.py @@ -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: @@ -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() @@ -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: @@ -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) @@ -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: diff --git a/tests/test_keynodes.py b/tests/test_keynodes.py index 4329a49..7c61910 100644 --- a/tests/test_keynodes.py +++ b/tests/test_keynodes.py @@ -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 diff --git a/tests/test_utils/test_action_utils.py b/tests/test_utils/test_action_utils.py index f0baa3e..e18bb24 100644 --- a/tests/test_utils/test_action_utils.py +++ b/tests/test_utils/test_action_utils.py @@ -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 diff --git a/tests/test_utils/test_common_utils.py b/tests/test_utils/test_common_utils.py index 8fb425a..1a0325a 100644 --- a/tests/test_utils/test_common_utils.py +++ b/tests/test_utils/test_common_utils.py @@ -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, @@ -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) @@ -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() @@ -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 @@ -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] diff --git a/tests/test_utils/test_iteration_utils.py b/tests/test_utils/test_iteration_utils.py index 5a84dea..46056d5 100644 --- a/tests/test_utils/test_iteration_utils.py +++ b/tests/test_utils/test_iteration_utils.py @@ -1,5 +1,5 @@ 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 @@ -7,7 +7,7 @@ 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)