From e1243ca17f707059b1ddb33a56609f03ffd80b2a Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Mon, 22 Apr 2024 22:11:39 +0200 Subject: [PATCH] backend/reqif: reqif_to_sdoc: simplify iteration Related to #1788 --- .github/workflows/ci-mac.yml | 4 +- .../reqif/p01_sdoc/reqif_to_sdoc_converter.py | 103 ++++++++++-------- 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/.github/workflows/ci-mac.yml b/.github/workflows/ci-mac.yml index f0a7c0240..1459d02a7 100644 --- a/.github/workflows/ci-mac.yml +++ b/.github/workflows/ci-mac.yml @@ -8,12 +8,12 @@ on: jobs: build: - runs-on: macOS-latest + runs-on: macos-13 strategy: matrix: python-version: [ - "3.7", "3.12" + "3.8", "3.12" ] steps: diff --git a/strictdoc/backend/reqif/p01_sdoc/reqif_to_sdoc_converter.py b/strictdoc/backend/reqif/p01_sdoc/reqif_to_sdoc_converter.py index cc07ce998..53f1f7fbd 100644 --- a/strictdoc/backend/reqif/p01_sdoc/reqif_to_sdoc_converter.py +++ b/strictdoc/backend/reqif/p01_sdoc/reqif_to_sdoc_converter.py @@ -1,5 +1,5 @@ # mypy: disable-error-code="arg-type,no-untyped-call,no-untyped-def,union-attr,operator" -from typing import Dict, List, Optional, Set, Union +from typing import Any, Dict, List, Optional, Set, Union from reqif.models.reqif_data_type import ReqIFDataTypeDefinitionEnumeration from reqif.models.reqif_spec_object import ReqIFSpecObject @@ -88,6 +88,38 @@ def convert_requirement_field_from_reqif(field_name: str) -> str: return REQIF_MAP_TO_SDOC_FIELD_MAP[field_name] return field_name + @staticmethod + def _iterate( + specification: ReqIFSpecification, + reqif_bundle: ReqIFBundle, + section_stack: List[Any], + get_level_lambda, + ): + for current_hierarchy in reqif_bundle.iterate_specification_hierarchy( + specification + ): + current_section = section_stack[-1] + section_level = get_level_lambda(current_section) + + spec_object = reqif_bundle.get_spec_object_by_ref( + current_hierarchy.spec_object + ) + + if current_hierarchy.level <= section_level: + for _ in range( + 0, + (section_level - current_hierarchy.level) + 1, + ): + assert len(section_stack) > 0 + section_stack.pop() + + is_section = P01_ReqIFToSDocConverter.is_spec_object_section( + spec_object, + reqif_bundle=reqif_bundle, + ) + + yield current_hierarchy, section_stack, spec_object, is_section + @staticmethod def _create_document_from_reqif_specification( *, @@ -99,66 +131,43 @@ def _create_document_from_reqif_specification( ) elements: List[GrammarElement] = [] document.section_contents = [] - section_stack = [document] + section_stack: List[Union[SDocDocument, SDocSection]] = [document] used_spec_object_types_ids: Set[str] = set() - for current_hierarchy in reqif_bundle.iterate_specification_hierarchy( - specification + for ( + current_hierarchy_, + section_stack_, + spec_object_, + is_section_, + ) in P01_ReqIFToSDocConverter._iterate( + specification, + reqif_bundle, + section_stack, + lambda s: s.ng_level, ): - current_section = section_stack[-1] + used_spec_object_types_ids.add(spec_object_.spec_object_type) - spec_object = reqif_bundle.get_spec_object_by_ref( - current_hierarchy.spec_object - ) - used_spec_object_types_ids.add(spec_object.spec_object_type) - if P01_ReqIFToSDocConverter.is_spec_object_section( - spec_object, - reqif_bundle=reqif_bundle, - ): + current_section: Union[SDocDocument, SDocSection] = section_stack_[ + -1 + ] + if is_section_: section = ( P01_ReqIFToSDocConverter.create_section_from_spec_object( - spec_object, - current_hierarchy.level, + spec_object_, + current_hierarchy_.level, reqif_bundle=reqif_bundle, ) ) - if current_hierarchy.level > current_section.ng_level: - current_section.section_contents.append(section) - section_stack.append(section) - elif current_hierarchy.level < current_section.ng_level: - for _ in range( - 0, - (current_section.ng_level - current_hierarchy.level) - + 1, - ): - assert len(section_stack) > 0 - section_stack.pop() - current_section = section_stack[-1] - current_section.section_contents.append(section) - section_stack.append(section) - else: - section_stack.pop() - current_section = section_stack[-1] - current_section.section_contents.append(section) - section_stack.append(section) - elif P01_ReqIFToSDocConverter.is_spec_object_requirement( - spec_object - ): + current_section.section_contents.append(section) + section_stack.append(section) + else: requirement: SDocNode = P01_ReqIFToSDocConverter.create_requirement_from_spec_object( - spec_object=spec_object, + spec_object=spec_object_, parent_section=section_stack[-1], reqif_bundle=reqif_bundle, - level=current_hierarchy.level, + level=current_hierarchy_.level, ) - for _ in range( - 0, (current_section.ng_level - current_hierarchy.level) + 1 - ): - assert len(section_stack) > 0 - section_stack.pop() - current_section = section_stack[-1] current_section.section_contents.append(requirement) - else: - raise NotImplementedError(spec_object) from None # See SDOC_IMPL_1. if (