コード例 #1
0
        public void Fire_MatchingNotPatternFactsTwoInsertedThenSecondUpdated_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);

            fact3.TestProperty = "Invalid Value 3";
            Session.Update(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
コード例 #2
0
        public void Fire_MatchingMainFactAndBothPartsOfOrGroup_FiresTwice()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType2 {
                TestProperty = "Invalid Value 3", JoinProperty = fact1.TestProperty
            };
            var fact4 = new FactType3 {
                TestProperty = "Valid Value 4", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);
            Session.Insert(fact4);

            //Act
            Session.Fire();

            //Assert
            AssertFiredTwice();
        }
コード例 #3
0
        public void Fire_MatchingNotPatternFactsInsertedThenRetractSomeOfThemButNotAll_StillDoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };

            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType2 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };
            var fact4 = new FactType2 {
                TestProperty = "Valid Value 4", JoinProperty = fact1.TestProperty
            };

            var fact5 = new FactType3 {
                TestProperty = "Valid Value 5", JoinProperty = fact1.TestProperty
            };
            var fact6 = new FactType3 {
                TestProperty = "Valid Value 6", JoinProperty = fact1.TestProperty
            };

            var allFacts = new object[] { fact1, fact2, fact3, fact4, fact5, fact6 };

            Session.InsertAll(allFacts);
            Session.RetractAll(new object[] { fact3, fact4, fact5 });

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
コード例 #4
0
        public void Fire_InvalidFactsInsertedThenUpdated_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Invalid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Invalid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);

            fact2.TestProperty = "Valid Value 2";
            Session.Update(fact2);

            fact3.TestProperty = "Valid Value 3";
            Session.Update(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
コード例 #5
0
        public void Fire_MatchingFactsInsertedThenSomeRetractedRetracted_StillFires()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };
            var fact4 = new FactType3 {
                TestProperty = "Valid Value 4", JoinProperty = fact1.TestProperty
            };

            var facts = new object[] { fact1, fact2, fact3, fact4 };

            Session.InsertAll(facts);
            Session.Retract(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
コード例 #6
0
        public void Fire_MatchingFactsInsertedThenRetracted_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);
            Session.Retract(fact2);
            Session.Retract(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
コード例 #7
0
        public void Fire_TwoMatchingFactsOfOneKindAndJoinedGroups_FiresTwice()
        {
            //Arrange
            var fact11 = new FactType1 {
                TestProperty = "Valid Value 11", JoinProperty = "Group 1"
            };
            var fact12 = new FactType1 {
                TestProperty = "Valid Value 12", JoinProperty = "Group 2"
            };
            var fact21 = new FactType2(1)
            {
                TestProperty = "Value 1"
            };
            var fact31 = new FactType3()
            {
                TestProperty = "Valid Value 31", GroupProperty = "Group 1"
            };
            var fact32 = new FactType3()
            {
                TestProperty = "Valid Value 32", GroupProperty = "Group 2"
            };

            Session.Insert(fact11);
            Session.Insert(fact12);
            Session.Insert(fact21);
            Session.Insert(fact31);
            Session.Insert(fact32);

            //Act
            Session.Fire();

            //Assert
            AssertFiredTwice();
            Assert.AreEqual(2, fact11.EvalCount);
        }
コード例 #8
0
            public override void Define()
            {
                FactType0 f0 = null;
                FactType1 f1 = null;
                FactType2 f2 = null;
                FactType3 f3 = null;

                When()
                .Or(x => x
                    .And(xx => xx
                         .Match(() => f0)
                         .Match(() => f1)
                         .Match(() => f2)
                         .Having(() => Condition(f0, f1, f2))
                         .Having(() => Condition(f2, f3, f0)))
                    .And(xx => xx
                         .Match(() => f3)
                         .Match(() => f0)
                         .Having(() => Condition(f0, f3))
                         .Having(() => Condition(f3, f0)))
                    );

                Then()
                .Do(ctx => Action(f2, f0, f3));
            }
コード例 #9
0
        public void Fire_MatchingSetFactOfSecondKindUpdated_FiresOnceThenFiresOnceAgain()
        {
            //Arrange
            var fact11 = new FactType1 {
                TestProperty = "Valid Value 11", JoinProperty = "Group 1"
            };
            var fact21 = new FactType2(1)
            {
                TestProperty = "Value 1"
            };
            var fact31 = new FactType3()
            {
                TestProperty = "Valid Value 31", GroupProperty = "Group 1"
            };

            Session.Insert(fact11);
            Session.Insert(fact21);
            Session.Insert(fact31);

            //Act - 1
            Session.Fire();

            //Assert - 1
            AssertFiredOnce();
            Assert.AreEqual(1, fact11.EvalCount);

            //Act - 2
            Session.Update(fact21);
            Session.Fire();

            //Assert - 2
            AssertFiredTwice();
            Assert.AreEqual(2, fact11.EvalCount);
        }
コード例 #10
0
        public void Fire_OneMatchingFactInsertedThenUpdated_FiresFirstRuleAndChainsSecondLinkedFactsUpdated()
        {
            //Arrange
            FactType2 matchedFact2 = null;
            FactType3 matchedFact3 = null;

            Session.Events.FactInsertedEvent += (sender, args) =>
            {
                if (args.Fact.Type == typeof(FactType2))
                {
                    matchedFact2 = (FactType2)args.Fact.Value;
                }
                if (args.Fact.Type == typeof(FactType3))
                {
                    matchedFact3 = (FactType3)args.Fact.Value;
                }
            };
            Session.Events.FactUpdatedEvent += (sender, args) =>
            {
                if (args.Fact.Type == typeof(FactType2))
                {
                    matchedFact2 = (FactType2)args.Fact.Value;
                }
                if (args.Fact.Type == typeof(FactType3))
                {
                    matchedFact3 = (FactType3)args.Fact.Value;
                }
            };

            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1", ChainProperty = "Valid Value 1"
            };

            Session.Insert(fact1);

            //Act - I
            Session.Fire();

            //Assert - I
            AssertFiredOnce <ForwardChainingFirstRule>();
            AssertFiredOnce <ForwardChainingSecondRule>();
            Assert.Equal(1, matchedFact2.UpdateCount);
            Assert.Equal("Valid Value 1", matchedFact2.TestProperty);
            Assert.Equal("Valid Value 1", matchedFact3.TestProperty);

            //Act - II
            fact1.ChainProperty = "Valid Value 2";
            Session.Update(fact1);
            Session.Fire();

            //Assert - II
            AssertFiredTwice <ForwardChainingFirstRule>();
            AssertFiredTwice <ForwardChainingSecondRule>();
            Assert.Equal(2, matchedFact2.UpdateCount);
            Assert.Equal("Valid Value 2", matchedFact2.TestProperty);
            Assert.Equal("Valid Value 2", matchedFact3.TestProperty);
        }
