Repository navigation
Expand file tree
/
Copy pathserializer.py
More file actions
200 lines (169 loc) · 6.48 KB
/
Copy pathserializer.py
File metadata and controls
200 lines (169 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# This file was generated with the assistance of an AI coding tool.
from __future__ import annotations
import uuid
from collections.abc import Iterable, Iterator
from itertools import groupby
from pathlib import Path
from xml.etree import ElementTree as ET
from . import mvdxml_expression
from .model import (
_iter_expressions,
concept_or_applicability,
concept_root,
mvd_item,
rule,
template,
)
NAMESPACE = "http://buildingsmart-tech.org/mvd/XML/1.1"
ET.register_namespace("", NAMESPACE)
def _add(parent: ET.Element, tag: str, **attributes: str | None) -> ET.Element:
return ET.SubElement(
parent,
f"{{{NAMESPACE}}}{tag}",
{key: value for key, value in attributes.items() if value is not None},
)
def _identifier(value: object) -> str:
return getattr(value, "uuid", None) or str(uuid.uuid4())
def _expression(values: tuple[mvdxml_expression.node | str, ...]) -> str:
def token(value: mvdxml_expression.node | str) -> str:
if isinstance(value, str):
return value
qualification = f"[{value.b}]" if value.b is not None else ""
return f"{value.a or ''}{qualification}={value.c}"
return " ".join(map(token, values))
_CONTAINERS = {
"AttributeRule": "AttributeRules",
"EntityRule": "EntityRules",
"Constraint": "Constraints",
}
def _rule(parent: ET.Element, value: rule, referenced: list[tuple[str, template]]) -> None:
attributes: dict[str, str] = {}
if value.tag == "AttributeRule":
attributes = {"AttributeName": str(value.attribute)}
if value.bind:
attributes["RuleID"] = value.bind
elif value.tag == "EntityRule":
attributes = {"EntityName": str(value.attribute)}
elif value.tag == "Constraint":
attributes = {
"Expression": ";".join(map(_expression, _iter_expressions(value.attribute)))
}
element = _add(parent, value.tag, **attributes)
for tag, children in groupby(value.nodes, key=lambda child: child.tag):
if value.tag == "EntityRule" and tag == "EntityRule":
# Consecutive entity rules are an in-memory type restriction.
# Write them as partial-template references in mvdXML.
container = _add(element, "References")
for child in children:
identifier = str(uuid.uuid4())
_add(container, "Template", ref=identifier)
referenced.append(
(identifier, template(child.attribute, None, child.nodes))
)
continue
container = _add(element, _CONTAINERS[tag])
for child in children:
_rule(container, child, referenced)
def _template(
parent: ET.Element,
value: template,
identifier: str,
referenced: list[tuple[str, template]],
*,
partial: bool = False,
) -> None:
element = _add(
parent,
"ConceptTemplate",
uuid=identifier,
name=value.name,
status="sample",
applicableSchema="IFC4",
applicableEntity=value.entity,
isPartial="true" if partial else None,
)
if value.rules:
rules = _add(element, "Rules")
for item in value.rules:
_rule(rules, item, referenced)
def _key(value: template) -> str | int:
return value.uuid.casefold() if value.uuid else id(value)
def _item_templates(item: mvd_item) -> Iterator[template]:
if isinstance(item, template):
yield item
else:
if item.parsed_applicability:
yield item.parsed_applicability.template()
yield from (concept.template() for concept in item.parsed_concepts)
def _concept(
parent: ET.Element,
value: concept_or_applicability,
template_ids: dict[str | int, str],
) -> None:
applicability = value.is_root_applicability
element = _add(
parent,
"Applicability" if applicability else "Concept",
uuid=str(uuid.uuid4()),
name=None if applicability else value.name,
status="sample",
override=None if applicability else "false",
)
_add(element, "Template", ref=template_ids[_key(value.template())])
expressions = list(_iter_expressions(value.rules()))
if expressions:
rules = _add(element, "TemplateRules", operator="and")
for expression in expressions:
_add(rules, "TemplateRule", Parameters=_expression(expression) + ";")
def serialize(items: Iterable[mvd_item], destination: str | Path) -> None:
"""Serialize parsed mvdXML dataclasses, folding templates by UUID."""
parsed = tuple(items)
templates: dict[str | int, template] = {}
for item in parsed:
for value in _item_templates(item):
templates.setdefault(_key(value), value)
template_ids = {key: _identifier(value) for key, value in templates.items()}
document = ET.Element(
f"{{{NAMESPACE}}}mvdXML",
{"uuid": str(uuid.uuid4()), "name": "", "status": "sample"},
)
template_elements = _add(document, "Templates")
referenced: list[tuple[str, template]] = []
for key, value in templates.items():
_template(template_elements, value, template_ids[key], referenced)
# Serializing a partial template can discover further specializations.
index = 0
while index < len(referenced):
identifier, value = referenced[index]
_template(template_elements, value, identifier, referenced, partial=True)
index += 1
roots = [item for item in parsed if isinstance(item, concept_root)]
if roots:
views = _add(document, "Views")
view = _add(
views,
"ModelView",
uuid=str(uuid.uuid4()),
name="IfcOpenShell mvdXML",
status="sample",
applicableSchema="IFC4",
)
root_elements = _add(view, "Roots")
for root in roots:
element = _add(
root_elements,
"ConceptRoot",
uuid=str(uuid.uuid4()),
name=root.name,
status="sample",
applicableRootEntity=root.entity,
)
if root.parsed_applicability:
_concept(element, root.parsed_applicability, template_ids)
if root.parsed_concepts:
concepts = _add(element, "Concepts")
for concept in root.parsed_concepts:
_concept(concepts, concept, template_ids)
tree = ET.ElementTree(document)
ET.indent(tree, space=" ")
tree.write(destination, encoding="utf-8", xml_declaration=True)