/// <summary>
        /// A tree has found to be a candidate for substitution. Apply logic.
        /// </summary>
        /// <param name="builder">Tree builder class</param>
        /// <param name="characterName">name of the character evolding</param>
        /// <param name="action">action that triggered evolution</param>
        /// <param name="decisionTreeToChange">Decision Tree To evolve</param>
        /// <returns></returns>
        public bool SubstituteNode(ITreeBuilder builder, string characterName, RNPC.Core.Action.Action action, string decisionTreeToChange)
        {
            var initialAction = ((Reaction)action).InitialEvent as RNPC.Core.Action.Action;

            if (initialAction == null)
            {
                return(false);
            }

            var substitution = _substitutionMapper.GetSubstitutableSubTreeForLeaf(action.EventName);

            if (substitution == null)
            {
                return(false);
            }

            var firstNode = builder.BuildTreeFromDocument(_fileController, initialAction, characterName);

            if (firstNode == null)
            {
                return(false);
            }

            var replacementNode = builder.BuildSubTreeFromRepository(_fileController, substitution.SubTreeName, ConfigurationDirectory.Instance.SubTreeRepository);

            if (replacementNode == null)
            {
                return(false);
            }

            if (!FindAndReplaceMatchingNode(firstNode, substitution, replacementNode))
            {
                return(false);
            }

            string fileName = initialAction.ActionType + "-" + initialAction.Intent + "-" + initialAction.EventName;

            builder.BuildAndSaveXmlDocumentFromTree(_fileController, firstNode, fileName, characterName);

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Handles all social interactions with the character
        /// </summary>
        /// <param name="action">The action that the character is reacting to</param>
        /// <returns>The character's reaction</returns>
        private List <Reaction> HandleSocialInteraction(RNPC.Core.Action.Action action)
        {
            try
            {
                if (FileController == null || DecisionTreeBuilder == null)
                {
                    throw new RnpcParameterException("Objects FileController or DecisionTreeBuilder has not been properly initialized.");
                }



                var decisionTreeRootNode = DecisionTreeBuilder.BuildTreeFromDocument(FileController, action, MyName);

                if (decisionTreeRootNode == null)
                {
                    throw new NullNodeException("Root node has not been initialized. Check BuildTreeFromDocument method.");
                }

                //var contextInfo = ContextAnalyzer.EvaluateEventContext(action);

                //var strategy = ContextStrategyFactory.GetContextStrategy(contextInfo);

                //var reaction = strategy.Evaluate(this, action, decisionTreeRootNode);

                var reaction = decisionTreeRootNode.Evaluate(MyTraits, MyMemory, action);

                MyMemory.AddNodeTestResults(((AbstractDecisionNode)decisionTreeRootNode).GetNodeTestsData());

                reaction[0].ReactionScore = ((AbstractDecisionNode)decisionTreeRootNode).GetNodeTestsData().Sum(info => info.ProfileScore);

                MyMemory.AddRecentReactions(reaction);

                return(reaction);

                //return strategy.Evaluate(this, action, decisionTreeRootNode);
            }
            //TODO: logging
            catch (XmlException exception)
            {
                return(new List <Reaction>
                {
                    new Reaction
                    {
                        Intent = Intent.Neutral,
                        ActionType = ActionType.Verbal,
                        EventType = EventType.Interaction,
                        Tone = Tone.Confused,
                        InitialEvent = action,
                        ReactionScore = 0,
                        Target = null,
                        EventName = "XmlException",
                        ErrorMessages = exception.Message,
                        Message = ErrorMessages.IDontKnow,
                        Source = MyName
                    }
                });
            }
            catch (NullNodeException exception)
            {
                return(new List <Reaction>
                {
                    new Reaction
                    {
                        Intent = Intent.Neutral,
                        ActionType = ActionType.Verbal,
                        EventType = EventType.Interaction,
                        Tone = Tone.Confused,
                        InitialEvent = action,
                        ReactionScore = 0,
                        Target = null,
                        EventName = "NullNodeException",
                        ErrorMessages = exception.Message,
                        Message = ErrorMessages.ICantDecide,
                        Source = MyName
                    }
                });
            }
            catch (RnpcParameterException exception)
            {
                return(new List <Reaction>
                {
                    new Reaction
                    {
                        Intent = Intent.Neutral,
                        ActionType = ActionType.Verbal,
                        EventType = EventType.Interaction,
                        Tone = Tone.Pensive,
                        InitialEvent = action,
                        ReactionScore = 0,
                        Target = null,
                        EventName = "RnpcParameterException",
                        ErrorMessages = exception.Message,
                        Message = ErrorMessages.FeelsLike,
                        Source = MyName
                    }
                });
            }
            catch (NodeInitializationException exception
                   ) //The decision tree could not be initialized properly. Check the xml files.
            {
                return(new List <Reaction>
                {
                    new Reaction
                    {
                        Intent = Intent.Neutral,
                        ActionType = ActionType.Verbal,
                        EventType = EventType.Interaction,
                        Tone = Tone.Confused,
                        InitialEvent = action,
                        ReactionScore = 0,
                        Target = null,
                        EventName = "NodeInitializationException",
                        ErrorMessages = exception.Message,
                        Message = ErrorMessages.MyThoughtsAreJumbled,
                        Source = MyName
                    }
                });
            }
            catch (Exception e) //A generic exception was raised while initializing the decision tree
            {
                return(new List <Reaction>
                {
                    new Reaction
                    {
                        Intent = Intent.Neutral,
                        ActionType = ActionType.Verbal,
                        EventType = EventType.Interaction,
                        Tone = Tone.Confused,
                        InitialEvent = action,
                        ReactionScore = 0,
                        Target = null,
                        EventName = "Exception",
                        ErrorMessages = e.Message,
                        Message = ErrorMessages.IHaveAHeadache,
                        Source = MyName
                    }
                });
            }
        }