|
| 1 | +from enum import Enum |
| 2 | +from typing import Any, Dict, Optional |
| 3 | + |
| 4 | +from reqif.models.reqif_spec_object import ReqIFSpecObject |
| 5 | +from reqif.models.reqif_spec_object_type import ( |
| 6 | + ReqIFSpecObjectType, |
| 7 | + SpecAttributeDefinition, |
| 8 | +) |
| 9 | +from reqif.reqif_bundle import ReqIFBundle |
| 10 | + |
| 11 | + |
| 12 | +class ReqIFSchema: |
| 13 | + class ReqIFSchemaType(Enum): |
| 14 | + POLARION = 1 |
| 15 | + DEFAULT = 2 |
| 16 | + |
| 17 | + def __init__(self, reqif_bundle: ReqIFBundle): |
| 18 | + data_type_definitions: Dict[str, Any] = {} |
| 19 | + spec_object_type_attributes: Dict[str, SpecAttributeDefinition] = {} |
| 20 | + spec_object_type_names: Dict[str, str] = {} |
| 21 | + detected_section_spec_type: Optional[ReqIFSpecObjectType] = None |
| 22 | + detected_chapter_name_attribute: Optional[ |
| 23 | + SpecAttributeDefinition |
| 24 | + ] = None |
| 25 | + detected_schema_type: ReqIFSchema.ReqIFSchemaType = ( |
| 26 | + ReqIFSchema.ReqIFSchemaType.DEFAULT |
| 27 | + ) |
| 28 | + |
| 29 | + # First, collect iterate over all spec object types and collect all |
| 30 | + # attributes which are essentially the requirements fields. |
| 31 | + assert reqif_bundle.core_content is not None |
| 32 | + assert reqif_bundle.core_content.req_if_content is not None |
| 33 | + assert reqif_bundle.core_content.req_if_content.spec_types is not None |
| 34 | + for spec_type in reqif_bundle.core_content.req_if_content.spec_types: |
| 35 | + if not isinstance(spec_type, ReqIFSpecObjectType): |
| 36 | + continue |
| 37 | + |
| 38 | + assert spec_type.long_name is not None |
| 39 | + if spec_type.long_name == "Heading": |
| 40 | + detected_section_spec_type = spec_type |
| 41 | + detected_schema_type = ReqIFSchema.ReqIFSchemaType.POLARION |
| 42 | + |
| 43 | + spec_object_type_names[spec_type.identifier] = spec_type.long_name |
| 44 | + |
| 45 | + assert spec_type.attribute_definitions is not None |
| 46 | + for attribute_definition in spec_type.attribute_definitions: |
| 47 | + if attribute_definition.long_name == "ReqIF.ChapterName": |
| 48 | + detected_chapter_name_attribute = attribute_definition |
| 49 | + spec_object_type_attributes[ |
| 50 | + attribute_definition.identifier |
| 51 | + ] = attribute_definition |
| 52 | + |
| 53 | + assert reqif_bundle.core_content.req_if_content.data_types is not None |
| 54 | + for data_type in reqif_bundle.core_content.req_if_content.data_types: |
| 55 | + data_type_definitions[data_type.identifier] = data_type |
| 56 | + |
| 57 | + self.data_type_definitions: Dict[str, Any] = data_type_definitions |
| 58 | + self.spec_object_type_attributes: Dict[ |
| 59 | + str, SpecAttributeDefinition |
| 60 | + ] = spec_object_type_attributes |
| 61 | + self.spec_object_type_names: Dict[str, str] = spec_object_type_names |
| 62 | + self.reqif_bundle: ReqIFBundle = reqif_bundle |
| 63 | + self.detected_heading_spec_type = detected_section_spec_type |
| 64 | + self.detected_chapter_name_attribute: Optional[ |
| 65 | + SpecAttributeDefinition |
| 66 | + ] = detected_chapter_name_attribute |
| 67 | + self.detected_schema_type: ReqIFSchema.ReqIFSchemaType = ( |
| 68 | + detected_schema_type |
| 69 | + ) |
| 70 | + |
| 71 | + def is_spec_object_a_heading(self, spec_object: ReqIFSpecObject): |
| 72 | + if self.detected_heading_spec_type is not None: |
| 73 | + return ( |
| 74 | + spec_object.spec_object_type |
| 75 | + == self.detected_heading_spec_type.identifier |
| 76 | + ) |
| 77 | + if self.detected_chapter_name_attribute is not None: |
| 78 | + return ( |
| 79 | + self.detected_chapter_name_attribute.identifier |
| 80 | + in spec_object.attribute_map |
| 81 | + ) |
| 82 | + |
| 83 | + # If neither a Heading/Section spec type nor ReqIF.ChapterName attribute |
| 84 | + # could be detected, assume that all spec objects in the ReqIF file are |
| 85 | + # simply requirements. |
| 86 | + return False |
0 commit comments