Skip to content

Commit da65059

Browse files
authored
Merge pull request #163 from strictdoc-project/stanislaw/capella_fixture
ReqIF-to-JSON example: simplify iteration/conversion procedure
2 parents aef6d00 + 3263c33 commit da65059

1 file changed

Lines changed: 50 additions & 36 deletions

File tree

  • tests/integration/examples/04_convert_reqif_to_json

tests/integration/examples/04_convert_reqif_to_json/script.py

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
from dataclasses import dataclass, field
55
from pathlib import Path
6-
from typing import Dict, List, Optional, Union
6+
from typing import Any, Dict, List, Optional
77

88
from dataclasses_json import dataclass_json
99

@@ -15,6 +15,7 @@
1515
ReqIFDataTypeDefinitionXHTML,
1616
)
1717
from reqif.models.reqif_spec_object import ReqIFSpecObject
18+
from reqif.models.reqif_specification import ReqIFSpecification
1819
from reqif.parser import ReqIFParser, ReqIFZParser
1920
from reqif.reqif_bundle import ReqIFBundle, ReqIFZBundle
2021

@@ -96,54 +97,67 @@ def convert(reqif_bundle: ReqIFBundle) -> ReqDict:
9697
specification_dict: Specification = Specification(
9798
name=specification.long_name, nodes=[]
9899
)
99-
section_stack: List[Union[Specification, Node]] = [
100-
specification_dict
101-
]
102100

103-
for (
104-
current_hierarchy
105-
) in reqif_bundle.iterate_specification_hierarchy(specification):
101+
def node_converter_lambda(
102+
current_hierarchy_,
103+
current_section_,
104+
):
106105
spec_object = reqif_bundle.get_spec_object_by_ref(
107-
current_hierarchy.spec_object
106+
current_hierarchy_.spec_object
108107
)
109108
node: Node = ReqIFToDictConverter.convert_spec_object_to_node(
110109
spec_object,
111110
reqif_bundle,
112111
reqif_schema,
113-
current_hierarchy.level,
112+
current_hierarchy_.level,
114113
)
115-
current_node = section_stack[-1]
116-
if not reqif_schema.is_spec_object_a_heading(spec_object):
117-
if node.level > current_node.level:
118-
assert node.level == (
119-
current_node.level + 1
120-
), "Something went wrong with the spec hierarchy levels."
121-
current_node.nodes.append(node)
122-
elif node.level == current_node.level:
123-
section_stack.pop()
124-
current_node = section_stack[-1]
125-
current_node.nodes.append(node)
126-
else:
127-
raise NotImplementedError
128-
else:
129-
if node.level > current_node.level:
130-
section_stack.append(node)
131-
current_node.nodes.append(node)
132-
elif node.level == current_node.level:
133-
section_stack[-1] = node
134-
current_node.nodes.append(node)
135-
else:
136-
while current_node.level >= node.level:
137-
assert not isinstance(current_node, Specification)
138-
section_stack.pop()
139-
current_node = section_stack[-1]
140-
section_stack.append(node)
141-
current_node.nodes.append(node)
114+
current_section_.nodes.append(node)
115+
is_section = reqif_schema.is_spec_object_a_heading(spec_object)
116+
return node, is_section
117+
118+
ReqIFToDictConverter._iterate(
119+
specification,
120+
reqif_bundle,
121+
specification_dict,
122+
lambda s: s.level,
123+
node_converter_lambda,
124+
)
142125

143126
reqif_dict.documents.append(specification_dict)
144127

145128
return reqif_dict
146129

130+
@staticmethod
131+
def _iterate(
132+
specification: ReqIFSpecification,
133+
reqif_bundle: ReqIFBundle,
134+
root_node: Any,
135+
get_level_lambda,
136+
node_converter_lambda,
137+
):
138+
section_stack: List = [root_node]
139+
140+
for current_hierarchy in reqif_bundle.iterate_specification_hierarchy(
141+
specification
142+
):
143+
current_section = section_stack[-1]
144+
section_level = get_level_lambda(current_section)
145+
146+
if current_hierarchy.level <= section_level:
147+
for _ in range(
148+
0,
149+
(section_level - current_hierarchy.level) + 1,
150+
):
151+
assert len(section_stack) > 0
152+
section_stack.pop()
153+
154+
current_section = section_stack[-1]
155+
converted_node, converted_node_is_section = node_converter_lambda(
156+
current_hierarchy, current_section
157+
)
158+
if converted_node_is_section:
159+
section_stack.append(converted_node)
160+
147161
@staticmethod
148162
def convert_spec_object_to_node(
149163
spec_object: ReqIFSpecObject,

0 commit comments

Comments
 (0)