public override Unifiable RecurseChildren()
        {
            var vorNull = ComputeInnerOrNull();

            if (!Unifiable.IsNull(vorNull))
            {
                return(vorNull);
            }
            writeToLogWarn("Why are we in Recurse?");
            return(base.Recurse());
        }
Esempio n. 2
0
        public override Unifiable RecurseChildren()
        {
            var vorNull = ComputeInnerOrNull();

            if (!Unifiable.IsNull(vorNull))
            {
                return(vorNull);
            }
            writeToLogWarn("Why is a star in Recurse?");
            return(((AIMLTagHandler)this).Recurse());
        }
Esempio n. 3
0
        public object getPosterBoard(object slot)
        {
            string sslot = "" + slot;

            sslot = sslot.ToLower();
            var u = TheBot.GlobalSettings.grabSetting(sslot);

            if (Unifiable.IsNull(u))
            {
                return(null);
            }
            if (TextPatternUtils.IsNullOrEmpty(u))
            {
                return("");
            }
            return(u);//.ToValue(null);
        }
        /// <summary>
        /// Recursively evaluates the template nodes returned from the Proccessor
        /// </summary>
        /// <param name="node">the node to evaluate</param>
        /// <param name="query">the query that produced targetBot node</param>
        /// <param name="request">the request from the user</param>
        /// <param name="result">the result to be sent to the user</param>
        /// <param name="user">the user who originated the request</param>
        /// <returns>the output Unifiable</returns>
        public string processNodeVV(XmlNode node, SubQuery query,
                                    Request request, Result result, User user,
                                    AIMLTagHandler parentHandlerU, bool protectChild, bool copyParent,
                                    AIMLTagHandler tagHandlerU, out bool childSuccess)
        {
            AltBot TargetBot = tagHandlerU.bot;

            if (request != null)
            {
                TargetBot = request.TargetBot;
            }
            childSuccess = true;
            if (node == null)
            {
                string whyError = "ERROR null NODE " + tagHandlerU;
                writeToLog(whyError);
                throw new ChatSignalOverBudget(request, whyError);
            }
            if (node.NodeType == XmlNodeType.Text)
            {
                childSuccess = true;
                if (tagHandlerU != null)
                {
                    tagHandlerU.QueryHasSuceeded = true;
                }
                string s = StaticAIMLUtils.Trim(StaticAIMLUtils.TextNodeValue(node));
                if (!String.IsNullOrEmpty(s))
                {
                    return(StaticAIMLUtils.ValueText(s));
                }
                //return s;
            }
            if (tagHandlerU == null)
            {
                string whyError = "ERROR null THND " + node;
                childSuccess = false;
                writeToLog(whyError);
            }
            bool isTraced = (request != null && (request.IsTraced || !request.GraphsAcceptingUserInput)) ||
                            (query != null && query.IsTraced);

            // check for timeout (to avoid infinite loops)
            bool overBudget = false;

            if (request != null && request.IsComplete(result))
            {
                object gn = request.Graph;
                if (query != null)
                {
                    gn = query.Graph;
                }
                string s = StaticAIMLUtils.SafeFormat("WARNING! Request " + request.WhyComplete +
                                                      ". User: {0} raw input: {3} \"{1}\" processing {2} templates: \"{4}\"",
                                                      request.Requester.UserID, Unifiable.DescribeUnifiable(request.rawInput),
                                                      (query == null ? "-NOQUERY-" : query.Templates.Count.ToString()), gn, node);
                if (isTraced)
                {
                    request.writeToLog(s);
                }
                overBudget = true;
                if (!request.IsToplevelRequest)
                {
                    throw new ChatSignalOverBudget(request, s);
                }
            }


            // process the node
            if (ReferenceEquals(null, tagHandlerU))
            {
                childSuccess = true;
                if (node.NodeType == XmlNodeType.Comment)
                {
                    return(Unifiable.Empty);
                }
                if (node.NodeType == XmlNodeType.Text)
                {
                    string s = StaticAIMLUtils.Trim(StaticAIMLUtils.TextNodeValue(node));
                    if (String.IsNullOrEmpty(s))
                    {
                        return(Unifiable.Empty);
                    }
                    return(s);
                }
                // ReSharper disable ConditionIsAlwaysTrueOrFalse
                OutputDelegate del = (request != null) ? request.writeToLog : writeToLog;
                // ReSharper restore ConditionIsAlwaysTrueOrFalse
                if (overBudget)
                {
                    return(Unifiable.Empty);
                }
                string nodeInner = node.InnerXml;
                TargetBot.EvalAiml(node, request, del ?? TextPatternUtils.DEVNULL);
                return(node.InnerXml);
            }

            XmlNode oldNode         = node;
            bool    wasReadonlyNode = oldNode.IsReadOnly;

            // copy the node!?!
            if (protectChild)
            {
                copyParent = true;
                LineInfoElementImpl newnode = StaticAIMLUtils.CopyNode(node, copyParent);
                newnode.ReadOnly = false;
                node             = newnode;
            }

            if (overBudget)
            {
                tagHandlerU.Dispose();
                tagHandlerU  = null;
                childSuccess = true;
                return(Unifiable.Empty);
            }

            tagHandlerU.SetParent(parentHandlerU);
            //if (parent!=null) parent.AddChild(tagHandler);

            Unifiable cp = tagHandlerU.Transform();

            if (Unifiable.IsNullOrEmpty(cp) && (!tagHandlerU.QueryHasSuceeded || tagHandlerU.QueryHasFailed))
            {
                bool needsOneMoreTry = !request.SuspendSearchLimits &&
                                       (request.IsToplevelRequest /*|| result.ParentRequest.IsToplevelRequest*/);
                if (isTraced || needsOneMoreTry)
                {
                    //writeDebugLine("ERROR: Try Again since NULL " + tagHandler);
                    bool wsl = request.SuspendSearchLimits;
                    try
                    {
                        request.SuspendSearchLimits = true;
                        cp = tagHandlerU.Transform();
                        if (Unifiable.IsNull(cp))
                        {
                            childSuccess = false;
                            return(tagHandlerU.GetTemplateNodeInnerText());
                        }
                        if (false && Unifiable.IsNullOrEmpty(cp))
                        {
                            // trace the next line to see why
                            AIMLTagHandler handlerU = tagHandlerU;
                            TargetBot.TraceTest("ERROR: Try Again since NULL " + handlerU,
                                                () => { cp = handlerU.Transform(); });
                        }
                    }
                    finally
                    {
                        request.SuspendSearchLimits = wsl;
                    }
                }
            }
            if (tagHandlerU.QueryHasFailed)
            {
                childSuccess = false;
                return(tagHandlerU.FAIL);
            }
            childSuccess = !tagHandlerU.QueryHasFailed;
            if (!childSuccess)
            {
            }
            var st  = StaticAIMLUtils.IsSilentTag(node);
            var ine = Unifiable.IsNullOrEmpty(cp);

            if (!ine || st)
            {
                childSuccess = true;
                return(cp);
            }
            if (Unifiable.IsNull(cp))
            {
                cp = tagHandlerU.GetTemplateNodeInnerText();
                if (tagHandlerU.QueryHasSuceeded)
                {
                    childSuccess = true;
                    return(cp);
                }
                return(cp);
            }
            return(cp);
        }
        /// <summary>
        /// Recursively evaluates the template nodes returned from the Proccessor
        /// </summary>
        /// <param name="node">the node to evaluate</param>
        /// <param name="query">the query that produced targetBot node</param>
        /// <param name="request">the request from the user</param>
        /// <param name="result">the result to be sent to the user</param>
        /// <param name="user">the user who originated the request</param>
        /// <returns>the output Unifiable</returns>
        public string processNode(XmlNode node, SubQuery query,
                                  Request request, Result result, User user,
                                  AIMLTagHandler parentHandlerU, bool protectChild, bool copyParent,
                                  AIMLTagHandler nodeHandlerU, bool suspendLimits, out bool templateSucceeded)
        {
            Request originalSalientRequest = Request.GetOriginalSalientRequest(request);
            bool    osrExists          = originalSalientRequest != null;
            var     wasSuspendRestrati = request == null || request.SuspendSearchLimits;

            templateSucceeded = true;
            if (request != null)
            {
                request.SuspendSearchLimits = suspendLimits;
            }
            Dictionary <Unifiable, Unifiable> sraiMark = null;

            if (osrExists && ChatOptions.UseSraiLimitersBasedOnTextContent)
            {
                sraiMark = originalSalientRequest.CreateSRAIMark();
            }
            try
            {
                bool   childSuccess;
                string outputSentence = processNodeVV(node, query,
                                                      request, result, user, parentHandlerU,
                                                      protectChild, copyParent, nodeHandlerU, out childSuccess);
                if (!childSuccess)
                {
                    templateSucceeded = false;
                }
                if (Unifiable.IsNull(outputSentence))
                {
                    //outputSentence = tagHandler.GetTemplateNodeInnerText();
                    templateSucceeded = false;
                    return(outputSentence);
                }
                if (!Unifiable.IsNullOrEmpty(outputSentence))
                {
                    return(outputSentence);
                }
                if (StaticAIMLUtils.IsSilentTag(node) && !Unifiable.IsEMPTY(outputSentence))
                {
                    return("");
                }
                if (nodeHandlerU.FinalResultValid)
                {
                    return(nodeHandlerU.FinalResult);
                }
                return(outputSentence);
            }
            finally
            {
                if (osrExists && ChatOptions.UseSraiLimitersBasedOnTextContent)
                {
                    originalSalientRequest.ResetSRAIResults(sraiMark);
                }
                if (request != null)
                {
                    request.SuspendSearchLimits = wasSuspendRestrati;
                }
            }
        }
