1 | #!/usr/bin/env python |
---|
2 | # SPDX-License-Identifier: BSD-2-Clause |
---|
3 | """ Views the specification. """ |
---|
4 | |
---|
5 | # Copyright (C) 2021 embedded brains GmbH & Co. KG |
---|
6 | # |
---|
7 | # Redistribution and use in source and binary forms, with or without |
---|
8 | # modification, are permitted provided that the following conditions |
---|
9 | # are met: |
---|
10 | # 1. Redistributions of source code must retain the above copyright |
---|
11 | # notice, this list of conditions and the following disclaimer. |
---|
12 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | # notice, this list of conditions and the following disclaimer in the |
---|
14 | # documentation and/or other materials provided with the distribution. |
---|
15 | # |
---|
16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | # POSSIBILITY OF SUCH DAMAGE. |
---|
27 | |
---|
28 | import argparse |
---|
29 | import itertools |
---|
30 | import sys |
---|
31 | from typing import Any, Dict, List, Optional, Set, Tuple |
---|
32 | |
---|
33 | from rtemsspec.build import gather_files |
---|
34 | from rtemsspec.items import EmptyItem, Item, ItemCache, ItemMapper, \ |
---|
35 | ItemGetValueContext |
---|
36 | from rtemsspec.rtems import augment_with_test_links, gather_api_items, \ |
---|
37 | recursive_is_enabled, validate |
---|
38 | from rtemsspec.sphinxcontent import SphinxContent |
---|
39 | from rtemsspec.transitionmap import Transition, TransitionMap |
---|
40 | from rtemsspec.util import load_config |
---|
41 | from rtemsspec.validation import augment_with_test_case_links |
---|
42 | |
---|
43 | _CHILD_ROLES = [ |
---|
44 | "requirement-refinement", "interface-ingroup", "interface-ingroup-hidden", |
---|
45 | "interface-function", "glossary-member", "runtime-measurement-request", |
---|
46 | "test-case" |
---|
47 | ] |
---|
48 | |
---|
49 | _PARENT_ROLES = [ |
---|
50 | "function-implementation", "interface-enumerator", |
---|
51 | "performance-runtime-limits" |
---|
52 | ] |
---|
53 | |
---|
54 | |
---|
55 | def _get_value_dummy(_ctx: ItemGetValueContext) -> Any: |
---|
56 | return "" |
---|
57 | |
---|
58 | |
---|
59 | _MAPPER = ItemMapper(EmptyItem()) |
---|
60 | _MAPPER.add_get_value("requirement/functional/action:/text-template", |
---|
61 | _get_value_dummy) |
---|
62 | _MAPPER.add_get_value("glossary/term:/plural", _get_value_dummy) |
---|
63 | _MAPPER.add_get_value( |
---|
64 | "requirement/non-functional/performance-runtime:/limit-kind", |
---|
65 | _get_value_dummy) |
---|
66 | _MAPPER.add_get_value( |
---|
67 | "requirement/non-functional/performance-runtime:/limit-condition", |
---|
68 | _get_value_dummy) |
---|
69 | _MAPPER.add_get_value( |
---|
70 | "requirement/non-functional/performance-runtime:/environment", |
---|
71 | _get_value_dummy) |
---|
72 | _MAPPER.add_get_value( |
---|
73 | "requirement/non-functional/performance-runtime-limits:/text-template", |
---|
74 | _get_value_dummy) |
---|
75 | |
---|
76 | _SPEC_TYPES = [ |
---|
77 | "requirement/functional/action", |
---|
78 | "requirement/functional/capability", |
---|
79 | "requirement/functional/function", |
---|
80 | "requirement/non-functional/design", |
---|
81 | "requirement/non-functional/design-group", |
---|
82 | "requirement/non-functional/interface-requirement", |
---|
83 | "requirement/non-functional/performance", |
---|
84 | "requirement/non-functional/performance-runtime", |
---|
85 | "requirement/non-functional/quality", |
---|
86 | ] |
---|
87 | |
---|
88 | for type_name in _SPEC_TYPES: |
---|
89 | _MAPPER.add_get_value(f"{type_name}:/spec", _get_value_dummy) |
---|
90 | |
---|
91 | |
---|
92 | def _visit_action_conditions(item: Item, name: str) -> None: |
---|
93 | for index, condition in enumerate(item[name]): |
---|
94 | for index_2, state in enumerate(condition["states"]): |
---|
95 | _MAPPER.substitute(state["text"], item, |
---|
96 | f"{name}[{index}]/states[{index_2}]/text") |
---|
97 | |
---|
98 | |
---|
99 | def _visit_action(item: Item) -> None: |
---|
100 | _visit_action_conditions(item, "pre-conditions") |
---|
101 | _visit_action_conditions(item, "post-conditions") |
---|
102 | |
---|
103 | |
---|
104 | _VISITORS = { |
---|
105 | "requirement/functional/action": _visit_action, |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | def _info(item: Item) -> str: |
---|
110 | if not item.get("_pre_qualified", True): |
---|
111 | return ", not-pre-qualified" |
---|
112 | try: |
---|
113 | if item["_validated"]: |
---|
114 | return "" |
---|
115 | return ", not-validated" |
---|
116 | except KeyError: |
---|
117 | return "" |
---|
118 | |
---|
119 | |
---|
120 | _TEXT_ATTRIBUTES = [ |
---|
121 | "brief", |
---|
122 | "description", |
---|
123 | "notes", |
---|
124 | "rationale", |
---|
125 | "test-brief", |
---|
126 | "test-description", |
---|
127 | "text", |
---|
128 | ] |
---|
129 | |
---|
130 | |
---|
131 | def _visit_item(item: Item, level: int, role: Optional[str], |
---|
132 | validated_filter: str) -> bool: |
---|
133 | validated = item.get("_validated", True) |
---|
134 | if validated_filter == "yes" and not validated: |
---|
135 | return False |
---|
136 | if validated_filter == "no" and validated: |
---|
137 | return False |
---|
138 | role_info = "" if role is None else f", role={role}" |
---|
139 | print( |
---|
140 | f"{' ' * level}{item.uid} (type={item.type}{role_info}{_info(item)})") |
---|
141 | for name in _TEXT_ATTRIBUTES: |
---|
142 | if name in item: |
---|
143 | _MAPPER.substitute(item[name], item) |
---|
144 | try: |
---|
145 | visitor = _VISITORS[item.type] |
---|
146 | except KeyError: |
---|
147 | pass |
---|
148 | else: |
---|
149 | visitor(item) |
---|
150 | return True |
---|
151 | |
---|
152 | |
---|
153 | def _view_interface_placment(item: Item, level: int, |
---|
154 | validated_filter: str) -> None: |
---|
155 | for link in item.links_to_children("interface-placement"): |
---|
156 | if _visit_item(link.item, level, link.role, validated_filter): |
---|
157 | _view_interface_placment(link.item, level + 1, validated_filter) |
---|
158 | |
---|
159 | |
---|
160 | def _view(item: Item, level: int, role: Optional[str], |
---|
161 | validated_filter: str) -> None: |
---|
162 | if not _visit_item(item, level, role, validated_filter): |
---|
163 | return |
---|
164 | for child in item.children("validation"): |
---|
165 | _visit_item(child, level + 1, "validation", validated_filter) |
---|
166 | _view_interface_placment(item, level + 1, validated_filter) |
---|
167 | for link in item.links_to_children(_CHILD_ROLES): |
---|
168 | _view(link.item, level + 1, link.role, validated_filter) |
---|
169 | for link in item.links_to_parents(_PARENT_ROLES): |
---|
170 | _view(link.item, level + 1, link.role, validated_filter) |
---|
171 | |
---|
172 | |
---|
173 | def _validation_count(item: Item) -> int: |
---|
174 | return len(list(child for child in item.children("validation"))) |
---|
175 | |
---|
176 | |
---|
177 | def _no_validation(item: Item, path: List[str]) -> List[str]: |
---|
178 | path_2 = path + [item.uid] |
---|
179 | leaf = _validation_count(item) == 0 |
---|
180 | for child in item.children(_CHILD_ROLES): |
---|
181 | path_2 = _no_validation(child, path_2) |
---|
182 | leaf = False |
---|
183 | for parent in item.parents(_PARENT_ROLES): |
---|
184 | path_2 = _no_validation(parent, path_2) |
---|
185 | leaf = False |
---|
186 | if leaf and not item.get("_validated", True): |
---|
187 | for index, component in enumerate(path_2): |
---|
188 | if component: |
---|
189 | print(f"{' ' * index}{component}") |
---|
190 | path_2[index] = "" |
---|
191 | return path_2[:-1] |
---|
192 | |
---|
193 | |
---|
194 | _REFINEMENTS = ["interface-function", "requirement-refinement"] |
---|
195 | |
---|
196 | _GROUPS = ["requirement/non-functional/design-group", "interface/group"] |
---|
197 | |
---|
198 | |
---|
199 | def _is_refinement(item: Item, other: Item) -> bool: |
---|
200 | for parent in item.parents(_REFINEMENTS): |
---|
201 | if parent == other: |
---|
202 | return True |
---|
203 | if _is_refinement(parent, other): |
---|
204 | return True |
---|
205 | return False |
---|
206 | |
---|
207 | |
---|
208 | def _gather_design_components(item: Item, components: List[Item]) -> bool: |
---|
209 | if item.type in _GROUPS: |
---|
210 | components.append(item) |
---|
211 | return True |
---|
212 | if item.type.startswith("requirement"): |
---|
213 | for parent in item.parents("interface-function"): |
---|
214 | components.append(parent) |
---|
215 | for parent in item.parents("requirement-refinement"): |
---|
216 | _gather_design_components(parent, components) |
---|
217 | return True |
---|
218 | return False |
---|
219 | |
---|
220 | |
---|
221 | def _design(item_cache: ItemCache) -> None: |
---|
222 | for item in item_cache.values(): |
---|
223 | if not item.enabled: |
---|
224 | continue |
---|
225 | components: List[Item] = [] |
---|
226 | if not _gather_design_components(item, components): |
---|
227 | continue |
---|
228 | compact: Set[Item] = set() |
---|
229 | for component in components: |
---|
230 | for component_2 in components: |
---|
231 | if component != component_2: |
---|
232 | if _is_refinement(component_2, component): |
---|
233 | break |
---|
234 | else: |
---|
235 | compact.add(component) |
---|
236 | if compact: |
---|
237 | text = ", ".join(component.uid for component in compact) |
---|
238 | else: |
---|
239 | text = "N/A" |
---|
240 | print(f"{item.uid}\t{text}") |
---|
241 | |
---|
242 | |
---|
243 | def _gather_interface_placement(item: Item, spec: Set) -> None: |
---|
244 | for child in item.children("interface-placement"): |
---|
245 | spec.add(child) |
---|
246 | _gather_interface_placement(child, spec) |
---|
247 | |
---|
248 | |
---|
249 | def _gather(item: Item, spec: Set) -> None: |
---|
250 | spec.add(item) |
---|
251 | _gather_interface_placement(item, spec) |
---|
252 | for child in item.children("validation"): |
---|
253 | spec.add(child) |
---|
254 | for child in item.children(_CHILD_ROLES): |
---|
255 | _gather(child, spec) |
---|
256 | for parent in item.parents(_PARENT_ROLES): |
---|
257 | _gather(parent, spec) |
---|
258 | |
---|
259 | |
---|
260 | def _make_row(transition_map: TransitionMap, map_idx: int, |
---|
261 | variant: Transition) -> Tuple[str, ...]: |
---|
262 | return tuple( |
---|
263 | itertools.chain( |
---|
264 | [str(map_idx), str(variant.desc_idx)], |
---|
265 | (transition_map.pre_co_idx_st_idx_to_st_name(co_idx, st_idx) |
---|
266 | for co_idx, st_idx in enumerate( |
---|
267 | transition_map.map_idx_to_pre_co_states( |
---|
268 | map_idx, variant.pre_cond_na))), |
---|
269 | (transition_map.post_co_idx_st_idx_to_st_name(co_idx, st_idx) |
---|
270 | for co_idx, st_idx in enumerate(variant.post_cond)))) |
---|
271 | |
---|
272 | |
---|
273 | def _action_table(item: Item) -> None: |
---|
274 | rows = [ |
---|
275 | tuple( |
---|
276 | itertools.chain(["Entry", "Descriptor"], |
---|
277 | (condition["name"] |
---|
278 | for condition in item["pre-conditions"]), |
---|
279 | (condition["name"] |
---|
280 | for condition in item["post-conditions"]))) |
---|
281 | ] |
---|
282 | transition_map = TransitionMap(item) |
---|
283 | for map_idx, variant in transition_map.get_variants(item.cache.enabled): |
---|
284 | rows.append(_make_row(transition_map, map_idx, variant)) |
---|
285 | content = SphinxContent() |
---|
286 | content.add_simple_table(rows) |
---|
287 | print(str(content)) |
---|
288 | |
---|
289 | |
---|
290 | def _to_name(transition_map, co_idx: int, st_idx: int) -> str: |
---|
291 | return (f"{transition_map.post_co_idx_to_co_name(co_idx)} = " |
---|
292 | f"{transition_map.post_co_idx_st_idx_to_st_name(co_idx, st_idx)}") |
---|
293 | |
---|
294 | |
---|
295 | def _action_list(item: Item) -> None: |
---|
296 | transition_map = TransitionMap(item) |
---|
297 | for post_cond, pre_conds in transition_map.get_post_conditions( |
---|
298 | item.cache.enabled): |
---|
299 | print("") |
---|
300 | if post_cond[0]: |
---|
301 | print(transition_map.skip_idx_to_name(post_cond[0])) |
---|
302 | else: |
---|
303 | names: List[str] = [] |
---|
304 | for co_idx, st_idx in enumerate(post_cond[1:]): |
---|
305 | st_name = transition_map.post_co_idx_st_idx_to_st_name( |
---|
306 | co_idx, st_idx) |
---|
307 | if st_name != "NA": |
---|
308 | co_name = transition_map.post_co_idx_to_co_name(co_idx) |
---|
309 | names.append(f"{co_name} = {st_name}") |
---|
310 | print(", ".join(names)) |
---|
311 | for row in pre_conds: |
---|
312 | entries = [] |
---|
313 | for co_idx, co_states in enumerate(row): |
---|
314 | co_name = transition_map.pre_co_idx_to_co_name(co_idx) |
---|
315 | states = [ |
---|
316 | transition_map.pre_co_idx_st_idx_to_st_name( |
---|
317 | co_idx, st_idx) for st_idx in set(co_states) |
---|
318 | ] |
---|
319 | if len(states) == 1: |
---|
320 | if states[0] != "NA": |
---|
321 | entries.append(f"{co_name} = {states[0]}") |
---|
322 | else: |
---|
323 | entries.append(f"{co_name} = {{ " + ", ".join(states) + |
---|
324 | " }") |
---|
325 | print("") |
---|
326 | print(" * " + ", ".join(entries)) |
---|
327 | |
---|
328 | |
---|
329 | def _list_api(item_cache: ItemCache) -> None: |
---|
330 | items: Dict[str, List[Item]] = {} |
---|
331 | gather_api_items(item_cache, items) |
---|
332 | for group, group_items in sorted(items.items()): |
---|
333 | print(group) |
---|
334 | for item in group_items: |
---|
335 | print(f"\t{item['name']}") |
---|
336 | |
---|
337 | |
---|
338 | def main() -> None: |
---|
339 | """ Views the specification. """ |
---|
340 | |
---|
341 | # pylint: disable=too-many-branches |
---|
342 | parser = argparse.ArgumentParser() |
---|
343 | parser.add_argument('--filter', |
---|
344 | choices=[ |
---|
345 | "none", "api", "build", "orphan", "no-validation", |
---|
346 | "action-table", "action-list", "design", "types" |
---|
347 | ], |
---|
348 | type=str.lower, |
---|
349 | default="none", |
---|
350 | help="filter the items") |
---|
351 | parser.add_argument('--validated', |
---|
352 | choices=["all", "yes", "no"], |
---|
353 | type=str.lower, |
---|
354 | default="all", |
---|
355 | help="filter the items by the validated status") |
---|
356 | parser.add_argument( |
---|
357 | "--enabled", |
---|
358 | help=("a comma separated list of enabled options used to evaluate " |
---|
359 | "enabled-by expressions")) |
---|
360 | parser.add_argument("UIDs", |
---|
361 | metavar="UID", |
---|
362 | nargs="*", |
---|
363 | help="an UID of a specification item") |
---|
364 | args = parser.parse_args(sys.argv[1:]) |
---|
365 | config = load_config("config.yml") |
---|
366 | item_cache_config = config["spec"] |
---|
367 | item_cache_config["enabled"] = args.enabled.split( |
---|
368 | ",") if args.enabled else [] |
---|
369 | item_cache = ItemCache(item_cache_config, |
---|
370 | is_item_enabled=recursive_is_enabled) |
---|
371 | augment_with_test_links(item_cache) |
---|
372 | augment_with_test_case_links(item_cache) |
---|
373 | root = item_cache["/req/root"] |
---|
374 | |
---|
375 | if args.filter == "none": |
---|
376 | validate(root) |
---|
377 | _view(root, 0, None, args.validated) |
---|
378 | elif args.filter == "action-table": |
---|
379 | for uid in args.UIDs: |
---|
380 | _action_table(item_cache[uid]) |
---|
381 | elif args.filter == "action-list": |
---|
382 | for uid in args.UIDs: |
---|
383 | _action_list(item_cache[uid]) |
---|
384 | elif args.filter == "orphan": |
---|
385 | validate(root) |
---|
386 | for item in item_cache.values(): |
---|
387 | if item["type"] in ["build", "spec"]: |
---|
388 | continue |
---|
389 | if item.enabled and "_validated" not in item: |
---|
390 | print(item.uid) |
---|
391 | elif args.filter == "no-validation": |
---|
392 | validate(root) |
---|
393 | _no_validation(root, []) |
---|
394 | elif args.filter == "api": |
---|
395 | validate(root) |
---|
396 | _list_api(item_cache) |
---|
397 | elif args.filter == "design": |
---|
398 | _design(item_cache) |
---|
399 | elif args.filter == "types": |
---|
400 | for name in sorted(item_cache.items_by_type.keys()): |
---|
401 | print(name) |
---|
402 | elif args.filter == "build": |
---|
403 | for name in gather_files(config["build"], item_cache, False): |
---|
404 | print(name) |
---|
405 | |
---|
406 | |
---|
407 | if __name__ == "__main__": |
---|
408 | main() |
---|