diff --git a/src/AuDaGe/formats/__init__.py b/src/AuDaGe/formats/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py index 4c5967a..4b9342d 100644 --- a/src/AuDaGe/formats/controlexpert/xsy.py +++ b/src/AuDaGe/formats/controlexpert/xsy.py @@ -1,16 +1,21 @@ import os -import sys -import typing from collections.abc import Sequence from importlib import resources +from enum import StrEnum +from timeit import default_timer as timer import pandas as pd from pandas import DataFrame -import AuDaGe -from AuDaGe.iec import DataTypes, ElementaryVariable, UDT +import AuDaGe.iec from AuDaGe.format import Format +class DefaultPath(StrEnum): + PACKAGE = "AuDaGe.formats.controlexpert.xsy" + DB_SS = "assets/flatten_datablock.xslt" + DDT_SS = "assets/flatten_ddtsource.xslt" + BUILTINS = "assets/builtins.xsy" + class XSY(Format): """Schneider Electric EcoStruxure Control Expert variable exchange file (XSY). @@ -36,118 +41,165 @@ class XSY(Format): def __init__( self, - dir_path: os.path = "", def_paths: Sequence[os.path] = [], builtins_path: os.path = None, db_ss_path: os.path = None, ddt_ss_path: os.path = None - ): - self.dir_path = "" - self.def_paths = [] + ): self.defs = None - self._set_fields( - dir_path=dir_path, - def_paths=def_paths, - builtins_path=builtins_path, - db_ss_path=db_ss_path, - ddt_ss_path=ddt_ss_path - ) + self.dataBlock_stylesheet = db_ss_path + self.definition_stylesheet = ddt_ss_path + self.builtins = builtins_path + self.definition_paths = def_paths + + @property + def builtins(self) -> os.path[str]: + return self._builtins + + @builtins.setter + def builtins( + self, + path: os.PathLike = None + ): + if path is None: + path = DefaultPath.BUILTINS + self._builtins = resources.files(DefaultPath.PACKAGE).joinpath(path) + + @property + def dataBlock_stylesheet(self) -> os.path[str]: + return self._db_ss_path + + @dataBlock_stylesheet.setter + def dataBlock_stylesheet( + self, + path: os.Pathlike = None + ): + if path is None: + path = DefaultPath.DB_SS + self._db_ss_path = resources.files(DefaultPath.PACKAGE).joinpath(path) + + @property + def definition_stylesheet(self) -> os.path[str]: + return self._ddt_ss_path + + @definition_stylesheet.setter + def definition_stylesheet( + self, + path: os.PathLike = None + ): + if path is None: + path = DefaultPath.DDT_SS + self._ddt_ss_path = resources.files(DefaultPath.PACKAGE).joinpath(path) + + @property + def definition_paths(self) -> set[os.path[str]]: + return self._definition_paths + + @definition_paths.setter + def definition_paths( + self, + paths: (os.PathLike | Sequence[os.PathLike]) = set() + ): + paths_set = set(paths) + try: + self._definition_paths.add(paths_set) + except AttributeError: + self._definition_paths = paths_set + self.refresh_definitions() def read( self, xsy_path: os.path, - expand: bool = True, - dir_path: os.path = "", - def_paths: Sequence[os.path] = [], - db_ss_path: os.path = None, - ddt_ss_path: os.path = None, - ) -> DataFrame: + expand: bool = True + ) -> tuple[DataFrame, DataFrame]: """Read the dataBlock of an XSY file into a dataframe.""" - self._set_fields( - dir_path=dir_path, - def_paths=def_paths, - db_ss_path=db_ss_path, - ddt_ss_path=ddt_ss_path - ) - xsy_path = os.path.join(dir_path, xsy_path) with ( open(xsy_path) as xsy_file, - open(self.db_ss_path) as ss_file - ): - datablock = pd.read_xml(xsy_file, stylesheet=ss_file) + open(self.dataBlock_stylesheet) as stylesheet + ): + datablock = pd.read_xml(xsy_file, stylesheet=stylesheet) + with ( + open(xsy_path) as xsy_file, + open(self.definition_stylesheet) as stylesheet + ): + definitions = pd.read_xml(xsy_file, stylesheet=stylesheet) if expand: - return self.expand_datablock(datablock) + return (self.expand_datablock(datablock, definitions), definitions) else: - return datablock + return (datablock, definitions) + + def refresh_definitions(self): + defs: list[DataFrame] = [AuDaGe.iec.iec_definitions()] + definition_paths = self._definition_paths | {self.builtins} + for path in definition_paths: + with ( + open(path) as def_file, + open(self.definition_stylesheet) as stylesheet + ): + defs.append(pd.read_xml(def_file, stylesheet=stylesheet)) + # defs.append(AuDaGe.iec.iec_definitions()) + self.definitions = pd.concat(defs) def write(self): """Write dataframe to XSY file.""" raise NotImplementedError() - def expand_datablock(self, datablock: DataFrame) -> DataFrame: - datablock.set_index("type") - datablock_expanded = ( - datablock - .join( - self.defs, - how="cross", - rsuffix="_def" - ) - .query( - "type==parent_def" - ) - ) - return datablock_expanded - - def _read_defs(self, xsy_path: os.path = None) -> DataFrame: - """Read the DDTSource nodes of one or more XSY files into a dataframe. - """ - if xsy_path is not None: - self.def_paths.append(xsy_path) - defs: list[DataFrame] = [] - with open(self.ddt_ss_path) as ss_file: - for def_path in self.def_paths: - with open(def_path) as def_file: - def_df = pd.read_xml(def_file, stylesheet=ss_file) - defs.append(def_df) - return pd.concat(defs) - - def _set_fields( + def expand_datablock( self, - dir_path: os.path = "", - def_paths: Sequence[os.path] = [], - builtins_path: os.path = None, - db_ss_path: os.path = None, - ddt_ss_path: os.path = None - ): - # Directory - if dir_path != "": - self.dir_path = dir_path - # Builtins - if builtins_path is None: - builtins_path = "data/builtins.xsy" - self.def_paths.append(resources.files( - "AuDaGe.formats.controlexpert.xsy" - ).joinpath(builtins_path)) - # Stylesheets - if db_ss_path is None: - db_ss_path = "data/flatten_datablock.xslt" - self.db_ss_path: os.path = resources.files( - "AuDaGe.formats.controlexpert.xsy" - ).joinpath(db_ss_path) - if ddt_ss_path is None: - ddt_ss_path = "data/flatten_ddtsource.xslt" - self.ddt_ss_path: os.path = resources.files( - "AuDaGe.formats.controlexpert.xsy" - ).joinpath(ddt_ss_path) - # Definitions - if def_paths != []: - self.def_paths.extend([ - os.path.join(self.dir_path, def_path) - for def_path in def_paths - ]) - # Update definitions. - self.defs = self._read_defs() - elif self.defs is None: - self.defs = self._read_defs() + datablock: DataFrame, + definitions: DataFrame = None + ) -> DataFrame: + start = timer() + if definitions is None: + definitions = self.definitions + else: + definitions = self._calculate_offsets( + pd.concat([definitions, self.definitions]) + ) + print(f"Calculate Offsets: {timer() - start}") + datablock_expanded = datablock.join( + definitions, + how="cross", + rsuffix="_def" + ) + print(f"Cross join: {timer() - start}") + datablock_queried = datablock_expanded[ + datablock_expanded.type == datablock_expanded.parent_def + ] + print(f"Query: {timer() - start}") + return datablock_queried + def _calculate_offsets( + self, + definitions: DataFrame + ) -> DataFrame: + unique_parents = definitions.parent.unique() + for parent in unique_parents: + definitions[definitions.parent == parent] + return definitions + + + +if __name__ == "__main__": + start = timer() + xsy = XSY() + (datablock, definitions) = xsy.read("tests/assets/rowlett2.xsy", expand=False) + out_path = "tests/output" + datablock.to_csv( + os.path.join(out_path, "datablock.csv"), + index=False + ) + definitions.to_csv( + os.path.join(out_path, "defs.csv"), + index=False + ) + xsy.definitions.to_csv( + os.path.join(out_path, "builtins.csv"), + index=False + ) + xsy.expand_datablock(datablock, definitions).to_csv( + os.path.join(out_path, "expanded.csv"), + index=False + ) + end = timer() + print(end - start) \ No newline at end of file diff --git a/src/AuDaGe/iec.py b/src/AuDaGe/iec.py index 5c28185..ef92564 100644 --- a/src/AuDaGe/iec.py +++ b/src/AuDaGe/iec.py @@ -1,8 +1,11 @@ """ Definitions for IEC 61131 programming paradigms. """ +import dataclasses from dataclasses import dataclass -from enum import Enum +from enum import Enum, IntEnum + +import pandas as pd @dataclass class DataType: @@ -16,19 +19,49 @@ class DataType: self.length_bytes = length_bits // 8 self.length_words = length_bits // word_length -class DataTypes(DataType, Enum): - BOOL = 1 - BYTE = 8 - CHAR = 8 - INT = 16 - UINT = 16 - WORD = 16 - DATE = 32 - DT = 32 - DINT = 32 +# DataTypes = { +# "BOOL": 1, +# "BYTE": 8, +# "CHAR": 8, +# "INT": 16, +# "UINT": 16, +# "WORD": 16, +# "DATE": 32, +# "DT": 32, +# "DINT": 32, +# "DWORD": 32, +# "REAL": 32, +# "TIME": 32, +# "UDINT": 32 +# } +# class DataTypes(DataType, Enum): +# BOOL = DataType(1) +# BYTE = DataType(8) +# CHAR = DataType(8) +# INT = DataType(16) +# UINT = DataType(16) +# WORD = DataType(16) +# DATE = DataType(32) +# DT = DataType(32) +# DINT = DataType(32) +# DWORD = DataType(32) +# REAL = DataType(32) +# TIME = DataType(32) +# UDINT = DataType(32) + +class DataTypes(int, Enum): + BOOL = 1 + BYTE = 8 + CHAR = 8 + INT = 16 + UINT = 16 + WORD = 16 + DATE = 32 + DT = 32 + DINT = 32 DWORD = 32 - REAL = 32 - TIME = 32 + REAL = 32 + TIME = 32 UDINT = 32 class ElementaryVariable: @@ -55,4 +88,23 @@ class UDT: ): self.name = name self.comment = comment - self.variables = variables \ No newline at end of file + self.variables = variables + + +def iec_definitions() -> pd.DataFrame: + empty = pd.Series([pd.NA] * len(DataTypes.__members__)) + return pd.DataFrame( + data={ + "name": empty, + "parent": DataTypes.__members__, + "type": empty, + "address": empty, + "bitrank": empty, + "save": empty, + "comment": empty + }, + # index = + ) + +if __name__ == "__main__": + iec_definitions().to_csv("tests/output/iec_definitions.csv") \ No newline at end of file diff --git a/tests/test_xsy.py b/tests/test_xsy.py index edae380..5c5b421 100644 --- a/tests/test_xsy.py +++ b/tests/test_xsy.py @@ -10,16 +10,17 @@ def test_init(): def test_export(): xsy = XSY() datablock = xsy.read("tests/data/hsps.xsy", expand=False) + print(xsy.def_paths) out_path = "tests/output" datablock.to_csv( - os.path.join(out_path, 'datablock.csv'), + os.path.join(out_path, "datablock.csv"), index=False ) xsy.defs.to_csv( - os.path.join(out_path, 'defs.csv'), + os.path.join(out_path, "defs.csv"), index=False ) xsy.expand_datablock(datablock).to_csv( - os.path.join(out_path, 'expanded.csv'), + os.path.join(out_path, "expanded.csv"), index=False )