Esempio n. 6
0
        internal Unifiable ProcessChangeSrai(Request request, SubQuery query,
                                             Unifiable templateNodeInnerValue, XmlNode templateNode, string initialString, OutputDelegate writeToLog)
        {
#endif
            if (IsNullOrEmpty(templateNodeInnerValue))
            {
                writeToLogWarn("ERROR BAD REQUEST " + request);
                return(templateNodeInnerValue);
            }
            var salientRequest = MasterRequest.GetOriginalSalientRequest(request);
            try
            {
                Unifiable prevResult;
                var       CurrentTemplate = query.CurrentTemplate;
                if (!salientRequest.EnterSalientSRAI(templateNodeInnerValue, out prevResult))
                {
                    writeToLogWarn("ERROR EnterSailentSRAI: " + prevResult);
                    if (true)
                    {
                        var disable = CurrentTemplate;
                        if (disable != null && disable.IsDisabled)
                        {
                            request.CurrentResult.ResultTemplates.Add(disable);
                            disable.IsDisabled = true;
                            request.AddUndo("undisable loop " + disable.ToFileString(request.Requester.PrintOptions), () => { disable.IsDisabled = false; });
                        }
                    }
                    //return null;
                }
                int depth = request.SraiDepth.Current;
                if (!ChatOptions.UseSraiLimitersBasedOnTextContent)
                {
                    depth = 0;
                }
                if (CurrentTemplate != null)
                {
                    if (CurrentTemplate.IsHighlyUsefull)
                    {
                        writeToLog("IsHighlyUsefull: " + CurrentTemplate.ToFileString(request.Requester.PrintOptions));
                        request.SuspendSearchLimits = true;
                        request.depth = 0;
                    }
                }
                Unifiable subResultOutput = null;
#if false
                writeToLog = writeToLog ?? DEVNULL;
                AltBot mybot = request.TargetBot;
                User   user  = request.Requester;
#endif
                var thisrequest = request;
                var thisresult  = request.CurrentResult;

                /*
                 * writeToLog("WARNING Depth pretty deep " + templateNode + " returning empty");
                 */
                //string s;
                //if (ResultReady(out s)) return s;

                /*
                 * int d = request.GetCurrentDepth();
                 * if (d > request.SraiDepth.Max)
                 * {
                 *  writeToLog(prefix + " WARNING Depth pretty deep " + templateNode + " returning empty");
                 *  return Unifiable.Empty;
                 * }
                 */
                //if (CheckNode("srai"))
                {
                    string prefix =
                        query.prefix =
                            SafeFormat("ProcessChangeSrai: {0}: \"{1}\"\n", request.Graph, Unifiable.DescribeUnifiable(templateNodeInnerValue));

                    if (request.SraiDepth.IsOverMax && ChatOptions.UseSraiLimitersBasedOnTextContent)
                    {
                        string sss = prefix + " request.SraiDepth.IsOverMax '" + request.SraiDepth.Current + "'";
                        writeToLog(sss);
                        if (!request.SuspendSearchLimits)
                        {
                            throw new ChatSignalOverBudget(request, sss);
                            return(Unifiable.INCOMPLETE);
                        }
                    }
                    string why = request.WhyComplete;
                    if (why != null && ChatOptions.UseSraiLimitersBasedOnTextContent)
                    {
                        string sss = prefix + " " + why;
                        writeToLog(sss);
                        if (!request.SuspendSearchLimits)
                        {
                            throw new ChatSignalOverBudget(request, sss);
                            return(Unifiable.INCOMPLETE);
                        }
                    }
                    //Unifiable templateNodeInnerValue = Recurse();
                    try
                    {
                        Request subRequest = request.CreateSubRequest(templateNodeInnerValue, null,
                                                                      RequestKind.TagHandler | RequestKind.SraiTag);


                        string requestGraphSrai = request.SraiGraph;
                        subRequest.Graph = request.GetGraph(requestGraphSrai);

                        var ti = query.CurrentTemplate;
                        // make sure we veto later use of this template
                        if (ti != null)
                        {
                            subRequest.RequestTemplates.Add(ti);
                        }

                        // make sure we don't keep adding time to the request
                        bool   showDebug          = DebugSRAIs;
                        string subRequestrawInput = subRequest.rawInput;
                        if (showDebug && subRequestrawInput.Contains("SYSTEMANIM") ||
                            subRequestrawInput.Contains("HANSANIM"))
                        {
                            showDebug = false;
                        }
                        if (base.IsTraced)
                        {
                            showDebug = true;
                        }

                        if (showDebug)
                        {
                            writeToLog(prefix + " CALLING '" + subRequestrawInput + "'");
                        }


                        if (mybot.chatTrace)
                        {
                            //  mybot.bot.writeChatTrace("\"L{0}\" -> \"S{1}\" ;\n", depth - 1, depth-1);
                            // mybot.bot.writeChatTrace("\"L{0}\" -> \"S{1}\" ;\n", depth, depth);
                            //mybot.bot.writeChatTrace("\"S{0}\" -> \"SRC:{1}\" ;\n", depth, CatTextInfo());

                            //mybot.bot.writeChatTrace("\"S{0}\" -> \"S{1}\" ;\n", depth - 1, depth);
                            //mybot.bot.writeChatTrace("\"S{0}\" -> \"SIN:{1}\" ;\n", depth, subRequestrawInput);
                            //mybot.bot.writeChatTrace("\"SIN:{0}\" -> \"LN:{1}\" ;\n", subRequestrawInput, CatTextInfo());
                        }
                        if (depth > 200 && ChatOptions.UseSraiLimitersBasedOnTextContent)
                        {
                            writeToLog(prefix + " FAILING TOOOO DEEEEP '" + request + "'");
                            return(Unifiable.INCOMPLETE);
                        }
                        if (subRequestrawInput.Contains("STDCATCHALL STDCATCHALL"))
                        {
                            // @TODO @debug this
                            writeToLog(prefix + " STDCATCHALL X 2 was '" + subRequestrawInput + "'");
                            return(Unifiable.INCOMPLETE);

                            throw new InvalidCastException("loop STDCATCHALL STDCATCHALL");
                        }
                        MasterResult subResult;
                        string       subQueryRawOutputText;
                        subResult = GetSubResult(prefix, request, user, mybot, (MasterRequest)subRequest, showDebug,
                                                 out subResultOutput,
                                                 out subQueryRawOutputText, writeToLog);


                        string whyComplete          = thisrequest.WhyComplete = subRequest.WhyComplete;
                        string subResultOutputTrace = Unifiable.DescribeUnifiable(subResultOutput);
                        if (Unifiable.IsNull(subResultOutput))
                        {
                            if (showDebug)
                            {
                                writeToLog(prefix + "MISSING RETURN " + whyComplete);
                            }
                            subResult            = (MasterResult)mybot.ChatFor1Result(subRequest, subResult, RequestKind.TagHandler | RequestKind.SraiTag);
                            subResultOutput      = subResult.Output;
                            subResultOutputTrace = Unifiable.DescribeUnifiable(subResultOutput);
                            //subQueryRawOutput = subResult.RawOutput.Trim();
                            if (!IsNullOrEmpty(subResultOutput))
                            {
                                writeToLog(prefix + "RESCUED RETURN " + subResultOutput);
                                //  subQueryRawOutput = "" + subResultOutput;
                            }
                            // This is the failure cases
                            if (Unifiable.IsNull(subResultOutput))
                            {
                                if (mybot.chatTrace)
                                {
                                    ShowChatTrace(subRequestrawInput, mybot, depth, subResult, templateNode);
                                }
                                request.IsTraced = true;
                                why = subRequest.WhyComplete ?? "ERRORY";
                                writeToLog("{0} NULL?! RETURN {1} {2} '{3}'", why + ": " + prefix, subRequestrawInput,
                                           subResult.Score, subResultOutputTrace);
                                return(subResultOutput);
                            }
                        }
                        if (Unifiable.IsEMPTY(subResultOutput))
                        {
                            request.IsTraced = true;
                            why = subRequest.WhyComplete ?? "ERRORY";
                            writeToLog("{0} EMPTY?! RETURN {1} {2} '{3}'", why + ": " + prefix, subRequestrawInput,
                                       subResult.Score, subResultOutputTrace);
                            return(subResultOutput);
                        }
                        {
                            // ReSharper disable ConditionIsAlwaysTrueOrFalse
                            if (query != null)
                            // ReSharper restore ConditionIsAlwaysTrueOrFalse
                            {
                                double before = query.Request.TopLevelScore;
                                if (query.CurrentTemplate != null)
                                {
                                    query.Request.TopLevelScore *= (subResult.Score * query.CurrentTemplate.TemplateRating);
                                    if (showDebug)
                                    {
                                        writeToLog("SCORE {0}*{1}*{2}->{3} ",
                                                   before, subResult.Score, query.CurrentTemplate.TemplateRating,
                                                   query.Request.TopLevelScore);
                                        writeToLog("{0} SUCCESS RETURN {1}  {2} '{3}'", prefix, subRequestrawInput,
                                                   subResult.Score, subResultOutputTrace);
                                    }
                                }
                            }
                            thisrequest.AddSubResult(thisresult);
                            thisrequest.AddSubResult(subResult);
                        }

                        if (mybot.chatTrace)
                        {
                            ShowChatTrace(subRequestrawInput, mybot, depth, subResult, templateNode);
                        }
                        //salientRequest.ExitSalientSRAI(templateNodeInnerValue, subResultOutput);
                        return(subResultOutput);
                    }
                    finally
                    {
                        if (subResultOutput != null)
                        {
                            salientRequest.ExitSalientSRAI(templateNodeInnerValue, subResultOutput);
                        }
                        // @TODO @HACK @BUG stops loops?
                        request.DisableTemplateUntilFinished(CurrentTemplate);
                    }
                }
                return(Unifiable.Empty);
            }
            finally
            {
                //depth--;
            }
        }
