예제 #1
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="Proc">The Proc involved in this request</param>
 /// <param name="user">The user making the request</param>
 /// <param name="query">The query that originated this node</param>
 /// <param name="request">The request inputted into the system</param>
 /// <param name="result">The result to be passed to the user</param>
 /// <param name="templateNode">The node to be processed</param>
 public LoadingTagHandler(AltBot bot,
                          User user,
                          SubQuery query,
                          Request request,
                          Result result,
                          XmlNode templateNode)
     : base(bot, user, query, request, result, templateNode)
 {
     isRecursive = false;
 }
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="bot">The bot involved in this request</param>
 /// <param name="user">The user making the request</param>
 /// <param name="query">The query that originated this node</param>
 /// <param name="request">The request inputted into the system</param>
 /// <param name="result">The result to be passed to the user</param>
 /// <param name="templateNode">The node to be processed</param>
 public AIMLFormatingTagHandler(AltBot bot,
                                User user,
                                SubQuery query,
                                Request request,
                                Result result,
                                XmlNode templateNode)
     : base(bot, user, query, request, result, templateNode)
 {
     isRecursive      = true;
     IsStarAtomically = true;
 }
예제 #3
0
        private static Unifiable ResolveVariableValue(string str, SubQuery query, ISettingsDictionary dict, out bool found)
        {
            Unifiable value = null;
            bool      rfound;

            if (str.Contains("{"))
            {
                str = str.Replace("{", "").Replace("}", "");
            }
            if (str.StartsWith("query."))
            {
                ISettingsDictionary dict2 = query;
                str   = str.Substring(4);
                value = GetValue(query, dict2, str, out rfound);
                if (!IsNullOrEmpty(value))
                {
                    found = true;
                    return(value);
                }
            }
            if (str.StartsWith("bot."))
            {
                ISettingsDictionary dict2 = query.Request.Responder;
                str   = str.Substring(4);
                value = GetValue(query, dict2, str, out found);
                if (!IsNullOrEmpty(value))
                {
                    found = true;
                    return(value);
                }
            }
            else if (str.StartsWith("user."))
            {
                ISettingsDictionary dict2 = query.Request.Requester;
                str   = str.Substring(5);
                value = GetValue(query, dict2, str, out found);
                if (!IsNullOrEmpty(value))
                {
                    found = true;
                    return(value);
                }
            }
            if (dict != null)
            {
                value = GetValue(query, dict, str, out found);
                if (!IsNullOrEmpty(value))
                {
                    found = true;
                    return(value);
                }
            }
            found = false;
            return(null);
        }
예제 #4
0
 public void AddBindingSet(SubQuery query)
 {
     lock (this)
     {
         if (Bindings == null)
         {
             Bindings = new List <SubQuery>();
         }
         Bindings.Add(query);
         AddPattern(query.Pattern);
     }
 }
예제 #5
0
//        public event Func<SubQuery, Request, bool> OutputsCreateOnSuccees;
//        public event Func<SubQuery, Request, bool> TemplateSucceededCallback;
//        public event Func<SubQuery, Request, bool> TemplateFailedCallback;

        public void OnOutputsCreated(SubQuery query, Request request)
        {
            if (NeckCut)
            {
                query.Result.WhyResultComplete = "OnOutputsCreated cut from " + ToString();
                if (request.SuspendSearchLimits)
                {
                    request.SuspendSearchLimits = false;
                }
            }
            //if (OutputsCreateOnSuccees != null) OutputsCreateOnSuccees(query, request);
        }
예제 #6
0
 public void OnTemplatesFailed(SubQuery query, Request request)
 {
     if (NeckCut)
     {
         query.Result.WhyResultComplete = "OnTemplatesFailed cut from " + ToString();
         if (request.SuspendSearchLimits)
         {
             request.SuspendSearchLimits = false;
         }
     }
     //if (TemplateFailedCallback != null) TemplateFailedCallback(query, request);
 }
