118 lines
5.5 KiB
Python
118 lines
5.5 KiB
Python
# processors/process_blkmov.py
|
|
# -*- coding: utf-8 -*-
|
|
import sympy
|
|
import traceback
|
|
import re
|
|
# Usar las nuevas utilidades
|
|
from .processor_utils import get_sympy_representation, sympy_expr_to_scl, get_target_scl_name, format_variable_name
|
|
from .symbol_manager import SymbolManager, extract_plc_variable_name
|
|
|
|
SCL_SUFFIX = "_sympy_processed" # Usar el nuevo sufijo
|
|
|
|
def process_blkmov(instruction, network_id, sympy_map, symbol_manager: SymbolManager, data):
|
|
"""
|
|
Genera SCL usando BLKMOV directamente como nombre de función,
|
|
simplificando la condición EN.
|
|
ADVERTENCIA: Sintaxis BLKMOV probablemente no compile en TIA estándar.
|
|
"""
|
|
instr_uid = instruction["instruction_uid"]
|
|
instr_type_original = instruction.get("type", "BlkMov") # Asegurar que el tipo base sea correcto
|
|
current_type = instruction.get("type","")
|
|
if current_type.endswith(SCL_SUFFIX) or "_error" in current_type:
|
|
return False
|
|
|
|
# --- Obtener Entradas ---
|
|
en_input = instruction["inputs"].get("en")
|
|
# Obtener EN como expresión SymPy
|
|
sympy_en_expr = get_sympy_representation(en_input, network_id, sympy_map, symbol_manager) if en_input else sympy.true
|
|
|
|
srcblk_info = instruction["inputs"].get("SRCBLK")
|
|
# Obtener nombre RAW de SRCBLK (como se hacía antes, si es necesario para BLKMOV)
|
|
# Este nombre NO pasa por SymPy, se usa directo en el string SCL final
|
|
raw_srcblk_name = srcblk_info.get("name") if srcblk_info else None
|
|
|
|
# Verificar dependencias (EN debe estar resuelto, SRCBLK debe tener nombre)
|
|
if sympy_en_expr is None:
|
|
# print(f"DEBUG BlkMov {instr_uid}: EN dependency not ready")
|
|
return False
|
|
if raw_srcblk_name is None:
|
|
print(f"Error: BLKMOV {instr_uid} sin información válida para SRCBLK.")
|
|
instruction["scl"] = f"// ERROR: BLKMOV {instr_uid} sin SRCBLK válido."
|
|
instruction["type"] = instr_type_original + "_error"
|
|
return True
|
|
|
|
# --- Obtener Destinos (Salidas) ---
|
|
# RET_VAL (Obtener nombre SCL formateado)
|
|
retval_target_scl = get_target_scl_name(instruction, "RET_VAL", network_id, default_to_temp=True)
|
|
if retval_target_scl is None: # get_target_scl_name ya imprime error si falla y default_to_temp=True
|
|
instruction["scl"] = f"// ERROR: BLKMOV {instr_uid} no pudo generar destino RET_VAL"
|
|
instruction["type"] = instr_type_original + "_error"
|
|
return True
|
|
|
|
# DSTBLK (Obtener nombre RAW como antes, si se necesita)
|
|
raw_dstblk_name = None
|
|
dstblk_output_list = instruction.get("outputs", {}).get("DSTBLK", [])
|
|
if dstblk_output_list and isinstance(dstblk_output_list, list) and len(dstblk_output_list) == 1:
|
|
dest_access = dstblk_output_list[0]
|
|
if dest_access.get("type") == "variable":
|
|
raw_dstblk_name = dest_access.get("name")
|
|
# Manejar error si no se encuentra DSTBLK
|
|
if raw_dstblk_name is None:
|
|
print(f"Error: No se encontró un destino único y válido para DSTBLK en BLKMOV {instr_uid}.")
|
|
instruction["scl"] = f"// ERROR: BLKMOV {instr_uid} sin destino DSTBLK válido."
|
|
instruction["type"] = instr_type_original + "_error"
|
|
return True
|
|
|
|
# --- Formateo especial (mantener nombres raw si es necesario para BLKMOV) ---
|
|
# Estos nombres van directo al string SCL, no necesitan pasar por SymPy
|
|
srcblk_final_str = raw_srcblk_name # Asumiendo que ya viene con comillas si las necesita
|
|
dstblk_final_str = raw_dstblk_name # Asumiendo que ya viene con comillas si las necesita
|
|
|
|
# --- Generar SCL Core (Usando la sintaxis no estándar BLKMOV) ---
|
|
scl_core = (
|
|
f"{retval_target_scl} := BLKMOV(SRCBLK := {srcblk_final_str}, "
|
|
f"DSTBLK => {dstblk_final_str}); " # Usar => para Out/InOut
|
|
f"// ADVERTENCIA: BLKMOV usado directamente, probablemente no compile!"
|
|
)
|
|
|
|
# --- Aplicar Condición EN (Simplificando EN) ---
|
|
scl_final = ""
|
|
if sympy_en_expr != sympy.true:
|
|
try:
|
|
#simplified_en_expr = sympy.simplify_logic(sympy_en_expr, force=True)
|
|
simplified_en_expr = sympy.logic.boolalg.to_dnf(sympy_en_expr, simplify=True)
|
|
except Exception as e:
|
|
print(f"Error simplifying EN for {instr_type_original} {instr_uid}: {e}")
|
|
simplified_en_expr = sympy_en_expr # Fallback
|
|
en_condition_scl = sympy_expr_to_scl(simplified_en_expr, symbol_manager)
|
|
|
|
# Evitar IF TRUE/FALSE THEN...
|
|
if en_condition_scl == "TRUE":
|
|
scl_final = scl_core
|
|
elif en_condition_scl == "FALSE":
|
|
scl_final = f"// {instr_type_original} {instr_uid} condition simplified to FALSE."
|
|
else:
|
|
indented_core = "\n".join([f" {line}" for line in scl_core.splitlines()])
|
|
scl_final = f"IF {en_condition_scl} THEN\n{indented_core}\nEND_IF;"
|
|
else:
|
|
scl_final = scl_core
|
|
|
|
# --- Actualizar Instrucción y Mapa SymPy ---
|
|
instruction["scl"] = scl_final # SCL final generado
|
|
instruction["type"] = instr_type_original + SCL_SUFFIX
|
|
|
|
# Propagar ENO (expresión SymPy)
|
|
map_key_eno = (network_id, instr_uid, "eno")
|
|
sympy_map[map_key_eno] = sympy_en_expr
|
|
|
|
# Propagar el valor de retorno (nombre SCL string del destino de RET_VAL)
|
|
map_key_ret_val = (network_id, instr_uid, "RET_VAL")
|
|
sympy_map[map_key_ret_val] = retval_target_scl
|
|
|
|
return True
|
|
|
|
# --- Processor Information Function ---
|
|
def get_processor_info():
|
|
"""Devuelve la información para el procesador BLKMOV."""
|
|
# Asegurarse que el type_name coincida con el JSON ('blkmov' parece probable)
|
|
return {'type_name': 'blkmov', 'processor_func': process_blkmov, 'priority': 6} |