/// <summary>
 /// The method returns instances of all versions used in the rules.
 /// </summary>
 /// <returns></returns>
 private List <IVersionFact> GetAllVersion(IWantActionContext <WantAction, FactContainer> context)
 {
     return(new List <IVersionFact>
     {
         new Version2019(),
         new Version2020(),
     });
 }
Esempio n. 2
0
 protected IEnumerable <IFact> GetPriorityFacts(IWantActionContext <WantAction, FactContainer> context)
 {
     return(new List <IFact>
     {
         new Priority1(),
         new Priority2(),
     });
 }
 public void Initialize()
 {
     Context = GetWantActionContext(
         GetWantAction((FactResult _) => { }),
         new FactContainer
     {
         new Priority1(),
         new Priority2(),
     });
 }
 public void Initialize()
 {
     Rule     = GetFactRule((Priority1 p, Fact1 f) => new FactResult(f.Value + p));
     NodeInfo = new NodeByFactRuleInfo <FactRule>
     {
         BuildFailedConditions = new List <IBuildConditionFact>(),
         Rule = Rule,
         BuildSuccessConditions = new List <IBuildConditionFact>(),
         RuntimeConditions      = new List <IRuntimeConditionFact>(),
     };
     Node = new NodeByFactRule <FactRule>
     {
         Childs = new List <NodeByFactRule <FactRule> >(),
         Info   = NodeInfo,
         Parent = null,
     };
     Context = GetWantActionContext(
         GetWantAction((FactResult result) => { }),
         new FactContainer
     {
         new Priority1(),
         new Priority2(),
     });
 }
        /// <inheritdoc/>
        public virtual bool TryGetRelatedRules <TFactRule, TWantAction, TFactContainer>(IWantActionContext <TWantAction, TFactContainer> context, out IFactRuleCollection <TFactRule> relatedRules)
            where TFactRule : IFactRule
            where TWantAction : IWantAction
            where TFactContainer : IFactContainer
        {
            relatedRules = null;

            if (_relatedRules is IFactRuleCollection <TFactRule> result)
            {
                relatedRules = result;
                return(true);
            }

            if (_getRelatedRulesFunc is Func <TFactRule, IFactRuleCollection <TFactRule>, IWantActionContext <TWantAction, TFactContainer>, IFactRuleCollection <TFactRule> > getRelatedRulesFunc &&
                _rules is IFactRuleCollection <TFactRule> rules &&
                _rule is TFactRule rule)
            {
                relatedRules  = getRelatedRulesFunc(rule, rules, context);
                _relatedRules = relatedRules;
                return(true);
            }

            return(false);
        }
        /// <inheritdoc/>
        /// <remarks>Additionally checks version compatibility.</remarks>
        public override bool CanExtractFact <TFactWork, TWantAction, TFactContainer>(IFactType factType, TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context)
        {
            if (factType.IsFactType <ISpecialFact>())
            {
                return(base.CanExtractFact(factType, factWork, context));
            }

            List <IFact> facts = context
                                 .Container
                                 .WhereFactsByFactType(factType, context.Cache)
                                 .ToList();

            if (facts.Count == 0)
            {
                return(false);
            }

            var maxVersion = context.WantAction.InputFactTypes.GetVersionFact(context);

            if (maxVersion == null)
            {
                return(true);
            }

            return(facts.Exists(fact => fact.IsRelevantFactByVersioned(maxVersion)));
        }
        /// <summary>
        /// Synchronize the tree level with years ready for calculation.
        /// </summary>
        /// <typeparam name="TFactRule"></typeparam>
        /// <typeparam name="TWantAction"></typeparam>
        /// <typeparam name="TFactContainer"></typeparam>
        /// <param name="treeLevel">Tree level.</param>
        /// <param name="finishedNodes">Nodes by which the rule can already be calculated. Key - node info, value - node</param>
        /// <param name="context">Context.</param>
        private void SyncTreeLevelAndFinishedNodes <TFactRule, TWantAction, TFactContainer>(List <NodeByFactRule <TFactRule> > treeLevel, Dictionary <NodeByFactRuleInfo <TFactRule>, NodeByFactRule <TFactRule> > finishedNodes, IWantActionContext <TWantAction, TFactContainer> context)
            where TFactRule : IFactRule
            where TWantAction : IWantAction
            where TFactContainer : IFactContainer
        {
            foreach (var finishedNode in finishedNodes)
            {
                List <NodeByFactRule <TFactRule> > parentNodes = treeLevel
                                                                 .Where(node => node.Info.Rule.EqualsWork(finishedNode.Key.Rule, context.WantAction, context.Container))
                                                                 .Select(node => node.Parent)
                                                                 .Distinct()
                                                                 .ToList();

                foreach (NodeByFactRule <TFactRule> parentNode in parentNodes)
                {
                    if (parentNode == null)
                    {
                        continue;
                    }

                    for (int i = parentNode.Childs.Count - 1; i >= 0; i--)
                    {
                        NodeByFactRule <TFactRule> childNode = parentNode.Childs[i];
                        if (!childNode.Info.Rule.OutputFactType.EqualsFactType(finishedNode.Key.Rule.OutputFactType))
                        {
                            continue;
                        }

                        parentNode.Childs.Remove(childNode);
                        int indexNodeInTreeLevel = treeLevel.IndexOf(childNode);
                        if (indexNodeInTreeLevel != -1)
                        {
                            treeLevel.RemoveAt(indexNodeInTreeLevel);
                        }
                    }

                    if (parentNode == finishedNode.Value.Parent)
                    {
                        parentNode.Childs.Add(finishedNode.Value);
                    }
                    else
                    {
                        parentNode.Childs.Add(finishedNode.Value.Copy(parentNode));
                    }
                }
            }
        }
 public override bool Condition <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context, Func <IWantActionContext <TWantAction, TFactContainer>, IFactRuleCollection <TFactRule> > getCompatibleRules)
 {
     throw new NotImplementedException();
 }
        /// <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));
        }
