Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if validate:
validate_tree(root, 'SystemStructureDescription.xsd')
ssd = SystemStructureDescription()
ssd.version = root.get('version')
ssd.name = root.get('name')
# Units
for u in root.findall('ssd:Units/ssc:Unit', namespaces=ns):
attr = dict(u.attrib)
bu = u.find('ssc:BaseUnit', namespaces=ns)
attr.update(bu.attrib)
ssd.units.append(Unit(**attr))
ssd.system = System()
system = root.find('ssd:System', namespaces=ns)
ssd.system = handle_system(system, filename=filename)
# add parent elements
add_tree_info(ssd.system)
ssd.system.parent = None
return ssd
def handle_system(system, filename):
"""
Parameters:
system ...
filename filename of the SSP file
Returns:
A System object
"""
system_obj = System()
system_obj.name = system.get('name')
system_obj.description = system.get('description')
_handle_element(system_obj, system, filename)
# for b in system.findall('ssd:ParameterBindings/ssd:ParameterBinding', namespaces=ns):
# system_obj.parameterBindings.append(
# ParameterBinding(prefix=b.get('prefix'), source=b.get('source'), type=b.get('type')))
system_obj.connectors = get_connectors(system)
# Components
for c in system.findall('ssd:Elements/ssd:Component', namespaces=ns):
component = Component(name=c.get('name'), source=c.get('source'), type=c.get('type'))
_handle_element(component, c, filename)
component.connectors = get_connectors(c)
def add_path(element, path=''):
if isinstance(element, System):
for child in element.elements:
add_path(child, path + child.name + '.')
for connector in element.connectors:
connector.path = path + connector.name
components = find_components(ssd.system)
connectors = find_connectors(ssd.system)
connections = get_connections(ssd.system)
# resolve connections
connections_reversed = {}
for a, b in connections:
connections_reversed[b] = a
new_connections = []
# trace connections back to the actual start connector
for a, b in connections:
while isinstance(a.parent, System) and a.parent.parent is not None:
a = connections_reversed[a]
new_connections.append((a, b))
connections = new_connections
# extract the SSP
ssp_unzipdir = extract(ssp_filename)
# initialize the connectors
for connector in connectors:
connector.value = 0.0
# instantiate the FMUs
for component in components:
instantiate_fmu(component, ssp_unzipdir, start_time, parameter_set)
def add_tree_info(element):
if isinstance(element, System):
for child in element.elements:
child.parent = element
add_tree_info(child)
for connector in element.connectors:
connector.parent = element