60 lines
2.7 KiB
Python
60 lines
2.7 KiB
Python
# processors/process_contact.py
|
|
import sympy
|
|
from .processor_utils import get_sympy_representation, format_variable_name # Use new util
|
|
from .symbol_manager import SymbolManager, extract_plc_variable_name # Need symbol manager access
|
|
|
|
# Define SCL_SUFFIX or import if needed globally
|
|
SCL_SUFFIX = "_sympy_processed" # Indicate processing type
|
|
|
|
def process_contact(instruction, network_id, sympy_map, symbol_manager, data): # Pass symbol_manager
|
|
"""Genera la expresión SymPy para Contact (normal o negado)."""
|
|
instr_uid = instruction["instruction_uid"]
|
|
instr_type_original = instruction.get("type", "Contact")
|
|
|
|
# Check if already processed with the new method
|
|
if instr_type_original.endswith(SCL_SUFFIX) or "_error" in instr_type_original:
|
|
return False
|
|
|
|
is_negated = instruction.get("negated_pins", {}).get("operand", False)
|
|
|
|
# Get incoming SymPy expression (RLO)
|
|
in_input = instruction["inputs"].get("in")
|
|
sympy_expr_in = get_sympy_representation(in_input, network_id, sympy_map, symbol_manager)
|
|
|
|
# Get operand SymPy Symbol
|
|
operand_info = instruction["inputs"].get("operand")
|
|
operand_plc_name = extract_plc_variable_name(operand_info)
|
|
sympy_symbol_operand = symbol_manager.get_symbol(operand_plc_name) if operand_plc_name else None
|
|
|
|
# Check dependencies
|
|
if sympy_expr_in is None or sympy_symbol_operand is None:
|
|
# print(f"DEBUG Contact {instr_uid}: Dependency not ready (In: {sympy_expr_in is not None}, Op: {sympy_symbol_operand is not None})")
|
|
return False # Dependencies not ready
|
|
|
|
# Apply negation using SymPy
|
|
current_term = sympy.Not(sympy_symbol_operand) if is_negated else sympy_symbol_operand
|
|
|
|
# Combine with previous RLO using SymPy
|
|
# Simplify common cases: TRUE AND X -> X
|
|
if sympy_expr_in == sympy.true:
|
|
sympy_expr_out = current_term
|
|
else:
|
|
# Could add FALSE AND X -> FALSE optimization here too
|
|
sympy_expr_out = sympy.And(sympy_expr_in, current_term)
|
|
|
|
# Store the resulting SymPy expression object in the map
|
|
map_key_out = (network_id, instr_uid, "out")
|
|
sympy_map[map_key_out] = sympy_expr_out
|
|
|
|
# Mark instruction as processed (SCL field is now less relevant here)
|
|
instruction["scl"] = f"// SymPy Contact: {sympy_expr_out}" # Optional debug comment
|
|
instruction["type"] = instr_type_original + SCL_SUFFIX # Use the new suffix
|
|
# Contact doesn't usually have ENO, it modifies the RLO ('out')
|
|
|
|
return True
|
|
|
|
# --- Processor Information Function ---
|
|
def get_processor_info():
|
|
"""Devuelve la información para el procesador Contact."""
|
|
# Ensure 'data' argument is added if needed by the processor function signature change
|
|
return {'type_name': 'contact', 'processor_func': process_contact, 'priority': 1} |