Esempio n. 1
0
        public void CannotDerivedOneFactFromOne2TestCase()
        {
            IFactType wantFact          = GetFactType <Input6Fact>();
            string    expectedReason    = $"Failed to derive one or more facts for the action ({wantFact.FactName}).";
            var       expectedNeedFacts = new List <IFactType>
            {
                GetFactType <Input3Fact>(),
                GetFactType <Input5Fact>(),
            };

            GivenCreateFactFactory()
            .AndAddRules(RuleCollectionHelper.GetRulesForNotAvailableInput6Fact())
            .And("Want fact.", factory =>
                 factory.WantFacts((Input6Fact fact) => { }))
            .When("Derive facts.", factory =>
                  ExpectedDeriveException(() => factory.Derive()))
            .ThenAssertErrorDetail(ErrorCode.FactCannotDerived, expectedReason)
            .And("Check error", error =>
            {
                DeriveErrorDetail detail = error.Details.First();
                Assert.AreEqual(1, detail.RequiredFacts.Count, "A different amount of required facts was expected.");

                DeriveFactErrorDetail factDetail = detail.RequiredFacts.First();
                Assert.IsTrue(wantFact.EqualsFactType(factDetail.RequiredFact), "They expected another fact to be required.");

                Assert.AreEqual(expectedNeedFacts.Count, factDetail.NeedFacts.Count, "Another number of missing facts expected.");
                List <IFactType> needFacts = factDetail.NeedFacts.ToList();

                for (int i = 0; i < expectedNeedFacts.Count; i++)
                {
                    Assert.IsTrue(expectedNeedFacts[i].EqualsFactType(needFacts[i]), "Another missing fact was expected.");
                }
            })
            .Run();
        }
Esempio n. 2
0
        public void TwoFactsInOneActionCannotDeriveTestCase()
        {
            List <IFactType> expectedRequiredFacts = new List <IFactType>
            {
                GetFactType <Input1Fact>(),
                GetFactType <Input2Fact>(),
            };
            string expectedReason = $"Failed to derive one or more facts for the action ({string.Join(", ", expectedRequiredFacts.ConvertAll(f => f.FactName))}).";

            var setNeedFacts = new List <List <IFactType> >
            {
                new List <IFactType>
                {
                    GetFactType <Input3Fact>(),
                },
                new List <IFactType>
                {
                    GetFactType <Input4Fact>(),
                },
            };

            GivenCreateFactFactory()
            .AndAddRules(new Collection
            {
                (Input3Fact fact) => new Input1Fact(fact.Value),
                (Input4Fact fact) => new Input2Fact(fact.Value)
            })
            .And("Want fact1.", factory =>
                 factory.WantFacts((Input1Fact fact1, Input2Fact fact2) => { }))
            .When("Derive facts.", factory =>
                  ExpectedDeriveException(() => factory.Derive()))
            .ThenAssertErrorDetail(ErrorCode.FactCannotDerived, expectedReason)
            .And("Check error detail 1.", error =>
            {
                DeriveErrorDetail detail = error.Details.First();
                Assert.AreEqual(expectedRequiredFacts.Count, detail.RequiredFacts.Count, "A different amount of required facts was expected.");
                List <DeriveFactErrorDetail> factDetails = detail.RequiredFacts.ToList();

                for (int i = 0; i < expectedRequiredFacts.Count; i++)
                {
                    DeriveFactErrorDetail factDetail = factDetails[i];
                    Assert.IsTrue(expectedRequiredFacts[i].EqualsFactType(factDetail.RequiredFact), "They expected another fact to be required.");

                    List <IFactType> expectedNeedFacts = setNeedFacts[i];
                    List <IFactType> needFacts         = factDetail.NeedFacts.ToList();

                    for (int j = 0; i < expectedNeedFacts.Count; i++)
                    {
                        Assert.IsTrue(expectedNeedFacts[j].EqualsFactType(needFacts[j]), "Another missing fact was expected.");
                    }
                }
            })
            .Run();
        }
Esempio n. 3
0
        internal static bool CanDeriveFact <TFactWork, TFactRule, TWantAction, TFactContainer>(IRuntimeConditionFact conditionFact, IFactType searchFactType, TFactWork factWork, IFactRulesContext <TFactRule, TWantAction, TFactContainer> context)
            where TFactWork : IFactWork
            where TFactRule : IFactRule
            where TWantAction : IWantAction
            where TFactContainer : IFactContainer
        {
            if (context.SingleEntity.CanExtractFact(searchFactType, factWork, context))
            {
                return(true);
            }

            var rulesWithoutConditionFact = context.FactRules
                                            .FindAll(rule => rule.InputFactTypes
                                                     .All(factType => !factType.EqualsFactType(context.Cache.GetFactType(conditionFact))));

            var request = new BuildTreeForFactInfoRequest <TFactRule, TWantAction, TFactContainer>
            {
                WantFactType = searchFactType,
                Context      = new FactRulesContext <TFactRule, TWantAction, TFactContainer>
                {
                    Cache        = context.Cache,
                    Container    = context.Container,
                    FactRules    = rulesWithoutConditionFact,
                    SingleEntity = context.SingleEntity,
                    TreeBuilding = context.TreeBuilding,
                    WantAction   = context.WantAction,
                    Engine       = context.Engine,
                },
            };

            try
            {
                return(context.TreeBuilding.TryBuildTreeForFactInfo(request, out var _, out var _));
            }
            catch (InvalidDeriveOperationException ex)
            {
                if (ex.Details != null && ex.Details.Count == 1)
                {
                    DeriveErrorDetail detail = ex.Details.First();

                    if (detail.Code == ErrorCode.RuleNotFound || detail.Code == ErrorCode.EmptyRuleCollection)
                    {
                        return(false);
                    }
                }

                throw;
            }
        }