internal static bool CompatibleRule <TFactRule, TWantAction, TFactContainer>(this TFactRule factRule, IVersionFact maxVersion, IWantActionContext <TWantAction, TFactContainer> context) where TFactRule : IFactRule where TWantAction : IWantAction where TFactContainer : IFactContainer { var version = factRule.InputFactTypes.GetVersionFact(context); if (version == null) { return(false); } return(maxVersion.CompareTo(version) >= 0); }
/// <summary> /// Checks the value of the version of the <paramref name="fact"/>. /// </summary> /// <typeparam name="TFact">Type fact.</typeparam> /// <param name="fact">Fact.</param> /// <param name="version">Version fact.</param> /// <returns>Does the version match the fact of the <paramref name="version"/>?</returns> public static bool HasVersionParameter <TFact>(this TFact fact, IVersionFact version) where TFact : IFact { var factVersion = fact.FindVersionParameter(); if (version == null) { return(factVersion == null); } if (factVersion == null) { return(false); } return(version.CompareTo(factVersion) == 0); }
/// <summary> /// Checks if a fact contains a valid version. /// </summary> /// <typeparam name="TFact">Type fact.</typeparam> /// <param name="fact">Fact.</param> /// <param name="maxVersion">Max version (optional).</param> /// <returns>Whether the version of the fact is within the valid versions?</returns> public static bool IsRelevantFactByVersioned <TFact>(this TFact fact, IVersionFact maxVersion) where TFact : IFact { if (maxVersion == null || !fact.IsCalculatedByRule()) { return(true); } var value = fact.GetParameter(VersionedFactParametersCodes.Version)?.Value; if (value == null) { return(false); } if (value is IVersionFact factVersion) { return(maxVersion.CompareTo(factVersion) >= 0); } return(false); }
/// <summary> /// Compares rules based on version 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 CompareByVersion <TFactRule, TWantAction, TFactContainer>(this TFactRule x, TFactRule y, IWantActionContext <TWantAction, TFactContainer> context) where TFactRule : IFactRule where TWantAction : IWantAction where TFactContainer : IFactContainer { var xVersionType = x.InputFactTypes?.SingleOrDefault(type => type.IsFactType <IVersionFact>()); var yVersionType = y.InputFactTypes?.SingleOrDefault(type => type.IsFactType <IVersionFact>()); if (xVersionType == null) { return(yVersionType == null ? 0 : 1); } if (yVersionType == null) { return(-1); } IVersionFact xVersion = context.Container.FirstVersionFactByFactType(xVersionType, context.Cache); IVersionFact yVersion = context.Container.FirstVersionFactByFactType(yVersionType, context.Cache); return(xVersion.CompareTo(yVersion)); }