예제 #7
0
        public static bool IsPredMatch(Unifiable required, Unifiable actualValue, SubQuery subquery)
        {
            if (IsNull(required))
            {
                return(IsNullOrEmpty(actualValue));
            }
            required = required.Trim();
            if (required.IsAnyText)
            {
                return(!IsNullOrEmpty(actualValue));
            }

            string requiredToUpper = required.ToUpper();

            if (requiredToUpper == "*")
            {
                return(!IsUnknown(actualValue));
            }

            if (requiredToUpper == "OM" || IsNullOrEmpty(required) || requiredToUpper == "$MISSING")
            {
                return(IsNullOrEmpty(actualValue) || actualValue == "OM");
            }
            if (IsIncomplete(required))
            {
                return(IsIncomplete(actualValue));
            }
            if (IsNull(actualValue))
            {
                return(IsNullOrEmpty(required));
            }
            actualValue = actualValue.Trim();
            if (actualValue.WillUnify(required, subquery))
            {
                return(true);
            }
            string requiredAsStringReplaceReplace = required.AsString().Replace(" ", "\\s")
                                                    .Replace("*", "[\\sA-Z0-9]+").Replace("_", "[A-Z0-9]+");
            Regex matcher = new Regex("^" + requiredAsStringReplaceReplace + "$",
                                      RegexOptions.IgnoreCase);

            if (matcher.IsMatch(actualValue))
            {
                return(true);
            }
            if (requiredToUpper == "UNKNOWN" && (IsUnknown(actualValue)))
            {
                return(true);
            }
            return(false);
        }
