mdxml 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mdxml/__init__.py +39 -0
- mdxml/__main__.py +5 -0
- mdxml/_cli.py +96 -0
- mdxml/_convert.py +151 -0
- mdxml/_dict.py +42 -0
- mdxml/_errors.py +39 -0
- mdxml/_grammar.py +259 -0
- mdxml/_header.py +92 -0
- mdxml/_lookup.py +54 -0
- mdxml/_protocols.py +135 -0
- mdxml/_read.py +75 -0
- mdxml/_reader.py +191 -0
- mdxml/_schema.py +131 -0
- mdxml/_source.py +192 -0
- mdxml/_variables.py +426 -0
- mdxml/fmi2/__init__.py +73 -0
- mdxml/fmi2/_annotations.py +34 -0
- mdxml/fmi2/_header.py +525 -0
- mdxml/fmi2/_model.py +73 -0
- mdxml/fmi2/_structure.py +100 -0
- mdxml/fmi2/_variables.py +323 -0
- mdxml/fmi3/__init__.py +125 -0
- mdxml/fmi3/_annotations.py +43 -0
- mdxml/fmi3/_header.py +635 -0
- mdxml/fmi3/_model.py +59 -0
- mdxml/fmi3/_structure.py +103 -0
- mdxml/fmi3/_variables.py +603 -0
- mdxml/py.typed +0 -0
- mdxml-0.1.0.dist-info/METADATA +202 -0
- mdxml-0.1.0.dist-info/RECORD +33 -0
- mdxml-0.1.0.dist-info/WHEEL +4 -0
- mdxml-0.1.0.dist-info/entry_points.txt +3 -0
- mdxml-0.1.0.dist-info/licenses/LICENSE +21 -0
mdxml/fmi2/_structure.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""The FMI 2.0 Model Structure: outputs, derivatives and initial unknowns, with their dependencies.
|
|
2
|
+
|
|
3
|
+
Every entry refers to a Model Variable by its Variable Index (1-based
|
|
4
|
+
position in ``ModelVariables``), exactly as written; nothing is resolved
|
|
5
|
+
(ADR 0002).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import Enum
|
|
12
|
+
|
|
13
|
+
from mdxml import _convert as c
|
|
14
|
+
from mdxml._grammar import Collect, Grammar
|
|
15
|
+
from mdxml._schema import EMPTY, Attr, Frame, attributes, table
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class DependenciesKind(str, Enum):
|
|
19
|
+
"""How an unknown depends on one of its ``dependencies``."""
|
|
20
|
+
|
|
21
|
+
DEPENDENT = "dependent"
|
|
22
|
+
CONSTANT = "constant"
|
|
23
|
+
FIXED = "fixed"
|
|
24
|
+
TUNABLE = "tunable"
|
|
25
|
+
DISCRETE = "discrete"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
29
|
+
class Unknown:
|
|
30
|
+
"""One entry of the Model Structure.
|
|
31
|
+
|
|
32
|
+
``dependencies`` is ``None`` when the attribute is absent, which the
|
|
33
|
+
standard defines as "may depend on all knowns", and ``()`` when it's
|
|
34
|
+
present but empty ("depends on none").
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
index: int
|
|
38
|
+
"""Variable Index of the unknown."""
|
|
39
|
+
dependencies: tuple[int, ...] | None = None
|
|
40
|
+
"""Variable Indexes of the knowns it depends on."""
|
|
41
|
+
dependencies_kind: tuple[DependenciesKind | str, ...] | None = None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
45
|
+
class ModelStructure:
|
|
46
|
+
"""The ``ModelStructure`` element."""
|
|
47
|
+
|
|
48
|
+
outputs: tuple[Unknown, ...] = ()
|
|
49
|
+
derivatives: tuple[Unknown, ...] = ()
|
|
50
|
+
initial_unknowns: tuple[Unknown, ...] = ()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
UNKNOWN_ATTRS = table(
|
|
54
|
+
Attr("index", "index", c.UNSIGNED_INT, required=True),
|
|
55
|
+
Attr("dependencies", "dependencies", c.ListOf(c.UNSIGNED_INT)),
|
|
56
|
+
Attr("dependenciesKind", "dependencies_kind", c.ListOf(c.EnumConverter(DependenciesKind))),
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def build_unknown(frame: Frame, *, strict: bool) -> Unknown:
|
|
61
|
+
"""Build an ``Unknown``; in strict mode its two lists must have the same length."""
|
|
62
|
+
unknown = Unknown(**attributes(frame, UNKNOWN_ATTRS, strict=strict))
|
|
63
|
+
if strict and unknown.dependencies_kind is not None:
|
|
64
|
+
count = len(unknown.dependencies or ())
|
|
65
|
+
if unknown.dependencies is None or len(unknown.dependencies_kind) != count:
|
|
66
|
+
raise frame.error(
|
|
67
|
+
f"<Unknown index={unknown.index}> has {len(unknown.dependencies_kind)} dependenciesKind entries "
|
|
68
|
+
f"for {count} dependencies"
|
|
69
|
+
)
|
|
70
|
+
return unknown
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def build_model_structure(frame: Frame, *, strict: bool) -> ModelStructure:
|
|
74
|
+
"""Build the ``ModelStructure`` from its three lists."""
|
|
75
|
+
attributes(frame, EMPTY, strict=strict)
|
|
76
|
+
return ModelStructure(
|
|
77
|
+
outputs=frame.one("Outputs", strict=strict) or (),
|
|
78
|
+
derivatives=frame.one("Derivatives", strict=strict) or (),
|
|
79
|
+
initial_unknowns=frame.one("InitialUnknowns", strict=strict) or (),
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
STRUCTURE_GRAMMAR = Grammar(
|
|
84
|
+
root="ModelStructure",
|
|
85
|
+
stop_at="",
|
|
86
|
+
build_root=build_model_structure,
|
|
87
|
+
children={
|
|
88
|
+
"ModelStructure": {
|
|
89
|
+
"Outputs": Collect("Unknown"),
|
|
90
|
+
"Derivatives": Collect("Unknown"),
|
|
91
|
+
"InitialUnknowns": Collect("Unknown"),
|
|
92
|
+
},
|
|
93
|
+
"Outputs": {"Unknown": build_unknown},
|
|
94
|
+
"Derivatives": {"Unknown": build_unknown},
|
|
95
|
+
"InitialUnknowns": {"Unknown": build_unknown},
|
|
96
|
+
},
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
AFTER_MODEL_STRUCTURE: frozenset[str] = frozenset()
|
|
100
|
+
"""Root elements allowed after ``ModelStructure`` (none in FMI 2)."""
|
mdxml/fmi2/_variables.py
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
"""FMI 2.0 Model Variables: the ``ScalarVariable`` elements in ``ModelVariables``.
|
|
2
|
+
|
|
3
|
+
Each ``ScalarVariable`` holds exactly one typed child (``Real``, ``Integer``,
|
|
4
|
+
``Boolean``, ``String`` or ``Enumeration``). Its attributes are merged into
|
|
5
|
+
one object per variable, of the class for that kind; ``type_name`` says which.
|
|
6
|
+
Every variable also gets its Variable Index: its 1-based position in
|
|
7
|
+
``ModelVariables``, which FMI 2's Model Structure uses to refer to it.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from collections.abc import Mapping
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from enum import Enum
|
|
15
|
+
from types import MappingProxyType
|
|
16
|
+
from typing import Any, cast
|
|
17
|
+
|
|
18
|
+
from mdxml import _convert as c
|
|
19
|
+
from mdxml._grammar import SKIP, Builder, ChildRule, Grammar
|
|
20
|
+
from mdxml._schema import Attr, AttrTable, Frame, attributes, double, flag, required_text, table, text
|
|
21
|
+
from mdxml.fmi2 import _annotations as annotations_rule
|
|
22
|
+
from mdxml.fmi2._annotations import ToolAnnotation
|
|
23
|
+
|
|
24
|
+
# --- Enums ----------------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Causality(str, Enum):
|
|
28
|
+
"""``causality`` of a ScalarVariable."""
|
|
29
|
+
|
|
30
|
+
PARAMETER = "parameter"
|
|
31
|
+
CALCULATED_PARAMETER = "calculatedParameter"
|
|
32
|
+
INPUT = "input"
|
|
33
|
+
OUTPUT = "output"
|
|
34
|
+
LOCAL = "local"
|
|
35
|
+
INDEPENDENT = "independent"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Variability(str, Enum):
|
|
39
|
+
"""``variability`` of a ScalarVariable."""
|
|
40
|
+
|
|
41
|
+
CONSTANT = "constant"
|
|
42
|
+
FIXED = "fixed"
|
|
43
|
+
TUNABLE = "tunable"
|
|
44
|
+
DISCRETE = "discrete"
|
|
45
|
+
CONTINUOUS = "continuous"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Initial(str, Enum):
|
|
49
|
+
"""``initial`` of a ScalarVariable."""
|
|
50
|
+
|
|
51
|
+
EXACT = "exact"
|
|
52
|
+
APPROX = "approx"
|
|
53
|
+
CALCULATED = "calculated"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# The FMI 2.0 standard's table of causality x variability (section 2.2.7): the default
|
|
57
|
+
# `initial` for each valid combination, or None where `initial` isn't allowed (inputs and
|
|
58
|
+
# the independent variable). Combinations not listed are invalid and have no default.
|
|
59
|
+
_DEFAULT_INITIAL: Mapping[tuple[Causality, Variability], Initial | None] = MappingProxyType(
|
|
60
|
+
{
|
|
61
|
+
# Case A, where only "exact" is allowed.
|
|
62
|
+
(Causality.OUTPUT, Variability.CONSTANT): Initial.EXACT,
|
|
63
|
+
(Causality.LOCAL, Variability.CONSTANT): Initial.EXACT,
|
|
64
|
+
(Causality.PARAMETER, Variability.FIXED): Initial.EXACT,
|
|
65
|
+
(Causality.PARAMETER, Variability.TUNABLE): Initial.EXACT,
|
|
66
|
+
# Case B, where "calculated" (the default) or "approx" is allowed.
|
|
67
|
+
(Causality.CALCULATED_PARAMETER, Variability.FIXED): Initial.CALCULATED,
|
|
68
|
+
(Causality.CALCULATED_PARAMETER, Variability.TUNABLE): Initial.CALCULATED,
|
|
69
|
+
(Causality.LOCAL, Variability.FIXED): Initial.CALCULATED,
|
|
70
|
+
(Causality.LOCAL, Variability.TUNABLE): Initial.CALCULATED,
|
|
71
|
+
# Case C, where "calculated" (the default), "exact" or "approx" is allowed.
|
|
72
|
+
(Causality.OUTPUT, Variability.DISCRETE): Initial.CALCULATED,
|
|
73
|
+
(Causality.OUTPUT, Variability.CONTINUOUS): Initial.CALCULATED,
|
|
74
|
+
(Causality.LOCAL, Variability.DISCRETE): Initial.CALCULATED,
|
|
75
|
+
(Causality.LOCAL, Variability.CONTINUOUS): Initial.CALCULATED,
|
|
76
|
+
# Case D, inputs, which have no initial.
|
|
77
|
+
(Causality.INPUT, Variability.DISCRETE): None,
|
|
78
|
+
(Causality.INPUT, Variability.CONTINUOUS): None,
|
|
79
|
+
# Case E, the independent variable, which has no initial.
|
|
80
|
+
(Causality.INDEPENDENT, Variability.CONTINUOUS): None,
|
|
81
|
+
}
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def default_initial(causality: Causality | str, variability: Variability | str) -> Initial | None:
|
|
86
|
+
"""Return the FMI 2.0 standard's default ``initial`` for a causality and variability.
|
|
87
|
+
|
|
88
|
+
``None`` when ``initial`` isn't allowed for the combination, when the
|
|
89
|
+
combination is invalid, or when either value isn't a known enum value.
|
|
90
|
+
"""
|
|
91
|
+
if not isinstance(causality, Causality) or not isinstance(variability, Variability):
|
|
92
|
+
return None
|
|
93
|
+
return _DEFAULT_INITIAL.get((causality, variability))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# --- Variables ------------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
100
|
+
class ScalarVariable:
|
|
101
|
+
"""Attributes every FMI 2.0 Model Variable has, whatever its kind."""
|
|
102
|
+
|
|
103
|
+
name: str
|
|
104
|
+
value_reference: int
|
|
105
|
+
index: int
|
|
106
|
+
"""The Variable Index: 1-based position in ``ModelVariables``."""
|
|
107
|
+
description: str | None = None
|
|
108
|
+
causality: Causality | str = Causality.LOCAL
|
|
109
|
+
variability: Variability | str = Variability.CONTINUOUS
|
|
110
|
+
initial: Initial | str | None = None
|
|
111
|
+
"""``initial`` exactly as written; see :attr:`effective_initial`."""
|
|
112
|
+
can_handle_multiple_set_per_time_instant: bool | None = None
|
|
113
|
+
declared_type: str | None = None
|
|
114
|
+
annotations: tuple[ToolAnnotation, ...] | None = None
|
|
115
|
+
"""``None`` unless read with ``annotations=True``."""
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def effective_initial(self) -> Initial | str | None:
|
|
119
|
+
"""``initial`` as written, or else the standard's default for this causality and variability."""
|
|
120
|
+
if self.initial is not None:
|
|
121
|
+
return self.initial
|
|
122
|
+
return default_initial(self.causality, self.variability)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
126
|
+
class RealVariable(ScalarVariable):
|
|
127
|
+
"""A ScalarVariable with a ``Real`` element."""
|
|
128
|
+
|
|
129
|
+
type_name: str = field(default="Real", init=False)
|
|
130
|
+
quantity: str | None = None
|
|
131
|
+
unit: str | None = None
|
|
132
|
+
display_unit: str | None = None
|
|
133
|
+
relative_quantity: bool = False
|
|
134
|
+
min: float | None = None
|
|
135
|
+
max: float | None = None
|
|
136
|
+
nominal: float | None = None
|
|
137
|
+
unbounded: bool = False
|
|
138
|
+
start: float | None = None
|
|
139
|
+
derivative: int | None = None
|
|
140
|
+
"""Variable Index of the state this variable is the derivative of."""
|
|
141
|
+
reinit: bool = False
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
145
|
+
class IntegerVariable(ScalarVariable):
|
|
146
|
+
"""A ScalarVariable with an ``Integer`` element."""
|
|
147
|
+
|
|
148
|
+
type_name: str = field(default="Integer", init=False)
|
|
149
|
+
quantity: str | None = None
|
|
150
|
+
min: int | None = None
|
|
151
|
+
max: int | None = None
|
|
152
|
+
start: int | None = None
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
156
|
+
class BooleanVariable(ScalarVariable):
|
|
157
|
+
"""A ScalarVariable with a ``Boolean`` element."""
|
|
158
|
+
|
|
159
|
+
type_name: str = field(default="Boolean", init=False)
|
|
160
|
+
start: bool | None = None
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
164
|
+
class StringVariable(ScalarVariable):
|
|
165
|
+
"""A ScalarVariable with a ``String`` element."""
|
|
166
|
+
|
|
167
|
+
type_name: str = field(default="String", init=False)
|
|
168
|
+
start: str | None = None
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
172
|
+
class EnumerationVariable(ScalarVariable):
|
|
173
|
+
"""A ScalarVariable with an ``Enumeration`` element. ``declared_type`` names its EnumerationType."""
|
|
174
|
+
|
|
175
|
+
type_name: str = field(default="Enumeration", init=False)
|
|
176
|
+
quantity: str | None = None
|
|
177
|
+
min: int | None = None
|
|
178
|
+
max: int | None = None
|
|
179
|
+
start: int | None = None
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
Variable = RealVariable | IntegerVariable | BooleanVariable | StringVariable | EnumerationVariable
|
|
183
|
+
"""Any FMI 2.0 Model Variable."""
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# --- Attribute tables -----------------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
SCALAR_VARIABLE_ATTRS = table(
|
|
189
|
+
required_text("name", "name"),
|
|
190
|
+
Attr("valueReference", "value_reference", c.UNSIGNED_INT, required=True),
|
|
191
|
+
text("description", "description"),
|
|
192
|
+
Attr("causality", "causality", c.EnumConverter(Causality)),
|
|
193
|
+
Attr("variability", "variability", c.EnumConverter(Variability)),
|
|
194
|
+
Attr("initial", "initial", c.EnumConverter(Initial)),
|
|
195
|
+
flag("canHandleMultipleSetPerTimeInstant", "can_handle_multiple_set_per_time_instant"),
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
REAL_ATTRS = table(
|
|
199
|
+
text("declaredType", "declared_type"),
|
|
200
|
+
text("quantity", "quantity"),
|
|
201
|
+
text("unit", "unit"),
|
|
202
|
+
text("displayUnit", "display_unit"),
|
|
203
|
+
flag("relativeQuantity", "relative_quantity"),
|
|
204
|
+
double("min", "min"),
|
|
205
|
+
double("max", "max"),
|
|
206
|
+
double("nominal", "nominal"),
|
|
207
|
+
flag("unbounded", "unbounded"),
|
|
208
|
+
double("start", "start"),
|
|
209
|
+
Attr("derivative", "derivative", c.UNSIGNED_INT),
|
|
210
|
+
flag("reinit", "reinit"),
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
INTEGER_ATTRS = table(
|
|
214
|
+
text("declaredType", "declared_type"),
|
|
215
|
+
text("quantity", "quantity"),
|
|
216
|
+
Attr("min", "min", c.INT),
|
|
217
|
+
Attr("max", "max", c.INT),
|
|
218
|
+
Attr("start", "start", c.INT),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
BOOLEAN_ATTRS = table(
|
|
222
|
+
text("declaredType", "declared_type"),
|
|
223
|
+
flag("start", "start"),
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
STRING_ATTRS = table(
|
|
227
|
+
text("declaredType", "declared_type"),
|
|
228
|
+
text("start", "start"),
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
ENUMERATION_ATTRS = table(
|
|
232
|
+
required_text("declaredType", "declared_type"),
|
|
233
|
+
text("quantity", "quantity"),
|
|
234
|
+
Attr("min", "min", c.INT),
|
|
235
|
+
Attr("max", "max", c.INT),
|
|
236
|
+
Attr("start", "start", c.INT),
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
KINDS: Mapping[str, tuple[type[ScalarVariable], AttrTable]] = MappingProxyType( # typed child -> class, attributes
|
|
240
|
+
{
|
|
241
|
+
"Real": (RealVariable, REAL_ATTRS),
|
|
242
|
+
"Integer": (IntegerVariable, INTEGER_ATTRS),
|
|
243
|
+
"Boolean": (BooleanVariable, BOOLEAN_ATTRS),
|
|
244
|
+
"String": (StringVariable, STRING_ATTRS),
|
|
245
|
+
"Enumeration": (EnumerationVariable, ENUMERATION_ATTRS),
|
|
246
|
+
}
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
VariableFields = tuple[type[ScalarVariable], dict[str, Any]]
|
|
250
|
+
"""A variable's class and constructor arguments, everything but its Variable Index."""
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# --- Builders -------------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class _Kind:
|
|
257
|
+
"""Builds a typed child (e.g. ``<Real>``) into its variable class and attributes."""
|
|
258
|
+
|
|
259
|
+
__slots__ = ("_attrs", "_cls")
|
|
260
|
+
|
|
261
|
+
def __init__(self, tag: str) -> None:
|
|
262
|
+
self._cls, self._attrs = KINDS[tag]
|
|
263
|
+
|
|
264
|
+
def __call__(self, frame: Frame, /, *, strict: bool) -> VariableFields:
|
|
265
|
+
"""Return the class and the typed child's attributes."""
|
|
266
|
+
return self._cls, attributes(frame, self._attrs, strict=strict)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class _ScalarVariableBuilder(Builder[VariableFields]):
|
|
270
|
+
"""Merges a ``ScalarVariable``'s attributes with its typed child's (and its annotations, if requested)."""
|
|
271
|
+
|
|
272
|
+
__slots__ = ("_annotations",)
|
|
273
|
+
|
|
274
|
+
def __init__(self, *, annotations: bool) -> None:
|
|
275
|
+
self._annotations = annotations
|
|
276
|
+
|
|
277
|
+
def __call__(self, frame: Frame, /, *, strict: bool) -> VariableFields:
|
|
278
|
+
"""Return the variable's class and fields."""
|
|
279
|
+
common = attributes(frame, SCALAR_VARIABLE_ATTRS, strict=strict)
|
|
280
|
+
kinds = [obj for tag, obj in frame.children if tag in KINDS]
|
|
281
|
+
if not kinds:
|
|
282
|
+
raise frame.error(
|
|
283
|
+
f"<ScalarVariable name={common['name']!r}> has no Real, Integer, Boolean, String or Enumeration"
|
|
284
|
+
)
|
|
285
|
+
if len(kinds) > 1 and strict:
|
|
286
|
+
raise frame.error(f"<ScalarVariable name={common['name']!r}> has {len(kinds)} type elements, expected one")
|
|
287
|
+
cls, kind_fields = cast("VariableFields", kinds[0])
|
|
288
|
+
fields = {**common, **kind_fields}
|
|
289
|
+
if self._annotations:
|
|
290
|
+
fields["annotations"] = frame.one("Annotations", strict=strict) or ()
|
|
291
|
+
return cls, fields
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _variable_grammars(*, annotations: bool) -> Mapping[str, Grammar[VariableFields]]:
|
|
295
|
+
children: dict[str, dict[str, ChildRule]] = {
|
|
296
|
+
"ScalarVariable": {
|
|
297
|
+
**{tag: _Kind(tag) for tag in KINDS},
|
|
298
|
+
"Annotations": annotations_rule.CONTAINER if annotations else SKIP,
|
|
299
|
+
},
|
|
300
|
+
"Annotations": {"Tool": annotations_rule.TOOL},
|
|
301
|
+
}
|
|
302
|
+
grammar = Grammar(
|
|
303
|
+
root="ScalarVariable",
|
|
304
|
+
stop_at="", # a ScalarVariable is always read whole
|
|
305
|
+
build_root=_ScalarVariableBuilder(annotations=annotations),
|
|
306
|
+
children=children,
|
|
307
|
+
)
|
|
308
|
+
return MappingProxyType({"ScalarVariable": grammar})
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
VARIABLE_GRAMMARS = _variable_grammars(annotations=False)
|
|
312
|
+
"""The grammar for each variable element, keyed by element name; Tool Annotations are skipped."""
|
|
313
|
+
|
|
314
|
+
VARIABLE_GRAMMARS_WITH_ANNOTATIONS = _variable_grammars(annotations=True)
|
|
315
|
+
"""The same, keeping each variable's Tool Annotations."""
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def filter_values(element: str, attrs: Mapping[str, str]) -> tuple[str, str]:
|
|
319
|
+
"""Return a variable's causality and variability from its raw attributes, with XSD defaults applied."""
|
|
320
|
+
return (
|
|
321
|
+
attrs.get("causality", Causality.LOCAL.value).strip(),
|
|
322
|
+
attrs.get("variability", Variability.CONTINUOUS.value).strip(),
|
|
323
|
+
)
|
mdxml/fmi3/__init__.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""FMI 3.0 Model Description types, mirroring the FMI 3.0 XSD."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from mdxml.fmi3._annotations import Annotation
|
|
6
|
+
from mdxml.fmi3._header import (
|
|
7
|
+
BaseUnit,
|
|
8
|
+
BinaryType,
|
|
9
|
+
BooleanType,
|
|
10
|
+
Category,
|
|
11
|
+
ClockType,
|
|
12
|
+
CoSimulation,
|
|
13
|
+
DefaultExperiment,
|
|
14
|
+
DisplayUnit,
|
|
15
|
+
EnumerationType,
|
|
16
|
+
Float32Type,
|
|
17
|
+
Float64Type,
|
|
18
|
+
Int8Type,
|
|
19
|
+
Int16Type,
|
|
20
|
+
Int32Type,
|
|
21
|
+
Int64Type,
|
|
22
|
+
IntervalVariability,
|
|
23
|
+
Item,
|
|
24
|
+
ModelExchange,
|
|
25
|
+
ModelHeader,
|
|
26
|
+
ScheduledExecution,
|
|
27
|
+
StringType,
|
|
28
|
+
TypeDefinition,
|
|
29
|
+
UInt8Type,
|
|
30
|
+
UInt16Type,
|
|
31
|
+
UInt32Type,
|
|
32
|
+
UInt64Type,
|
|
33
|
+
Unit,
|
|
34
|
+
VariableNamingConvention,
|
|
35
|
+
)
|
|
36
|
+
from mdxml.fmi3._model import ModelDescription
|
|
37
|
+
from mdxml.fmi3._structure import DependenciesKind, ModelStructure, Unknown
|
|
38
|
+
from mdxml.fmi3._variables import (
|
|
39
|
+
AbstractVariable,
|
|
40
|
+
Alias,
|
|
41
|
+
ArrayableVariable,
|
|
42
|
+
BinaryVariable,
|
|
43
|
+
BooleanVariable,
|
|
44
|
+
Causality,
|
|
45
|
+
ClockVariable,
|
|
46
|
+
Dimension,
|
|
47
|
+
EnumerationVariable,
|
|
48
|
+
Float32Variable,
|
|
49
|
+
Float64Variable,
|
|
50
|
+
Initial,
|
|
51
|
+
Int8Variable,
|
|
52
|
+
Int16Variable,
|
|
53
|
+
Int32Variable,
|
|
54
|
+
Int64Variable,
|
|
55
|
+
StringVariable,
|
|
56
|
+
UInt8Variable,
|
|
57
|
+
UInt16Variable,
|
|
58
|
+
UInt32Variable,
|
|
59
|
+
UInt64Variable,
|
|
60
|
+
Variability,
|
|
61
|
+
Variable,
|
|
62
|
+
default_initial,
|
|
63
|
+
default_variability,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
__all__ = [
|
|
67
|
+
"AbstractVariable",
|
|
68
|
+
"Alias",
|
|
69
|
+
"Annotation",
|
|
70
|
+
"ArrayableVariable",
|
|
71
|
+
"BaseUnit",
|
|
72
|
+
"BinaryType",
|
|
73
|
+
"BinaryVariable",
|
|
74
|
+
"BooleanType",
|
|
75
|
+
"BooleanVariable",
|
|
76
|
+
"Category",
|
|
77
|
+
"Causality",
|
|
78
|
+
"ClockType",
|
|
79
|
+
"ClockVariable",
|
|
80
|
+
"CoSimulation",
|
|
81
|
+
"DefaultExperiment",
|
|
82
|
+
"DependenciesKind",
|
|
83
|
+
"Dimension",
|
|
84
|
+
"DisplayUnit",
|
|
85
|
+
"EnumerationType",
|
|
86
|
+
"EnumerationVariable",
|
|
87
|
+
"Float32Type",
|
|
88
|
+
"Float32Variable",
|
|
89
|
+
"Float64Type",
|
|
90
|
+
"Float64Variable",
|
|
91
|
+
"Initial",
|
|
92
|
+
"Int8Type",
|
|
93
|
+
"Int8Variable",
|
|
94
|
+
"Int16Type",
|
|
95
|
+
"Int16Variable",
|
|
96
|
+
"Int32Type",
|
|
97
|
+
"Int32Variable",
|
|
98
|
+
"Int64Type",
|
|
99
|
+
"Int64Variable",
|
|
100
|
+
"IntervalVariability",
|
|
101
|
+
"Item",
|
|
102
|
+
"ModelDescription",
|
|
103
|
+
"ModelExchange",
|
|
104
|
+
"ModelHeader",
|
|
105
|
+
"ModelStructure",
|
|
106
|
+
"ScheduledExecution",
|
|
107
|
+
"StringType",
|
|
108
|
+
"StringVariable",
|
|
109
|
+
"TypeDefinition",
|
|
110
|
+
"UInt8Type",
|
|
111
|
+
"UInt8Variable",
|
|
112
|
+
"UInt16Type",
|
|
113
|
+
"UInt16Variable",
|
|
114
|
+
"UInt32Type",
|
|
115
|
+
"UInt32Variable",
|
|
116
|
+
"UInt64Type",
|
|
117
|
+
"UInt64Variable",
|
|
118
|
+
"Unit",
|
|
119
|
+
"Unknown",
|
|
120
|
+
"Variability",
|
|
121
|
+
"Variable",
|
|
122
|
+
"VariableNamingConvention",
|
|
123
|
+
"default_initial",
|
|
124
|
+
"default_variability",
|
|
125
|
+
]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""FMI 3.0 annotations: ``<Annotation type="…">`` elements, kept as raw XML."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from mdxml._grammar import Collect, Grammar, Raw
|
|
8
|
+
from mdxml._schema import Frame, attributes, required_text, table
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
12
|
+
class Annotation:
|
|
13
|
+
"""An ``<Annotation>`` element of an ``Annotations`` list.
|
|
14
|
+
|
|
15
|
+
``type`` identifies the tool or extension the annotation is for. ``xml`` is
|
|
16
|
+
the element's content (elements and text) exactly as written in the file, so
|
|
17
|
+
it can be parsed again by the caller; mdxml doesn't interpret it.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
type: str
|
|
21
|
+
xml: str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
ANNOTATION_ATTRS = table(required_text("type", "type"))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _build_annotation(frame: Frame, xml: str, *, strict: bool) -> Annotation:
|
|
28
|
+
return Annotation(**attributes(frame, ANNOTATION_ATTRS, strict=strict), xml=xml)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
ANNOTATION = Raw(_build_annotation)
|
|
32
|
+
"""Rule for an ``<Annotation>`` element."""
|
|
33
|
+
|
|
34
|
+
CONTAINER = Collect("Annotation")
|
|
35
|
+
"""Rule for an ``Annotations`` element: the tuple of its annotations."""
|
|
36
|
+
|
|
37
|
+
MODEL_ANNOTATIONS_GRAMMAR = Grammar(
|
|
38
|
+
root="Annotations",
|
|
39
|
+
stop_at="",
|
|
40
|
+
build_root=CONTAINER,
|
|
41
|
+
children={"Annotations": {"Annotation": ANNOTATION}},
|
|
42
|
+
)
|
|
43
|
+
"""Grammar for the model-level ``<Annotations>`` after ``<ModelStructure>``."""
|