public void TestObviationCondition(IBlackboard blackboard, ReactiveKnowledgeSource ks, IUnit[] unitsToAdd) { blackboard.Clear(); // Clear the blackboard so that there aren't KUs laying around from previous tests. // Add the units in unitsToAdd foreach (IUnit unitToAdd in unitsToAdd) { blackboard.AddUnit(unitToAdd); } // Call KnowledgeSource.Precondition() to get the activated KSs. IEnumerable <IKnowledgeSourceActivation> KSAs = ks.Precondition(); // If there are any activated KSs... if (KSAs.Any()) { // First, the obviation condition should evaluate to false since the matching KUs are still on the blackboard. foreach (IKnowledgeSourceActivation KSA in KSAs) { Assert.False(KSA.EvaluateObviationCondition()); } // Second, remove the units from the blackboard foreach (IUnit unitToRemove in unitsToAdd) { blackboard.RemoveUnit(unitToRemove); } // Finally, the obviation condition should now evaluate to true since the matching KUs are no longer on the blackboard. foreach (IKnowledgeSourceActivation KSA in KSAs) { Assert.True(KSA.EvaluateObviationCondition()); } } }
public void TestPrecondition(IBlackboard blackboard, ReactiveKnowledgeSource ks, IUnit[] unitsToAdd, bool previouslyMatched, int numActivatedKSs) { blackboard.Clear(); // Clear the blackboard so that there aren't KUs laying around from previous tests. // Add the units in unitsToAdd foreach (IUnit unitToAdd in unitsToAdd) { blackboard.AddUnit(unitToAdd); // If they should be marked as previously matched, set KSPreconditionMatched if (previouslyMatched) { unitToAdd.Slots[KSPreconditionMatched] = new HashSet <ReactiveKnowledgeSource> { ks }; } } // Call KnowledgeSource.Precondition() to get the activated KSs. IEnumerable <IKnowledgeSourceActivation> KSAs = ks.Precondition(); // Check that the number of activated KSs equals the number we're expecting int count = KSAs.Count(); Assert.Equal(numActivatedKSs, count); /* * fixme: need to remove this because we're no longer storing matches on the matching units. Instead store match sets on the KnowledgeSources. * For now I've created a separate test for Selector Knowledge Sources (which already implement match set storage) until we get this * resolved for all KnowledgeSources. * * If there were activated KSs, check that the KUs were marked as having been matched. */ if (count > 0) { foreach (IUnit u in unitsToAdd) { Assert.True(u.Slots.ContainsKey(KSPreconditionMatched)); bool containsKS = ((HashSet <ReactiveKnowledgeSource>)u.Slots[KSPreconditionMatched]).Contains(ks); Assert.True(containsKS); } } // Run the preconditions again to verify that on a second running they don't activate any KSs. KSAs = ks.Precondition(); count = KSAs.Count(); Assert.Equal(0, count); }