Esempio n. 10
0
        /// <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));
        }
Esempio n. 11
0
        /// <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));
        }
        /// <inheritdoc/>
        public override async ValueTask <IFact> CalculateFactAsync <TFactRule, TWantAction, TFactContainer>(NodeByFactRule <TFactRule> node, IWantActionContext <TWantAction, TFactContainer> context)
        {
            var fact = await base.CalculateFactAsync(node, context).ConfigureAwait(false);

            WriteLog(() => $"FactFactory | FactRule\n{node.Info.Rule}\nInput facts: {string.Join(", ", GetRequireFacts(node.Info.Rule, context))}\nResult: {fact}");
            return(fact);
        }
        /// <inheritdoc/>
        public override IFact CalculateFact <TFactRule, TWantAction, TFactContainer>(NodeByFactRule <TFactRule> node, IWantActionContext <TWantAction, TFactContainer> context)
        {
            var fact = base.CalculateFact(node, context);

            WriteLog(() => $"FactFactory | FactRule\n{node.Info.Rule}\nInput facts: {string.Join(", ", GetRequireFacts(node.Info.Rule, context))}\nResult: {fact}");
            return(fact);
        }
 public override bool Condition <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context, Func <IWantActionContext <TWantAction, TFactContainer>, IFactRuleCollection <TFactRule> > getCompatibleRules)
 {
     return(context.Container.Contains <OtherFact>());
 }
Esempio n. 15
0
 /// <inheritdoc/>
 protected override IEnumerable <IFact> GetDefaultFacts(IWantActionContext <WantAction, VersionedFactContainer> context)
 {
     return(_provider.GetDefaultFacts());
 }
        /// <inheritdoc/>
        /// <remarks>Additionally checks version compatibility.</remarks>
        public override IFactRuleCollection <TFactRule> GetCompatibleRules <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork target, IFactRuleCollection <TFactRule> factRules, IWantActionContext <TWantAction, TFactContainer> context)
        {
            var result     = base.GetCompatibleRules(target, factRules, context);
            var maxVersion = context.WantAction.InputFactTypes.GetVersionFact(context);

            if (maxVersion == null)
            {
                return(result);
            }

            return(result.FindAll(rule => rule.CompatibleRule(maxVersion, context)));
        }
Esempio n. 17
0
        /// <summary>
        /// Compares rules by priority and base attribute
        /// (<see cref="SingleEntityOperationsFacade.CompareFactRules{TFactRule, TWantAction, TFactContainer}(TFactRule, TFactRule, IWantActionContext{TWantAction, TFactContainer})"/>).
        /// </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">Secon role.</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 override int CompareFactRules <TFactRule, TWantAction, TFactContainer>(TFactRule x, TFactRule y, IWantActionContext <TWantAction, TFactContainer> context)
        {
            int priorityResult = x.CompareByPriority(y, context);

            return(priorityResult != 0
                ? priorityResult
                : x.CompareTo(y));
        }
Esempio n. 18
0
 /// <summary>
 /// Checks if a <typeparamref name="TFact"/> fact cannot be retrieved from a container.
 /// </summary>
 /// <inheritdoc/>
 public override bool Condition <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context, Func <IWantActionContext <TWantAction, TFactContainer>, IFactRuleCollection <TFactRule> > getCompatibleRules)
 {
     return(!context.SingleEntity.CanExtractFact(GetFactType <TFact>(), factWork, context));
 }
Esempio n. 19
0
        /// <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));
        }
Esempio n. 20
0
 protected override IEnumerable <IFact> GetDefaultFacts(IWantActionContext <GetcuReone.FactFactory.Entities.WantAction, Container> context)
 {
     return(DefaultFacts);
 }
