Simatic_XML_Parser_to_SCL/processors/process_comparison.py

82 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
from .processor_utils import get_scl_representation, format_variable_name,get_target_scl_name
# TODO: Import necessary functions from processor_utils
# Example: from .processor_utils import get_scl_representation, format_variable_name
# Or: import processors.processor_utils as utils
# TODO: Define constants if needed (e.g., SCL_SUFFIX) or import them
SCL_SUFFIX = "_scl"
# --- Function code starts ---
def process_comparison(instruction, network_id, scl_map, access_map, data):
"""
Genera la expresión SCL para Comparadores (GT, LT, GE, LE, NE).
El resultado se propaga por scl_map['out'].
"""
instr_uid = instruction["instruction_uid"]
instr_type = instruction["type"] # GT, LT, GE, LE, NE
if instr_type.endswith(SCL_SUFFIX) or "_error" in instr_type:
return False
# Mapa de tipos a operadores SCL
op_map = {"GT": ">", "LT": "<", "GE": ">=", "LE": "<=", "NE": "<>"}
scl_operator = op_map.get(instr_type)
if not scl_operator:
instruction["scl"] = f"// ERROR: Tipo de comparación no soportado: {instr_type}"
instruction["type"] += "_error"
return True
# Obtener operandos
in1_info = instruction["inputs"].get("in1")
in2_info = instruction["inputs"].get("in2")
in1_scl = get_scl_representation(in1_info, network_id, scl_map, access_map)
in2_scl = get_scl_representation(in2_info, network_id, scl_map, access_map)
if in1_scl is None or in2_scl is None:
return False # Dependencias no listas
# Formatear operandos si son variables
op1 = format_variable_name(in1_scl) if in1_info and in1_info.get("type") == "variable" else in1_scl
op2 = format_variable_name(in2_scl) if in2_info and in2_info.get("type") == "variable" else in2_scl
# Añadir paréntesis si contienen espacios (poco probable tras formatear)
op1 = f"({op1})" if " " in op1 and not op1.startswith("(") else op1
op2 = f"({op2})" if " " in op2 and not op2.startswith("(") else op2
comparison_scl = f"{op1} {scl_operator} {op2}"
# Guardar resultado en el mapa para 'out'
map_key_out = (network_id, instr_uid, "out")
scl_map[map_key_out] = f"({comparison_scl})" # Poner paréntesis por seguridad
# Manejar entrada 'pre'/RLO -> ENO (como en EQ)
pre_input = instruction["inputs"].get("pre") # Asumir 'pre' como en EQ
en_scl = get_scl_representation(pre_input, network_id, scl_map, access_map) if pre_input else "TRUE"
if en_scl is None:
return False # Dependencia 'pre'/'en' no lista
map_key_eno = (network_id, instr_uid, "eno")
scl_map[map_key_eno] = en_scl
instruction["scl"] = f"// Comparison {instr_type} {instr_uid}: {comparison_scl}"
instruction["type"] = instr_type + SCL_SUFFIX
return True
# --- Procesador de Matemáticas (ADD ya existe, añadir otros) ---
# --- Function code ends ---
# --- Processor Information Function ---
def get_processor_info():
"""Devuelve la información para los comparadores (excepto EQ)."""
# Esta función maneja múltiples tipos de comparación.
return [
{'type_name': 'gt', 'processor_func': process_comparison, 'priority': 2}, # >
{'type_name': 'lt', 'processor_func': process_comparison, 'priority': 2}, # <
{'type_name': 'ge', 'processor_func': process_comparison, 'priority': 2}, # >=
{'type_name': 'le', 'processor_func': process_comparison, 'priority': 2}, # <=
{'type_name': 'ne', 'processor_func': process_comparison, 'priority': 2} # <>
]