private EventHandler <PresenterExecuteEventArgs> GenerateEventHandler(ContentUnit selectedCU, ContentUnit[] choices, IBlackboard blackboard) { return((object sender, PresenterExecuteEventArgs eventArgs) => { var presenterEventArgs = eventArgs as PresenterExecuteEventArgs; if (selectedCU != null) { Assert.Equal(selectedCU.Content[Text], presenterEventArgs.TextToDisplay); int numOfChoices = choices.Length; Assert.Equal(numOfChoices, presenterEventArgs.Choices.Length); foreach (ContentUnit choice in choices) { Assert.True(Array.Exists(presenterEventArgs.ChoicesToDisplay, element => element.Equals(choice.Content[Text]))); } } else { Assert.Equal("", presenterEventArgs.TextToDisplay); } // Iterate through each of the choices selecting it and confirming that the correct U_IDSelectRequest is added. IChoicePresenter_Old cp = (IChoicePresenter_Old)sender; for (uint i = 0; i < presenterEventArgs.ChoicesToDisplay.Length; i++) { cp.SelectChoice((ContentUnit[])presenterEventArgs.Choices, i); U_IDSelectRequest idSelectRequest = blackboard.LookupSingleton <U_IDSelectRequest>(); Assert.True(idSelectRequest.TargetContentUnitID.Equals(choices[i].Metadata[TargetContentUnitID])); blackboard.RemoveUnit(idSelectRequest); // Remove the U_IDSelect request before the next iteration. } }); }
public static void RemoveUnits(IBlackboard blackboard, IUnit[] unitsToRemove) { foreach (IUnit unit in unitsToRemove) { Assert.True(blackboard.RemoveUnit(unit)); } }
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 CFGExpansionController(Unit rootNode, string grammarRulePool, IBlackboard blackboard) { this.blackboard = blackboard; RootNode = rootNode; /* * Replace with three filters: KS_SelectTreeLeaves, which creates a content pool containing all the tree leaves meeting some condition, * KS_ScheduledTierSelector, which, given a component with a sortable value, selects the lowest (in this case it will be order in which a leaf is added to tree), and * KS_ProcessTreeNode, which in this case will activate the ID request and save a reference to the node. KS_ScheduledTierSelector will become abstract, with * several children: KS_ScheduledHighestTierSelector, KS_ScheduledLowestTierSelector, KS_UniformTopNTierSelector, KS_UniformBottomNTierSelector, * KS_ExponentialDistTierSelector. * This decoupling allows other logic to be used in the choice of leaf to expand (such as computing a heuristic for picking a node to expand). */ m_pickLeftmostNodeToExpand = new KS_ScheduledExecute( () => { var nonTerminalLeafNodes = from Unit node in blackboard.LookupUnits <Unit>() where node.HasComponent <KC_TreeNode>() && node.IsTreeLeaf() where node.HasComponent <KC_IDSelectionRequest>() select node; if (nonTerminalLeafNodes.Any()) { nonTerminalLeafNodes.First().SetActiveRequest(true); /* * Save a reference to the current tree node we're expanding on the blackboard. */ Unit leafExpansionRef = new Unit(); leafExpansionRef.AddComponent(new KC_UnitReference(CurrentTreeNodeExpansion, true, nonTerminalLeafNodes.First())); blackboard.AddUnit(leafExpansionRef); } } ); // string idOutputPool = "pool" + DateTime.Now.Ticks; string idOutputPool = "idOutputPool"; m_lookupGrammarRules = new KS_ScheduledIDSelector(blackboard, grammarRulePool, idOutputPool); // string uniformDistOutputPool = "pool" + DateTime.Now.Ticks; string uniformDistOutputPool = "uniformDistOutputPool"; m_chooseGrammarRule = new KS_ScheduledUniformDistributionSelector(blackboard, idOutputPool, uniformDistOutputPool, 1); /* * Replace with KS_ExpandTreeNode. An instance of KS_ExpandTreeNode has: * 1) The name of a content pool a unit with a decomposition. * 2) The name of a KC_UnitReference containing a pointer to the node to expand. */ m_treeExpander = new KS_ScheduledExecute( () => { var rule = from unit in blackboard.LookupUnits <Unit>() where unit.HasComponent <KC_ContentPool>() && unit.ContentPoolEquals(uniformDistOutputPool) select unit; /* * Grab the reference to the current leaf node we're expanding. */ var nodeToExpandQuery = from unit in blackboard.LookupUnits <Unit>() where unit.HasComponent <KC_UnitReference>() select unit; Unit nodeToExpandRef = nodeToExpandQuery.First(); if (rule.Any()) { Debug.Assert(rule.Count() == 1); // Only one rule is picked to expand a symbol Debug.Assert(nodeToExpandQuery.Count() == 1); // Should be only one reference we're expanding. Unit selectedRule = rule.First(); Unit ruleNode = new Unit(selectedRule); // Remove the KC_Decomposition (not needed) and KC_ContentPool (will cause node to be prematurely cleaned up) components ruleNode.RemoveComponent(ruleNode.GetComponent <KC_Decomposition>()); ruleNode.RemoveComponent(ruleNode.GetComponent <KC_ContentPool>()); // fixme: consider defining conversion operators so this looks like // new KC_TreeNode((KC_TreeNode)nodeToExpand); ruleNode.AddComponent(new KC_TreeNode(nodeToExpandRef.GetUnitReference().GetComponent <KC_TreeNode>())); blackboard.AddUnit(ruleNode); // For each of the Units in the decomposition, add them to the tree as children of ruleCopy. foreach (Unit child in selectedRule.GetDecomposition()) { // Make a copy of Unit in the decomposition and add it to the tree. Unit childNode = new Unit(child); blackboard.AddUnit(childNode); childNode.AddComponent(new KC_TreeNode(ruleNode.GetComponent <KC_TreeNode>())); } } else { // No rule was found. Create a pseudo-decomposition consisting of just the TargetUnitID in ## (borrowing from Tracery). Unit noRuleTextDecomp = new Unit(); noRuleTextDecomp.AddComponent(new KC_TreeNode(nodeToExpandRef.GetUnitReference().GetComponent <KC_TreeNode>())); noRuleTextDecomp.AddComponent(new KC_Text("#" + nodeToExpandRef.GetUnitReference().GetTargetUnitID() + "#", true)); blackboard.AddUnit(noRuleTextDecomp); } blackboard.RemoveUnit(nodeToExpandRef); // Remove the reference to the leaf node to expand (it has been expanded). } ); m_cleanSelectionPools = new KS_ScheduledFilterPoolCleaner(blackboard, new string[] { idOutputPool, uniformDistOutputPool }); bool GenSequencePrecond() { var leafNodes = from Unit node in blackboard.LookupUnits <Unit>() where node.HasComponent <KC_TreeNode>() && node.IsTreeLeaf() select node; // This is ready to run if no leaf node contains a KC_IDSelectionRequest component (meaning it's a non-terminal). return(leafNodes.All(node => !node.HasComponent <KC_IDSelectionRequest>())); } /* * Replace with KS_LinearizeTreeLeaves. */ void GenSequenceExec() { // Walk the tree to find the leafs from left to right. IList <Unit> leafs = new List <Unit>(); AddLeafs(RootNode, leafs); // Write out the leafs of the generated tree foreach (Unit leaf in leafs) { Console.Write(leaf.GetText() + " "); } // Delete the tree. var treeNodes = from Unit node in blackboard.LookupUnits <Unit>() where node.HasComponent <KC_TreeNode>() select node; foreach (var node in treeNodes) { blackboard.RemoveUnit(node); } } m_addGeneratedSequence = new KS_ScheduledExecute(GenSequenceExec, GenSequencePrecond); }