Add support for multidimensional arrays to IEC.
This commit is contained in:
@@ -12,7 +12,7 @@ import re
|
|||||||
import warnings
|
import warnings
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from enum import Enum, EnumType, IntEnum
|
from enum import Enum, EnumType, IntEnum
|
||||||
from math import ceil
|
from math import ceil, prod
|
||||||
|
|
||||||
from pandas import NA, Series
|
from pandas import NA, Series
|
||||||
|
|
||||||
@@ -23,8 +23,11 @@ class DataUnit(IntEnum):
|
|||||||
"""The length in bits of various data units."""
|
"""The length in bits of various data units."""
|
||||||
|
|
||||||
BIT = 1
|
BIT = 1
|
||||||
|
NIBBLE = 4
|
||||||
BYTE = 8
|
BYTE = 8
|
||||||
WORD = 16
|
WORD = 16
|
||||||
|
DWORD = 32
|
||||||
|
QWORD = 64
|
||||||
|
|
||||||
|
|
||||||
class IECType:
|
class IECType:
|
||||||
@@ -189,7 +192,10 @@ class IECTypes(Enum, metaclass=AliasEnumType):
|
|||||||
class IECArray(IECType):
|
class IECArray(IECType):
|
||||||
"""An IEC 61131-3 array data type variable."""
|
"""An IEC 61131-3 array data type variable."""
|
||||||
|
|
||||||
ARRAY_SPEC_PATTERN = r"^\s*arr(?:ay)? *\[(\d+)\.{2,3}(\d+)\] *(?:of +)?(\w+)\s*$"
|
BOUNDS_PATTERN = r"\d+"
|
||||||
|
ARRAY_SPEC_PATTERN = (
|
||||||
|
r"^\s*arr(?:ay)?\s*\[((?:\d+\s*\.{2,3}\s*\d+\s*,?\s*)+)\]\s*(?:of\s+)?(\w+)\s*$"
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, spec: str, *args, **kwargs):
|
def __init__(self, spec: str, *args, **kwargs):
|
||||||
"""Create an instance of IECArray based on an IEC 61131-3 array
|
"""Create an instance of IECArray based on an IEC 61131-3 array
|
||||||
@@ -228,16 +234,18 @@ class IECArray(IECType):
|
|||||||
if round_function is None:
|
if round_function is None:
|
||||||
round_function = self.round
|
round_function = self.round
|
||||||
# Validate the array specification.
|
# Validate the array specification.
|
||||||
m = re.match(self.ARRAY_SPEC_PATTERN, self.spec, re.IGNORECASE)
|
match = re.match(self.ARRAY_SPEC_PATTERN, self.spec, re.IGNORECASE)
|
||||||
|
bounds = re.findall(self.BOUNDS_PATTERN, match.groups()[0], re.IGNORECASE)
|
||||||
# Parse the array specification.
|
# Parse the array specification.
|
||||||
start_index = int(m.groups()[0])
|
data_type = match.groups()[-1].upper()
|
||||||
end_index = int(m.groups()[1])
|
|
||||||
data_type = m.groups()[2].upper()
|
|
||||||
# Calculate the length of the array in bits.
|
# Calculate the length of the array in bits.
|
||||||
length_bits = (
|
length_bits = (
|
||||||
(end_index - start_index + 1)
|
prod(
|
||||||
|
(int(ubound) - int(lbound) + 1)
|
||||||
|
for (lbound, ubound) in zip(bounds[::2], bounds[1::2])
|
||||||
|
)
|
||||||
* IECTypes[data_type].value.length_bits
|
* IECTypes[data_type].value.length_bits
|
||||||
) # fmt: skip
|
)
|
||||||
if length_bits % unit != 0:
|
if length_bits % unit != 0:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
f"Array length is not divisible by the selected unit. The "
|
f"Array length is not divisible by the selected unit. The "
|
||||||
@@ -260,18 +268,21 @@ class IECArray(IECType):
|
|||||||
"""
|
"""
|
||||||
if array_spec is None:
|
if array_spec is None:
|
||||||
array_spec = self.spec
|
array_spec = self.spec
|
||||||
m = re.match(self.ARRAY_SPEC_PATTERN, array_spec, re.IGNORECASE)
|
match = re.match(self.ARRAY_SPEC_PATTERN, array_spec, re.IGNORECASE)
|
||||||
# Check whether the regex matches at all.
|
# Check whether the regex matches at all.
|
||||||
if m is None:
|
if match is None:
|
||||||
return False
|
return False
|
||||||
|
bounds = re.findall(self.BOUNDS_PATTERN, match.groups()[0], re.IGNORECASE)
|
||||||
# Check whether the data type is valid.
|
# Check whether the data type is valid.
|
||||||
if not m.groups()[2].upper() in IECTypes:
|
if not match.groups()[-1].upper() in IECTypes:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
# Check that the upper bound is greater than the lower bound.
|
# Check that each dimension's upper bound is greater than or equal
|
||||||
if int(m.groups()[0]) > int(m.groups()[1]):
|
# to its lower bound.
|
||||||
|
for lbound, ubound in zip(bounds[::2], bounds[1::2]):
|
||||||
|
if int(lbound) > int(ubound):
|
||||||
return False
|
return False
|
||||||
# ValueError is raised if the array bounds are not integers.
|
# ValueError is raised if the array bounds are not coercible to integers.
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
return False
|
||||||
# Return True if all the tests succeeded.
|
# Return True if all the tests succeeded.
|
||||||
@@ -318,6 +329,7 @@ class IECString(IECType):
|
|||||||
round_function = self.round
|
round_function = self.round
|
||||||
m = re.match(self.STRING_SPEC_PATTERN, self.spec, re.IGNORECASE)
|
m = re.match(self.STRING_SPEC_PATTERN, self.spec, re.IGNORECASE)
|
||||||
try:
|
try:
|
||||||
|
# 1 ASCII character == 1 byte.
|
||||||
length_bits = int(m.groups()[0]) * DataUnit.BYTE
|
length_bits = int(m.groups()[0]) * DataUnit.BYTE
|
||||||
except (AttributeError, ValueError):
|
except (AttributeError, ValueError):
|
||||||
raise ValueError("The provided string specification is invalid.")
|
raise ValueError("The provided string specification is invalid.")
|
||||||
|
|||||||
@@ -24,13 +24,13 @@ class TestIEC(unittest.TestCase):
|
|||||||
("CHAR", DataUnit.BIT, 8),
|
("CHAR", DataUnit.BIT, 8),
|
||||||
("INT", DataUnit.BIT, 16),
|
("INT", DataUnit.BIT, 16),
|
||||||
("BOOL", DataUnit.WORD, 1),
|
("BOOL", DataUnit.WORD, 1),
|
||||||
("ARRAY [0..10] OF INT", DataUnit.WORD, 11),
|
("ARRAY [1..10, 0..7] OF INT", DataUnit.WORD, 80),
|
||||||
("STRING[10]", DataUnit.WORD, 5),
|
("STRING[10]", DataUnit.WORD, 5),
|
||||||
("UNKNOWN_UDT", DataUnit.WORD, -1),
|
("UNKNOWN_UDT", DataUnit.WORD, -1),
|
||||||
]
|
]
|
||||||
for data_type in data_types:
|
for data_type in data_types:
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
get_data_length(data_type[0], data_type[1]), data_type[2], data_type
|
get_data_length(data_type[0], data_type[1]), data_type[2], data_type[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_iec_definitions(self):
|
def test_iec_definitions(self):
|
||||||
@@ -43,7 +43,10 @@ class TestIECArray(unittest.TestCase):
|
|||||||
def test_array_length(self):
|
def test_array_length(self):
|
||||||
good_array_specs = [
|
good_array_specs = [
|
||||||
("ARRAY [0..3] OF INT", DataUnit.WORD, ceil, 4),
|
("ARRAY [0..3] OF INT", DataUnit.WORD, ceil, 4),
|
||||||
|
("ARRAY [0..0] OF REAL", DataUnit.WORD, ceil, 2),
|
||||||
|
("ARRAY [0..3, 0..3] OF INT", DataUnit.WORD, ceil, 16),
|
||||||
("array[15..20] of bool", DataUnit.BIT, ceil, 6),
|
("array[15..20] of bool", DataUnit.BIT, ceil, 6),
|
||||||
|
("ARRAY [0..1, 0..2, 0..3, 0..4] OF INT", DataUnit.WORD, ceil, 120),
|
||||||
]
|
]
|
||||||
warning_array_specs = [
|
warning_array_specs = [
|
||||||
("array[15..20] of bool", DataUnit.WORD, ceil, 1),
|
("array[15..20] of bool", DataUnit.WORD, ceil, 1),
|
||||||
@@ -51,11 +54,9 @@ class TestIECArray(unittest.TestCase):
|
|||||||
]
|
]
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter("ignore")
|
warnings.simplefilter("ignore")
|
||||||
self.assertTrue(
|
for spec in good_array_specs + warning_array_specs:
|
||||||
all(
|
self.assertEqual(
|
||||||
IECArray(spec[0]).get_length(spec[1], spec[2]) == spec[3]
|
IECArray(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0]
|
||||||
for spec in good_array_specs + warning_array_specs
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
for spec in warning_array_specs:
|
for spec in warning_array_specs:
|
||||||
self.assertWarns(
|
self.assertWarns(
|
||||||
@@ -77,7 +78,8 @@ class TestIECArray(unittest.TestCase):
|
|||||||
"array [1..7] of float", # Invalid data type
|
"array [1..7] of float", # Invalid data type
|
||||||
"array [5..1] of dint", # Lower bound greater than upper bound
|
"array [5..1] of dint", # Lower bound greater than upper bound
|
||||||
)
|
)
|
||||||
self.assertTrue(all(IECArray(spec).is_valid() for spec in valid_array_specs))
|
for spec in valid_array_specs:
|
||||||
|
self.assertTrue(IECArray(spec).is_valid(), spec)
|
||||||
for spec in invalid_array_specs:
|
for spec in invalid_array_specs:
|
||||||
self.assertRaises(ValueError, IECArray, spec)
|
self.assertRaises(ValueError, IECArray, spec)
|
||||||
|
|
||||||
@@ -100,11 +102,9 @@ class TestIECString(unittest.TestCase):
|
|||||||
]
|
]
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter("ignore")
|
warnings.simplefilter("ignore")
|
||||||
self.assertTrue(
|
for spec in good_string_specs + warning_string_specs:
|
||||||
all(
|
self.assertEqual(
|
||||||
IECString(spec[0]).get_length(spec[1], spec[2]) == spec[3]
|
IECString(spec[0]).get_length(spec[1], spec[2]), spec[3], spec[0]
|
||||||
for spec in good_string_specs + warning_string_specs
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
for spec in warning_string_specs:
|
for spec in warning_string_specs:
|
||||||
self.assertWarns(
|
self.assertWarns(
|
||||||
|
|||||||
Reference in New Issue
Block a user