Ejemplo n.º 1
0
        public Reply GetReply(string input, State state)
        {
            state.Vars["_input"]      = input;
            state.Vars["_lastinput"]  = state.Lastinput;
            state.Vars["_lastfired"]  = state.Lastfired;
            state.Vars["_time"]       = DateTime.Now.ToString("h:mm tt");
            state.Vars["_time24"]     = DateTime.Now.ToString("HH:mm");
            state.Vars["_date"]       = DateTime.Now.ToString("MMM. d, yyyy");
            state.Vars["_month"]      = DateTime.Now.ToString("MMMM");
            state.Vars["_dayofmonth"] = DateTime.Now.ToString("d ").Trim();
            state.Vars["_year"]       = DateTime.Now.ToString("yyyy");
            state.Vars["_dayofweek"]  = DateTime.Now.ToString("dddd");

            if (input.Length == 0)
            {
                input = "_blank";
            }

            state.Lastinput = input;
            bool  standardNoRuleFiredDefined = false;
            Reply noRuleFiredReply           = new Reply("", "", "", "", 0.0, null);

            foreach (string stPath in state.CurrentKBs)
            {
                if (this.compiledKnowledgeBases.ContainsKey(stPath))
                {
                    CompiledKnowledgeBase ckb = (CompiledKnowledgeBase)this.compiledKnowledgeBases[stPath];
                    string ckbOutput          = ckb.CSToolbox.ExecuteOnBeforeRuleFired(state);
                    //update our input var incase the Before Rule Fired changes it
                    input           = (string)state.Vars["_input"];
                    state.Lastinput = input;
                    Reply reply = ckb.GetReply(input, state.Lastfired, state.Vars, ckbOutput);
                    if (reply != null)
                    {
                        ckbOutput = ckb.CSToolbox.ExecuteOnAfterRuleFired(state, reply);
                        string outputText = ckb.DoTextReplacements(ckbOutput);
                        string agentText  = ckb.DoAgentTextReplacements(ckbOutput);
                        reply.Text               += outputText;
                        reply.AgentText          += agentText;
                        state.Lastfired           = reply.RuleId;
                        state.Vars["_lastoutput"] = reply.Text;

                        return(reply);
                    }
                    if (ckb.CSToolbox.StandardNoRuleFiredDefined)
                    {
                        standardNoRuleFiredDefined = true;
                        //if this ckb has a no rule fired handler, fire it
                        noRuleFiredReply.KBItem = ckb.KnowledgeBaseItem;
                        string noReplyText = ckb.CSToolbox.ExecuteOnNoRuleFired(state, noRuleFiredReply);
                        noRuleFiredReply.Text      += ckb.DoTextReplacements(noReplyText);
                        noRuleFiredReply.AgentText += ckb.DoAgentTextReplacements(noReplyText);
                    }
                }
            }
            if (standardNoRuleFiredDefined)
            {
                return(noRuleFiredReply);
            }
            return(null); //if there's no reply, return null
        }                 //GetReply(string input)