Esempio n. 7
0
 protected Unifiable ProcessChangeSraiPre(Unifiable templateNodeInnerValue)
 {
     throw bot.RaiseError("wrong SRAI!");
     {
         try
         {
             { if (false && IsNull(templateNodeInnerValue))
               {
                   templateNodeInnerValue = Recurse();
               }
               TemplateInfo queryTemplate = query.CurrentTemplate;
               if (queryTemplate != null)
               {
                   if (!result.CanResultUseTemplate(queryTemplate))
                   {
                       writeToLogWarn("!result.CanResultUseTemplate " + queryTemplate);
                       return(Unifiable.INCOMPLETE);
                   }
                   if (!request.CanUseRequestTemplate(queryTemplate))
                   {
                       writeToLogWarn("!request.CanUseRequestTemplate " + queryTemplate);
                       return(Unifiable.INCOMPLETE);
                   }
               }
               templateNodeInnerValue = AltBot.CleanupCyc(templateNodeInnerValue);
               var vv = ProcessChangeSrai(templateNodeInnerValue);
               if (!Unifiable.IsNullOrEmpty(vv))
               {
                   return(vv);
               }
               if (Unifiable.IsNull(vv))
               {
                   writeToLogWarn("NULL SRAI!?!");
               }
               else
               {
                   if (IsSilentTag(templateNode.ChildNodes))
                   {
                       return(Unifiable.Empty);
                   }
                   return(vv); // Empty
               }
               if (ProcessChange12)
               {
                   writeToLogWarn("ProcessChange12 cant get result");
                   return(null);
               }
               if (Unifiable.IsNull(vv))
               {
                   vv = GetTemplateNodeInnerText();
                   return(FAIL);
               }
               return(vv); }
         }
         catch (ChatSignal ex)
         {
             throw;
         }
         catch (Exception e)
         {
             writeToLog("ERROR: " + e);
             throw;
         }
     }
 }