Files

154 lines
6.0 KiB
C#

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