/// <inheritdoc/> public override int CompareTo(IPriorityFact other) { switch (other) { case PriorityBase <int> priority: return(priority.PriorityValue.CompareTo(PriorityValue)); case PriorityBase <uint> priority: return(priority.PriorityValue.CompareTo(PriorityValue)); case PriorityBase <long> priority: return(priority.PriorityValue.CompareTo(PriorityValue)); case PriorityBase <ulong> priority: return(priority.PriorityValue.CompareTo(PriorityValue)); case FactBase <int> priority: return(priority.Value.CompareTo(PriorityValue)); case FactBase <uint> priority: return(priority.Value.CompareTo(PriorityValue)); case FactBase <long> priority: return(priority.Value.CompareTo(PriorityValue)); case FactBase <ulong> priority: return(priority.Value.CompareTo(PriorityValue)); default: throw CreateIncompatibilityVersionException(other); } }
/// <summary> /// Calculates the fact and adds the priority fact to the parameters. /// </summary> /// <typeparam name="TFactRule">Type rule.</typeparam> /// <typeparam name="TWantAction">Type wantAction.</typeparam> /// <typeparam name="TFactContainer">Type fact container.</typeparam> /// <param name="node">Node containing information about the calculation rule.</param> /// <param name="context">Context</param> /// <returns>Fact.</returns> public override IFact CalculateFact <TFactRule, TWantAction, TFactContainer>(NodeByFactRule <TFactRule> node, IWantActionContext <TWantAction, TFactContainer> context) { IPriorityFact priority = node.Info.Rule.GetPriorityFact(context); return(base .CalculateFact(node, context) .AddPriorityParameter(priority)); }
/// <summary> /// Compares rules based on priority facts. /// </summary> /// <typeparam name="TFactRule">Type rule.</typeparam> /// <typeparam name="TWantAction">Type wantAction.</typeparam> /// <typeparam name="TFactContainer">Type fact container.</typeparam> /// <param name="x">First rule.</param> /// <param name="y">Second rule.</param> /// <param name="context">Context.</param> /// <returns> /// 1 - <paramref name="x"/> rule is greater than the <paramref name="y"/>, /// 0 - <paramref name="x"/> rule is equal than the <paramref name="y"/>, /// -1 - <paramref name="x"/> rule is less than the <paramref name="y"/>. /// </returns> public static int CompareByPriority <TFactRule, TWantAction, TFactContainer>(this TFactRule x, TFactRule y, IWantActionContext <TWantAction, TFactContainer> context) where TFactRule : IFactRule where TWantAction : IWantAction where TFactContainer : IFactContainer { var xPriorityType = x.InputFactTypes?.SingleOrDefault(type => type.IsFactType <IPriorityFact>()); var yPriorityType = y.InputFactTypes?.SingleOrDefault(type => type.IsFactType <IPriorityFact>()); if (xPriorityType == null) { return(yPriorityType == null ? 0 : -1); } if (yPriorityType == null) { return(1); } IPriorityFact xPriority = context.Container.FirstPriorityFactByFactType(xPriorityType, context.Cache); IPriorityFact yPriority = context.Container.FirstPriorityFactByFactType(yPriorityType, context.Cache); return(xPriority.CompareTo(yPriority)); }
/// <summary> /// Creates an error creating incompatibility priority facts. /// </summary> /// <param name="priorityFact">Priority fact.</param> /// <returns>Error creating incompatibility priority facts.</returns> protected virtual FactFactoryException CreateIncompatibilityVersionException(IPriorityFact priorityFact) { return(FactFactoryHelper.CreateException( ErrorCode.InvalidFactType, $"Unable to compare priorities {GetFactType().FactName} and {priorityFact.GetFactType().FactName}.")); }
/// <summary> /// Compares the priority fact to the <paramref name="other"/>. /// </summary> /// <param name="other">Priority fact for comparison</param> /// <returns>1 - more, 0 - equal, -1 less.</returns> public abstract int CompareTo(IPriorityFact other);
/// <summary> /// Calculates the fact asynchronously and adds the priority fact to the parameters. /// </summary> /// <typeparam name="TFactRule">Type rule.</typeparam> /// <typeparam name="TWantAction">Type wantAction.</typeparam> /// <typeparam name="TFactContainer">Type fact container.</typeparam> /// <param name="node">Node containing information about the calculation rule.</param> /// <param name="context">Context</param> /// <returns>Fact.</returns> public override async ValueTask <IFact> CalculateFactAsync <TFactRule, TWantAction, TFactContainer>(NodeByFactRule <TFactRule> node, IWantActionContext <TWantAction, TFactContainer> context) { IPriorityFact priority = node.Info.Rule.GetPriorityFact(context); return((await base.CalculateFactAsync(node, context).ConfigureAwait(false)).AddPriorityParameter(priority)); }