Ejemplo n.º 1
0
        static MasterResult GetSubResult(String prefix, Request prevRequest, User user, AltBot mybot, Request subRequest, bool showDebug, out Unifiable subResultOutput, out string subQueryRawOutput1, OutputDelegate writeToLog)
        {
            var          prev      = subRequest.GraphsAcceptingUserInput;
            var          prevSO    = user.SuspendAddResultToUser;
            MasterResult subResult = subRequest.CreateResult(subRequest);

            try
            {
                Dictionary <Unifiable, Unifiable> sraiMark = null;
                var originalSalientRequest = Request.GetOriginalSalientRequest(prevRequest);
                if (ChatOptions.UseSraiLimitersBasedOnTextContent)
                {
                    sraiMark = originalSalientRequest.CreateSRAIMark();
                }
                subRequest.GraphsAcceptingUserInput = true;
                //var newresult = new AIMLbot.Result(request.user, Proc, request);
                //subRequest.result = newresult;
                user.SuspendAddResultToUser = true;
                if (prevRequest.IsTraced)
                {
                    subRequest.IsTraced = !showDebug;
                }
                subRequest.IsTraced = true;
                subResult           = (MasterResult)mybot.ChatWithToplevelResults(subRequest, subResult, false,
                                                                                  RequestKind.TagHandler | RequestKind.SraiTag);
                subResultOutput = subResult.RawOutput;
                int resultCount = subResult.OutputSentences.Count;
                if (AltBot.BE_COMPLETE_NOT_FAST && resultCount == 0)
                {
                    subRequest.ResetValues(false);
                    if (ChatOptions.UseSraiLimitersBasedOnTextContent)
                    {
                        originalSalientRequest.ResetSRAIResults(sraiMark);
                    }
                    if (Unifiable.IsNullOrEmpty(subResultOutput))
                    {
                        subResult       = (MasterResult)mybot.ChatFor1Result(subRequest, subResult, RequestKind.TagHandler | RequestKind.SraiTag);
                        subResultOutput = subResult.Output;
                        if (!IsNullOrEmpty(subResultOutput))
                        {
                            writeToLog(prefix + "RESCUED RETURN " + subResultOutput);
                            //  subQueryRawOutput = "" + subResultOutput;
                        }
                    }
                }
            }
            finally
            {
                user.SuspendAddResultToUser         = prevSO;
                subRequest.GraphsAcceptingUserInput = prev;
            }
            subQueryRawOutput1 = subResultOutput;//.Trim();
            return(subResult);
        }
Ejemplo n.º 2
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--;
            }
        }