Пример #1
0
 public override string ToString()
 {
     if (IsValid)
     {
         return(_value);
     }
     return("!Valid " + Unifiable.DescribeUnifiable(_value));
 }
Пример #2
0
 /// <summary>
 /// The method that does the actual Processing of the text.
 ///
 /// This like the think tag retunns nothihng.. but logs it to the console
 /// </summary>
 /// <returns>The resulting Processed text</returns>
 protected override Unifiable Format(Unifiable templateNodeInnerText)
 {
     if (CheckNode("gossip"))
     {
         // gossip is merely logged by the Proc and written to log files
         if (!IsNullOrEmpty(templateNodeInnerText))
         {
             Unifiable intext = Unifiable.DescribeUnifiable(templateNodeInnerText);
             writeToLog(SafeFormat("GOSSIP from user: {0}, '{1}'", user.UserID, intext));
             return(intext);
         }
     }
     return(Succeed(templateNodeInnerText));
 }
Пример #3
0
        public bool pushValues(ISettingsDictionary settings, string n, Unifiable v)
        {
            bool local         = settings.containsLocalCalled(n);
            bool containsAtAll = settings.containsSettingCalled(n);

            Unifiable oldValue = settings.grabSetting(n);

            if (oldValue == v)
            {
                return(false);
            }
            string debugStr = String.Format("pushValues {0} {1} {2}", settings, n, v);

            if (!local)
            {
                settings.addSetting(n, v);
                AddUndo(debugStr, () =>
                {
                    Unifiable newValue = settings.grabSetting(n);
                    if (newValue != v)
                    {
                        writeToLog("ERROR unexpected '" + n + "'='" +
                                   Unifiable.DescribeUnifiable(newValue) + "' expecting '" +
                                   Unifiable.DescribeUnifiable(v) + "' ");
                    }
                    settings.removeSetting(n);
                });
            }
            else
            {
                settings.updateSetting(n, v);
                AddUndo(debugStr, () =>
                {
                    Unifiable newValue = settings.grabSetting(n);
                    if (newValue != v)
                    {
                        writeToLog("ERROR unexpected '" + n + "'='" +
                                   Unifiable.DescribeUnifiable(newValue) + "' expecting '" +
                                   Unifiable.DescribeUnifiable(v) + "' ");
                    }
                    settings.updateSetting(n, oldValue);
                });
            }
            return(true);
        }
Пример #4
0
 protected Unifiable ProcessChange0()
 {
     if (CheckNode("get,bot"))
     {
         string    name = GetAttribValue(templateNode, "name,var", () => templateNodeInnerText, ReduceStarAttribute);
         bool      succeed;
         Unifiable v = GetActualValue(name, templateNode.Name, out succeed);
         if (!IsValue(v))
         {
             if (!succeed)
             {
                 Unifiable defaultVal = GetAttribValue("default", null);
                 if (defaultVal == null)
                 {
                     if (v == null)
                     {
                         return("");
                     }
                     if (!QueryHasSuceeded)
                     {
                         QueryHasFailed = true;
                         return(v);
                     }
                 }
                 return(defaultVal);
             }
             // trace the next line to see why
             Proc.TraceTest("!IsValue(" + Unifiable.DescribeUnifiable(v) + ") from success?!",
                            () => GetActualValue(name, templateNode.Name, out succeed));
             return(v);
         }
         if (succeed)
         {
             Succeed();
             return(v);
         }
         //if (request.IsToplevelRequest)
         return(v);
         //return FAIL;
     }
     return(Unifiable.Empty);
 }
        /// <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);
        }
Пример #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--;
            }
        }