using Enigmos.Boards; using Enigmos.Exceptions; using Enigmos.Modules.ControllingModules; using Enigmos.Modules.InterlayerModules; using Enigmos.Modules.Other; using Enigmos.Ports.DataPorts; using Nocturnis.DataStructures; using Nocturnis.DataStructures.DataPortGroups; using Nocturnis.Enigmos.Modules; using Nocturnis.Enigmos.Modules.InterlayerModules; using Nocturnis.Enigmos.Ports; using Nocturnis.Enigmos.Ports.DataPorts; using Nocturnis.Enigmos.Ports.DataPorts.Directions; using Nocturnis.GlobalManagement.Constants; using Nocturnis.GlobalManagement.Providers; namespace Enigmos.Modules.ProgrammableModules; public partial class FilterModule : ProgrammableModule, IPolymorphismModule, IFilterModule { private IDataInPort? Input { get; set; } private IInterlayerDataInModule[] ExplicitInputs { get; set; } = Array.Empty(); private FilterOutputModule? Output { get; set; } private IterativeOutputModule? IterativeOutput { get; set; } private IndicateInputModule? Indicate { get; set; } private IDataPortGroup[] InterLayerGroups { get; set; } = Array.Empty(); private IDataPortGroup? ArrayGroup { get; set; } private bool FilterEnded { get; set; } private IData[] CachedInputArray { get; set; } = Array.Empty(); public IData[] CachedResult { get; set; } = Array.Empty(); private List CachedListResult { get; set; } = new(); //private List CachedResult { get; set; } = new(); private int CachedIndex { get; set; } public new FilterModuleBoard UnderlyingBoard { get => (base.UnderlyingBoard as FilterModuleBoard)!; set => base.UnderlyingBoard = value; } public override IEnumerable Ports => new[] { Input! }; public override IBaseModule[] SubModules() => ExplicitInputs.Union(new[] { Output! }).ToArray(); public override IEnumerable ExplicitPorts => new IBasePort[] { Output!.Output! } .Union(ExplicitInputs.Select(c => c.DataIn))! .ToArray(); public override IEnumerable ImplicitPorts => new IBasePort[] { IterativeOutput!.Output!, Indicate!.Input! } .Union(UnderlyingBoard.Outputs.Select(c => c.DataOut)) .ToArray()!; public HashSet ConfigurablePortGroups { get; set; } = new(); public void Inference() => IterativeOutput!.Output! .SetDataType(GlobalProvider.DataStructureProvider!.ArrayToElement(ArrayGroup!.SelectedType)); public void Filter() { if (FilterEnded) { CachedInputArray = Input!.GetData.Get!.Array; CachedIndex = 0; CachedListResult = new List(); } while (CachedIndex < CachedInputArray.Length) { SoftReset(); IterativeOutput!.Output!.OutData.UpdateCalculation(cache => (CachedInputArray[CachedIndex].Data, IterativeOutput.Output.DataType)!); IterativeOutput.Output.OutData.Expire(); if(Indicate!.Input!.GetData.Get!.Bit) CachedListResult.Add(CachedInputArray[CachedIndex]); CachedIndex += 1; } CachedResult = CachedListResult.ToArray(); FilterEnded = true; } private void SoftReset() => UnderlyingBoard.Reset(); public override void Init() { base.Init(); UnderlyingBoard = GlobalProvider.SceneProvider! .AssetMapper() .Instantiate(); UnderlyingBoard.Init(); ExplicitInputs = new InterlayerDataInModule[3]; InterLayerGroups = new IDataPortGroup[3]; IterativeOutput = UnderlyingBoard.IterativeOutput; IterativeOutput.Output.SetDataType(EnigmosConstant.DataPortTypes.Real); Indicate = UnderlyingBoard.Indicate; for (int i = 0; i < 3; i++) { ExplicitInputs[i] = GetModule($"EI{i + 1}"); ExplicitInputs[i].DualModule = UnderlyingBoard.Outputs[i]; UnderlyingBoard.Outputs[i].DualModule = ExplicitInputs[i]; } FilterEnded = true; Input = this.GetPort("Input"); Output = GetModule("Output"); for (int i = 0; i < 3; i++) { InterLayerGroups[i] = GlobalProvider.DataStructureProvider.NewDataPortGroup( this, new IDataPort[] { ExplicitInputs[i].DataIn, UnderlyingBoard.Outputs[i].DataOut }, $"Exterior In -> Interior Out{i}", EnigmosConstant.DataPortTypes.Real, EnigmosConstant.DataPortTypes.AnyType ); ExplicitInputs[i].Board = Board; } ArrayGroup = GlobalProvider.DataStructureProvider!.NewDataPortGroup( this, new IDataPort[] { Input, Output.Output! }, "Array Type", EnigmosConstant.DataPortTypes.RealArray, EnigmosConstant.DataPortTypes.AnyArray ); ConfigurablePortGroups = InterLayerGroups.Union(new[] { ArrayGroup }).ToHashSet(); PostInit(); } }