Esempio n. 1
0
        protected override Unifiable ProcessChangeU()
        {
            if (FinalResultValid)
            {
                return(FinalResult);
            }
            TemplateInfo queryTemplate = query.CurrentTemplate;

            if (queryTemplate != null)
            {
                if (queryTemplate.IsDisabledOutput)
                {
                    return(Unifiable.INCOMPLETE);
                }
                Succeed();
                request.MarkTemplate(queryTemplate);
            }
            Unifiable templateResult = RecurseReal(templateNode, false);
            Unifiable test           = templateResult;

            if (Unifiable.IsEMPTY(test))
            {
                if (QueryHasFailed)
                {
                    return(FAIL);
                }
                if (IsSilentTag(templateNode))
                {
                    return(templateResult);
                }
                ResetValues(true);
                templateResult = RecurseReal(templateNode, false);
            }
            if (templateResult == null)
            {
                return(null);
            }
            return(AltBot.ReTrimAndspace(AltBot.CleanupCyc(templateResult)));
        }
        static public Unifiable GetSettingForType(string subject, SubQuery query, ISettingsDictionary dict, string name, out string realName, string gName, Unifiable defaultVal, out bool succeed, XmlNode node)
        {
            Request             request    = query.Request;
            OutputDelegate      writeToLog = request.writeToLog ?? TextPatternUtils.DEVNULL;
            AltBot              TargetBot  = request.TargetBot;
            ISettingsDictionary udict;
            string              dictName = AIMLTagHandler.GetNameOfDict(query, subject ?? dict.NameSpace, node, out udict);
            // try to use a global blackboard predicate
            User gUser = TargetBot.ExemplarUser;

            defaultVal = StaticXMLUtils.GetAttribValue(node, "default,defaultValue", defaultVal);
            gName      = StaticXMLUtils.GetAttribValue(node, "global_name", gName);

            string realName0;


            var vv = ScriptManager.GetGroup(query.TargetBot.ObjectRequester, dictName, name);
            {
                if (vv != null)
                {
                    if (vv.Count == 0)
                    {
                        succeed  = true;
                        realName = name;
                        return("");
                    }
                    succeed  = true;
                    realName = name;
                    foreach (var e in vv)
                    {
                        return(Unifiable.Create(e));
                    }
                }
            }
            Unifiable resultGet = SettingsDictionaryReal.grabSettingDefaultDict(udict, name, out realName0);

            if (ReferenceEquals(resultGet, null))
            {
                realName  = null;
                resultGet = Unifiable.NULL;
            }
            // if ((!String.IsNullOrEmpty(result)) && (!result.IsWildCard())) return result; // we have a local one

            String realNameG;
            // try to use a global blackboard predicate
            Unifiable gResult = SettingsDictionaryReal.grabSettingDefaultDict(gUser.Predicates, gName, out realNameG);

            if ((Unifiable.IsMissing(resultGet)) && (!Unifiable.IsMissing(gResult)))
            {
                // result=nothing, gResult=something => return gResult
                writeToLog("SETTINGS OVERRIDE " + gResult);
                succeed  = true;
                realName = realNameG;
                // return gResult;
            }
            string sresultGet = resultGet.ToValue(query);

            // if Unknown or empty
            if (UseLuceneForGet && Unifiable.IsMissing(sresultGet))
            {
                Unifiable userName = udict.grabSetting("id");
                if (Unifiable.IsNullOrEmpty(userName))
                {
                    writeToLog("ERROR IsNullOrEmpty id in " + udict.NameSpace);
                }
                ITripleStore userbotLuceneIndexer = (ITripleStore)query.Request.TargetBot.TripleStore;
                string       resultLucene         = userbotLuceneIndexer.queryTriple(userName, name, node);
                if (!string.IsNullOrEmpty(resultLucene))
                {
                    succeed  = true;
                    realName = name;
                    return(resultLucene);
                }
            }


            if (sresultGet != null)
            {
                if (sresultGet.ToUpper() == "UNKNOWN")
                {
                    succeed  = false;
                    realName = null;
                    return(sresultGet + " " + name);
                }
                else if (Unifiable.IsEMPTY(resultGet))
                {
                    succeed  = true;
                    realName = name;
                    return(resultGet);
                }
                else if (Unifiable.IsUnknown(resultGet))
                {
                    succeed  = false;
                    realName = name;
                    return(resultGet);
                }
            }
            if (!String.IsNullOrEmpty(sresultGet))
            {
                succeed  = true;
                realName = realName0;
                query.GetDictValue++;
                if (!IsNullOrEmpty(gResult))
                {
                    if (resultGet.IsWildCard)
                    {
                        realName = realNameG;
                        // result=*, gResult=something => return gResult
                        return(gResult);
                    }
                    // result=something, gResult=something => return result
                    return(resultGet);
                }
                else
                {
                    // result=something, gResult=nothing => return result
                    return(resultGet);
                }
            }
            if (defaultVal == null)
            {
                succeed  = false;
                realName = null;
                return(defaultVal);
            }
            // default => return defaultVal
            succeed  = true;
            realName = realName0;
            return(ReturnSetSetting(udict, name, defaultVal));
            //return defaultVal;
        }
        /// <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. 4
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--;
            }
        }