public void TestAddDeleteContains()
        {
            IBlackboard blackboard = new Blackboard();
            TestUnit1   u1         = new TestUnit1("one");
            TestUnit1   u2         = new TestUnit1("two");
            TestUnit1   u3         = new TestUnit1("three");

            blackboard.AddUnit(u1);
            blackboard.AddUnit(u2);
            blackboard.AddUnit(u3);
            Assert.True(blackboard.ContainsUnit(u1));
            Assert.True(blackboard.ContainsUnit(u2));
            Assert.True(blackboard.ContainsUnit(u3));
            blackboard.DeleteUnit(u3);
            Assert.False(blackboard.ContainsUnit(u3));
            Assert.True(blackboard.ContainsUnit(u2));
            Assert.True(blackboard.ContainsUnit(u1));
            blackboard.DeleteUnit(u2);
            Assert.False(blackboard.ContainsUnit(u3));
            Assert.False(blackboard.ContainsUnit(u2));
            Assert.True(blackboard.ContainsUnit(u1));
            blackboard.DeleteUnit(u1);
            Assert.False(blackboard.ContainsUnit(u3));
            Assert.False(blackboard.ContainsUnit(u2));
            Assert.False(blackboard.ContainsUnit(u1));
        }
        public void TestLookupCount()
        {
            IBlackboard blackboard = new Blackboard();

            TestUnit1 u1 = new TestUnit1("one");
            TestUnit1 u2 = new TestUnit1("two");
            TestUnit1 u3 = new TestUnit1("three");

            TestUnit2 u4 = new TestUnit2(1);
            TestUnit2 u5 = new TestUnit2(2);

            string type1 = u1.GetType().FullName;
            string type2 = u4.GetType().FullName;

            blackboard.AddUnit(u1);
            blackboard.AddUnit(u2);
            blackboard.AddUnit(u3);
            blackboard.AddUnit(u4);
            blackboard.AddUnit(u5);

            ISet <IUnit> set1 = blackboard.LookupUnits(type1);
            ISet <IUnit> set2 = blackboard.LookupUnits(type2);

            Assert.Equal(3, set1.Count);
            Assert.Equal(2, set2.Count);
        }
