-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess.py
185 lines (159 loc) · 6.03 KB
/
process.py
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
import os
import sys
import pickle
import zipfile
from dash import html
import dash_bootstrap_components as dbc
from fmpy import read_model_description, supported_platforms
from fmpy.validation import validate_fmu
def process_fmu(fmu_filename):
basename, _ = os.path.splitext(fmu_filename)
pickle_filename = basename + '.p'
fmu_hash = os.path.basename(basename)
try:
model_description = read_model_description(fmu_filename, validate=False)
except Exception as e:
alert = dbc.Alert(
[html.I(className='fas fa-times me-3'), f"Failed to read model description. {e}"],
id='alert', color='danger', className='mt-3')
with open(pickle_filename, 'wb') as f:
pickle.dump([alert], f)
return
platforms = supported_platforms(fmu_filename)
with zipfile.ZipFile(fmu_filename, 'r') as zf:
nl = filter(lambda n: not n.endswith('/'), zf.namelist())
fmi_types = []
if model_description.modelExchange:
fmi_types.append('Model Exchange')
if model_description.coSimulation:
fmi_types.append('Co-Simulation')
def na(attr):
value = getattr(model_description, attr)
if value:
return value
else:
return html.Span('n/a', className='text-muted')
rows = [
dbc.Row([
dbc.Col(html.Span("FMI Version"), width=4),
dbc.Col(html.Span(model_description.fmiVersion), width=8),
], className='py-1'),
dbc.Row([
dbc.Col("FMI Type", width=4),
dbc.Col(', '.join(fmi_types), width=8),
], className='py-1'),
dbc.Row([
dbc.Col("Model Name", width=4),
dbc.Col(model_description.modelName, width=8),
], className='py-1'),
dbc.Row([
dbc.Col("Platforms", width=4),
dbc.Col(', '.join(platforms), width=8),
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("Continuous States"), width=4),
dbc.Col(html.Span(model_description.numberOfContinuousStates), width=8),
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("Event Indicators"), width=4),
dbc.Col(html.Span(model_description.numberOfEventIndicators), width=8),
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("Model Variables"), width=4),
dbc.Col(html.Span(len(model_description.modelVariables)), width=8),
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("Generation Date"), width=4),
dbc.Col(na('generationDateAndTime'), width=8)
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("Generation Tool"), width=4),
dbc.Col(na('generationTool'), width=8)
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("Description"), width=4),
dbc.Col(na('description'), width=8)
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("SHA256"), width=4),
dbc.Col(html.Span(fmu_hash), width=8),
], className='py-1'),
dbc.Row([
dbc.Col(html.Span("File Size"), width=4),
dbc.Col(html.Span(f'{os.path.getsize(fmu_filename)} bytes'), width=8),
], className='py-1'),
]
try:
problems = validate_fmu(fmu_filename)
except Exception as e:
problems = [str(e)]
if problems:
alert = dbc.Alert(
[
html.P(
[
html.I(className='fas fa-exclamation-circle me-3'),
f"Validation failed. {len(problems)} {'problem was' if len(problems) == 1 else 'problems were'} found:"
]
),
html.Ul(
[html.Li(problem) for problem in problems]
)
],
id='alert', color='danger', className='mt-3')
else:
alert = dbc.Alert([html.I(className='fas fa-check me-3'), "Validation passed. No problems found."],
id='alert', color='success', className='mt-3')
variables = []
table_header = [
html.Thead(html.Tr([
html.Th("Type"),
html.Th("Name"),
html.Th("Causality"),
html.Th("Start", className='text-right'),
html.Th("Unit"),
html.Th("Description")
]))
]
for variable in model_description.modelVariables:
unit = variable.unit
if unit is None and variable.declaredType is not None:
unit = variable.declaredType.unit
if variable.type == 'Boolean':
color = '#c900c9'
elif variable.type == 'Binary':
color = '#ab0000'
elif variable.type.startswith(('Int', 'Enum')):
color = '#c78f00'
elif variable.type.startswith(('Real', 'Float')):
color = '#0000bf'
else: # String
color = '#00a608'
variables.append(
html.Tr(
[
html.Td(html.Small(variable.type, style={
'color': color, 'border': '1px solid ' + color, 'border-radius': '1em', 'padding': '0 0.5em 0 0.5em',
})),
html.Td(variable.name),
# html.Td(variable.variability),
html.Td(variable.causality),
html.Td(variable.start, className='text-right'),
html.Td(unit),
html.Td(variable.description, className='text-muted')
]
)
)
table = dbc.Table(table_header + [html.Tbody(variables)], borderless=True, size='sm')
tabs = dbc.Tabs(
[
dbc.Tab(rows, label="Model Info", className='p-4'),
dbc.Tab(table, label="Variables", className='p-4'),
dbc.Tab(html.Pre('\n'.join(nl)), label="Files", className='p-4'),
],
id='tabs'
)
with open(pickle_filename, 'wb') as f:
pickle.dump([alert, tabs], f)
if __name__ == '__main__':
process_fmu(sys.argv[1])