Beispiel #1
0
        /// <summary>
        /// Is it possible to start a <see cref="IFactWork"/>.
        /// </summary>
        /// <typeparam name="TFactWork"></typeparam>
        /// <param name="inputFacts">Input facts.</param>
        /// <param name="factWork"></param>
        /// <param name="cache"></param>
        /// <returns></returns>
        public virtual bool CanInvokeWork <TFactWork>(IEnumerable <IFact> inputFacts, TFactWork factWork, IFactTypeCache cache)
            where TFactWork : IFactWork
        {
            foreach (IFactType requiredFactType in factWork.InputFactTypes)
            {
                if (inputFacts.All(inputFact => !cache.GetFactType(inputFact).EqualsFactType(requiredFactType)))
                {
                    return(false);
                }
            }

            return(true);
        }
 /// <summary>
 /// The first fact of the same type.
 /// </summary>
 /// <typeparam name="TFact"></typeparam>
 /// <param name="facts">Fact list.</param>
 /// <param name="factType">Fact type.</param>
 /// <param name="cache">Cache.</param>
 /// <returns>Fact or null.</returns>
 public static TFact FirstFactByFactType <TFact>(this IEnumerable <TFact> facts, IFactType factType, IFactTypeCache cache)
     where TFact : IFact
 {
     return(facts.FirstOrDefault(fact => cache.GetFactType(fact).EqualsFactType(factType)));
 }
        /// <summary>
        /// Checking the equality of facts.
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="cache"></param>
        /// <param name="includeFactParams">
        /// True - parameters of facts will be compared using the <see cref="EqualsFactParameters(IFactParameter, IFactParameter)"/> method.
        /// False - Parameters of facts will be compared using the method.
        /// </param>
        /// <returns></returns>
        public static bool EqualsFacts(IFact first, IFact second, IFactTypeCache cache = null, bool includeFactParams = true)
        {
            if (first == null)
            {
                return(second == null);
            }
            else if (second == null)
            {
                return(false);
            }
            else if (first == second)
            {
                return(true);
            }

            if (first is ISpecialFact xSpecialFact && second is ISpecialFact ySpecialFact)
            {
                return(xSpecialFact.EqualsInfo(ySpecialFact));
            }

            IFactType firstFactType;
            IFactType secondFactType;

            if (cache != null)
            {
                firstFactType  = cache.GetFactType(first);
                secondFactType = cache.GetFactType(second);
            }
            else
            {
                firstFactType  = first.GetFactType();
                secondFactType = second.GetFactType();
            }

            if (!firstFactType.EqualsFactType(secondFactType))
            {
                return(false);
            }
            if (!includeFactParams)
            {
                return(true);
            }

            IReadOnlyCollection <IFactParameter> firstParameters  = first.GetParameters();
            IReadOnlyCollection <IFactParameter> secondParameters = second.GetParameters();

            if (firstParameters.IsNullOrEmpty() && secondParameters.IsNullOrEmpty())
            {
                return(true);
            }
            if (firstParameters.IsNullOrEmpty() || secondParameters.IsNullOrEmpty())
            {
                return(false);
            }
            if (firstParameters.Count != secondParameters.Count)
            {
                return(false);
            }

            foreach (IFactParameter xParameter in firstParameters)
            {
                bool found = false;

                foreach (IFactParameter yParameter in secondParameters)
                {
                    if (EqualsFactParameters(xParameter, yParameter))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }

            return(true);
        }