Ejemplo n.º 3
0
        public void TestAddDeleteContains()
        {
            IBlackboard blackboard = new Blackboard();
            Unit        u1         = new Unit();

            u1.AddComponent(new KC_UnitID("one", true));

            Unit u2 = new Unit();

            u2.AddComponent(new KC_UnitID("two", true));

            Unit u3 = new Unit();

            u3.AddComponent(new KC_UnitID("three", true));

            blackboard.AddUnit(u1);
            blackboard.AddUnit(u2);
            blackboard.AddUnit(u3);
            Assert.True(blackboard.ContainsUnit(u1));
            Assert.True(blackboard.ContainsUnit(u2));
            Assert.True(blackboard.ContainsUnit(u3));
            blackboard.RemoveUnit(u3);
            Assert.False(blackboard.ContainsUnit(u3));
            Assert.True(blackboard.ContainsUnit(u2));
            Assert.True(blackboard.ContainsUnit(u1));
            blackboard.RemoveUnit(u2);
            Assert.False(blackboard.ContainsUnit(u3));
            Assert.False(blackboard.ContainsUnit(u2));
            Assert.True(blackboard.ContainsUnit(u1));
            blackboard.RemoveUnit(u1);
            Assert.False(blackboard.ContainsUnit(u3));
            Assert.False(blackboard.ContainsUnit(u2));
            Assert.False(blackboard.ContainsUnit(u1));
        }
        public void TestExecute_KS_IDSelector_SelectedUnit()
        {
            IBlackboard blackboard           = new Blackboard();
            KS_Old_ReactiveIDSelector ks     = new KS_Old_ReactiveIDSelector(blackboard);
            List <U_IDSelectRequest>  kuList = new List <U_IDSelectRequest>
            {
                new U_IDSelectRequest("foo"),
            };

            foreach (IUnit u in kuList)
            {
                blackboard.AddUnit(u);
            }

            List <ContentUnit> cuList = new List <ContentUnit>
            {
                new ContentUnit(),
                new ContentUnit(),
                new ContentUnit()
            };

            cuList[0].Metadata[ContentUnitID] = "foo";
            cuList[1].Metadata[ContentUnitID] = "bar";
            cuList[2].Metadata[ContentUnitID] = "baz";

            foreach (IUnit u in cuList)
            {
                blackboard.AddUnit(u);
            }

            var KSAs  = ks.Precondition();
            int count = KSAs.Count();

            Assert.Equal(1, count);
            KSAs.ElementAt(0).Execute();

            // Four content units total (the original three plus a new selected one)
            ISet <ContentUnit> cuSet = blackboard.LookupUnits <ContentUnit>();

            Assert.Equal(4, cuSet.Count);

            // Query for selected content units
            var selectedList = from cu in blackboard.LookupUnits <ContentUnit>()
                               where cu.HasMetadataSlot(SelectedContentUnit)
                               select cu;

            // One content unit has been selected.
            int size = selectedList.Count();

            Assert.Equal(1, size);

            // The right content unit has been selected.
            Assert.Equal("foo", selectedList.ElementAt(0).Metadata[ContentUnitID]);

            // The query has been deleted.
            ISet <U_IDSelectRequest> querySet = blackboard.LookupUnits <U_IDSelectRequest>();

            Assert.Equal(0, querySet.Count);
        }
        public void TestExecute_KS_IDSelector_NoSelectedUnit()
        {
            IBlackboard blackboard           = new Blackboard();
            KS_Old_ReactiveIDSelector ks     = new KS_Old_ReactiveIDSelector(blackboard);
            List <U_IDSelectRequest>  kuList = new List <U_IDSelectRequest>
            {
                new U_IDSelectRequest("qux"),
            };

            foreach (IUnit u in kuList)
            {
                blackboard.AddUnit(u);
            }

            List <ContentUnit> cuList = new List <ContentUnit>
            {
                new ContentUnit(),
                new ContentUnit(),
                new ContentUnit()
            };

            cuList[0].Metadata[ContentUnitID] = "foo";
            cuList[1].Metadata[ContentUnitID] = "bar";
            cuList[2].Metadata[ContentUnitID] = "baz";

            foreach (IUnit u in cuList)
            {
                blackboard.AddUnit(u);
            }

            var KSAs  = ks.Precondition();
            int count = KSAs.Count();

            Assert.Equal(1, count);
            KSAs.ElementAt(0).Execute();

            // Three content units total (no selected unit)
            ISet <ContentUnit> cuSet = blackboard.LookupUnits <ContentUnit>();

            Assert.Equal(3, cuSet.Count);

            // Query for selected content units
            var selectedList = from cu in blackboard.LookupUnits <ContentUnit>()
                               where cu.HasMetadataSlot(SelectedContentUnit)
                               select cu;

            // No content unit selected (since "qux" matches no ID.
            int size = selectedList.Count();

            Assert.Equal(0, size);

            // The query has been deleted.
            ISet <U_IDSelectRequest> querySet = blackboard.LookupUnits <U_IDSelectRequest>();

            Assert.Equal(0, querySet.Count);
        }
        public void TestLookupNotNull()
        {
            IBlackboard blackboard = new Blackboard();

            TestUnit1 u1    = new TestUnit1("one");
            TestUnit2 u2    = new TestUnit2(1);
            string    type1 = u1.GetType().FullName;
            string    type2 = u2.GetType().FullName;

            blackboard.AddUnit(u1);
            blackboard.AddUnit(u2);

            Assert.NotNull(blackboard.LookupUnits(type1));
            Assert.NotNull(blackboard.LookupUnits(type2));
        }
        /*public*/
        void TestExecute_PriorityController()
        {
            PriorityController_PublicMethods controller = new PriorityController_PublicMethods();
            IBlackboard blackboard        = new Blackboard();
            KS_Old_ReactiveIDSelector ks1 = new KS_Old_ReactiveIDSelector(blackboard);

            ks1.Properties[Priority] = 10;
            controller.AddKnowledgeSource(ks1);
            blackboard.AddUnit(new U_IDSelectRequest("foo"));

            List <ContentUnit> cuList = new List <ContentUnit>
            {
                new ContentUnit(),
                new ContentUnit(),
                new ContentUnit()
            };

            cuList[0].Metadata[ContentUnitID] = "foo";
            cuList[1].Metadata[ContentUnitID] = "bar";
            cuList[2].Metadata[ContentUnitID] = "baz";

            foreach (IUnit u in cuList)
            {
                blackboard.AddUnit(u);
            }

            controller.Execute();

            // Four content units total (the original three plus a new selected one)
            ISet <ContentUnit> cuSet = blackboard.LookupUnits <ContentUnit>();

            Assert.Equal(4, cuSet.Count);

            var selectedUnit = from unit in cuSet
                               where unit.HasMetadataSlot(SelectedContentUnit)
                               select unit;

            // Exactly 1 selected content unit
            int count = selectedUnit.Count();

            Assert.Equal(1, count);

            // The selected content unit is "foo" (matches the query request)
            Assert.True(selectedUnit.ElementAt(0).Metadata[ContentUnitID].Equals("foo"));

            // Since we only added one KS to controller, and there was only one matching blackboard pattern, after execution the agenda should be empty
            Assert.Empty(controller.Agenda);
        }
