Skip to content

Commit

Permalink
backend/reqif: reqif_to_sdoc: simplify iteration
Browse files Browse the repository at this point in the history
Related to #1788
  • Loading branch information
stanislaw committed Apr 22, 2024
1 parent 4290f1c commit 3c904b7
Showing 1 changed file with 56 additions and 47 deletions.
103 changes: 56 additions & 47 deletions strictdoc/backend/reqif/p01_sdoc/reqif_to_sdoc_converter.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
*,
Expand All @@ -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 (
Expand Down

0 comments on commit 3c904b7

Please sign in to comment.