namespace MyBWRSimulator.Core.Components.Abstract
{
using MyBWRSimulator.Core.Interfaces;
using MyBWRSimulator.Core.Ports;
using System.Collections.Generic;
using System.Linq; // Needed for .ToList()
///
/// Abstract base class for all simulated physical components.
/// Implements ISimulatable, IFluidConnectable, and IThermalConnectable.
///
public abstract class Component : ISimulatable, IFluidConnectable, IThermalConnectable
{
///
/// Unique identifier for the component.
///
public string ID { get; protected set; }
///
/// Human-readable name of the component.
///
public string Name { get; protected set; }
///
/// Detailed description of the component.
///
public string Description { get; protected set; }
///
/// An integer value indicating the order in which components should be updated.
/// Lower numbers mean higher priority (updated first).
///
public int UpdatePriority { get; protected set; }
///
/// Indicates if the component is receiving sufficient Thermal power.
///
public bool IsPowered { get; protected set; }
///
/// Collection of fluid ports belonging to this component, accessible by their ID.
///
protected Dictionary FluidPorts { get; set; } = new Dictionary();
///
/// Collection of Thermal ports belonging to this component, accessible by their ID.
///
protected Dictionary ThermalPorts { get; set; } = new Dictionary();
///
/// Constructor for the Component base class.
///
/// Unique identifier.
/// Human-readable name.
/// Description of the component.
/// Priority for simulation updates.
protected Component(string id, string name, string description, int updatePriority)
{
ID = id;
Name = name;
Description = description;
UpdatePriority = updatePriority;
}
///
/// Initializes the component. Called once when the simulation starts or component is added.
///
public virtual void Initialize()
{
Console.WriteLine($"Initializing {Name} ({ID})...");
// Placeholder for component-specific initialization logic
}
///
/// Shuts down the component. Called once when the simulation ends or component is removed.
///
public virtual void Shutdown()
{
Console.WriteLine($"Shutting down {Name} ({ID})...");
// Placeholder for component-specific cleanup logic
}
///
/// Abstract method to update the component's state over a time step.
/// Must be implemented by concrete component classes.
///
/// The time elapsed.
public abstract void Update(double deltaTime);
///
/// Returns a list of all fluid ports for this component.
///
/// A list of FluidPort objects.
public List GetFluidPorts() => FluidPorts.Values.ToList();
///
/// Gets a fluid port by its unique ID.
///
/// The ID of the fluid port.
/// The FluidPort object if found, otherwise null.
public FluidPort GetFluidPortById(string id)
{
FluidPorts.TryGetValue(id, out FluidPort port);
return port;
}
///
/// Returns a list of all Thermal ports for this component.
///
/// A list of Thermal objects.
public List GetThermalPorts() => ThermalPorts.Values.ToList();
///
/// Gets an Thermal port by its unique ID.
///
/// The ID of the Thermal port.
/// The ThermalPort object if found, otherwise null.
public ThermalPort GetThermalPortById(string id)
{
ThermalPorts.TryGetValue(id, out ThermalPort port);
return port;
}
///
/// Helper method to add a fluid port to this component.
///
/// The FluidPort to add.
/// Thrown if a port with the same ID already exists.
protected void AddFluidPort(FluidPort port)
{
if (FluidPorts.ContainsKey(port.ID))
{
throw new ArgumentException($"A fluid port with ID '{port.ID}' already exists on component '{Name}' ({ID}).");
}
port.ParentComponent = this;
FluidPorts.Add(port.ID, port);
}
///
/// Helper method to add an Thermal port to this component.
///
/// The ThermalPort to add.
/// Thrown if an Thermal port with the same ID already exists.
protected void AddThermalPort(ThermalPort port)
{
if (ThermalPorts.ContainsKey(port.ID))
{
throw new ArgumentException($"An Thermal port with ID '{port.ID}' already exists on component '{Name}' ({ID}).");
}
port.ParentComponent = this;
ThermalPorts.Add(port.ID, port);
}
}
}