90 lines
4.2 KiB
Python
90 lines
4.2 KiB
Python
# processors/process_convert.py
|
|
# -*- coding: utf-8 -*-
|
|
import sympy
|
|
import traceback
|
|
from .processor_utils import get_sympy_representation, sympy_expr_to_scl, get_target_scl_name, format_variable_name
|
|
from .symbol_manager import SymbolManager
|
|
|
|
SCL_SUFFIX = "_sympy_processed"
|
|
|
|
def process_convert(instruction, network_id, sympy_map, symbol_manager: SymbolManager, data):
|
|
"""Genera SCL para Convert, tratando la conversión como una asignación."""
|
|
instr_uid = instruction["instruction_uid"]
|
|
instr_type_original = instruction.get("type", "Convert")
|
|
if instr_type_original.endswith(SCL_SUFFIX) or "_error" in instr_type_original:
|
|
return False
|
|
|
|
# Obtener EN y IN
|
|
en_input = instruction["inputs"].get("en")
|
|
sympy_en_expr = get_sympy_representation(en_input, network_id, sympy_map, symbol_manager) if en_input else sympy.true
|
|
in_info = instruction["inputs"].get("in")
|
|
sympy_or_const_in = get_sympy_representation(in_info, network_id, sympy_map, symbol_manager)
|
|
|
|
# Obtener destino SCL
|
|
target_scl_name = get_target_scl_name(instruction, "out", network_id, default_to_temp=True)
|
|
|
|
# Verificar dependencias
|
|
if sympy_en_expr is None or sympy_or_const_in is None or target_scl_name is None:
|
|
return False
|
|
|
|
# Convertir la entrada (SymPy o Constante) a SCL
|
|
# La simplificación aquí no suele aplicar a la conversión en sí,
|
|
# pero sí podría aplicar a la condición EN.
|
|
input_scl = sympy_expr_to_scl(sympy_or_const_in, symbol_manager)
|
|
|
|
# Determinar el tipo de destino (esto sigue siendo un desafío sin info completa)
|
|
# Usaremos funciones de conversión SCL explícitas si podemos inferirlas.
|
|
target_type_hint = instruction.get("template_values", {}).get("destType", "").upper() # Ejemplo
|
|
source_type_hint = "" # Necesitaríamos info del tipo de origen
|
|
conversion_func_name = None
|
|
|
|
# Heurística MUY básica (necesita mejorar con info de tipos real)
|
|
if target_type_hint and source_type_hint and target_type_hint != source_type_hint:
|
|
conversion_func_name = f"{source_type_hint}_TO_{target_type_hint}"
|
|
|
|
# Generar SCL Core
|
|
if conversion_func_name:
|
|
# Usar función explícita si la inferimos
|
|
scl_core = f"{target_scl_name} := {conversion_func_name}({input_scl});"
|
|
else:
|
|
# Asignación directa (MOVE implícito) si no hay conversión clara
|
|
# ADVERTENCIA: Esto puede causar errores de tipo en el PLC si los tipos no coinciden.
|
|
scl_core = f"{target_scl_name} := {input_scl};"
|
|
if target_type_hint: # Añadir comentario si al menos conocemos el destino
|
|
scl_core += f" // TODO: Verify implicit conversion to {target_type_hint}"
|
|
|
|
|
|
# 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 Convert {instr_uid}: {e}")
|
|
simplified_en_expr = sympy_en_expr # Fallback
|
|
en_condition_scl = sympy_expr_to_scl(simplified_en_expr, symbol_manager)
|
|
|
|
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
|
|
instruction["scl"] = scl_final # SCL final generado
|
|
instruction["type"] = instr_type_original + SCL_SUFFIX
|
|
|
|
# Propagar valor de salida (el contenido del destino) y ENO
|
|
map_key_out = (network_id, instr_uid, "out")
|
|
# Guardar el *nombre* SCL del destino en el mapa, ya que contiene el valor
|
|
# O podríamos crear un símbolo SymPy para ello si fuera necesario aguas abajo? Por ahora, string.
|
|
sympy_map[map_key_out] = target_scl_name
|
|
map_key_eno = (network_id, instr_uid, "eno")
|
|
sympy_map[map_key_eno] = sympy_en_expr # Guardar la expresión SymPy para ENO
|
|
|
|
return True
|
|
|
|
# --- Processor Information Function ---
|
|
def get_processor_info():
|
|
"""Devuelve la información para el procesador Convert."""
|
|
return {'type_name': 'convert', 'processor_func': process_convert, 'priority': 4} |