diff --git a/DataAccess/DTOs/Converters/ConvertDirection.cs b/DataAccess/DTOs/Converters/ConvertDirection.cs index 230f58c..870b57a 100644 --- a/DataAccess/DTOs/Converters/ConvertDirection.cs +++ b/DataAccess/DTOs/Converters/ConvertDirection.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DataAccess.DTOs +namespace DataAccess.DTOs { public enum ConvertDirection { diff --git a/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataFromDTOConvertStrategy.cs index e85e405..ce0a644 100644 --- a/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataFromDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataFromDTOConvertStrategy.cs @@ -11,21 +11,18 @@ namespace DataAccess.DTOs { public class CurvatureCalculatorInputDataFromDTOConvertStrategy : ConvertStrategy { - private IHasPrimitivesProcessLogic primitivesProcessLogic; - private IHasForceActionsProcessLogic actionsProcessLogic; + private IProcessLogic forcesAndPrimitivesProcessLogic; private IUpdateStrategy updateStrategy; private IConvertStrategy deflectionConvertStrategy; - + private IProcessLogic ForcesAndPrimitivesProcessLogic => forcesAndPrimitivesProcessLogic ??= new HasForcesAndPrimitivesProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; + private IUpdateStrategy UpdateStrategy => updateStrategy ??= new CurvatureCalculatorInputDataUpdateStrategy() { UpdateChildren = false }; + private IConvertStrategy DeflectionConvertStrategy => deflectionConvertStrategy ??= new DeflectionFactorFromDTOConvertStrategy(this); public CurvatureCalculatorInputDataFromDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy) { } - - private IHasPrimitivesProcessLogic PrimitivesProcessLogic => primitivesProcessLogic ??= new HasPrimitivesProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; - private IHasForceActionsProcessLogic ActionsProcessLogic => actionsProcessLogic ??= new HasForceActionsProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; - private IUpdateStrategy UpdateStrategy => updateStrategy ??= new CurvatureCalculatorInputDataUpdateStrategy() { UpdateChildren = false }; - private IConvertStrategy DeflectionConvertStrategy => deflectionConvertStrategy ??= new DeflectionFactorFromDTOConvertStrategy(this); public override CurvatureCalculatorInputData GetNewItem(CurvatureCalculatorInputDataDTO source) { + ChildClass = this; NewItem = new(source.Id); UpdateStrategy.Update(NewItem, source); if (source.DeflectionFactor is not DeflectionFactorDTO deflectionFactorDTO) @@ -33,22 +30,16 @@ namespace DataAccess.DTOs throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(source.DeflectionFactor) + ": deflection factor"); } NewItem.DeflectionFactor = DeflectionConvertStrategy.Convert(deflectionFactorDTO); - ProcessPrimitives(source); - ProcessActions(source); + ProcessForcesAndPrimitives(source); return NewItem; } - private void ProcessPrimitives(IHasPrimitives source) + + private void ProcessForcesAndPrimitives(IHasForcesAndPrimitives source) { - PrimitivesProcessLogic.Source = source; - PrimitivesProcessLogic.Target = NewItem; - PrimitivesProcessLogic.Process(); - } - private void ProcessActions(IHasForceActions source) - { - ActionsProcessLogic.Source = source; - ActionsProcessLogic.Target = NewItem; - ActionsProcessLogic.Process(); + ForcesAndPrimitivesProcessLogic.Source = source; + ForcesAndPrimitivesProcessLogic.Target = NewItem; + ForcesAndPrimitivesProcessLogic.Process(); } } } diff --git a/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataToDTOConvertStrategy.cs index 2918a7c..b7cad6a 100644 --- a/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/Curvatures/CurvatureCalculatorInputDataToDTOConvertStrategy.cs @@ -7,13 +7,11 @@ namespace DataAccess.DTOs { public class CurvatureCalculatorInputDataToDTOConvertStrategy : ConvertStrategy { - private IHasPrimitivesProcessLogic primitivesProcessLogic; - private IHasForceActionsProcessLogic actionsProcessLogic; + private IProcessLogic actionsProcessLogic; private IUpdateStrategy updateStrategy; private IConvertStrategy deflectionConvertStrategy; - private IHasPrimitivesProcessLogic PrimitivesProcessLogic => primitivesProcessLogic ??= new HasPrimitivesProcessLogic(ConvertDirection.ToDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger}; - private IHasForceActionsProcessLogic ActionsProcessLogic => actionsProcessLogic ??= new HasForceActionsProcessLogic(ConvertDirection.ToDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger}; + private IProcessLogic ForceAndPrimitivesLogic => actionsProcessLogic ??= new HasForcesAndPrimitivesProcessLogic(ConvertDirection.ToDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; private IUpdateStrategy UpdateStrategy => updateStrategy ??= new CurvatureCalculatorInputDataUpdateStrategy() { UpdateChildren = false}; private IConvertStrategy DeflectionConvertStrategy => deflectionConvertStrategy ??= new DeflectionFactorToDTOConvertStrategy(this); @@ -23,25 +21,19 @@ namespace DataAccess.DTOs public override CurvatureCalculatorInputDataDTO GetNewItem(ICurvatureCalculatorInputData source) { + ChildClass = this; NewItem = new(source.Id); UpdateStrategy.Update(NewItem, source); NewItem.DeflectionFactor = DeflectionConvertStrategy.Convert(source.DeflectionFactor); - ProcessPrimitives(source); ProcessActions(source); return NewItem; } - private void ProcessPrimitives(IHasPrimitives source) + private void ProcessActions(IHasForcesAndPrimitives source) { - PrimitivesProcessLogic.Source = source; - PrimitivesProcessLogic.Target = NewItem; - PrimitivesProcessLogic.Process(); - } - private void ProcessActions(IHasForceActions source) - { - ActionsProcessLogic.Source = source; - ActionsProcessLogic.Target = NewItem; - ActionsProcessLogic.Process(); + ForceAndPrimitivesLogic.Source = source; + ForceAndPrimitivesLogic.Target = NewItem; + ForceAndPrimitivesLogic.Process(); } } } diff --git a/DataAccess/DTOs/Converters/HasForceActionsFromDTOUpdateStrategy.cs b/DataAccess/DTOs/Converters/HasForceActionsFromDTOUpdateStrategy.cs index 3a540aa..983be7e 100644 --- a/DataAccess/DTOs/Converters/HasForceActionsFromDTOUpdateStrategy.cs +++ b/DataAccess/DTOs/Converters/HasForceActionsFromDTOUpdateStrategy.cs @@ -1,12 +1,6 @@ -using StructureHelper.Models.Materials; -using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Forces; using StructureHelperCommon.Services; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace DataAccess.DTOs { diff --git a/DataAccess/DTOs/Converters/HasForceActionsProcessLogic.cs b/DataAccess/DTOs/Converters/HasForceActionsProcessLogic.cs index e7e0592..4e56ab6 100644 --- a/DataAccess/DTOs/Converters/HasForceActionsProcessLogic.cs +++ b/DataAccess/DTOs/Converters/HasForceActionsProcessLogic.cs @@ -1,12 +1,7 @@ -using StructureHelperCommon.Infrastructures.Interfaces; -using StructureHelperCommon.Models.Forces; +using StructureHelperCommon.Infrastructures.Exceptions; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using StructureHelperCommon.Infrastructures.Exceptions; +using StructureHelperCommon.Models.Forces; namespace DataAccess.DTOs { diff --git a/DataAccess/DTOs/Converters/HasForcesAndPrimitivesProcessLogic.cs b/DataAccess/DTOs/Converters/HasForcesAndPrimitivesProcessLogic.cs new file mode 100644 index 0000000..55aa606 --- /dev/null +++ b/DataAccess/DTOs/Converters/HasForcesAndPrimitivesProcessLogic.cs @@ -0,0 +1,67 @@ +using DataAccess.DTOs.Converters; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Services; +using StructureHelperLogics.NdmCalculations.Primitives; +using System; +using System.Collections.Generic; +using System.Text; + +namespace DataAccess.DTOs +{ + public class HasForcesAndPrimitivesProcessLogic : IProcessLogic + { + private ConvertDirection convertDirection; + private IProcessLogic forcesLogic; + private IProcessLogic primitivesLogic; + private IProcessLogic ForcesLogic => forcesLogic ??= new HasForceActionsProcessLogic(convertDirection) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger}; + private IProcessLogic PrimitivesLogic => primitivesLogic ??=new HasPrimitivesProcessLogic(convertDirection) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; + public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } + public IHasForcesAndPrimitives Source { get; set; } + public IHasForcesAndPrimitives Target { get; set; } + public IShiftTraceLogger TraceLogger { get; set; } + + public HasForcesAndPrimitivesProcessLogic(ConvertDirection convertDirection) + { + this.convertDirection = convertDirection; + } + + public HasForcesAndPrimitivesProcessLogic( + ConvertDirection convertDirection, + IProcessLogic forcesLogic, + IProcessLogic primitivesLogic) + { + this.convertDirection = convertDirection; + this.forcesLogic = forcesLogic; + this.primitivesLogic = primitivesLogic; + } + + public void Process() + { + Check(); + ProcessForces(); + ProcessPrimitives(); + } + + private void Check() + { + CheckObject.ThrowIfNull(ReferenceDictionary, ": reference dictionary"); + CheckObject.ThrowIfNull(Source, ": source object"); + CheckObject.ThrowIfNull(Target, ": target object"); + } + + private void ProcessPrimitives() + { + PrimitivesLogic.Source = Source; + PrimitivesLogic.Target = Target; + PrimitivesLogic.Process(); + } + + private void ProcessForces() + { + ForcesLogic.Source = Source; + ForcesLogic.Target = Target; + ForcesLogic.Process(); + } + } +} diff --git a/DataAccess/DTOs/Converters/IHasForceActionsProcessLogic.cs b/DataAccess/DTOs/Converters/IHasForceActionsProcessLogic.cs index bbff949..77bff70 100644 --- a/DataAccess/DTOs/Converters/IHasForceActionsProcessLogic.cs +++ b/DataAccess/DTOs/Converters/IHasForceActionsProcessLogic.cs @@ -1,4 +1,5 @@ -using StructureHelperCommon.Infrastructures.Interfaces; +using DataAccess.DTOs.Converters; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models; namespace DataAccess.DTOs @@ -6,12 +7,7 @@ namespace DataAccess.DTOs /// /// Logic for antities which have force actions /// - public interface IHasForceActionsProcessLogic + public interface IHasForceActionsProcessLogic : IProcessLogic { - Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } - IHasForceActions Source { get; set; } - IHasForceActions Target { get; set; } - IShiftTraceLogger TraceLogger { get; set; } - void Process(); } } \ No newline at end of file diff --git a/DataAccess/DTOs/Converters/IHasPrimitivesProcessLogic.cs b/DataAccess/DTOs/Converters/IHasPrimitivesProcessLogic.cs index 5c8c3da..f330f58 100644 --- a/DataAccess/DTOs/Converters/IHasPrimitivesProcessLogic.cs +++ b/DataAccess/DTOs/Converters/IHasPrimitivesProcessLogic.cs @@ -4,13 +4,7 @@ using StructureHelperLogics.NdmCalculations.Primitives; namespace DataAccess.DTOs.Converters { - public interface IHasPrimitivesProcessLogic + public interface IHasPrimitivesProcessLogic : IProcessLogic { - Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } - IHasPrimitives Source { get; set; } - IHasPrimitives Target { get; set; } - IShiftTraceLogger TraceLogger { get; set; } - - void Process(); } } \ No newline at end of file diff --git a/DataAccess/DTOs/Converters/IProcessLogic.cs b/DataAccess/DTOs/Converters/IProcessLogic.cs new file mode 100644 index 0000000..952c6d6 --- /dev/null +++ b/DataAccess/DTOs/Converters/IProcessLogic.cs @@ -0,0 +1,17 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace DataAccess.DTOs +{ + public interface IProcessLogic + { + Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } + T Source { get; set; } + T Target { get; set; } + IShiftTraceLogger TraceLogger { get; set; } + void Process(); + } +} diff --git a/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryFromDTOConvertStrategy.cs index 61450ab..7b55b23 100644 --- a/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryFromDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryFromDTOConvertStrategy.cs @@ -3,17 +3,9 @@ using StructureHelper.Models.Materials; using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models; using StructureHelperCommon.Models.Calculators; -using StructureHelperCommon.Models.Forces; -using StructureHelperCommon.Models.Loggers; using StructureHelperLogics.Models.CrossSections; using StructureHelperLogics.Models.Materials; -using StructureHelperLogics.NdmCalculations.Cracking; using StructureHelperLogics.NdmCalculations.Primitives; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace DataAccess.DTOs { @@ -22,9 +14,12 @@ namespace DataAccess.DTOs private const string convertStarted = " converting is started"; private const string convertFinished = " converting has been finished successfully"; private CrossSectionRepository newRepository; + private ICheckEntityLogic checkEntityLogic; + private ICheckEntityLogic CheckEntityLogic => checkEntityLogic ??= new CrossSectionRepositoryCheckLogic() {Entity = oldRepository}; - private IHasPrimitivesProcessLogic primitivesProcessLogic = new HasPrimitivesProcessLogic(ConvertDirection.FromDTO); - private IHasForceActionsProcessLogic actionsProcessLogic = new HasForceActionsProcessLogic(ConvertDirection.FromDTO); + private IProcessLogic forcesAndPrimitivesLogic; + private IProcessLogic ForcesAndPrimitivesLogic => forcesAndPrimitivesLogic ??= new HasForcesAndPrimitivesProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger}; + private ICrossSectionRepository oldRepository; public CrossSectionRepositoryFromDTOConvertStrategy(Dictionary<(Guid id, Type type), ISaveable> referenceDictionary, IShiftTraceLogger traceLogger) : base(referenceDictionary, traceLogger) { @@ -32,11 +27,17 @@ namespace DataAccess.DTOs public override CrossSectionRepository GetNewItem(ICrossSectionRepository source) { + oldRepository = source; + if (CheckEntityLogic.Check() == false) + { + TraceLogger.AddMessage(CheckEntityLogic.CheckResult, TraceLogStatuses.Error); + TraceLogger.AddMessage("All calculators will be removed", TraceLogStatuses.Error); + oldRepository.Calculators.Clear(); + } TraceLogger?.AddMessage("Cross-Section repository" + convertStarted); newRepository = new(source.Id); ProcessMaterials(source); - ProcessActions(source); - ProcessPrimitives(source); + ProcessForcesAndPrimitives(source); ProcessCalculators(source); TraceLogger?.AddMessage("Cross-Section repository" + convertFinished); return newRepository; @@ -56,22 +57,12 @@ namespace DataAccess.DTOs TraceLogger?.AddMessage("Calculators" + convertFinished); } - private void ProcessPrimitives(IHasPrimitives source) - { - primitivesProcessLogic.Source = source; - primitivesProcessLogic.Target = newRepository; - primitivesProcessLogic.ReferenceDictionary = ReferenceDictionary; - primitivesProcessLogic.TraceLogger = TraceLogger; - primitivesProcessLogic.Process(); - } - private void ProcessActions(IHasForceActions source) + private void ProcessForcesAndPrimitives(IHasForcesAndPrimitives source) { - actionsProcessLogic.Source = source; - actionsProcessLogic.Target = newRepository; - actionsProcessLogic.ReferenceDictionary = ReferenceDictionary; - actionsProcessLogic.TraceLogger = TraceLogger; - actionsProcessLogic.Process(); + ForcesAndPrimitivesLogic.Source = source; + ForcesAndPrimitivesLogic.Target = newRepository; + ForcesAndPrimitivesLogic.Process(); } private void ProcessMaterials(IHasHeadMaterials source) diff --git a/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryToDTOConvertStrategy.cs index de016d0..03f73ab 100644 --- a/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/NdmCrossSections/CrossSectionRepositoryToDTOConvertStrategy.cs @@ -12,6 +12,10 @@ namespace DataAccess.DTOs public class CrossSectionRepositoryToDTOConvertStrategy : IConvertStrategy { private IConvertStrategy materialConvertStrategy; + private ICheckEntityLogic checkEntityLogic; + private ICrossSectionRepository oldRepository; + + private ICheckEntityLogic CheckEntityLogic => checkEntityLogic ??= new CrossSectionRepositoryCheckLogic() { Entity = oldRepository }; public CrossSectionRepositoryToDTOConvertStrategy(IConvertStrategy materialConvertStrategy) @@ -30,6 +34,13 @@ namespace DataAccess.DTOs public CrossSectionRepositoryDTO Convert(ICrossSectionRepository source) { + oldRepository = source; + if (CheckEntityLogic.Check() == false) + { + TraceLogger.AddMessage(CheckEntityLogic.CheckResult, TraceLogStatuses.Error); + TraceLogger.AddMessage("All calculators will be removed", TraceLogStatuses.Error); + oldRepository.Calculators.Clear(); + } Check(); InitializeStrategies(); try diff --git a/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataFromDTOConvertStrategy.cs index f4b2b11..efc7291 100644 --- a/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataFromDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataFromDTOConvertStrategy.cs @@ -11,22 +11,35 @@ namespace DataAccess.DTOs { private IUpdateStrategy updateStrategy; private IConvertStrategy diagramConvertStrategy; - private IHasPrimitivesProcessLogic primitivesProcessLogic; - private IHasForceActionsProcessLogic actionsProcessLogic; + private IProcessLogic forcesAndPrimitivesLogic; private IConvertStrategy accuracyConvertStrategy; + private IUpdateStrategy UpdateStrategy => updateStrategy ??= new ValueDiagramCalculatorInputDataUpdateStrategy() { UpdateChildren = false }; + private IConvertStrategy DiagramConvertStrategy => diagramConvertStrategy ??= new ValueDiagramEntityFromDTOConvertStrategy(this); + private IProcessLogic ForcesAndPrimitivesLogic => forcesAndPrimitivesLogic ??= new HasForcesAndPrimitivesProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; + private IConvertStrategy AccuracyConvertStrategy => accuracyConvertStrategy ??= new AccuracyFromDTOConvertStrategy() { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; public ValueDiagramCalculatorInputDataFromDTOConvertStrategy(IBaseConvertStrategy baseConvertStrategy) : base(baseConvertStrategy) { } + public ValueDiagramCalculatorInputDataFromDTOConvertStrategy( + IUpdateStrategy updateStrategy, + IConvertStrategy diagramConvertStrategy, + IProcessLogic forcesAndPrimitivesLogic, + IConvertStrategy accuracyConvertStrategy) + { + this.updateStrategy = updateStrategy; + this.diagramConvertStrategy = diagramConvertStrategy; + this.forcesAndPrimitivesLogic = forcesAndPrimitivesLogic; + this.accuracyConvertStrategy = accuracyConvertStrategy; + } + public override ValueDiagramCalculatorInputData GetNewItem(ValueDiagramCalculatorInputDataDTO source) { ChildClass = this; NewItem = new(source.Id); - InitializeStrategies(); - updateStrategy.Update(NewItem, source); - ProcessPrimitives(source); - ProcessActions(source); + UpdateStrategy.Update(NewItem, source); + ProcessForcesAndPrimitives(source); NewItem.Diagrams.Clear(); foreach (var diagram in source.Diagrams) { @@ -34,35 +47,16 @@ namespace DataAccess.DTOs { throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(diagram)); } - NewItem.Diagrams.Add(diagramConvertStrategy.Convert(diagramDTO)); + NewItem.Diagrams.Add(DiagramConvertStrategy.Convert(diagramDTO)); } return NewItem; } - private void ProcessPrimitives(IHasPrimitives source) + private void ProcessForcesAndPrimitives(IHasForcesAndPrimitives source) { - primitivesProcessLogic.Source = source; - primitivesProcessLogic.Target = NewItem; - primitivesProcessLogic.ReferenceDictionary = ReferenceDictionary; - primitivesProcessLogic.TraceLogger = TraceLogger; - primitivesProcessLogic.Process(); - } - private void ProcessActions(IHasForceActions source) - { - actionsProcessLogic.Source = source; - actionsProcessLogic.Target = NewItem; - actionsProcessLogic.ReferenceDictionary = ReferenceDictionary; - actionsProcessLogic.TraceLogger = TraceLogger; - actionsProcessLogic.Process(); - } - - private void InitializeStrategies() - { - updateStrategy ??= new ValueDiagramCalculatorInputDataUpdateStrategy() { UpdateChildren = false }; - diagramConvertStrategy ??= new ValueDiagramEntityFromDTOConvertStrategy(this); - primitivesProcessLogic ??= new HasPrimitivesProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; - actionsProcessLogic ??= new HasForceActionsProcessLogic(ConvertDirection.FromDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; - accuracyConvertStrategy ??= new AccuracyFromDTOConvertStrategy() { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; + ForcesAndPrimitivesLogic.Source = source; + ForcesAndPrimitivesLogic.Target = NewItem; + ForcesAndPrimitivesLogic.Process(); } } } diff --git a/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataToDTOConvertStrategy.cs index 861600a..9d9bb9a 100644 --- a/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/ValueDiagrams/ValueDiagramCalculatorInputDataToDTOConvertStrategy.cs @@ -9,8 +9,10 @@ namespace DataAccess.DTOs { private IUpdateStrategy updateStrategy; private IConvertStrategy diagramConvertStrategy; - private IHasPrimitivesProcessLogic primitivesProcessLogic; - private IHasForceActionsProcessLogic actionsProcessLogic; + private IConvertStrategy DiagramConvertStrategy => diagramConvertStrategy ??= new ValueDiagramEntityToDTOConvertStrategy(this); + private IProcessLogic actionsProcessLogic; + private IUpdateStrategy UpdateStrategy => updateStrategy ??= new ValueDiagramCalculatorInputDataUpdateStrategy() { UpdateChildren = false }; + private IProcessLogic ActionsProcessLogic => actionsProcessLogic ??= new HasForcesAndPrimitivesProcessLogic(ConvertDirection.ToDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; public ValueDiagramCalculatorInputDataToDTOConvertStrategy() : base() { @@ -24,41 +26,21 @@ namespace DataAccess.DTOs { ChildClass = this; NewItem = new(source.Id); - InitializeStrategies(); - updateStrategy.Update(NewItem, source); - ProcessPrimitives(source); + UpdateStrategy.Update(NewItem, source); ProcessActions(source); NewItem.Diagrams.Clear(); foreach (var diagram in source.Diagrams) { - NewItem.Diagrams.Add(diagramConvertStrategy.Convert(diagram)); + NewItem.Diagrams.Add(DiagramConvertStrategy.Convert(diagram)); } return NewItem; } - private void ProcessPrimitives(IHasPrimitives source) + private void ProcessActions(IHasForcesAndPrimitives source) { - primitivesProcessLogic.Source = source; - primitivesProcessLogic.Target = NewItem; - primitivesProcessLogic.ReferenceDictionary = ReferenceDictionary; - primitivesProcessLogic.TraceLogger = TraceLogger; - primitivesProcessLogic.Process(); - } - private void ProcessActions(IHasForceActions source) - { - actionsProcessLogic.Source = source; - actionsProcessLogic.Target = NewItem; - actionsProcessLogic.ReferenceDictionary = ReferenceDictionary; - actionsProcessLogic.TraceLogger = TraceLogger; - actionsProcessLogic.Process(); - } - - private void InitializeStrategies() - { - updateStrategy ??= new ValueDiagramCalculatorInputDataUpdateStrategy() { UpdateChildren = false}; - diagramConvertStrategy ??= new ValueDiagramEntityToDTOConvertStrategy(this); - primitivesProcessLogic ??= new HasPrimitivesProcessLogic(ConvertDirection.ToDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; - actionsProcessLogic ??= new HasForceActionsProcessLogic(ConvertDirection.ToDTO) { ReferenceDictionary = ReferenceDictionary, TraceLogger = TraceLogger }; + ActionsProcessLogic.Source = source; + ActionsProcessLogic.Target = NewItem; + ActionsProcessLogic.Process(); } } } diff --git a/StructureHelper/Services/Reports/Services/IMaterialService.cs b/StructureHelper/Services/Reports/Services/IMaterialService.cs new file mode 100644 index 0000000..1896570 --- /dev/null +++ b/StructureHelper/Services/Reports/Services/IMaterialService.cs @@ -0,0 +1,16 @@ +using FieldVisualizer.Entities.ColorMaps; +using FieldVisualizer.Entities.Values; +using HelixToolkit.Wpf.SharpDX; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureHelper.Services.Reports.Services +{ + public interface IMaterialService + { + PhongMaterial CreateValueMaterial(double value, IValueRange range, IColorMap colorMap); + PBRMaterial CreateTransparentPlaneMaterial(float opacity); + } + +} diff --git a/StructureHelper/Services/Reports/Services/MaterialService.cs b/StructureHelper/Services/Reports/Services/MaterialService.cs new file mode 100644 index 0000000..44c1d8f --- /dev/null +++ b/StructureHelper/Services/Reports/Services/MaterialService.cs @@ -0,0 +1,35 @@ +using FieldVisualizer.Entities.ColorMaps; +using FieldVisualizer.Entities.Values; +using FieldVisualizer.Services.ColorServices; +using HelixToolkit.Maths; +using HelixToolkit.Wpf.SharpDX; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureHelper.Services.Reports.Services +{ + public class MaterialService : IMaterialService + { + public PhongMaterial CreateValueMaterial(double value, IValueRange range, IColorMap colorMap) + { + var c = ColorOperations.GetColorByValue(range, colorMap, value); + return new PhongMaterial + { + DiffuseColor = new Color4(c.R / 255f, c.G / 255f, c.B / 255f, c.A / 255f), + SpecularShininess = 50f + }; + } + + public PBRMaterial CreateTransparentPlaneMaterial(float opacity) + { + return new PBRMaterial + { + AlbedoColor = new Color4(0.5f, 0.5f, 0.5f, opacity), + RoughnessFactor = 0.8f, + MetallicFactor = 0.0f + }; + } + } + +} diff --git a/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/GetModels3dByValuePrimivesLogic.cs b/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/GetModels3dByValuePrimivesLogic.cs index b61b8a2..6d6b9c6 100644 --- a/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/GetModels3dByValuePrimivesLogic.cs +++ b/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/GetModels3dByValuePrimivesLogic.cs @@ -5,17 +5,16 @@ using FieldVisualizer.Services.ColorServices; using HelixToolkit.Geometry; using HelixToolkit.Maths; using HelixToolkit.SharpDX; -using HelixToolkit.SharpDX.Core; using HelixToolkit.Wpf.SharpDX; -using System; +using StructureHelper.Services.Reports.Services; using System.Collections.Generic; using System.Numerics; -using System.Text; namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalculatorViews { public class GetModels3dByValuePrimivesLogic : IGetModels3dLogic { + private MaterialService materialService = new(); public double ZoomValue { get; set; } public bool InvertNormal { get; set; } public IColorMap ColorMap { get; set; } @@ -53,8 +52,7 @@ namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalcu var builder = new MeshBuilder(); Vector3 p0 = new Vector3((float)circle.CenterX, (float)circle.CenterY, 0); // bottom center - float cylinderHeight = (float)(circle.Value * ZoomValue); - Vector3 p1 = new Vector3((float)circle.CenterX, (float)circle.CenterY, cylinderHeight); // top center + Vector3 p1 = new Vector3((float)circle.CenterX, (float)circle.CenterY, (float)(circle.Value * ZoomValue)); // top center builder.AddCylinder(p0, p1, (float)circle.Diameter, 8); @@ -62,11 +60,7 @@ namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalcu var cylinderGeometry = builder.ToMeshGeometry3D(); // Create material (constant grey) - var material = new PhongMaterial - { - DiffuseColor = ToColor4(ColorOperations.GetColorByValue(ValueRange, ColorMap, circle.Value)), - SpecularShininess = 50f - }; + var material = materialService.CreateValueMaterial(circle.Value, ValueRange, ColorMap); // Create model var cylinderModel = new MeshGeometryModel3D @@ -79,24 +73,8 @@ namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalcu private MeshGeometryModel3D CreateZeroTriangle(ITrianglePrimitive triangle) { - // Triangle vertices - Vector3 p0 = new Vector3((float)triangle.Point1.X, (float)triangle.Point1.Y, 0); - Vector3 p1 = new Vector3((float)triangle.Point2.X, (float)triangle.Point2.Y, 0); - Vector3 p2 = new Vector3((float)triangle.Point3.X, (float)triangle.Point3.Y, 0); - - var builder = new MeshBuilder(); - builder.AddTriangle(p0, p1, p2); - - var mesh = builder.ToMeshGeometry3D(); - - var material = new PBRMaterial - { - AlbedoColor = new Color4(0.5f, 0.5f, 0.5f, 0.4f), // 40% opacity - RoughnessFactor = 0.8f, - MetallicFactor = 0.0f - }; - - + var mesh = CreateTriangleMesh(triangle, false); + var material = materialService.CreateTransparentPlaneMaterial(0.4f); var model = new MeshGeometryModel3D { Geometry = mesh, @@ -109,23 +87,8 @@ namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalcu private MeshGeometryModel3D CreateTriangle(ITrianglePrimitive triangle) { - // Triangle vertices - Vector3 p0 = new Vector3((float)triangle.Point1.X, (float)triangle.Point1.Y, (float)(triangle.ValuePoint1 * ZoomValue)); - Vector3 p1 = new Vector3((float)triangle.Point2.X, (float)triangle.Point2.Y, (float)(triangle.ValuePoint2 * ZoomValue)); - Vector3 p2 = new Vector3((float)triangle.Point3.X, (float)triangle.Point3.Y, (float)(triangle.ValuePoint3 * ZoomValue)); - - var builder = new MeshBuilder(); - builder.AddTriangle(p0, p1, p2); - - var mesh = builder.ToMeshGeometry3D(); - - var material = new PhongMaterial - { - DiffuseColor = ToColor4(ColorOperations.GetColorByValue(ValueRange, ColorMap, triangle.Value)), - SpecularShininess = 50f - }; - - + var mesh = CreateTriangleMesh(triangle, true); + var material = materialService.CreateValueMaterial(triangle.Value, ValueRange, ColorMap); var model = new MeshGeometryModel3D { Geometry = mesh, @@ -136,7 +99,24 @@ namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalcu return model; } - public static Color4 ToColor4(System.Windows.Media.Color c) + private HelixToolkit.SharpDX.MeshGeometry3D CreateTriangleMesh(ITrianglePrimitive triangle, bool scaleByValue) + { + float z1 = scaleByValue ? (float)(triangle.ValuePoint1 * ZoomValue) : 0; + float z2 = scaleByValue ? (float)(triangle.ValuePoint2 * ZoomValue) : 0; + float z3 = scaleByValue ? (float)(triangle.ValuePoint3 * ZoomValue) : 0; + + Vector3 p0 = new((float)triangle.Point1.X, (float)triangle.Point1.Y, z1); + Vector3 p1 = new((float)triangle.Point2.X, (float)triangle.Point2.Y, z2); + Vector3 p2 = new((float)triangle.Point3.X, (float)triangle.Point3.Y, z3); + + var builder = new MeshBuilder(); + builder.AddTriangle(p0, p1, p2); + + return builder.ToMeshGeometry3D(); + } + + + private Color4 ToColor4(System.Windows.Media.Color c) { return new Color4( c.R / 255f, diff --git a/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/IGetModels3dLogic.cs b/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/IGetModels3dLogic.cs index d177b1a..3890c04 100644 --- a/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/IGetModels3dLogic.cs +++ b/StructureHelper/Windows/CalculationWindows/CalculatorsViews/ForceCalculatorViews/IGetModels3dLogic.cs @@ -14,8 +14,6 @@ namespace StructureHelper.Windows.CalculationWindows.CalculatorsViews.ForceCalcu bool ShowZeroPlane { get; set; } IValueRange ValueRange { get; set; } double ZoomValue { get; set; } - - static abstract Color4 ToColor4(System.Windows.Media.Color c); void GetModels3d(IEnumerable valuePrimitives, Viewport3DX viewport); } } \ No newline at end of file diff --git a/StructureHelper/Windows/MainWindow/CrossSections/PrimitiveViewModelLogic.cs b/StructureHelper/Windows/MainWindow/CrossSections/PrimitiveViewModelLogic.cs index f0346a5..a8abcfa 100644 --- a/StructureHelper/Windows/MainWindow/CrossSections/PrimitiveViewModelLogic.cs +++ b/StructureHelper/Windows/MainWindow/CrossSections/PrimitiveViewModelLogic.cs @@ -9,6 +9,7 @@ using StructureHelperCommon.Models.Shapes; using StructureHelperLogics.Models.CrossSections; using StructureHelperLogics.Models.Primitives; using StructureHelperLogics.NdmCalculations.Analyses.ByForces; +using StructureHelperLogics.NdmCalculations.Analyses.Curvatures; using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams; using StructureHelperLogics.NdmCalculations.Cracking; using StructureHelperLogics.NdmCalculations.Primitives; @@ -76,13 +77,42 @@ namespace StructureHelper.Windows.ViewModels.NdmCrossSections var dialogResult = MessageBox.Show("Delete all primitives?", "Please, confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { - repository.Primitives.Clear(); - Items.Clear(); + ClearRepository(); Refresh(); OnPropertyChanged(nameof(Items)); OnPropertyChanged(nameof(PrimitivesCount)); } } + + private void ClearRepository() + { + repository.Primitives.Clear(); + foreach (var calculator in repository.Calculators) + { + if (calculator is IForceCalculator forceCalculator) + { + forceCalculator.InputData.Primitives.Clear(); + } + else if (calculator is ICrackCalculator crackCalculator) + { + crackCalculator.InputData.Primitives.Clear(); + } + else if (calculator is IValueDiagramCalculator valueDiagramCalculator) + { + valueDiagramCalculator.InputData.Primitives.Clear(); + } + else if (calculator is ICurvatureCalculator curvatureCalculator) + { + curvatureCalculator.InputData.Primitives.Clear(); + } + else + { + // skip + } + } + Items.Clear(); + } + public ICommand DeleteSelectedCommand => deleteSelectedCommand ??= new RelayCommand(DeleteSelected, o => Items.Count > 0); private void DeleteSelected(object commandParameter) @@ -98,8 +128,7 @@ namespace StructureHelper.Windows.ViewModels.NdmCrossSections .ToList(); foreach (var item in deletePrimitivesList) { - repository.Primitives.Remove(item.NdmPrimitive); - Items.Remove(item); + RemoveFromRepository(item); } Refresh(); OnPropertyChanged(nameof(Items)); @@ -107,6 +136,35 @@ namespace StructureHelper.Windows.ViewModels.NdmCrossSections } } + private void RemoveFromRepository(PrimitiveBase item) + { + repository.Primitives.Remove(item.NdmPrimitive); + foreach (var calculator in repository.Calculators) + { + if (calculator is IForceCalculator forceCalculator) + { + forceCalculator.InputData.Primitives.Remove(item.NdmPrimitive); + } + else if (calculator is ICrackCalculator crackCalculator) + { + crackCalculator.InputData.Primitives.Remove(item.NdmPrimitive); + } + else if (calculator is IValueDiagramCalculator valueDiagramCalculator) + { + valueDiagramCalculator.InputData.Primitives.Remove(item.NdmPrimitive); + } + else if (calculator is ICurvatureCalculator curvatureCalculator) + { + curvatureCalculator.InputData.Primitives.Remove(item.NdmPrimitive); + } + else + { + // skip + } + } + Items.Remove(item); + } + private void SetMaterialToPrimitives(object obj) { if (SelectedItem is null) { return; } diff --git a/StructureHelperLogics/Models/CrossSections/CrossSectionRepository.cs b/StructureHelperLogics/Models/CrossSections/CrossSectionRepository.cs index 9fd1367..fed9c3e 100644 --- a/StructureHelperLogics/Models/CrossSections/CrossSectionRepository.cs +++ b/StructureHelperLogics/Models/CrossSections/CrossSectionRepository.cs @@ -1,14 +1,7 @@ using StructureHelper.Models.Materials; using StructureHelperCommon.Models.Calculators; using StructureHelperCommon.Models.Forces; -using StructureHelperLogics.Models.Primitives; -using StructureHelperLogics.NdmCalculations.Analyses; using StructureHelperLogics.NdmCalculations.Primitives; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace StructureHelperLogics.Models.CrossSections { diff --git a/StructureHelperLogics/Models/CrossSections/ICrossSectionRepository.cs b/StructureHelperLogics/Models/CrossSections/ICrossSectionRepository.cs index abeab03..7a4e075 100644 --- a/StructureHelperLogics/Models/CrossSections/ICrossSectionRepository.cs +++ b/StructureHelperLogics/Models/CrossSections/ICrossSectionRepository.cs @@ -6,7 +6,7 @@ using StructureHelperLogics.NdmCalculations.Primitives; namespace StructureHelperLogics.Models.CrossSections { - public interface ICrossSectionRepository : ISaveable, IHasHeadMaterials, IHasPrimitives, IHasForceActions, IHasCalculators + public interface ICrossSectionRepository : ISaveable, IHasHeadMaterials, IHasForcesAndPrimitives, IHasCalculators { } diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ByForces/IForceCalculatorInputData.cs b/StructureHelperLogics/NdmCalculations/Analyses/ByForces/IForceCalculatorInputData.cs index 0507fc3..43c6f83 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ByForces/IForceCalculatorInputData.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ByForces/IForceCalculatorInputData.cs @@ -10,7 +10,7 @@ namespace StructureHelperLogics.NdmCalculations.Analyses.ByForces /// /// Input data fo roce tuple calculator /// - public interface IForceCalculatorInputData : IInputData, ISaveable, IHasPrimitives, IHasForceActions + public interface IForceCalculatorInputData : IInputData, ISaveable, IHasForcesAndPrimitives { /// /// Accuracy of calculating diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ByForces/Logics/ForceTupleCalcLogic.cs b/StructureHelperLogics/NdmCalculations/Analyses/ByForces/Logics/ForceTupleCalcLogic.cs index 0ff9f29..4867d82 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ByForces/Logics/ForceTupleCalcLogic.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ByForces/Logics/ForceTupleCalcLogic.cs @@ -24,6 +24,8 @@ namespace StructureHelperLogics.NdmCalculations.Analyses.ByForces private Calculator calculator; private ILoaderResults calcResult; private IStressLogic stressLogic = new StressLogic(); + private IStressPointsLogic stressPointsLogic; + private IStressPointsLogic StressPointsLogic => stressPointsLogic ??= new StressPointsLogic(); public IForceTupleInputData InputData { get; set; } @@ -138,10 +140,11 @@ namespace StructureHelperLogics.NdmCalculations.Analyses.ByForces List overStrainedNdms = new(); foreach (var ndm in InputData.NdmCollection) { - var strain = stressLogic.GetTotalStrain(calcResult.ForceStrainPair.StrainMatrix, ndm); - if (strain < ndm.Material.LimitNegativeStrain || strain > ndm.Material.LimitPositiveStrain) + IStrainMatrix strainMatrix = calcResult.ForceStrainPair.StrainMatrix; + var strains = StressPointsLogic.GetTotalStrains(strainMatrix, ndm); + if (strains.Min() < ndm.Material.LimitNegativeStrain || strains.Max() > ndm.Material.LimitPositiveStrain) { - TraceLogger?.AddMessage($"Elementary part x = {ndm.CenterX}, y = {ndm.CenterY} has strain epsilon = {strain}, positive limit strain epsilon+,max = {ndm.Material.LimitPositiveStrain}, negative limit strain epsilon-,min = {ndm.Material.LimitNegativeStrain}", TraceLogStatuses.Warning); + TraceLogger?.AddMessage($"Elementary part x = {ndm.CenterX}, y = {ndm.CenterY} has max strain epsilon,max = {strains.Max()}, min strain epsilon,min = {strains.Min()}, positive limit strain epsilon+,max = {ndm.Material.LimitPositiveStrain}, negative limit strain epsilon-,min = {ndm.Material.LimitNegativeStrain}", TraceLogStatuses.Warning); overStrainedNdms.Add(ndm); } }; diff --git a/StructureHelperLogics/NdmCalculations/Analyses/Curvatures/ICurvatureCalculatorInputData.cs b/StructureHelperLogics/NdmCalculations/Analyses/Curvatures/ICurvatureCalculatorInputData.cs index 706f6bc..9c35901 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/Curvatures/ICurvatureCalculatorInputData.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/Curvatures/ICurvatureCalculatorInputData.cs @@ -1,6 +1,5 @@ using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Calculators; -using StructureHelperCommon.Models.Forces; using StructureHelperLogics.NdmCalculations.Primitives; namespace StructureHelperLogics.NdmCalculations.Analyses.Curvatures @@ -8,7 +7,7 @@ namespace StructureHelperLogics.NdmCalculations.Analyses.Curvatures /// /// Input data for calculator of curvature of cross-section /// - public interface ICurvatureCalculatorInputData : IInputData, ISaveable, IHasForceActions, IHasPrimitives + public interface ICurvatureCalculatorInputData : IInputData, ISaveable, IHasForcesAndPrimitives { IDeflectionFactor DeflectionFactor { get; set; } bool ConsiderSofteningFactor { get; set; } diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ValueDiagrams/IValueDiagramCalculatorInputData.cs b/StructureHelperLogics/NdmCalculations/Analyses/ValueDiagrams/IValueDiagramCalculatorInputData.cs index 1654a80..dcf8cc1 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ValueDiagrams/IValueDiagramCalculatorInputData.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ValueDiagrams/IValueDiagramCalculatorInputData.cs @@ -8,7 +8,7 @@ namespace StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams /// /// Implements input data for Value diagram calculator /// - public interface IValueDiagramCalculatorInputData : ISaveable, IInputData, IHasForceActions, IHasPrimitives + public interface IValueDiagramCalculatorInputData : ISaveable, IInputData, IHasForcesAndPrimitives { IStateCalcTermPair StateTermPair { get; set; } /// diff --git a/StructureHelperLogics/NdmCalculations/Cracking/ICrackCalculatorInputData.cs b/StructureHelperLogics/NdmCalculations/Cracking/ICrackCalculatorInputData.cs index 00b31e9..fd61816 100644 --- a/StructureHelperLogics/NdmCalculations/Cracking/ICrackCalculatorInputData.cs +++ b/StructureHelperLogics/NdmCalculations/Cracking/ICrackCalculatorInputData.cs @@ -11,7 +11,7 @@ namespace StructureHelperLogics.NdmCalculations.Cracking /// /// Input data for crack calculator /// - public interface ICrackCalculatorInputData : IInputData, IHasPrimitives, IHasForceActions, ISaveable + public interface ICrackCalculatorInputData : IInputData, IHasForcesAndPrimitives, ISaveable { /// /// Used difined data for crack width calculation diff --git a/StructureHelperLogics/NdmCalculations/Primitives/IHasForcesAndPrimitives.cs b/StructureHelperLogics/NdmCalculations/Primitives/IHasForcesAndPrimitives.cs new file mode 100644 index 0000000..3dccb21 --- /dev/null +++ b/StructureHelperLogics/NdmCalculations/Primitives/IHasForcesAndPrimitives.cs @@ -0,0 +1,11 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureHelperLogics.NdmCalculations.Primitives +{ + public interface IHasForcesAndPrimitives : IHasPrimitives, IHasForceActions + { + } +} diff --git a/StructureHelperLogics/NdmCalculations/Primitives/Logics/CrossSectionRepositoryCheckLogic.cs b/StructureHelperLogics/NdmCalculations/Primitives/Logics/CrossSectionRepositoryCheckLogic.cs new file mode 100644 index 0000000..b25177e --- /dev/null +++ b/StructureHelperLogics/NdmCalculations/Primitives/Logics/CrossSectionRepositoryCheckLogic.cs @@ -0,0 +1,61 @@ +using netDxf.Entities; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Services; +using StructureHelperLogics.Models.CrossSections; +using StructureHelperLogics.NdmCalculations.Analyses.ByForces; +using StructureHelperLogics.NdmCalculations.Analyses.Curvatures; +using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams; +using StructureHelperLogics.NdmCalculations.Cracking; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureHelperLogics.NdmCalculations.Primitives +{ + public class CrossSectionRepositoryCheckLogic : CheckEntityLogic + { + private bool result; + private + ICheckEntityLogic primitivesCheckLogic; + ICheckEntityLogic PrimitivesCheckLogic => primitivesCheckLogic ??= new RepositoryHasForcesAndPrimitivesCheckLogic(Entity) { TraceLogger = TraceLogger}; + public override bool Check() + { + result = true; + CheckObject.ThrowIfNull(Entity); + foreach (var item in Entity.Calculators) + { + if (item is IForceCalculator forceCalculator) + { + ProcessCalculatorInputData(forceCalculator.InputData); + } + else if (item is ICrackCalculator crackCalculator) + { + ProcessCalculatorInputData(crackCalculator.InputData); + } + else if (item is IValueDiagramCalculator valueDiagramCalculator) + { + ProcessCalculatorInputData(valueDiagramCalculator.InputData); + } + else if (item is ICurvatureCalculator curvatureCalculator) + { + ProcessCalculatorInputData(curvatureCalculator.InputData); + } + else + { + // skip + } + } + return result; + } + + private void ProcessCalculatorInputData(IHasForcesAndPrimitives inputData) + { + PrimitivesCheckLogic.Entity = inputData; + if (PrimitivesCheckLogic.Check() == false) + { + CheckResult += "\n" + PrimitivesCheckLogic.CheckResult; + result = false; + } + } + } +} diff --git a/StructureHelperLogics/NdmCalculations/Primitives/Logics/RepositoryHasForcesAndPrimitivesCheckLogic.cs b/StructureHelperLogics/NdmCalculations/Primitives/Logics/RepositoryHasForcesAndPrimitivesCheckLogic.cs new file mode 100644 index 0000000..784143c --- /dev/null +++ b/StructureHelperLogics/NdmCalculations/Primitives/Logics/RepositoryHasForcesAndPrimitivesCheckLogic.cs @@ -0,0 +1,38 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Services; +using StructureHelperLogics.Models.CrossSections; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureHelperLogics.NdmCalculations.Primitives +{ + public class RepositoryHasForcesAndPrimitivesCheckLogic : CheckEntityLogic + { + + ICheckEntityLogic primitivesCheckLogic; + ICheckEntityLogic PrimitivesCheckLogic => primitivesCheckLogic ??= new RepositoryHasPrimitivesCheckLogic(Repository) { TraceLogger = TraceLogger }; + public ICrossSectionRepository Repository { get; } + + public RepositoryHasForcesAndPrimitivesCheckLogic(ICrossSectionRepository repository) + { + Repository = repository; + } + + public override bool Check() + { + CheckObject.ThrowIfNull(Repository); + CheckObject.ThrowIfNull(Entity); + bool result = true; + PrimitivesCheckLogic.Entity = Entity; + if (PrimitivesCheckLogic.Check() == false) + { + CheckResult += "\n"; + CheckResult += PrimitivesCheckLogic.CheckResult; + result = false; + } + return result; + } + } +} diff --git a/StructureHelperLogics/NdmCalculations/Primitives/Logics/RepositoryHasPrimitivesCheckLogic.cs b/StructureHelperLogics/NdmCalculations/Primitives/Logics/RepositoryHasPrimitivesCheckLogic.cs new file mode 100644 index 0000000..d496d3e --- /dev/null +++ b/StructureHelperLogics/NdmCalculations/Primitives/Logics/RepositoryHasPrimitivesCheckLogic.cs @@ -0,0 +1,35 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Services; +using StructureHelperLogics.Models.CrossSections; +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureHelperLogics.NdmCalculations.Primitives +{ + public class RepositoryHasPrimitivesCheckLogic : CheckEntityLogic + { + public ICrossSectionRepository Repository { get; } + + public RepositoryHasPrimitivesCheckLogic(ICrossSectionRepository crossSectionRepository) + { + Repository = crossSectionRepository; + } + + public override bool Check() + { + bool result = true; + CheckObject.ThrowIfNull(Repository); + CheckObject.ThrowIfNull(Entity); + foreach (var primitive in Entity.Primitives) + { + if (! Repository.Primitives.Contains(primitive)) + { + TraceMessage($"Repository does not contain primitive Id = {primitive.Id}, Name = {primitive.Name}"); + result = false; + } + } + return result; + } + } +} diff --git a/StructureHelperTests/StructureHelperTests.csproj b/StructureHelperTests/StructureHelperTests.csproj index ccc75ff..ac71d1e 100644 --- a/StructureHelperTests/StructureHelperTests.csproj +++ b/StructureHelperTests/StructureHelperTests.csproj @@ -33,7 +33,6 @@ - diff --git a/StructureHelperTests/UnitTests/ConvertStrategiesTest/HasForceAndPrimitivesProcessLogicTests.cs b/StructureHelperTests/UnitTests/ConvertStrategiesTest/HasForceAndPrimitivesProcessLogicTests.cs new file mode 100644 index 0000000..c6aced6 --- /dev/null +++ b/StructureHelperTests/UnitTests/ConvertStrategiesTest/HasForceAndPrimitivesProcessLogicTests.cs @@ -0,0 +1,101 @@ +using DataAccess.DTOs; +using Moq; +using NUnit.Framework; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperLogics.NdmCalculations.Primitives; +using System; + +namespace StructureHelperTests.UnitTests.ConvertStrategiesTest +{ + + + [TestFixture] + public class HasForceAndPrimitivesProcessLogicTests + { + private Mock> mockForcesLogic; + private Mock> mockPrimitivesLogic; + private Mock mockSource; + private Mock mockTarget; + private Mock> mockDictionary; + + + [SetUp] + public void SetUp() + { + mockForcesLogic = new Mock>(MockBehavior.Strict); + mockPrimitivesLogic = new Mock>(MockBehavior.Strict); + mockDictionary = new Mock>(); + + mockSource = new Mock(); + mockTarget = new Mock(); + } + + [Test] + public void Process_WhenUsingDIInjects_ShouldCallBothInternalLogics() + { + // Arrange + var sut = new HasForcesAndPrimitivesProcessLogic( + ConvertDirection.FromDTO, + mockForcesLogic.Object, + mockPrimitivesLogic.Object + ) + { + Source = mockSource.Object, + Target = mockTarget.Object + }; + + // Setup mocked behavior + mockForcesLogic.SetupSet(x => x.Source = mockSource.Object); + mockForcesLogic.SetupSet(x => x.Target = mockTarget.Object); + mockForcesLogic.SetupSet(x => x.ReferenceDictionary = mockDictionary.Object); + mockForcesLogic.Setup(x => x.Process()); + + mockPrimitivesLogic.SetupSet(x => x.Source = mockSource.Object); + mockPrimitivesLogic.SetupSet(x => x.Target = mockTarget.Object); + mockPrimitivesLogic.SetupSet(x => x.ReferenceDictionary = mockDictionary.Object); + mockPrimitivesLogic.Setup(x => x.Process()); + sut.ReferenceDictionary = new Dictionary<(Guid id, Type type), ISaveable>(); + + // Act + sut.Process(); + + // Assert + mockForcesLogic.Verify(x => x.Process(), Times.Once); + mockPrimitivesLogic.Verify(x => x.Process(), Times.Once); + } + + [Test] + public void Process_PassesSourceAndTargetCorrectly_ToBothSubLogics() + { + // Arrange + var sut = new HasForcesAndPrimitivesProcessLogic( + ConvertDirection.ToDTO, + mockForcesLogic.Object, + mockPrimitivesLogic.Object) + { + Source = mockSource.Object, + Target = mockTarget.Object, + }; + + mockForcesLogic.SetupSet(x => x.Source = mockSource.Object); + mockForcesLogic.SetupSet(x => x.Target = mockTarget.Object); + mockForcesLogic.Setup(x => x.Process()); + + mockPrimitivesLogic.SetupSet(x => x.Source = mockSource.Object); + mockPrimitivesLogic.SetupSet(x => x.Target = mockTarget.Object); + mockPrimitivesLogic.Setup(x => x.Process()); + sut.ReferenceDictionary = new Dictionary<(Guid id, Type type), ISaveable>(); + + // Act + sut.Process(); + + // Assert + mockForcesLogic.VerifySet(x => x.Source = mockSource.Object); + mockForcesLogic.VerifySet(x => x.Target = mockTarget.Object); + + mockPrimitivesLogic.VerifySet(x => x.Source = mockSource.Object); + mockPrimitivesLogic.VerifySet(x => x.Target = mockTarget.Object); + } + } + +} diff --git a/StructureHelperTests/UnitTests/ConvertStrategiesTest/ValueDiagramCalculatorInputDataFromDTOConvertStrategyTests.cs b/StructureHelperTests/UnitTests/ConvertStrategiesTest/ValueDiagramCalculatorInputDataFromDTOConvertStrategyTests.cs new file mode 100644 index 0000000..6d77065 --- /dev/null +++ b/StructureHelperTests/UnitTests/ConvertStrategiesTest/ValueDiagramCalculatorInputDataFromDTOConvertStrategyTests.cs @@ -0,0 +1,128 @@ +using DataAccess.DTOs; +using Moq; +using NUnit.Framework; +using StructureHelperCommon.Infrastructures.Exceptions; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models.Calculators; +using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams; +using StructureHelperLogics.NdmCalculations.Primitives; +using System; +using System.Collections.Generic; + +namespace StructureHelperTests.UnitTests.ConvertStrategiesTest +{ + + + [TestFixture] + public class ValueDiagramCalculatorInputDataFromDTOConvertStrategyTests + { + private Mock> mockUpdate; + private Mock> mockDiagramConvert; + private Mock> mockForcePrimLogic; + private Mock> mockAccuracyConvert; + + private ValueDiagramCalculatorInputDataFromDTOConvertStrategy sut; + + private ValueDiagramCalculatorInputDataDTO sampleDTO; + private ValueDiagramEntityDTO diagramDTO; + private ValueDiagramEntity sampleDiagramEntity; + + [SetUp] + public void SetUp() + { + mockUpdate = new Mock>(MockBehavior.Strict); + mockDiagramConvert = new Mock>(MockBehavior.Strict); + mockForcePrimLogic = new Mock>(MockBehavior.Strict); + mockAccuracyConvert = new Mock>(MockBehavior.Strict); + + sut = new ValueDiagramCalculatorInputDataFromDTOConvertStrategy( + mockUpdate.Object, + mockDiagramConvert.Object, + mockForcePrimLogic.Object, + mockAccuracyConvert.Object + ); + + diagramDTO = new ValueDiagramEntityDTO(Guid.Empty); + sampleDiagramEntity = new ValueDiagramEntity(Guid.NewGuid()); + + sampleDTO = new ValueDiagramCalculatorInputDataDTO(Guid.NewGuid()) + { + }; + sampleDTO.Diagrams.Add(diagramDTO); + } + + [Test] + public void GetNewItem_ShouldReturnItemWithSameId() + { + // Arrange + mockUpdate.Setup(x => x.Update(It.IsAny(), sampleDTO)); + mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO); + mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny()); + mockForcePrimLogic.Setup(x => x.Process()); + mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity); + + // Act + var result = sut.GetNewItem(sampleDTO); + + // Assert + Assert.That(sampleDTO.Id, Is.EqualTo(result.Id)); + } + + [Test] + public void GetNewItem_ShouldCallUpdateStrategy() + { + // Arrange + mockUpdate.Setup(x => x.Update(It.IsAny(), sampleDTO)); + mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO); + mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny()); + mockForcePrimLogic.Setup(x => x.Process()); + mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity); + + // Act + sut.GetNewItem(sampleDTO); + + // Assert + mockUpdate.Verify(x => x.Update(It.IsAny(), sampleDTO), Times.Once); + } + + [Test] + public void GetNewItem_ShouldCallForcePrimitiveProcessing() + { + // Arrange + mockUpdate.Setup(x => x.Update(It.IsAny(), sampleDTO)); + + mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO); + mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny()); + mockForcePrimLogic.Setup(x => x.Process()); + + mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity); + + // Act + sut.GetNewItem(sampleDTO); + + // Assert + mockForcePrimLogic.Verify(x => x.Process(), Times.Once); + } + + [Test] + public void GetNewItem_ShouldConvertAllDiagrams() + { + // Arrange + mockUpdate.Setup(x => x.Update(It.IsAny(), sampleDTO)); + + mockForcePrimLogic.SetupSet(x => x.Source = sampleDTO); + mockForcePrimLogic.SetupSet(x => x.Target = It.IsAny()); + mockForcePrimLogic.Setup(x => x.Process()); + + mockDiagramConvert.Setup(x => x.Convert(diagramDTO)).Returns(sampleDiagramEntity); + + // Act + var result = sut.GetNewItem(sampleDTO); + + // Assert + Assert.That(1, Is.EqualTo(result.Diagrams.Count)); + Assert.That(sampleDiagramEntity, Is.SameAs(result.Diagrams[0])); + } + } + +}