public DemoEnsembleLite() { Blackboard = new Blackboard(); DemoEnsemble_DefineUnits(Blackboard); /* * This prolog evaluator evauates the conditions of rules in the rules pool. * Construct the prolog evaluator with the following parameters: * * The blackboard on which to do the work. * * The input pool to look for ContentUnits with prolog expressions to evaluate. * * The name of the prolog expression to evaluate (in this case the prolog expression named ApplTest_Prolog, for which a string constant is defined in KCNames). */ var prologEval = new KS_ScheduledPrologEval(Blackboard, inputPool: RulesPool, outputPool: EvalRulesPool, 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). * This filter selector selects all the rules whose prolog expression evaluated to true. */ var filterByPrologResult = new KS_ScheduledFilterSelector(Blackboard, inputPool: EvalRulesPool, outputPool: SatisfiedRulesPool, filter: KS_ScheduledPrologEval.FilterByPrologResult(ApplTest_Prolog, true)); /* * This KS prints out info about satisified rules (for debugging). */ var printFilteredRules = new KS_ScheduledPrintPool(Blackboard, SatisfiedRulesPool); /* * This KS takes a pool of things to weight (in this case dialog) and a pool of utility sources (in this case satisfied rules) and sums the utility sources * applicable to each unit to weight to compute a new pool of weighted units. */ var weightDialogWithRules = new KS_ScheduledUtilitySum(Blackboard, SatisfiedRulesPool, DialogPool, WeightedDialogPool); /* * This KS prints out the pool of weighted dialog (for debugging). */ var printWeightedDialog = new KS_ScheduledPrintPool(Blackboard, WeightedDialogPool); /* * This KS selects the most highly weighted dialog lines into a pool (e.g. if the highest weighting is 10, and their are two dialog lines * that share that weight, it will copy both of them into the output pool). */ var selectHighestWeightedDialog = new KS_ScheduledHighestTierSelector <KC_Utility>(Blackboard, WeightedDialogPool, HighestWeightedDialogPool); /* * This KS prints out the units in the HighestWeightedDialogPool (for debugging). */ var printHighestWeightedDialog = new KS_ScheduledPrintPool(Blackboard, HighestWeightedDialogPool); /* * This KS selects one line at random from the HighestWeightedDialogPool. */ var selectDialogLine = new KS_ScheduledUniformDistributionSelector(Blackboard, inputPool: HighestWeightedDialogPool, outputPool: SelectedDialogPool, numberToSelect: 1); /* * This KS prints out the units in the SelectedDialogPool (there will be only one dialog line in this pool). (for debugging) */ var printSelectedDialogLine = new KS_ScheduledPrintPool(Blackboard, SelectedDialogPool); /* * This KS is updating the prolog KB based on the effects on the selected dialog line. * fixme: for now I'm just using KS_ScheduledExecute to call the SelectChoice_PrologKBChanges. Need to refactor SelectChoice_PrologKBChanges because it's not just tied to SelectChoice and possibly move KB updates into * a special purpose KS. */ var updatePrologKB = new KS_ScheduledExecute( () => { var selectedDialodLines = from Unit node in Blackboard.LookupUnits <Unit>() where ScheduledKnowledgeSource.SelectFromPool(node, SelectedDialogPool) select node; foreach (Unit unit in selectedDialodLines) { /* * SelectChoice_PrologKBChanges() is an event handler, so it expects a sender as well as for args to be wrapped in EventArgs. * So I'm using this as the sender (PrologKBChanges doesn't actually use the sender) wrapping in SelectChoiceEventArgs. */ var eventArgs = new SelectChoiceEventArgs(unit, Blackboard); EventHandlers_ChoicePresenter.SelectChoice_PrologKBChanges(this, eventArgs); } } ); /* * This KS cleans up all the pools that were created during the selection process. */ var poolCleaner = new KS_ScheduledFilterPoolCleaner( Blackboard, new string[] { EvalRulesPool, SatisfiedRulesPool, WeightedDialogPool, HighestWeightedDialogPool, SelectedDialogPool } ); Controller = new ScheduledSequenceController(); Controller.AddKnowledgeSource(prologEval); Controller.AddKnowledgeSource(filterByPrologResult); Controller.AddKnowledgeSource(printFilteredRules); Controller.AddKnowledgeSource(weightDialogWithRules); Controller.AddKnowledgeSource(printWeightedDialog); Controller.AddKnowledgeSource(selectHighestWeightedDialog); Controller.AddKnowledgeSource(printHighestWeightedDialog); Controller.AddKnowledgeSource(selectDialogLine); Controller.AddKnowledgeSource(printSelectedDialogLine); Controller.AddKnowledgeSource(updatePrologKB); Controller.AddKnowledgeSource(poolCleaner); }
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); }