Ejemplo n.º 8
0
        // Deleting and adding elements to a returned set shouldn't change the set in the dictionary
        public void TestManipulatingSet()
        {
            IBlackboard blackboard = new Blackboard();

            Unit u1 = new Unit();

            u1.AddComponent(new KC_UnitID("one", true));

            Unit u2 = new Unit();

            u2.AddComponent(new KC_UnitID("two", true));

            blackboard.AddUnit(u1);
            ISet <Unit> set1 = blackboard.LookupUnits <Unit>();

            Assert.Equal(1, set1.Count);

            set1.Add(u2);
            ISet <Unit> set2 = blackboard.LookupUnits <Unit>();

            Assert.Equal(1, set2.Count);

            set2.Remove(u1);
            ISet <Unit> set3 = blackboard.LookupUnits <Unit>();

            Assert.Equal(1, set3.Count);
        }
        public void TestExecute_ScheduledSequenceController()
        {
            string id    = "id1";
            string pool1 = "pool1";
            string pool2 = "pool2";

            IBlackboard blackboard                 = new Blackboard();
            IScheduledKnowledgeSource   ks1        = new KS_ScheduledFilterSelector(blackboard, inputPool: null, outputPool: pool1, filter: (Unit u) => u.HasComponent <KC_UnitID>());
            IScheduledKnowledgeSource   ks2        = new KS_ScheduledIDSelector(blackboard, inputPool: pool1, outputPool: pool2);
            ScheduledSequenceController controller = new ScheduledSequenceController();

            controller.AddKnowledgeSource(ks1);
            controller.AddKnowledgeSource(ks2);

            Unit unit = new Unit();

            unit.AddComponent(new KC_UnitID(id));
            blackboard.AddUnit(unit);

            Unit req = new Unit();

            req.AddComponent(new KC_IDSelectionRequest(id));
            blackboard.AddUnit(req);
            req.SetActiveRequest(true);

            controller.Execute();

            var pool1Units = from u in blackboard.LookupUnits <Unit>()
                             where u.HasComponent <KC_ContentPool>()
                             where u.ContentPoolEquals(pool1)
                             select u;

            var pool2Units = from u in blackboard.LookupUnits <Unit>()
                             where u.HasComponent <KC_ContentPool>()
                             where u.ContentPoolEquals(pool2)
                             select u;

            int pool1Count = pool1Units.Count();
            int pool2Count = pool2Units.Count();

            Assert.Equal(1, pool1Count);
            Assert.Equal(1, pool2Count);

            Assert.True(pool1Units.First().UnitIDEquals(id));
            Assert.True(pool2Units.First().UnitIDEquals(id));
        }
