From 304c21b478b18d079d672f866bea36a9a5673e89 Mon Sep 17 00:00:00 2001
From: Silcantar <1491897+Silcantar@users.noreply.github.com>
Date: Fri, 2 Oct 2026 16:34:43 -0500
Subject: [PATCH] Get XSY module mostly feature-complete.
---
.../formats/controlexpert/assets/builtins.xsy | 473 ++++++++++++++++++
.../assets/flatten_ddtsource.xslt | 17 +-
src/AuDaGe/formats/controlexpert/xsy.py | 466 ++++++++++++++---
tests/test_xsy.py | 24 +-
4 files changed, 892 insertions(+), 88 deletions(-)
diff --git a/src/AuDaGe/formats/controlexpert/assets/builtins.xsy b/src/AuDaGe/formats/controlexpert/assets/builtins.xsy
index 11f9ed7..f689020 100644
--- a/src/AuDaGe/formats/controlexpert/assets/builtins.xsy
+++ b/src/AuDaGe/formats/controlexpert/assets/builtins.xsy
@@ -386,6 +386,479 @@
+
+
+
+
+
+
+
+
+
+
+
+ Common array for communication EF Mid-Range
+
+
+
+
+
+
+ Common array for EIO communication function blocks
+
+
+
+
+
+
+ Common array for communication EF
+
+
+
+
+ Datatype extended for master control block MSTR_CTL
+
+
+ operation code
+
+ error status
+
+ number of data units transferred
+
+ operation-dependent information
+
+ routing address
+
+ operation-dependent information
+
+
+
+
+
+
+
+
+ Data structure for the XMIT configuration
+
+
+ Data Rate
+
+
+ Data bits
+
+
+ Parity
+
+
+ Stop bits
+
+
+ Command word
+
+
+ Message Length
+
+
+ Retry limit
+
+
+ Start of transmission delay (ms)
+
+
+ End of transmission delay (ms)
+
+
+ Response time-out (ms)
+
+
+
+
+
+
+ Health bits of rack 0: slots 0 to 15
+
+
+ Health bits of rack 1: slots 0 to 15
+
+
+ Device name of the CRA drop
+
+
+ Firmware version of the CRA
+
+
+ Rotary switches value at power up
+
+
+ State of the CRA module
+
+
+ CRA diagnostic information
+
+
+ At least one IO error in drop when 0
+
+
+ CCOTF in progress
+
+
+ CCOTF error
+
+
+ IOPL mismatch
+
+
+ Rotary switches have changed since last power up
+
+
+ Drop communication health
+
+
+ Redundant power supply status
+
+
+ Execution time of the last CRA cycle (unit: 0.01 ms)
+
+
+ Longes CRA cycle execution time (unit: 0.01 ms)
+
+
+ Shortest CRA cycle execution time (uint: 0.01 ms)
+
+
+ Number of time stamp records available in the drop local buffer
+
+
+ Diagnostic information about the source time stamped events service
+
+
+ Time valid and synchronized
+
+
+ Clock failure
+
+
+ Clock not synchronized
+
+
+ Buffer full
+
+
+ Percentage of the buffer filled [0..100]
+
+
+ Main state of the TS events service
+
+
+ Ethernet status
+
+
+ Link up/down for Ethernet port 1
+
+
+ Link up/down for Ethernet port 2
+
+
+ Link up/down for Ethernet port 3
+
+
+ Link up/down for Ethernet backplane port
+
+
+ Ethernet/IP RPI change in progress
+
+
+ Redundant owner is present
+
+
+ 0: one or more services not operating normally / 1: all operational
+
+
+ One bit for each user-observable feature
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ Ethernet backplane status (0: failure, 1: OK)
+
+
+ Ethernet backplane error (0: error, 1: OK)
+
+
+ Ethernet port status
+
+
+ Time elapsed since last update from NTP server (unit: 100 ms)
+
+
+ Max packet interval for output packets (unit: 1 ms)
+
+
+ Number of bytes received on interface
+
+
+ Numberof inbound packets that contain errors
+
+
+ Number of bytes sent on interface
+
+
+ Number of outbound packets that contain errors
+
+
+ SOE uncertain (in timestamping system only)
+
+
+
+
+
+
+ Ethernet status
+
+
+ Link up/down for Ethernet port 1
+
+
+ Link up/down for Ethernet port 2
+
+
+ Link up/down for Ethernet port 3
+
+
+ Link up/down for Ethernet backplane port
+
+
+ Scanner OK and scanning at least 1 equipment (if at least 1 equipment configured)
+
+
+ 0: one or more services not operating normally / 1: all operational
+
+
+ 1: No traffic overload detected / 0: Traffic overload detected
+
+
+ One bit for each user-observable feature
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ Main IP address status (0 in caes of duplicate IP or no IP assigned)
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ 0: service not operating normally / 1: service operating normally or disabled
+
+
+ One bit for each user-observable feature
+
+
+ LLDP service status
+
+
+ 0: event log service not operating normally / 1: event log service operating normally
+
+
+ 1: No acknowledgement received from the syslog server / 0: otherwise
+
+
+ Ethernet port 1 and 2 status
+
+
+ Ethernet port 3 and backplane port status
+
+
+ MSB=Major revision, LSB=Minor revision
+
+
+ % of FDR server usage
+
+
+ Number of packets received on interface
+
+
+ Number of inbound packets that contain errors
+
+
+ Number of packets sent on interface
+
+
+ Number of outbound packets that contain errors
+
+
+ Signature of all files on local module FDR server
+
+
+ Local Slave health bits (local slave 1 to 16)
+
+
+ DIO health bits (1 bit per DIO up to 128 DIOs. 112-127 are reserved for future extension)
+
+
+ DIO control bits (1 bit per DIO up to 128 DIOs. 112-127 are reserved for future extension)
+
+
+
+
+
+
+ Status of the remote PLC
+
+
+ Applications of the primary and the standby are different from the origin
+
+
+ Command that allows logic mismatch between the primary and the standby
+
+
+ Applications of the primary and the standby are running with different step of online modification
+
+
+ Applications of the primary and the standby are running with difference at least one SFC section
+
+
+ Applications of the primary and the standby are running with different steps of offline modification
+
+
+ Number of differences between the application of the primary and application of the secondary
+
+
+ Number maximum of differences allowed between application of the primary and the application of the secondary
+
+
+ Command that allows firmware mismatch between the primary and the standby
+
+
+ Firmware of the primary is different from the firmware of the standby
+
+
+ Data layout of the primary is different from the data layout of the standby
+
+
+ Amount of data in KByte sent by the primary and discarded by the standby
+
+
+ Amount of data in KByte not updated by the standby (rounded to the upper)
+
+
+ Backup application of the primary and backup application fo the standby
+
+
+ Command that allows PLC A to go promary or standby (modification allowed only on primary)
+
+
+ Command that allows PLC B to go primary or standby (modification allowed only on primary)
+
+
+ Swap primary and standby command (modification allowed only on primary)
+
+
+ Transfer application from the primary to the standby command (modification allowed only on primary)
+
+
+ Command that automatically start the application in Run after transfer (modification allowed only on primary)
+
+
+ Run remote controller command (modification allowed only on primary)
+
+
+ Stop remote controller command (modification allowed on primary, on stop or on wait)
+
+
+ Command that start a comparison of the variable initial values (modification allowed only on primary)
+
+
+ Initial values comparision status
+
+
+ Exchanged data from the last MAST cycle standby receipt status
+
+
+ Exchanged data from the last FAST cycle standby receipt status
+
+
+ Exchanged data from the last SAFE cycle standby receipt status
+
+
+
+
+
+
+
+
+ Hot standby link error (no heartbeat connection on hot standby link)
+
+
+ Hot standby supplementary link error (no heartbeat connection on supplementary link)
+
+
+ The PLC is in Run but is waiting to be part of the hot standby system
+
+
+ The PLC is in Run Primary state
+
+
+ The PLC is in Run Standby state
+
+
+ The PLC is in Stop
+
+
+ The PLC switch is on A position
+
+
+ The PLC switch is on B position
+
+
+ The PLC does not see any of the configured EIO drops
+
+
+ An SD Card is inserted
+
+
+ True: the local rack is OK / False: the local rack is not ok (module missing)
+
+
+ Free space to exchange information between primary and standby PLCs.
+
+
+
diff --git a/src/AuDaGe/formats/controlexpert/assets/flatten_ddtsource.xslt b/src/AuDaGe/formats/controlexpert/assets/flatten_ddtsource.xslt
index 463d2d2..093107a 100644
--- a/src/AuDaGe/formats/controlexpert/assets/flatten_ddtsource.xslt
+++ b/src/AuDaGe/formats/controlexpert/assets/flatten_ddtsource.xslt
@@ -4,11 +4,16 @@
Copyright 2026 Prime Controls. All rights reserved.
-->
+
+
+
+
+
@@ -19,8 +24,8 @@
-
-
+
+
@@ -34,4 +39,12 @@
+
+
+ NULL
+
+ DFB
+
+
+
\ No newline at end of file
diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py
index 6024b48..51fee68 100644
--- a/src/AuDaGe/formats/controlexpert/xsy.py
+++ b/src/AuDaGe/formats/controlexpert/xsy.py
@@ -1,28 +1,34 @@
"""
AuDaGe XSY module
-name: test_xsy.py
+name: xsy.py
by: Josh Lucas
date: 2026-09-14
desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files.
"""
+import logging
import os
+import warnings
from collections.abc import Iterable
from dataclasses import dataclass
+from enum import IntEnum, StrEnum, auto
from importlib import resources
import pandas as pd
-from pandas import DataFrame
+from AuDaGe import iec
from AuDaGe.audage_frame import AuDaGeFrame
+from AuDaGe.debug_tools import MeasureTime, TimeUnit
from AuDaGe.format import Format
-from AuDaGe.iec import *
+
+ASSETS_PATH = "AuDaGe.formats.controlexpert.assets"
MAX_RECURSION = 5
-ASSETS_PATH = "AuDaGe.formats.controlexpert.assets"
+
+module_logger = logging.getLogger(__name__)
class XSYBlock(AuDaGeFrame):
@@ -31,15 +37,17 @@ class XSYBlock(AuDaGeFrame):
from_file class method.
"""
- _stylesheet_path = None
- _xsy_path = None
+ # _stylesheet_path: str # = None
+ # _xsy_path: str # = None
+ _name: str
@classmethod
def from_file(
cls,
xsy_path: os.PathLike,
stylesheet_path: os.PathLike,
- ) -> XSYBlock:
+ name_ext: str = "",
+ ) -> XSYBlock: # noqa: F821
"""Load an XSY file into a dataframe using the specified stylesheet."""
cls.stylesheet_path = stylesheet_path
cls.xsy_path = xsy_path
@@ -47,34 +55,41 @@ class XSYBlock(AuDaGeFrame):
resources.as_file(cls.xsy_path) as xsy_file,
resources.as_file(cls.stylesheet_path) as stylesheet_file,
):
- return cls(pd.read_xml(xsy_file, stylesheet=stylesheet_file))
+ # frame = AuDaGeFrame(pd.read_xml(xsy_file, stylesheet=stylesheet_file))
+ return cls(
+ pd.read_xml(xsy_file, stylesheet=stylesheet_file),
+ ".".join(
+ (os.path.splitext(os.path.basename(xsy_path))[0], name_ext)
+ ),
+ )
class DataBlock(XSYBlock):
"""Read and parse the dataBlock node of an XSY file."""
- DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath("flatten_datablock.xslt")
-
- udt_lengths: dict[str, int]
+ DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath(
+ "flatten_datablock.xslt"
+ )
@classmethod
def from_file(
cls, xsy_path: os.PathLike, stylesheet_path: os.PathLike | None = None
- ) -> DataBlock:
+ ) -> DataBlock: # noqa: F821
"""Read the dataBlock of an XSY file into a DataFrame."""
if stylesheet_path is None:
stylesheet_path = cls.DEFAULT_SS_PATH
- return cls(XSYBlock.from_file(xsy_path, stylesheet_path))
-
- def calculate_all_offsets(self, mwvoea: bool = True) -> DataFrame:
- """Calculate the memory offset for all UDT members in the datablock."""
+ return cls(XSYBlock.from_file(xsy_path, stylesheet_path, "DataBlock"))
class Definitions(XSYBlock):
"""Read and parse the DDTSource nodes of an XSY file."""
- DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath("flatten_ddtsource.xslt")
- DEFAULT_BUILTINS_PATH = resources.files(ASSETS_PATH).joinpath("builtins.xsy")
+ DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath(
+ "flatten_ddtsource.xslt"
+ )
+ DEFAULT_BUILTINS_PATH = resources.files(ASSETS_PATH).joinpath(
+ "builtins.xsy"
+ )
@classmethod
def from_file(
@@ -83,7 +98,7 @@ class Definitions(XSYBlock):
stylesheet_path: os.path | None = None,
definition_paths: Iterable[os.path] | None = None,
include_builtins: bool = True,
- ):
+ ) -> Definitions: # noqa: F821
"""Read the DDTSource nodes of an XSY file into a DataFrame."""
cls.stylesheet_path = (
cls.DEFAULT_SS_PATH if stylesheet_path is None else stylesheet_path
@@ -95,80 +110,370 @@ class Definitions(XSYBlock):
cls.definition_paths.append(cls.DEFAULT_BUILTINS_PATH)
# Get the definitions from the main XSY file and put it in the list of
# definitions to be concatenated.
- cls.definitions = [XSYBlock.from_file(xsy_path, cls.stylesheet_path)]
+ cls.definitions_list = [
+ XSYBlock.from_file(xsy_path, cls.stylesheet_path)
+ ]
# Read the definitions from the other definition files (including the
# builtins file) and add then to the list.
- cls.definitions.extend(
- XSYBlock.from_file(path, cls.stylesheet_path)
+ cls.definitions_list.extend(
+ XSYBlock.from_file(path, cls.stylesheet_path, "Definitions")
for path in cls.definition_paths
)
- # Concatenate the dataframes in the list.
- return cls(pd.concat(cls.definitions))
+ _name = ".".join(
+ (os.path.splitext(os.path.basename(xsy_path))[0], "Definitions")
+ )
+ with MeasureTime(
+ TimeUnit.MS,
+ True,
+ "{_name} offset calculation completed in {time} {unit}.".format(
+ _name=_name, time="{time:.3f}", unit="{unit}"
+ ),
+ ):
+ offsets = cls._calculate_offsets(
+ pd.concat(cls.definitions_list),
+ _name=_name,
+ )
+ return cls(offsets)
+
+ def _get_length(self, row, udt_lengths: dict[str, int]) -> int:
+ # Find packed-bit words by checking whether...
+ if (
+ # ...the first member is a 16-bit binary or integer type...
+ row["type"] in ("WORD", "INT", "UINT")
+ # ...and that the second member is a boolean...
+ and row["next_type"] == "BOOL"
+ # ...and that the second member has a bitrank.
+ and row["next_bitrank"].notna()
+ ):
+ # We don't increase the offset for packed-bit words because the
+ # following booleans overlap with them.
+ return 0
+ else:
+ return iec.get_data_length(
+ row["type"],
+ udt_lengths=udt_lengths,
+ unknown_length=pd.NA,
+ )
+
+ @classmethod
+ def _calculate_offsets(
+ cls,
+ definitions: XSYBlock | None = None,
+ udt_lengths: dict[str, int] | None = None,
+ max_recursion: int = MAX_RECURSION,
+ _current_recursion: int = 0,
+ _name: str = "",
+ ) -> Definitions: # noqa: F821
+ """Recursively calculate the memory offset for all UDT members in the
+ definitions.
+ """
+ with MeasureTime(
+ TimeUnit.MS,
+ True,
+ "{_name} offset calculation recursion {recursion} completed in "
+ "{time} {unit}.".format(
+ _name=_name,
+ recursion=_current_recursion,
+ time="{time:.3f}",
+ unit="{unit}",
+ ),
+ ):
+ # Set parameters to default values.
+ definitions = cls if definitions is None else definitions
+ udt_lengths = {} if udt_lengths is None else udt_lengths
+ # We don't care about DFBs so set their length at zero.
+ udt_lengths["DFB"] = 0
+ definitions_joined = definitions.shift((0, -1), axis=0)
+ definitions_joined.to_csv("definitions_joined.csv")
+ # Create a column with the length of each member.
+ definitions["length"] = definitions_joined.apply(
+ lambda row: (
+ 0
+ if (
+ row["type_0"] in ("WORD", "INT", "UINT")
+ and row["type_-1"] == "BOOL"
+ and pd.notna(row["bitrank_-1"])
+ )
+ else iec.get_data_length(
+ row["type_0"],
+ udt_lengths=udt_lengths,
+ unknown_length=pd.NA,
+ )
+ ),
+ axis=1,
+ ).shift(1, axis=0, fill_value=0)
+ definitions.to_csv("definitions.csv")
+ # Calculate how many bits need to be skipped before each member so
+ # that addressing rules are followed (e.g. word-length types on
+ # integer addresses, multi-word types on even addresses).
+ # definitions["padding"] = definitions.apply()
+ # Set a flag high for checking whether all offsets have been found.
+ # If this isn't set False by anything later then we're done.
+ all_offsets_found = True
+ # Calculate the cumulative sum of the data lengths for each UDT
+ # definition. This is the offset of the subsequent member.
+ definitions_calculated_list = []
+ for name, group in definitions.groupby("parent"):
+ group["offset"] = group["length"].cumsum(skipna=False)
+ # Check if all offsets for this UDT has been found. If so, we
+ # know its length so we save it to the `udt_lengths` dictionary
+ # for future reference.
+ if group["offset"].notna().all():
+ udt_lengths[name] = group["offset"].tail(1).values[0]
+ else:
+ # This UDT's offsets aren't finished so set the flag False.
+ all_offsets_found = False
+ definitions_calculated_list.append(group)
+ # Re-combine all the UDT definitions.
+ definitions_calculated = pd.concat(definitions_calculated_list)
+ if all_offsets_found:
+ # We're done! Log the recursion depth we reached and return our
+ # results.
+ print(
+ f"{_name} offset calculation recursion depth reached: "
+ f"{_current_recursion}."
+ )
+ definitions_finished = definitions_calculated
+ elif _current_recursion >= max_recursion:
+ # We've reached the maximum recursion depth so return now
+ # without finishing.
+ warnings.warn(
+ "Maximum recursion depth for offset calculation reached."
+ "Returning now."
+ )
+ definitions_finished = definitions_calculated
+ else:
+ # Recurse to find the offsets for nested UDTs.
+ definitions_finished = cls._calculate_offsets(
+ definitions_calculated,
+ udt_lengths,
+ max_recursion,
+ _current_recursion + 1,
+ _name,
+ )
+ return definitions_finished
@dataclass
class XSY:
"""Object representing a ControlExpert variable exchange (XSY) file."""
+ # Dataclass attributes:
datablock: DataBlock
definitions: Definitions
- mwvoea: bool
+ even_addresses: bool
+ name: str
- def compile_datablock(self, datablock: DataFrame | None = None) -> DataFrame:
- """Compile a cross-joined and queried DataBlock by combining the columns
- from the datablock with the columns from the definitions."""
- datablock_compiled = DataFrame()
- datablock_compiled["name"] = datablock.apply(
- lambda row: f"{row.name_def}.{row.name}", axis=1
- )
- datablock_compiled["parent"] = ""
- datablock_compiled["type"] = datablock.type_def
- datablock_compiled["address"] = datablock.address
- datablock_compiled["bitrank"] = datablock.bitrank_def
- datablock_compiled["save"] = datablock.save
- datablock_compiled["comment"] = datablock.apply(
- lambda row: f"{row.comment_def}: {row.comment}", axis=1
- )
- return datablock_compiled
+ class DebugStep(IntEnum):
+ MAKE_ARRAY_DEFS = auto() # 1
+ GROUP = auto() # 2
+ JOIN = auto() # 3
+ COMPILE = auto() # 4
+ NONE = auto() # 5
+
+ class IgnoredDataTypes(StrEnum):
+ DFB = auto()
def expand_datablock(
self,
datablock: DataBlock | None = None,
definitions: Definitions | None = None,
+ ignored_data_types: StrEnum = IgnoredDataTypes,
+ debug_step: DebugStep | int = DebugStep.NONE,
max_recursion: int = MAX_RECURSION,
- current_recursion: int = 0,
- ) -> DataFrame:
+ ) -> DataBlock:
"""Recursively expand the UDTs in `datablock` based on the UDT
definitions in `definitions` and return the resulting DataFrame.
+
+ Parameters
+ ----------
+ datablock : DataBlock | None
+ DataBlock to expand. By default use self.datablock.
+ definitions : Definitions | None
+ Definitions to use for expansion. By default use self.definitions.
+ ignored_data_types : StrEnum
+ Data types to ignore. Defaults to self.IgnoredDataTypes.
+ debug_step : DebugStep | int
+ If set, return early at the specified step with an intermediate
+ version of the datablock. Default DebugStep.NONE.
+ max_recursion : int
+ The maximum number of times to recursively expand UDTs. Default 5.
"""
- datablock = self.datablock if datablock is None else datablock
- definitions = self.definitions if definitions is None else definitions
- datablock_joined = datablock.join(
- definitions, how="cross", rsuffix="_def"
- )
- datablock_queried = datablock_joined[
- datablock_joined.type == datablock_joined.parent_def
- ]
- datablock_compiled = self.compile_datablock(datablock_queried)
- if all(
- get_data_length(data_type, DataUnit.WORD) != -1
- for data_type in datablock_compiled.type
+ datablock_expanded: DataBlock
+ with MeasureTime(
+ TimeUnit.MS,
+ True,
+ "{name} datablock expansion completed in {time} {unit}.".format(
+ name=self.name, time="{time:.3f}", unit="{unit}"
+ ),
):
- datablock_finished = datablock_compiled
- elif current_recursion >= max_recursion:
- warnings.warn("Maximum recursion depth reached. Returning now.")
- datablock_finished = datablock_compiled
- else:
- datablock_finished = self.expand_datablock(
- datablock_compiled, definitions, max_recursion, current_recursion + 1
+ datablock = self.datablock if datablock is None else datablock
+ definitions = (
+ self.definitions if definitions is None else definitions
)
- return datablock_finished
+ # Make UDT array definitions.
+ with MeasureTime(
+ TimeUnit.MS,
+ True,
+ "{name} array definition completed in {time} {unit}.".format(
+ name=self.name,
+ time="{time:.3f}",
+ unit="{unit}",
+ ),
+ ):
+ array_definitions = self._make_array_definitions(
+ datablock, definitions
+ )
+ definitions_extended = pd.concat(
+ (definitions, array_definitions)
+ )
+ if debug_step == self.DebugStep.MAKE_ARRAY_DEFS:
+ return definitions_extended
+ datablock_expanded = self._expand_udts(
+ datablock,
+ definitions_extended,
+ ignored_data_types,
+ debug_step,
+ max_recursion,
+ )
+ return datablock_expanded
+
+ def _compile_datablock(self, datablock: DataBlock) -> DataBlock:
+ """Compile a cross-joined and filtered DataBlock by combining the
+ columns from the datablock with the columns from the definitions."""
+ datablock_compiled = pd.DataFrame()
+ datablock_compiled["name"] = datablock.apply(
+ lambda row: f"{row['name']}.{row['name_def']}", axis=1
+ )
+ datablock_compiled["parent"] = ""
+ datablock_compiled["type"] = datablock["type_def"]
+ datablock_compiled["address"] = datablock["address"]
+ datablock_compiled["bitrank"] = datablock["bitrank_def"]
+ datablock_compiled["save"] = datablock["save"]
+ datablock_compiled["comment"] = datablock.apply(
+ lambda row: f"{row['comment']}: {row['comment_def']}", axis=1
+ )
+ return DataBlock(datablock_compiled)
+
+ def _expand_udts(
+ self,
+ datablock: DataBlock,
+ definitions: Definitions,
+ ignored_data_types: StrEnum,
+ debug_step: DebugStep,
+ max_recursion: int,
+ _current_recursion: int = 0,
+ ) -> DataBlock:
+ """Helper function for expand_datablock that does the recursive
+ expansion of UDTs.
+
+ Parameters
+ ----------
+ udts: DataBlock | None
+ UDTs to expand. By default use self.datablock.
+ definitions: Definitions | None
+ Definitions to use for expansion. By default use self.definitions.
+ debug_step: int
+ If in DebugStep, return early with an intermediate version of the
+ datablock. Default -1.
+ max_recursion: int
+ Maximum recursion depth. Default 5.
+ _current_recursion: int
+ Current recursion depth. Not to be used except within this method.
+ """
+ datablock_grouped = datablock.groupby("type")
+ edts = pd.concat(
+ group
+ for (name, group) in datablock_grouped
+ if name in iec.ElementaryTypes
+ )
+ udts = pd.concat(
+ group
+ for (name, group) in datablock_grouped
+ if name not in iec.ElementaryTypes
+ )
+ if debug_step == self.DebugStep.GROUP:
+ return pd.concat((edts, udts))
+ definitions_grouped = definitions.groupby("parent")
+ udts_grouped = udts.groupby("type")
+ group_list = []
+ # Cross-join each type group in udts_grouped with its corresponding
+ # group from definitions_grouped.
+ with MeasureTime(
+ TimeUnit.MS,
+ True,
+ "{name} datablock expansion recursion {recursion} completed in "
+ "{time} {unit}.".format(
+ name=self.name,
+ recursion=_current_recursion,
+ time="{time:.3f}",
+ unit="{unit}",
+ ),
+ ):
+ for name, group in udts_grouped:
+ if name.lower() not in ignored_data_types:
+ group_list.append(
+ group.join(
+ pd.DataFrame(
+ definitions_grouped.get_group(name.upper())
+ ),
+ how="cross",
+ rsuffix="_def",
+ )
+ )
+ udts_joined = pd.concat(group_list)
+ if debug_step == self.DebugStep.JOIN:
+ return udts_joined
+ udts_compiled = self._compile_datablock(udts_joined)
+ if debug_step == self.DebugStep.COMPILE:
+ return udts_compiled
+ if all(
+ udts_compiled.apply(
+ lambda row: row["type"] in iec.ElementaryTypes, axis=1
+ )
+ ):
+ print(
+ f"{self.name} datablock expansion recursion depth reached: "
+ f"{_current_recursion}."
+ )
+ udts_finished = udts_compiled
+ elif _current_recursion >= max_recursion:
+ warnings.warn(
+ "Maximum recursion depth for DataBlock expansion "
+ "reached. Returning now."
+ )
+ udts_finished = udts_compiled
+ else:
+ udts_finished = self._expand_udts(
+ udts_compiled,
+ definitions,
+ ignored_data_types,
+ debug_step,
+ max_recursion,
+ _current_recursion + 1,
+ )
+ return pd.concat((edts, udts_finished))
+
+ def _make_array_definitions(
+ self, datablock: DataBlock, definitions: Definitions
+ ) -> Definitions:
+ """Make definitions for the expansion of arrays of UDTs."""
+ everything_grouped = pd.concat((datablock, definitions)).groupby("type")
+ array_definitions_list = []
+ for name, group in everything_grouped:
+ try:
+ array = iec.Array(name)
+ if not array.is_elementary():
+ array_definitions_list.append(array.make_definition(""))
+ except iec.InvalidSpecError:
+ continue
+ return pd.concat(array_definitions_list)
class XSYReader(Format):
- """Class for manipulating Schneider Electric EcoStruxure Control Expert
- variable exchange (XSY) files. Can be re-used to read multiple XSY files
- using common stylesheets and UDT definitions.
+ """Class for reading and writing Schneider Electric EcoStruxure Control
+ Expert variable exchange (XSY) files. Can be re-used to read multiple XSY
+ files using common stylesheets and UDT definitions.
"""
def __init__(
@@ -176,7 +481,7 @@ class XSYReader(Format):
definition_paths: Iterable[os.path] | None = None,
datablock_ss_path: os.path | None = None,
definitions_ss_path: os.path | None = None,
- mwvoea: bool = True,
+ even_addresses: bool = True,
):
"""Initialize XSYReader object.
@@ -184,28 +489,37 @@ class XSYReader(Format):
----------
definition_paths : iterable of pathlike (optional)
List of additional XSY files to read UDT (DDT) definitions from.
- db_ss_path: pathlike (optional)
+ datablock_ss_path: pathlike (optional)
Path to XSLT stylesheet to flatten the XSY file and extract the
dataBlock node before importing into pandas.
- ddt_ss_path: pathlike (optional)
+ definitions_ss_path: pathlike (optional)
Path to XSLT stylesheet to flatten the XSY file and extract the
DDTSource nodes before importing into pandas.
+ even_addresses: bool
+ Boolean indicating whether multi-word variables must be located on
+ even-numbered addresses. Default True.
"""
- self.definition_paths = [] if definition_paths is None else definition_paths
+ self.definition_paths = (
+ [] if definition_paths is None else definition_paths
+ )
self.datablock_ss_path = datablock_ss_path
self.definitions_ss_path = definitions_ss_path
- self.mwvoea = mwvoea
- """Dict for storing the length of all the UDTs included in the
- definitions.
- """
+ self.even_addresses = even_addresses
def read(self, xsy_path: os.path, expand: bool = True) -> XSY:
- """Read the dataBlock and definitions of an XSY file into an XSY object."""
+ """Read the dataBlock and definitions of an XSY file into an XSY
+ object.
+ """
self.datablock = DataBlock.from_file(xsy_path, self.datablock_ss_path)
self.definitions = Definitions.from_file(
xsy_path, self.definitions_ss_path, self.definition_paths
)
- return XSY(self.datablock, self.definitions, self.mwvoea)
+ return XSY(
+ datablock=self.datablock,
+ definitions=self.definitions,
+ even_addresses=self.even_addresses,
+ name=os.path.basename(xsy_path),
+ )
def write(self):
"""Write XSY object to file."""
diff --git a/tests/test_xsy.py b/tests/test_xsy.py
index d6e3da2..5312a9b 100644
--- a/tests/test_xsy.py
+++ b/tests/test_xsy.py
@@ -9,6 +9,8 @@ desc: Unit tests for the AuDaGe XSY module.
"""
import os
+
+# import time
import unittest
from importlib import resources
@@ -17,9 +19,6 @@ import output
import assets
from AuDaGe.formats.controlexpert.xsy import *
-ASSETS_PATH = "assets"
-OUTPUT_PATH = "output"
-
class TestXSY(unittest.TestCase):
"""This test case tests the XSYBlock, DataBlock, and Definitions classes in
@@ -69,17 +68,22 @@ class TestXSYReader(unittest.TestCase):
file_name = os.path.splitext(os.path.split(path)[-1])[0]
xsy = self.xsy_reader.read(path, expand=False)
xsy.datablock.to_csv(
- self.out_path.joinpath(f"{file_name}_datablock.csv"), index=False
+ self.out_path.joinpath(f"{file_name}_datablock.csv"),
+ index=False,
)
xsy.definitions.to_csv(
- self.out_path.joinpath(f"{file_name}_definitions.csv"), index=False
- )
- xsy.datablock.update(xsy.expand_datablock())
- xsy.datablock.to_csv(
- self.out_path.joinpath(f"{file_name}_expanded.csv"), index=False
+ self.out_path.joinpath(f"{file_name}_definitions.csv"),
+ index=False,
)
+ debug = False
+ for debug_step in XSY.DebugStep if debug else [XSY.DebugStep.NONE]:
+ xsy.expand_datablock(debug_step=debug_step).to_csv(
+ self.out_path.joinpath(
+ f"{file_name}_debug_{debug_step.name}.csv"
+ ),
+ index=False,
+ )
if __name__ == "__main__":
- __package__ = "test_xsy"
unittest.main(failfast=True)