예제 #8
0
        public bool IsSatisfied(SubQuery subQuery)
        {
            var rules = this.Preconds;

            if (rules != null && rules.Count > 0)
            {
                foreach (var s in rules)
                {
                    if (!s.IsConditionTrue(subQuery))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
예제 #9
0
        /*
         * public static int NonAlphaCount(string input)
         * {
         *  input = CleanWhitepaces(input);
         *  int na = 0;
         *  foreach (char s in input)
         *  {
         *      if (char.IsLetterOrDigit(s)) continue;
         *      na++;
         *  }
         *  return na;
         * }
         *
         * public static string NodeInfo(XmlNode templateNode, Func<string, XmlNode, string> funct)
         * {
         *  string s = null;
         *  XmlNode nxt = templateNode;
         *  s = funct("same", nxt);
         *  if (s != null) return s;
         *  nxt = templateNode.NextSibling;
         *  s = funct("next", nxt);
         *  if (s != null) return s;
         *  nxt = templateNode.PreviousSibling;
         *  s = funct("prev", nxt);
         *  if (s != null) return s;
         *  nxt = templateNode.ParentNode;
         *  s = funct("prnt", nxt);
         *  if (s != null) return s;
         *  return s;
         * }
         */

        public static T ReduceStar <T>(IConvertible name, SubQuery query, ISettingsDictionary dict, out bool rfound)
            where T : IConvertible
        {
            var nameSplit = name.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string nameS in nameSplit)
            {
                Unifiable r = AltStar(nameS, query, dict, out rfound);
                if (!IsNullOrEmpty(r) || rfound)
                {
                    PASSTHRU <T>(r);
                }
                continue;
            }
            rfound = false;
            return(PASSTHRU <T>(name));
        }
예제 #10
0
        public static bool IsLogicTF(Unifiable v, SubQuery subquery)
        {
            if (IsFalse(v))
            {
                return(false);
            }
            String value = v.ToValue(subquery).ToLower();

            if (value.Length == 0)
            {
                return(false);
            }
            char c = value[0];

            if (c == 'n' || c == 'f')
            {
                return(false);
            }
            return(true);
        }
예제 #11
0
        public bool IsConditionTrue(SubQuery subQuery)
        {
            string tagName = this.TagName;
            Func <int, int, User, Unifiable> getThat = subQuery.GetMatcher(tagName);

            int[] idx2      = GetIndex2(IndexVal);
            User  responder = null;
            var   val       = getThat(idx2[0], idx2[1], responder);

            try
            {
                var res = UnifibleTagHandler.UnifyStars((string)Pattern, (string)val);
                return(res != null);
            }
            catch (Exception e)
            {
                writeToLog("ERROR: " + e);
                return(false);

                throw;
            }
        }
예제 #12
0
        internal static string GetNameOfDict_S(SubQuery query, string dictName, XmlNode templateNode, out ISettingsDictionary dict)
        {
            string type = TextPatternUtils.ToLower(dictName);

            //ISettingsDictionary udict = query.GetDictionary(type, templateNode, dict);
            while (templateNode != null)
            {
                string type0 = TextPatternUtils.GetAttribValue(templateNode, "type,dict", null);
                if (type0 != null)
                {
                    type = type0;
                    break;
                }
                string uname = TextPatternUtils.GetAttribValue(templateNode, "user", null);
                if (uname != null)
                {
                    type0 = TextPatternUtils.GetNamedType("user", uname);

                    if (type0 != null)
                    {
                        type = type0;
                        break;
                    }
                }
                string bname = TextPatternUtils.GetAttribValue(templateNode, "bot", null);
                if (bname != null)
                {
                    type0 = TextPatternUtils.GetNamedType("bot", bname);
                    if (type0 != null)
                    {
                        type = type0;
                        break;
                    }
                }
                dict = query.Request.GetDictionary(templateNode.LocalName);
                if (dict != null)
                {
                    type = dict.NameSpace;
                    break;
                }
                templateNode = templateNode.ParentNode;
            }
            if (type == null)
            {
                type = dictName;
            }
            bool preferBotOverUser = (type == "bot");

            if (preferBotOverUser)
            {
                dict = query.Request.Responder.Predicates;
            }
            else
            {
                dict = query.Request.GetDictionary(type);
            }
            if (dict == null)
            {
                dict = query.Request.TargetSettings;
            }
            return(type ?? dict.NameSpace);
        }
예제 #13
0
        public static int FromInsideLoaderContext(XmlNode currentNode, Request request, SubQuery query, Func <int> doit)
        {
            int total = 0;

            query = query ?? request.CurrentQuery;
            //Result result = query.Result;
            AltBot     RProcessor = request.TargetBot;
            AIMLLoader prev       = RProcessor.Loader;

            try
            {
                // RProcessor.Loader = this;
                // Get a list of the nodes that are children of the <aiml> tag
                // these nodes should only be either <topic> or <category>
                // the <topic> nodes will contain more <category> nodes
                string currentNodeName = currentNode.Name.ToLower();

                var ts = EnterTag(request, currentNode, query);
                try
                {
                    total += doit();
                }
                finally
                {
                    ts();
                }
            }
            finally
            {
                RProcessor.Loader = prev;
            }
            return(total);
        }
예제 #14
0
 public void OnTemplatesSucceeded(SubQuery query, Request request)
 {
     //if (TemplateSucceededCallback != null) TemplateSucceededCallback(query, request);
 }
예제 #15
0
 public static Unifiable AltStar(string name, SubQuery query, ISettingsDictionary dict, out bool rfound)
 {
     try
     {
         if (name.Contains(","))
         {
             foreach (string subname in NamesStrings(name))
             {
                 var val = AltStar(name, query, dict, out rfound);
                 if (rfound)
                 {
                     rfound = true;
                     return(val);
                 }
             }
         }
         if (name.StartsWith("star_"))
         {
             return(GetDictData(query.InputStars, name, 5, out rfound));
         }
         else if (name.StartsWith("inputstar_"))
         {
             return(GetDictData(query.InputStars, name, 10, out rfound));
         }
         else if (name.StartsWith("input_"))
         {
             return(GetDictData(query.InputStars, name, 6, out rfound));
         }
         else if (name.StartsWith("thatstar_"))
         {
             return(GetDictData(query.ThatStars, name, 9, out rfound));
         }
         else if (name.StartsWith("that_"))
         {
             return(GetDictData(query.ThatStars, name, 5, out rfound));
         }
         else if (name.StartsWith("topicstar_"))
         {
             return(GetDictData(query.TopicStar, name, 10, out rfound));
         }
         else if (name.StartsWith("topic_"))
         {
             return(GetDictData(query.TopicStar, name, 6, out rfound));
         }
         else if (name.StartsWith("guardstar_"))
         {
             return(GetDictData(query.GuardStar, name, 10, out rfound));
         }
         else if (name.StartsWith("guard_"))
         {
             return(GetDictData(query.GuardStar, name, 6, out rfound));
         }
         else if (name.StartsWith("@"))
         {
             Unifiable value = query.Request.TargetBot.SystemExecute(name, null, query.Request);
             rfound = true;
             if (!IsNullOrEmpty(value))
             {
                 return(value);
             }
             return(value);
         }
         else if (name.StartsWith("%dictvar_"))
         {
             Unifiable value = value = GetValue(query, dict, name.Substring(8), out rfound);
             if (rfound)
             {
                 return(value);
             }
             return(value);
         }
         else
         {
             if (name.StartsWith("%") || name.StartsWith("$"))
             {
                 string str = name.Substring(1);
                 var    vv  = ResolveVariableValue(str, query, dict, out rfound);
                 if (rfound)
                 {
                     return(vv);
                 }
                 return(vv);
             }
             else if (name.Contains("."))
             {
                 var vv = ResolveVariableValue(name, query, dict, out rfound);
                 if (rfound)
                 {
                     return(vv);
                 }
                 return(vv);
             }
             rfound = false;
             return(name);
         }
     }
     catch (Exception e)
     {
         AltBot.writeDebugLine("" + e);
         rfound = false;
         return(null);
     }
 }
예제 #16
0
 /// <summary>
 /// Provides an instantiation of the class represented by this tag-handler
 /// </summary>
 /// <param name="Assemblies">All the assemblies the bot knows about</param>
 /// <returns>The instantiated class</returns>
 public AIMLTagHandler Instantiate(Dictionary <string, Assembly> Assemblies, User user, SubQuery query,
                                   Request request, Result result, XmlNode node, AltBot bot)
 {
     lock (Assemblies)
         if (Assemblies.ContainsKey(this.AssemblyName))
         {
             if (type != null)
             {
                 if (info == null)
                 {
                     info = type.GetConstructor(CONSTRUCTOR_TYPES);
                 }
                 if (info != null)
                 {
                     return((AIMLTagHandler)info.Invoke(new object[] { bot, user, query, request, result, node }));
                 }
             }
             Assembly       tagDLL       = Assemblies[this.AssemblyName];
             AIMLTagHandler newCustomTag = (AIMLTagHandler)tagDLL.CreateInstance(ClassName);
             if (newCustomTag == null)
             {
                 return(null);
             }
             newCustomTag.query        = query;
             newCustomTag.request      = request;
             newCustomTag.result       = result;
             newCustomTag.templateNode = node;
             newCustomTag.bot          = bot;
             newCustomTag.user         = user;
             return(newCustomTag);
         }
         else
         {
             return(null);
         }
 }
예제 #17
0
        public static Action EnterTag(Request request, XmlNode templateNode, SubQuery query)
        {
            if (templateNode.NodeType != XmlNodeType.Element)
            {
                return(DoNothing);

                throw new NotImplementedException("EnterTag: " + templateNode.NodeType);
            }
            bool                needsUnwind = false;
            UndoStackHolder     thiz        = (UndoStackHolder)query ?? request;
            ISettingsDictionary dict        = query;

            if (dict == null && request != null)
            {
                dict = request.TargetSettings;
            }
            XmlAttributeCollection collection = templateNode.Attributes;

            EnterContext(request, query);
            if (collection == null || collection.Count <= 0)
            {
                return(() =>
                {
                    ExitContext(request, query);
                });
            }
            else
            {
                var    used           = new List <XmlAttribute>();
                string defaultElement = "";
                Action gmrerstore;
                gmrerstore = request.WithAttributesForUnwind(templateNode, ref defaultElement, used);
                int       uc          = used.Count;
                UndoStack savedValues = null;

                foreach (XmlAttribute node in collection)
                {
                    if (used.Contains(node))
                    {
                        continue;
                    }
                    bool   found;
                    string n = node.Name.ToLower();
                    switch (n)
                    {
                    case "state":
                    case "flag":
                    case "graph":
                    case "topic":
                    case "that":
                        break;

                    default:
                    {
                        lock (ReservedAttributes)
                        {
                            if (ReservedAttributes.Contains(n))
                            {
                                continue;
                            }
                            bool prev = NamedValuesFromSettings.UseLuceneForGet;
                            try
                            {
                                NamedValuesFromSettings.UseLuceneForGet = false;
                                if (!dict.containsSettingCalled(n))
                                {
                                    ReservedAttributes.Add(n);
                                    request.writeToLog("ReservedAttributes: {0}", n);
                                }
                                else
                                {
                                    if (!PushableAttributes.Contains(n))
                                    {
                                        PushableAttributes.Add(n);
                                        request.writeToLog("PushableAttributes: {0}", n);
                                    }
                                }
                            }
                            finally
                            {
                                NamedValuesFromSettings.UseLuceneForGet = prev;
                            }
                        }

                        // now require temp vars to s   ay  with_id="tempId"
                        // to set the id="tempid" teporarily while evalig tags
                        if (!n.StartsWith("with_"))
                        {
                            continue;
                        }
                        else
                        {
                            n = n.Substring(5);
                        }

                        Unifiable v = ReduceStar <Unifiable>(node.Value, query, dict, out found);
                        UndoStack.FindUndoAll(thiz, true);
                        savedValues = savedValues ?? UndoStack.GetStackFor(thiz);
                        //savedValues = savedValues ?? query.GetFreshUndoStack();
                        savedValues.pushValues(dict, n, v);
                        needsUnwind = true;
                    }
                    break;
                    }
                }

                // unwind

                return(() =>
                {
                    if (needsUnwind)
                    {
                        try
                        {
                            EnterContext(request, query);
                            if (savedValues != null)
                            {
                                savedValues.UndoAll();
                            }
                        }
                        catch (Exception ex)
                        {
                            request.writeToLog("ERROR " + ex);
                        }
                        finally
                        {
                            ExitContext(request, query);
                        }
                    }
                    ExitContext(request, query);
                    if (uc > 0)
                    {
                        gmrerstore();
                    }
                });
            }
        }