Ejemplo n.º 10
0
        public void TestLookupNotNull()
        {
            IBlackboard blackboard = new Blackboard();

            Unit u1 = new Unit();

            u1.AddComponent(new KC_UnitID("one", true));

            blackboard.AddUnit(u1);

            Assert.NotNull(blackboard.LookupUnits <Unit>());
        }
        public Demo2()
        {
            Blackboard = new Blackboard();
            Demo2_DefineUnits(Blackboard);

            m_IDSelector = new KS_ScheduledIDSelector(Blackboard);

            m_prologEval = new KS_ScheduledPrologEval(Blackboard,
                                                      inputPool: m_IDSelector.OutputPool,
                                                      prologExpName: ApplTest_Prolog);

            /*
             * fixme: consider creating a filtered by boolean result KS or a more general filter by KC_EvaluatedExpression (once I have more EvaluatedExpressions than Prolog).
             */
            m_filterByPrologResult = new KS_ScheduledFilterSelector(Blackboard,
                                                                    inputPool: m_prologEval.OutputPool,
                                                                    outputPool: FilteredPrologResultPool,
                                                                    filter: KS_ScheduledPrologEval.FilterByPrologResult(ApplTest_Prolog, true));

            m_uniformRandomSelector = new KS_ScheduledUniformDistributionSelector(Blackboard,
                                                                                  inputPool: FilteredPrologResultPool,
                                                                                  outputPool: UniformlySelectedOutputPool,
                                                                                  numberToSelect: 1);

            m_choicePresenter = new KS_ScheduledChoicePresenter(Blackboard,
                                                                UniformlySelectedOutputPool);

            m_filterPoolCleaner = new KS_ScheduledFilterPoolCleaner(Blackboard,
                                                                    new string[]
            {
                m_IDSelector.OutputPool,           // Output pool for ID selector
                m_prologEval.OutputPool,           // Output pool for prolog eval
                m_filterByPrologResult.OutputPool, // Output pool for filter that filters by prolog result
                m_uniformRandomSelector.OutputPool // Final output pool (written into by UniformDistributionSelector)
            });

            Controller = new ScheduledSequenceController();
            Controller.AddKnowledgeSource(m_IDSelector);
            Controller.AddKnowledgeSource(m_prologEval);
            Controller.AddKnowledgeSource(m_filterByPrologResult);
            Controller.AddKnowledgeSource(m_uniformRandomSelector);
            Controller.AddKnowledgeSource(m_choicePresenter);
            Controller.AddKnowledgeSource(m_filterPoolCleaner);

            // Put request for starting content unit in blackboard
            Unit req = new Unit();

            req.AddComponent(new KC_IDSelectionRequest("start", true));
            req.SetActiveRequest(true);
            Blackboard.AddUnit(req);
        }
Ejemplo n.º 12
0
        public void TestLookupCountAfterDelete()
        {
            IBlackboard blackboard = new Blackboard();

            Unit u1 = new Unit();

            u1.AddComponent(new KC_UnitID("one", true));

            Unit u2 = new Unit();

            u2.AddComponent(new KC_UnitID("two", true));

            blackboard.AddUnit(u1);
            blackboard.AddUnit(u2);

            Assert.Equal(2, blackboard.LookupUnits <Unit>().Count);

            blackboard.RemoveUnit(u1);
            Assert.Equal(1, blackboard.LookupUnits <Unit>().Count);

            blackboard.RemoveUnit(u2);
            Assert.Equal(0, blackboard.LookupUnits <Unit>().Count);
        }
