Ejemplo n.º 1
0
        /// <summary>
        /// Add a goal to the brain.
        /// </summary>
        /// <param name="antecedent">Antecedent of goal. If there is no antecendent, an empty TransitiveAction can be given.</param>
        /// <param name="consequent">Consequent of goal</param>
        /// <param name="brain">Brain to create goal in</param>
        /// <returns>Name of the goal created</returns>
        private static string AddGoal(TransitiveAction antecedent, TransitiveAction consequent, CBrain brain)
        {
            // Simple validation: if the consequent has a null action, give up
            if (consequent.action == null)
            {
                return null;
            }

            // Set up the goal
            string name = MakeGoalName(antecedent, consequent);
            GoalBuilder goal = new GoalBuilder("NL" + name, brain);
            int ruleIndex = goal.AddRule("NL" + name);

            // Convert consequents to goals
            if (goWords.Contains(consequent.action))
            {
                brain.Mind.addFact(new CFact("(CurrentDestination Arg Value=" + consequent.target + ";1)"));
                return "gGotoX";
            }
            else if (tellWords.Contains(consequent.action))
            {
                if (experienceWords.Contains(antecedent.action))
                {
                    string type = TitleCase(antecedent.target);
                    string matchVar = "NotToldAbout" + type;
                    goal.AddAndAntecedent(ruleIndex, matchVar + "? " + type + " *=*;1");
                    goal.AddNegatedAndAntecedent(ruleIndex, "?" + matchVar + " " + type + " ToldAbout=*;1 *=*;1");
                    goal.AddAndAntecedent(ruleIndex, "CurrentView State " + type + "s=*?" + matchVar + "_*;1 *=*;1");
                    goal.AddConsequent(ruleIndex, "Execs", "Voice Say \"Commander, I see a " + type + ".\"");
                    goal.AddConsequent(ruleIndex, "Set", "?" + matchVar + ".Name " + type + " ToldAbout=true");
                    // Add a default Wait rule so the rule doesn't spin
                    int defaultRuleIndex = goal.AddRule("Default" + goal.Name);
                    goal.AddConsequent(defaultRuleIndex, "Wait", "1000");
                    // Confirm the plan back
                    int confirmRuleIndex = goal.AddRule("Initialize" + goal.Name);
                    goal.AddConsequent(confirmRuleIndex, "Execs", "Voice Say \"Okay, if I see a " +
                        antecedent.target + ", I'll let you know.\"");
                    goal.AddConsequent(confirmRuleIndex, "DisableRule", "self");
                    // This rule doesn't have a quit as it should stay standing
                }
                else
                {
                    // Say we don't know how to do it
                    brain.Mind.addFact(new CFact("(DontKnow Arg Value=" + antecedent.action + ";1)"));
                    return "gDontKnowHowToX";
                }
            }
            else if (consequent.action != null)
            {
                // Say we don't know how to do it
                brain.Mind.addFact(new CFact("(DontKnow Arg Value=" + consequent.action + ";1)"));
                return "gDontKnowHowToX";
            }
            else
            {
                return null;
            }

            // Commit the goal if we made it through
            goal.Commit();
            return goal.Name;
        }
        /// <summary>
        /// Create a parser and default goals.
        /// </summary>
        /// <param name="brain">The brain</param>
        /// <param name="robot">The robot</param>
        public override void Init(Brain.CBrain brain, IRobot robot)
        {
            this.brain = brain;
            this.robot = robot;

            // Load external NLP systems in their own threads
            restorerLoaded = new AutoResetEvent(false);
            parserLoaded = new AutoResetEvent(false);
            Thread parserLoader = new Thread(InitParser);
            Thread restorerLoader = new Thread(InitRestorer);
            parserLoader.Start();
            restorerLoader.Start();

            // Load up other systems in the meantime
            this.tokenizer = new EnglishMaximumEntropyTokenizer(sharpNLPPath + "EnglishTok.nbin");
            this.tagger = new EnglishMaximumEntropyPosTagger(sharpNLPPath + "EnglishPOS.nbin", sharpNLPPath + @"\Parser\tagdict");

            // Make default goals
            GoalBuilder gotoGoal = new GoalBuilder("GotoX", brain);
            int ruleIndex = gotoGoal.AddRule("GotoX");
            gotoGoal.AddAndAntecedent(ruleIndex, "CurrentDestination Arg *=*;1");
            gotoGoal.AddConsequent(ruleIndex, "Execs", "Voice Say \"Going to the $CurrentDestination_\"");
            gotoGoal.AddConsequent(ruleIndex, "Execs", "Motion GoTo $CurrentDestination close");
            gotoGoal.AddConsequent(ruleIndex, "Execs", "Voice Say \"Commander, I finished moving to the $CurrentDestination_\"");
            gotoGoal.AddConsequent(ruleIndex, "Remove", "CurrentDestination Arg");
            gotoGoal.AddConsequent(ruleIndex, "Quit", "");
            gotoGoal.Commit();

            failureGoal = new GoalBuilder("DidNotUnderstand", brain);
            ruleIndex = failureGoal.AddRule("DidNotUnderstand");
            failureGoal.AddConsequent(ruleIndex, "Execs", "Voice Say \"I didn't understand what you said.\"");
            failureGoal.AddConsequent(ruleIndex, "Quit", "");
            failureGoal.Commit();

            GoalBuilder goingToGoal = new GoalBuilder("SayGoingToX", brain);
            ruleIndex = goingToGoal.AddRule("SayGoingToX");
            goingToGoal.AddAndAntecedent(ruleIndex, "CurrentDestination Arg *=*;1");
            goingToGoal.AddConsequent(ruleIndex, "Execs", "Voice Say \"I'm Going to the $CurrentDestination_\"");
            goingToGoal.AddConsequent(ruleIndex, "Quit", "");
            goingToGoal.Commit();

            GoalBuilder goingNowhereGoal = new GoalBuilder("SayGoingNowhere", brain);
            ruleIndex = goingNowhereGoal.AddRule("SayGoingNowhere");
            goingNowhereGoal.AddConsequent(ruleIndex, "Execs", "Voice Say \"I'm not going anywhere right now.\"");
            goingNowhereGoal.AddConsequent(ruleIndex, "Quit", "");
            goingNowhereGoal.Commit();

            GoalBuilder dontKnowGoal = new GoalBuilder("DontKnowHowToX", brain);
            ruleIndex = dontKnowGoal.AddRule("DontKnowHowToX");
            dontKnowGoal.AddAndAntecedent(ruleIndex, "DontKnow Arg *=*;1");
            dontKnowGoal.AddConsequent(ruleIndex, "Execs", "Voice Say \"Sorry, but I don't know how to $DontKnow_\"");
            dontKnowGoal.AddConsequent(ruleIndex, "Remove", "DontKnow Arg");
            dontKnowGoal.AddConsequent(ruleIndex, "Quit", "");
            dontKnowGoal.Commit();

            // Wait for all systems to finish loading
            restorerLoaded.WaitOne();
            parserLoaded.WaitOne();
            this.semantics = new SemanticsInterface();
        }