74 lines
3.1 KiB
Python
74 lines
3.1 KiB
Python
# processors/process_rcoil.py
|
|
# -*- coding: utf-8 -*-
|
|
import sympy
|
|
import traceback
|
|
import re
|
|
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_rcoil(instruction, network_id, sympy_map, symbol_manager: SymbolManager, data ):
|
|
"""Genera SCL para Reset Coil (RCoil), simplificando la condición."""
|
|
instr_uid = instruction["instruction_uid"]
|
|
instr_type_original = instruction.get("type", "RCoil")
|
|
if instr_type_original.endswith(SCL_SUFFIX) or "_error" in instr_type_original:
|
|
return False
|
|
|
|
# Obtener condición de entrada (SymPy expr)
|
|
in_info = instruction["inputs"].get("in")
|
|
sympy_expr_in = get_sympy_representation(in_info, network_id, sympy_map, symbol_manager)
|
|
|
|
# Obtener operando (nombre SCL del destino)
|
|
target_scl_name = get_target_scl_name(instruction, "operand", network_id, default_to_temp=False) # RCoil necesita destino explícito
|
|
|
|
# Verificar dependencias
|
|
if sympy_expr_in is None: return False
|
|
if target_scl_name is None:
|
|
print(f"Error: RCoil {instr_uid} operando no es variable o falta info.")
|
|
instruction["scl"] = f"// ERROR: RCoil {instr_uid} operando no es variable."
|
|
instruction["type"] = instr_type_original + "_error"
|
|
return True
|
|
|
|
# No hacer nada si la condición es FALSE constante
|
|
if sympy_expr_in == sympy.false:
|
|
instruction["scl"] = f"// RCoil {instr_uid} con condición FALSE constante, optimizado."
|
|
instruction["type"] = instr_type_original + SCL_SUFFIX
|
|
return True
|
|
|
|
# Generar SCL Core (Reset)
|
|
scl_core = f"{target_scl_name} := FALSE;"
|
|
|
|
# Aplicar Condición IF si no es TRUE constante
|
|
scl_final = ""
|
|
if sympy_expr_in != sympy.true:
|
|
# Simplificar la condición ANTES de convertirla a SCL
|
|
try:
|
|
#simplified_expr = sympy.simplify_logic(sympy_expr_in, force=True)
|
|
simplified_expr = sympy.logic.boolalg.to_dnf(sympy_expr_in, simplify=True)
|
|
except Exception as e:
|
|
print(f"Error simplifying condition for RCoil {instr_uid}: {e}")
|
|
simplified_expr = sympy_expr_in # Fallback
|
|
condition_scl = sympy_expr_to_scl(simplified_expr, symbol_manager)
|
|
|
|
# Evitar IF TRUE THEN...
|
|
if condition_scl == "TRUE":
|
|
scl_final = scl_core
|
|
else:
|
|
indented_core = "\n".join([f" {line}" for line in scl_core.splitlines()])
|
|
scl_final = f"IF {condition_scl} THEN\n{indented_core}\nEND_IF;"
|
|
else:
|
|
# Condición es TRUE constante
|
|
scl_final = scl_core
|
|
|
|
# Actualizar instrucción
|
|
instruction["scl"] = scl_final # SCL final generado
|
|
instruction["type"] = instr_type_original + SCL_SUFFIX
|
|
# RCoil no tiene salida lógica para propagar en sympy_map
|
|
|
|
return True
|
|
|
|
# --- Processor Information Function ---
|
|
def get_processor_info():
|
|
"""Devuelve la información para la bobina Reset (RCoil)."""
|
|
return {'type_name': 'rcoil', 'processor_func': process_rcoil, 'priority': 3} |