コード例 #11
0
        public override void Define()
        {
            FactType3 fact3 = null;

            When()
            .Match <FactType3>(() => fact3, f => f.TestProperty.StartsWith("Valid"));
            Then()
            .Do(ctx => Action());
        }
コード例 #12
0
        public void Fire_MatchGroup2_FiresOnce()
        {
            //Arrange
            var f0 = new FactType0();
            var f3 = new FactType3();

            Session.InsertAll(new object[] { f0, f3 });

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
コード例 #13
0
            public override void Define()
            {
                FactType1 fact1 = null;
                FactType2 fact2 = null;
                FactType3 fact3 = null;

                When()
                .Or(x => x
                    .Match <FactType1>(() => fact1, f => f.TestProperty.StartsWith("Valid"))
                    .Or(xx => xx
                        .Match <FactType2>(() => fact2, f => f.TestProperty.StartsWith("Valid"))
                        .Match <FactType3>(() => fact3, f => f.TestProperty.StartsWith("Valid"))));

                Then()
                .Do(ctx => ctx.NoOp());
            }
コード例 #14
0
            public override void Define()
            {
                FactType1 fact1 = null;
                FactType2 fact2 = null;
                FactType3 fact3 = null;

                When()
                .Match <FactType1>(() => fact1, f => f.TestProperty.StartsWith("Valid"))
                .Or(x => x
                    .Match <FactType2>(() => fact2, f => f.TestProperty.StartsWith("Valid"), f => f.JoinProperty == fact1.TestProperty)
                    .And(xx => xx
                         .Match <FactType2>(() => fact2, f => f.TestProperty.StartsWith("Invalid"), f => f.JoinProperty == fact1.TestProperty)
                         .Match <FactType3>(() => fact3, f => f.TestProperty.StartsWith("Valid"), f => f.JoinProperty == fact1.TestProperty)));

                Then()
                .Do(ctx => Action(ctx));
            }
コード例 #15
0
            public override void Define()
            {
                IGrouping <string, FactType1> group  = default;
                IEnumerable <FactType2>       facts2 = default;
                FactType3 fact3 = default;

                When()
                .Query(() => group, x => x
                       .Match <FactType1>()
                       .GroupBy(f => f.Key))
                .Query(() => facts2, q => q
                       .Match <FactType2>(f => f.Join == group.First().Join)
                       .Collect())
                .Match <FactType3>(() => fact3, f => f.Join == group.First().Join);

                Then()
                .Do(ctx => ctx.NoOp());
            }
コード例 #16
0
        public void Fire_MatchingNotPatternFactsSecondKind_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
コード例 #17
0
        public void Fire_OneMatchingFactOfOneKindAndOneOfAnotherKind_FiresSecondRuleDirectlyAndChained()
        {
            //Arrange
            var fact1 = new FactType2 {
                TestProperty = "Valid Value 1", JoinProperty = "Valid Value 1"
            };
            var fact2 = new FactType3 {
                TestProperty = "Valid Value 2"
            };

            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce <ForwardChainingFirstRule>();
            AssertFiredTwice <ForwardChainingSecondRule>();
        }
コード例 #18
0
        public void Fire_AllPatternsMatch_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {
                Key = "key1", Join = "join1"
            };
            var fact2 = new FactType2 {
                Join = "join1"
            };
            var fact3 = new FactType3 {
                Join = "join1"
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
コード例 #19
0
        public void Fire_MatchingFacts_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
コード例 #20
0
        public void Fire_MatchingFactsSecondPart_FactsInContext()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Invalid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);

            IFactMatch[] matches = null;
            GetRuleInstance <TestRule>().Action = ctx =>
            {
                matches = ctx.Match.Facts.ToArray();
            };

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.Equal(3, matches.Length);
            Assert.Equal("fact1", matches[0].Declaration.Name);
            Assert.Same(fact1, matches[0].Value);
            Assert.Equal("fact2", matches[1].Declaration.Name);
            Assert.Same(fact2, matches[1].Value);
            Assert.Equal("fact3", matches[2].Declaration.Name);
            Assert.Same(fact3, matches[2].Value);
        }
コード例 #21
0
        public void Fire_AllPatternsMatchThenThirdFactRetracted_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {
                Key = "key1", Join = "join1"
            };
            var fact2 = new FactType2 {
                Join = "join1"
            };
            var fact3 = new FactType3 {
                Join = "join1"
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);

            //Act
            Session.Retract(fact3);
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
コード例 #22
0
        public void Fire_MatchingNotPatternFactsInsertedThenRetracted_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {
                TestProperty = "Valid Value 1"
            };
            var fact2 = new FactType2 {
                TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty
            };
            var fact3 = new FactType3 {
                TestProperty = "Valid Value 3", JoinProperty = fact1.TestProperty
            };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);
            Session.RetractAll(new object[] { fact2, fact3 });

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
コード例 #23
0
 private bool Condition(FactType0 f0, FactType3 f3)
 {
     return(true);
 }
コード例 #24
0
 private void Action(FactType2 f2, FactType0 f0, FactType3 f3)
 {
 }
コード例 #25
0
 private bool Condition(FactType2 f2, FactType3 f3, FactType0 f0)
 {
     return(true);
 }