Simatic_XML_Parser_to_SCL/processors/process_o.py

81 lines
3.1 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_o(instruction, network_id, scl_map, access_map, data):
instr_uid = instruction["instruction_uid"]
instr_type = instruction["type"]
if instr_type.endswith(SCL_SUFFIX) or "_error" in instr_type:
return False
# Buscar todas las entradas 'in', 'in1', 'in2', ...
input_pins = sorted([pin for pin in instruction["inputs"] if pin.startswith("in")])
if not input_pins:
print(f"Error: O {instr_uid} sin pines de entrada (inX).")
instruction["scl"] = f"// ERROR: O {instr_uid} sin pines inX"
instruction["type"] += "_error"
return True # Procesado con error
scl_parts = []
all_resolved = True
for pin in input_pins:
in_scl = get_scl_representation(
instruction["inputs"][pin], network_id, scl_map, access_map
)
if in_scl is None:
all_resolved = False
# print(f"DEBUG: O {instr_uid} esperando pin {pin}")
break # Salir del bucle for si una entrada no está lista
# Formatear término (añadir paréntesis si es necesario)
term = in_scl
if (" " in term or "AND" in term) and not (
term.startswith("(") and term.endswith(")")
):
term = f"({term})"
scl_parts.append(term)
if not all_resolved:
return False # Esperar a que todas las entradas estén resueltas
# Construir la expresión OR
result_scl = "FALSE" # Valor por defecto si no hay entradas válidas (raro)
if scl_parts:
result_scl = " OR ".join(scl_parts)
# Simplificar si solo hay un término
if len(scl_parts) == 1:
result_scl = scl_parts[0]
# Quitar paréntesis redundantes si solo hay un término y está entre paréntesis
if result_scl.startswith("(") and result_scl.endswith(")"):
# Comprobar si los paréntesis son necesarios (contienen operadores de menor precedencia)
# Simplificación: quitar siempre si solo hay un término. Podría ser incorrecto en casos complejos.
# result_scl = result_scl[1:-1] # Comentado por seguridad
pass
# Actualizar mapa SCL y la instrucción
map_key_out = (network_id, instr_uid, "out")
scl_map[map_key_out] = result_scl
instruction["scl"] = (
f"// Logic O {instr_uid}: {result_scl}" # Comentario informativo
)
instruction["type"] = instr_type + SCL_SUFFIX
# La instrucción 'O' no tiene ENO propio, propaga el resultado por 'out'
return True
# --- Function code ends ---
# --- Processor Information Function ---
def get_processor_info():
"""Devuelve la información para la operación lógica O (OR)."""
return {'type_name': 'o', 'processor_func': process_o, 'priority': 1}