ParamManagerScripts/backend/script_groups/XML Parser to SCL/processors/process_math.py

139 lines
5.0 KiB
Python

# processors/process_math.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
SCL_SUFFIX = "_sympy_processed"
def process_math(
instruction, network_id, sympy_map, symbol_manager: SymbolManager, data
):
"""
Genera SCL para operaciones matemáticas (SUB, MUL, DIV, CEIL), simplificando EN.
"""
instr_uid = instruction["instruction_uid"]
instr_type_original = instruction.get("type", "") # SUB, MUL, DIV, CEIL
if instr_type_original.endswith(SCL_SUFFIX) or "_error" in instr_type_original:
return False
# Mapa de tipos a operadores SCL string
op_map = {"SUB": "-", "MUL": "*", "DIV": "/", "CEIL": "CEIL"}
scl_operator = op_map.get(instr_type_original.upper())
if not scl_operator:
instruction["scl"] = (
f"// ERROR: Operación matemática no soportada: {instr_type_original}"
)
instruction["type"] = instr_type_original + "_error"
return True
# Obtener EN (SymPy), IN1, IN2 (SymPy o Constante/String)
en_input = instruction["inputs"].get("en")
in1_info = instruction["inputs"].get("in1")
in2_info = instruction["inputs"].get("in2")
in_info = instruction["inputs"].get(
"in"
) # Para funciones de un solo operando como CEIL
sympy_en_expr = (
get_sympy_representation(en_input, network_id, sympy_map, symbol_manager)
if en_input
else sympy.true
)
# Para CEIL solo necesitamos un operando
if instr_type_original.upper() == "CEIL":
op1_sympy_or_const = get_sympy_representation(
in_info, network_id, sympy_map, symbol_manager
)
op2_sympy_or_const = None
else:
op1_sympy_or_const = get_sympy_representation(
in1_info, network_id, sympy_map, symbol_manager
)
op2_sympy_or_const = get_sympy_representation(
in2_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 op1_sympy_or_const is None or target_scl_name is None:
return False
if instr_type_original.upper() != "CEIL" and op2_sympy_or_const is None:
return False
# Convertir operandos SymPy/Constante a SCL strings
op1_scl = sympy_expr_to_scl(op1_sympy_or_const, symbol_manager)
# Generar SCL Core
if instr_type_original.upper() == "CEIL":
scl_core = f"{target_scl_name} := CEIL({op1_scl});"
else:
op2_scl = sympy_expr_to_scl(op2_sympy_or_const, symbol_manager)
# Añadir paréntesis si contienen operadores (más seguro)
# La función sympy_expr_to_scl debería idealmente manejar esto, pero doble chequeo simple:
op1_scl_formatted = (
f"({op1_scl})" if re.search(r"[+\-*/ ]", op1_scl) else op1_scl
)
op2_scl_formatted = (
f"({op2_scl})" if re.search(r"[+\-*/ ]", op2_scl) else op2_scl
)
scl_core = f"{target_scl_name} := {op1_scl_formatted} {scl_operator} {op2_scl_formatted};"
# 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)
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 (nombre SCL del destino) y ENO (expresión SymPy)
map_key_out = (network_id, instr_uid, "out")
sympy_map[map_key_out] = target_scl_name # Guardar nombre del destino
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 info para SUB, MUL, DIV, CEIL."""
return [
{"type_name": "sub", "processor_func": process_math, "priority": 4},
{"type_name": "mul", "processor_func": process_math, "priority": 4},
{"type_name": "div", "processor_func": process_math, "priority": 4},
{"type_name": "ceil", "processor_func": process_math, "priority": 4},
]