Skip to content

Commit

Permalink
Fourth project part (#20)
Browse files Browse the repository at this point in the history
* ruleset rule S#1310 changed to None

* data validators for calculations refactored

* ui styles reworked

* main view redone using mvvm

* startup for main view using caliburn.micro

* tutorial view redone using mvvm

* results view redone using mvvm

* added calculation header

* added calculation footer

* interaction logic for header and footer

* bases for calculations interaction logic

* aded ui for new calculation working with 2D plane

* ui for dice calculation redone using mvvm

* interaction logic for dice calculation ui

* results placeholders for results view

* old dice calculation ui removed

* ui for coloredballs calculation redone

* interaction logic for coloredballs calculation ui

* removed old coloredballs calculation ui

* obsolete panels removed

* not needed extensions removed

* not needed interfaces removed

* not needed models removed

* not needed utility removed

* color translator refactored

* only needed ui extension refactored

* startup for ui and new styles added to App dictionary

* added icon for application

* added caliburn.micro for mvvm logic and livecharts library for results chart

* added domain extensions

* reworked calculation data objects

* added enums for integer resource managment in application

* removed not needed utility

* refactored number translator

* removed old resources

* refactored resources structure

* added czech localization

* added validation exception

* moved calculation interface to domain

* project file updated

* removed moved calculation data objects

* removed moved calculation interface

* added bases for calculations

* added calculation data validation bases

* refactored coloredballs calculation and validator

* refactored dice calculation and validator

* added new 2d plane calculation and validator

* removed renamed validators

* updated calculations project file

* updated solution
  • Loading branch information
mpolacek1 authored Apr 7, 2022
1 parent 98f622c commit be3d379
Show file tree
Hide file tree
Showing 197 changed files with 9,550 additions and 5,303 deletions.
3 changes: 2 additions & 1 deletion .ruleset
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="16.0">
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="17.0">
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1001" Action="Warning" />
<Rule Id="CA1009" Action="Warning" />
Expand Down Expand Up @@ -34,6 +34,7 @@
<Rule Id="SA1208" Action="None" />
<Rule Id="SA1305" Action="Warning" />
<Rule Id="SA1309" Action="None" />
<Rule Id="SA1310" Action="None" />
<Rule Id="SA1600" Action="None" />
<Rule Id="SA1601" Action="None" />
<Rule Id="SA1602" Action="None" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using ExperimentalProbability.Contracts.Enums;
using ExperimentalProbability.Contracts.Exceptions;
using ExperimentalProbability.Contracts.Models;
using ExperimentalProbability.Contracts.Properties.Resources.Validation;
using ExperimentalProbability.Contracts.Utilities;
using GeneralCalcResources = ExperimentalProbability.Contracts.Properties.Resources.Calculations.General.Resources;

namespace ExperimentalProbability.Calculations.CalculationDataValidation
{
public abstract class BaseCalculationDataValidator
{
public virtual void Validate(CalculationData data)
{
ValidateNumber(data.SimulationsToRun, (int)GeneralNumbers.MinSimulations, (int)GeneralNumbers.MaxSimulations, GeneralCalcResources.ElementName_SimulationsToRun);
}

protected void ValidateNumber(int number, int min, int max, string elementName)
{
if (number < min)
{
throw new ValidationException(elementName, Resources.Error_Number_Min, min);
}

if (number > max)
{
throw new ValidationException(elementName, Resources.Error_Number_Max, max);
}
}

protected string AppendNumberPositionToString(int index, string elementName)
{
return string.Concat(NumberTranslater.NumberToPosition[index], ' ', elementName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ExperimentalProbability.Contracts.Models;

namespace ExperimentalProbability.Calculations.CalculationDataValidation.Pool
{
public abstract class BasePoolCalculationDataValidator : BaseCalculationDataValidator
{
public override void Validate(CalculationData data)
{
base.Validate(data);
ValidatePoolData(data);
ValidateConditionData(data);
}

protected abstract void ValidatePoolData(CalculationData data);

protected abstract void ValidateConditionData(CalculationData data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using ExperimentalProbability.Contracts.Enums;
using ExperimentalProbability.Contracts.Exceptions;
using ExperimentalProbability.Contracts.Models;
using ExperimentalProbability.Contracts.Models.Pool;
using ExperimentalProbability.Contracts.Properties.Resources.Calculations.Pool.ColoredBalls;

namespace ExperimentalProbability.Calculations.CalculationDataValidation.Pool
{
public class ColoredBallsDataValidator : BasePoolCalculationDataValidator
{
protected override void ValidatePoolData(CalculationData data)
{
var calcData = (ColoredBallsCalculationData)data;
var ballCount = calcData.BallCount;

ValidateNumber(
ballCount,
(int)ColoredBallsNumbers.MinNumberOfBalls,
(int)ColoredBallsNumbers.MaxNumberOfBalls,
Resources.ElementName_NumberOfBalls);

var maxNumberOfColors = (int)ColoredBallsNumbers.MaxNumberOfColors;

ValidateNumber(
calcData.PoolColors.Length,
(int)ColoredBallsNumbers.MinNumberOfColors,
ballCount < maxNumberOfColors ? ballCount : maxNumberOfColors,
Resources.ElementName_NumberOfColors);

ValidateColors(calcData.PoolColors, Resources.ElementName_PoolColor);
ValidateColorCounts(calcData);
}

protected override void ValidateConditionData(CalculationData data)
{
var calcData = (ColoredBallsCalculationData)data;
var ballCount = calcData.BallCount;
var maxNumberOfTakenBalls = (int)ColoredBallsNumbers.MaxNumberOfTakenBalls;

ValidateNumber(
calcData.ConditionColors.Length,
(int)ColoredBallsNumbers.MinNumberOfTakenBalls,
ballCount < maxNumberOfTakenBalls ? ballCount : maxNumberOfTakenBalls,
Resources.ElementName_NumberOfTakenBalls);

ValidateColors(calcData.ConditionColors, Resources.ElementName_ConditionColor);
}

private void ValidateColorCounts(ColoredBallsCalculationData data)
{
var sum = default(int);
var counts = data.ColorCounts;
var ballCount = data.BallCount;

for (int i = default; i < counts.Length; i++)
{
var count = counts[i];

ValidateNumber(
count,
(int)ColoredBallsNumbers.MinColorCount,
ballCount - 1,
AppendNumberPositionToString(i, Resources.ElementName_Counter));

sum += count;
}

ValidateNumber(sum, ballCount, ballCount, Resources.ElementName_CounterSum);
}

private void ValidateColors(object[] colors, string elementName)
{
for (int i = default; i < colors.Length; i++)
{
ValidateColor(colors[i], AppendNumberPositionToString(i, elementName));
}
}

private void ValidateColor(object color, string elementName)
{
if (color == null)
{
throw new ValidationException(elementName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ExperimentalProbability.Contracts.Enums;
using ExperimentalProbability.Contracts.Models;
using ExperimentalProbability.Contracts.Models.Pool;
using ExperimentalProbability.Contracts.Properties.Resources.Calculations.Pool.Dice;

namespace ExperimentalProbability.Calculations.CalculationDataValidation.Pool
{
public class DiceDataValidator : BasePoolCalculationDataValidator
{
protected override void ValidatePoolData(CalculationData data)
{
ValidateNumber(
((DiceCalculationData)data).SideCount,
(int)DiceNumbers.SideCountTetrahedron,
(int)DiceNumbers.SideCountIcosahedron,
Resources.ElementName_Dice);
}

protected override void ValidateConditionData(CalculationData data)
{
var calcData = (DiceCalculationData)data;

ValidateNumber(
calcData.ConditionSides.Length,
(int)DiceNumbers.MinNumberOfRolls,
(int)DiceNumbers.MaxNumberOfRolls,
Resources.ElementName_NumberOfRolls);

ValidateConditionSides(calcData);
}

private void ValidateConditionSides(DiceCalculationData data)
{
var conditionSides = data.ConditionSides;

for (int i = default; i < conditionSides.Length; i++)
{
ValidateNumber(
conditionSides[i],
(int)DiceNumbers.MinRollResult,
data.SideCount,
AppendNumberPositionToString(i, Resources.ElementName_Roll));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ExperimentalProbability.Calculations.CalculationDataValidation.Calculations.TwoDPlane
{
public class QuarterCircleDataValidator : BaseCalculationDataValidator
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.ComponentModel;
using ExperimentalProbability.Calculations.CalculationDataValidation;
using ExperimentalProbability.Contracts.Enums;
using ExperimentalProbability.Contracts.Interfaces;
using ExperimentalProbability.Contracts.Models;

namespace ExperimentalProbability.Calculations.Calculations
{
public abstract class BaseCalculation : ICalculation
{
private readonly BaseCalculationDataValidator _validator;

private readonly BackgroundWorker _worker;

private readonly Random _random;

private CalculationResultData _resultData;

public BaseCalculation(BaseCalculationDataValidator validator, BackgroundWorker worker)
{
_validator = validator;
_worker = worker;
_random = new Random();
_resultData = new CalculationResultData();
}

public virtual CalculationResultData Run(CalculationData data, DoWorkEventArgs workerEventArgs)
{
ValidateData(data);
return Calculate(data, workerEventArgs);
}

public abstract decimal CalculateResult(CalculationResultData data);

public void ClearResultData()
{
_resultData = new CalculationResultData();
}

protected void ValidateData(CalculationData data)
{
_validator.Validate(data);
}

protected CalculationResultData Calculate(CalculationData data, DoWorkEventArgs workerEventArgs)
{
var updateProgress = data.SimulationsToRun / (int)GeneralNumbers.MaxProgress;
var progress = default(int);

for (int i = default; i < data.SimulationsToRun; i++)
{
if (_worker.CancellationPending)
{
workerEventArgs.Cancel = true;
return null;
}

RunSimulation(data, _random, _resultData);

progress = TryUpdateWorkerProgress(i, updateProgress, _resultData, data, progress);
}

CalculateResult(_resultData);
return _resultData;
}

protected abstract bool CheckCondition(CalculationData data, object simResult);

protected abstract object GetSimulationResult(CalculationData data, Random random);

private void RunSimulation(CalculationData data, Random random, CalculationResultData resultData)
{
if (CheckCondition(data, GetSimulationResult(data, random)))
{
resultData.ConditionMet++;
}

resultData.SimulationsRan++;
}

private int TryUpdateWorkerProgress(int currentSim, int updateProgress, CalculationResultData resultData, CalculationData data, int currentProgress)
{
if ((currentSim != 0 && currentSim % updateProgress == 0)
|| resultData.SimulationsRan == data.SimulationsToRun)
{
currentProgress++;
resultData.Result = CalculateResult(resultData);
_worker.ReportProgress(currentProgress, resultData.Result);
}

return currentProgress;
}
}
}
Loading

0 comments on commit be3d379

Please sign in to comment.