Esempio n. 21
0
 /// <inheritdoc/>
 protected override IEnumerable <IFact> GetDefaultFacts(IWantActionContext <WantAction, FactContainer> context)
 {
     return(_getDefaultFactsFunc?.Invoke(context));
 }
        internal static IVersionFact GetVersionFact <TWantAction, TFactContainer>(this IEnumerable <IFactType> factTypes, IWantActionContext <TWantAction, TFactContainer> context)
            where TWantAction : IWantAction
            where TFactContainer : IFactContainer
        {
            IFactType versionType = factTypes.SingleOrDefault(type => type.IsFactType <IVersionFact>());

            return(versionType != null
                ? (IVersionFact)context.Container.First(fact => context.Cache.GetFactType(fact).EqualsFactType(versionType))
                : null);
        }
Esempio n. 23
0
        internal static IPriorityFact GetPriorityFact <TFactWork, TWantAction, TFactContainer>(this TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context)
            where TFactWork : IFactWork
            where TWantAction : IWantAction
            where TFactContainer : IFactContainer
        {
            var priorityType = factWork.InputFactTypes?.FirstOrDefault(type => type.IsFactType <IPriorityFact>());

            return(priorityType != null
                ? context.Container.FirstPriorityFactByFactType(priorityType, context.Cache)
                : null);
        }
        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);
        }
Esempio n. 25
0
 /// <summary>
 /// Checks if a tree can be built for the fact.
 /// </summary>
 /// <inheritdoc/>
 public override bool Condition <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context, Func <IWantActionContext <TWantAction, TFactContainer>, IFactRuleCollection <TFactRule> > getCompatibleRules)
 {
     return(ConditionHelper.CanDeriveFact(
                this,
                GetFactType <TFact>(),
                factWork,
                getCompatibleRules(context),
                context));
 }
        /// <inheritdoc/>
        /// <remarks>Additionally checks version compatibility.</remarks>
        public override int CompareFactRules <TFactRule, TWantAction, TFactContainer>(TFactRule x, TFactRule y, IWantActionContext <TWantAction, TFactContainer> context)
        {
            int resultByPriority = x.CompareByPriority(y, context);

            if (resultByPriority != 0)
            {
                return(resultByPriority);
            }

            int resultByVersion = x.CompareByVersion(y, context);

            if (resultByVersion != 0)
            {
                return(resultByVersion);
            }

            return(x.CompareTo(y));
        }
 /// <inheritdoc/>
 public abstract bool Condition <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork factWork, IWantActionContext <TWantAction, TFactContainer> context, Func <IWantActionContext <TWantAction, TFactContainer>, IFactRuleCollection <TFactRule> > getCompatibleRules)
     where TFactWork : IFactWork
     where TFactRule : IFactRule
     where TWantAction : IWantAction
     where TFactContainer : IFactContainer;
 internal static IFactRuleCollection <TFactRule> GetCompatibleRulesEx <TFactWork, TFactRule, TWantAction, TFactContainer>(this TFactWork target, IFactRuleCollection <TFactRule> rules, IWantActionContext <TWantAction, TFactContainer> context)
     where TFactWork : IFactWork
     where TFactRule : IFactRule
     where TWantAction : IWantAction
     where TFactContainer : IFactContainer
 {
     return(context.SingleEntity.GetCompatibleRules(target, rules, context));
 }
 internal static Dictionary <NodeByFactRuleInfo <TFactRule>, NodeByFactRule <TFactRule> > GetCompatibleFinishedNodes <TFactRule, TWantAction, TFactContainer>(this NodeByFactRuleInfo <TFactRule> nodeInfo, Dictionary <NodeByFactRuleInfo <TFactRule>, NodeByFactRule <TFactRule> > finishedNodes, IWantActionContext <TWantAction, TFactContainer> context)
     where TFactRule : IFactRule
     where TWantAction : IWantAction
     where TFactContainer : IFactContainer
 {
     return(finishedNodes
            .Where(finishedNode => context.SingleEntity.CompatibleRule(nodeInfo.Rule, finishedNode.Key.Rule, context))
            .ToDictionary(finishedNode => finishedNode.Key, finishedNode => finishedNode.Value));
 }
        /// <inheritdoc/>
        /// <remarks>Additionally checks version compatibility.</remarks>
        public override bool CompatibleRule <TFactWork, TFactRule, TWantAction, TFactContainer>(TFactWork target, TFactRule rule, IWantActionContext <TWantAction, TFactContainer> context)
        {
            if (!base.CompatibleRule(target, rule, context))
            {
                return(false);
            }

            var maxVersion = context.WantAction.InputFactTypes.GetVersionFact(context);

            if (maxVersion == null)
            {
                return(true);
            }

            var ruleVersion = rule.InputFactTypes.GetVersionFact(context);

            return(ruleVersion != null
                ? maxVersion.CompareTo(ruleVersion) >= 0
                : false);
        }