<commit_before>﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using ESAPIX.Extensions;
using ESAPIX.Facade.API;
using static ESAPIX.Constraints.ResultType;

#endregion

namespace ESAPIX.Constraints
{
    public class PQAsserter
    {
        public List<ConstraintResult> Results { get; set; } = new List<ConstraintResult>();

        /// <summary>
        /// Returns the cumulative result of all assertions. Will either return the first failing or a passing result
        /// </summary>
        public ConstraintResult CumulativeResult
        {
            get
            {
                //If there are any failures, return first one, otherwise return the first result (which will be passing)
                return Results.Any(r => r.ResultType != PASSED)
                    ? Results.FirstOrDefault(r => r.ResultType != PASSED)
                    : Results.FirstOrDefault();
            }
        }

        public PQAsserter ContainsValidFractionNum(PlanningItem pi)
        {
            int? numFx = 0;
            if (pi is PlanSum)
            {
                numFx = (pi as PlanSum).PlanSetups.Sum(p => p?.NumberOfFractions());
            }
            else
                numFx = (pi as PlanSetup)?.NumberOfFractions();

            Results.Add(new ConstraintResult(null, numFx!=null?ResultType.PASSED:ResultType.NOT_APPLICABLE, "Not valid fraction number", string.Empty));
            return this;
        }

        /// <summary>
        /// Asserts contains image (not null)
        /// </summary>
        /// <param name="pi">the planing item</param>
        /// <returns>the asserter object</returns><commit_msg>Added inline assertions to PQ Asserter class<commit_after>﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using ESAPIX.Extensions;
using ESAPIX.Facade.API;
using static ESAPIX.Constraints.ResultType;

#endregion

namespace ESAPIX.Constraints
{
    public class PQAsserter
    {
        public List<ConstraintResult> Results { get; set; } = new List<ConstraintResult>();

        /// <summary>
        /// Returns the cumulative result of all assertions. Will either return the first failing or a passing result
        /// </summary>
        public ConstraintResult CumulativeResult
        {
            get
            {
                //If there are any failures, return first one, otherwise return the first result (which will be passing)
                return Results.Any(r => r.ResultType != PASSED)
                    ? Results.FirstOrDefault(r => r.ResultType != PASSED)
                    : Results.FirstOrDefault();
            }
        }

        /// <summary>
        /// Asserts the input assertion. Upon failure returns a Action Level 3 contraint result
        /// </summary>
        /// <param name="pi">the planning item to be evaluated</param>
        /// <param name="assertion">the assertion that if not passed, returns an "not applicable" result</param>
        /// <param name="failedMessage">the message to show in case of failure</param>
        /// <returns>the plan quality asserter</returns>
        public PQAsserter Assert(PlanningItem pi, Func<PlanningItem, bool> assertion, string failedMessage)
        {
            bool passed = false;
            try
            {
                passed = assertion(pi);
            }
            catch (Exception e) { failedMessage += $" => Exception thrown : {e.Message}"; }

            Results.Add(new ConstraintResult(null, passed ? ResultType.PASSED : ResultType.NOT_APPLICABLE, failedMessage));
            return this;
        }

        /// <summary>
        /// Asserts the input assertion. Upon failure returns a Action Level 3 contraint result
        /// </summary>
        /// <param name="pi">the planning item to be evaluated</param>
        /// <param name="assertion">the assertion that if not passed, returns an Action level 3 result</param>
        /// <param name="failedMessage">the message to show in case of failure</param>
        /// <returns>the plan quality asserter</returns>
        public PQAsserter AssertCriticalPriority(PlanningItem pi, Func<PlanningItem, bool> assertion, string failedMessage)
        {
            bool passed = false;
            try
            {
                passed = assertion(pi);
            }
            catch (Exception e) { failedMessage += $" => Exception thrown : {e.Message}"; }
            Results.Add(new ConstraintResult(null, passed ? ResultType.PASSED : ResultType.ACTION_LEVEL_3, failedMessage));
            return this;
        }

        /// <summary>
        /// Asserts the input assertion. Upon failure returns a Action Level 3 contraint result
        /// </summary>
        /// <param name="pi">the planning item to be evaluated</param>
        /// <param name="assertion">the assertion that if not passed, returns an Action level 2 result</param>
        /// <param name="failedMessage">the message to show in case of failure</param>
        /// <returns>the plan quality asserter</returns>
        public PQAsserter AssertMidPriority(PlanningItem pi, Func<PlanningItem, bool> assertion, string failedMessage)
        {
            bool passed = false;
            try
            {
                passed = assertion(pi);
            }
            catch (Exception e) { failedMessage += $" => Exception thrown : {e.Message}"; }
            Results.Add(new ConstraintResult(null, passed ? ResultType.PASSED : ResultType.ACTION_LEVEL_2, failedMessage));
            return this;
        }

        /// <summary>
        /// Asserts the input assertion. Upon failure returns a Action Level 1 contraint result
        /// </summary>
        /// <param name="pi">the planning item to be evaluated</param>
        /// <param name="assertion">the assertion that if not passed, returns an Action level 3 result</param>
        /// <param name="failedMessage">the message to show in case of failure</param>
        /// <returns>the plan quality asserter</returns>
        public PQAsserter AssertLowPriority(PlanningItem pi, Func<PlanningItem, bool> assertion, string failedMessage)
        {
            bool passed = false;
            try
            {
                passed = assertion(pi);
            }
            catch (Exception e) { failedMessage += $" => Exception thrown : {e.Message}"; }
            Results.Add(new ConstraintResult(null, passed ? ResultType.PASSED : ResultType.ACTION_LEVEL_1, failedMessage));
            return this;
        }

        public PQAsserter ContainsValidFractionNum(PlanningItem pi)
        {
            int? numFx = 0;
            if (pi is PlanSum)
            {
                numFx = (pi as PlanSum).PlanSetups.Sum(p => p?.NumberOfFractions());
            }
            else
                numFx = (pi as PlanSetup)?.NumberOfFractions();

            Results.Add(new ConstraintResult(null, numFx != null ? ResultType.PASSED : ResultType.NOT_APPLICABLE, "Not valid fraction number", string.Empty));
            return this;
        }

        /// <summary>
        /// Asserts contains image (not null)
        /// </summary>
        /// <param name="pi">the planing item</param>
        /// <returns>the asserter object</returns>
{'fraction_non_alphanumeric': 0.10398230088495575, 'fraction_numerical': 0.00165929203539823, 'mean_word_length': 2.03779924401512, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4800869', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1830, 'n_words': 559}