Ejemplo n.º 13
0
        public void TestLookupCount()
        {
            IBlackboard blackboard = new Blackboard();

            Unit u1 = new Unit();

            u1.AddComponent(new KC_UnitID("one", true));

            Unit u2 = new Unit();

            u2.AddComponent(new KC_UnitID("two", true));

            Unit u3 = new Unit();

            u3.AddComponent(new KC_UnitID("three", true));

            blackboard.AddUnit(u1);
            blackboard.AddUnit(u2);
            blackboard.AddUnit(u3);

            ISet <Unit> set1 = blackboard.LookupUnits <Unit>();

            Assert.Equal(3, set1.Count);
        }
        /*public*/
        void TestUpdateAgenda_Controller()
        {
            Controller_PublicUpdateAgenda controller = new Controller_PublicUpdateAgenda();
            IBlackboard blackboard       = new Blackboard();
            KS_Old_ReactiveIDSelector ks = new KS_Old_ReactiveIDSelector(blackboard);

            controller.AddKnowledgeSource(ks);
            IUnit u = new U_IDSelectRequest("foo");

            blackboard.AddUnit(u);
            Assert.Equal(0, controller.Agenda.Count);

            controller.UpdateAgenda();
            Assert.Equal(1, controller.Agenda.Count);

            blackboard.RemoveUnit(u);
            controller.UpdateAgenda();
            Assert.Equal(0, controller.Agenda.Count);
        }
        /*public*/
        void TestSelectKSForExecution_PriorityController()
        {
            PriorityController_PublicMethods controller = new PriorityController_PublicMethods();
            IBlackboard blackboard        = new Blackboard();
            KS_Old_ReactiveIDSelector ks1 = new KS_Old_ReactiveIDSelector(blackboard);
            KS_Old_ReactiveIDSelector ks2 = new KS_Old_ReactiveIDSelector(blackboard);
            KS_Old_ReactiveIDSelector ks3 = new KS_Old_ReactiveIDSelector(blackboard);

            ks1.Properties[Priority] = 10;
            ks2.Properties[Priority] = 30;
            ks3.Properties[Priority] = 20;
            controller.AddKnowledgeSource(ks1);
            controller.AddKnowledgeSource(ks2);
            controller.AddKnowledgeSource(ks3);
            blackboard.AddUnit(new U_IDSelectRequest("foo"));

            controller.UpdateAgenda();
            Assert.Equal(3, controller.Agenda.Count);

            IKnowledgeSourceActivation KSA = controller.SelectKSForExecution();

            Assert.Equal(30, KSA.Properties[KSProps.Priority]);
        }
        public Demo1_Reactive()
        {
            Blackboard = new Blackboard();

            // Set up the ContentUnits on the blackboard
            Demo1_Slots_DefineCUs(Blackboard);

            // Set up the knowledge sources
            m_IDSelector = new KS_Old_ReactiveIDSelector(Blackboard);

            // fixme: Need to come up with interfaces for presenters, but won't know what the general presenter framework looks like until I've written more of them.
            m_KSChoicePresenter = new KS_Old_ReactiveChoicePresenter(Blackboard);
            m_IDSelector.Properties[Priority]        = 20;
            m_KSChoicePresenter.Properties[Priority] = 10;

            // Set up the controller
            Controller = new ReactivePriorityController();
            Controller.AddKnowledgeSource(m_IDSelector);
            Controller.AddKnowledgeSource(m_KSChoicePresenter);

            // Put request for starting content unit in blackboard
            Blackboard.AddUnit(new U_IDSelectRequest("start"));
        }
Ejemplo n.º 17
0
        public Demo1_Scheduled()
        {
            Blackboard = new Blackboard();
            Demo1_KC_DefineUnits(Blackboard);

            m_IDSelector            = new KS_ScheduledIDSelector(Blackboard);
            m_uniformRandomSelector = new KS_ScheduledUniformDistributionSelector(Blackboard, inputPool: m_IDSelector.OutputPool, outputPool: null, numberToSelect: 1);
            m_choicePresenter       = new KS_ScheduledChoicePresenter(Blackboard, inputPool: m_uniformRandomSelector.OutputPool);
            m_filterPoolCleaner     = new KS_ScheduledFilterPoolCleaner(Blackboard,
                                                                        new string[] { m_IDSelector.OutputPool, m_uniformRandomSelector.OutputPool });

            Controller = new ScheduledSequenceController();
            Controller.AddKnowledgeSource(m_IDSelector);
            Controller.AddKnowledgeSource(m_uniformRandomSelector);
            Controller.AddKnowledgeSource(m_choicePresenter);
            Controller.AddKnowledgeSource(m_filterPoolCleaner);

            // Put request for starting content unit in blackboard
            Unit req = new Unit();

            req.AddComponent(new KC_IDSelectionRequest("start", true));
            req.SetActiveRequest(true);
            Blackboard.AddUnit(req);
        }
        // Deleting and adding elements to a returned set shouldn't change the set in the dictionary
        public void TestManipulatingSet()
        {
            IBlackboard blackboard = new Blackboard();

            TestUnit1 u1 = new TestUnit1("one");
            TestUnit1 u2 = new TestUnit1("two");

            string type1 = u1.GetType().FullName;

            blackboard.AddUnit(u1);
            ISet <IUnit> set1 = blackboard.LookupUnits(type1);

            Assert.Equal(1, set1.Count);

            set1.Add(u2);
            ISet <IUnit> set2 = blackboard.LookupUnits(type1);

            Assert.Equal(1, set2.Count);

            set2.Remove(u1);
            ISet <IUnit> set3 = blackboard.LookupUnits(type1);

            Assert.Equal(1, set3.Count);
        }