Ejemplo n.º 1
0
        static public string ToEnglish(string sentenceIn, ISettingsDictionary OutputSubstitutions)
        {
            var writeToLog = (OutputDelegate)null;

            if (sentenceIn == null)
            {
                return(null);
            }
            sentenceIn = sentenceIn.Trim();
            if (sentenceIn == "")
            {
                return("");
            }
            var    sentence      = "";
            string xmlsentenceIn = ToEnglishT(sentenceIn);

            if (xmlsentenceIn == "")
            {
                return("");
            }
            sentence = ApplySubstitutions.Substitute(OutputSubstitutions, xmlsentenceIn);
            if (sentenceIn != sentence)
            {
                if (writeToLog != null)
                {
                    writeToLog("SUBTS: " + sentenceIn + " -> " + sentence);
                }
            }
            sentence = AltBot.CleanupCyc(sentence);
            sentence = ApplySubstitutions.Substitute(OutputSubstitutions, sentence);
            return(sentence.Trim());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a bespoke setting to the Settings class (accessed via the grabSettings(string name)
        /// method.
        /// </summary>
        /// <param name="name">The name of the new setting</param>
        /// <param name="value">The value associated with this setting</param>
        public bool addSetting(string name, object value)
        {
            ISettingsDictionary dict = Request.TargetSettings;

            if (!UseLuceneForSet)
            {
                return(SettingsDictionaryReal.addSettingWithUndoCommit(this, dict, dict.addSetting, name, value));
            }
            else
            {
                string realName;
                bool   succeed;

                var prev = NamedValuesFromSettings.UseLuceneForGet;
                NamedValuesFromSettings.UseLuceneForGet = true;
                try
                {
                    NamedValuesFromSettings.SetSettingForType(dict.NameSpace, this, dict, name, null, value, null, null);
                }
                finally
                {
                    NamedValuesFromSettings.UseLuceneForGet = prev;
                }
                return(true);
            }
        }
        static public Unifiable SetSettingForType(string subject, SubQuery query, ISettingsDictionary dict, string name, string gName, object value, string setReturn, XmlNode templateNode)
        {
            string _sreturn = setReturn;

            setReturn = StaticXMLUtils.GetAttribValue <string>(templateNode, "set-return", () => _sreturn, query.ReduceStarAttribute <string>);

            Request request   = query.Request;
            AltBot  TargetBot = request.TargetBot;
            // try to use a global blackboard predicate
            User gUser = TargetBot.ExemplarUser;

            string    realName;
            Unifiable resultGet = SettingsDictionaryReal.grabSettingDefaultDict(dict, name, out realName);
            bool      shouldSet = ShouldSet(templateNode, dict, realName, value, resultGet, query);

            User         user = query.CurrentUser;
            ITripleStore userbotLuceneIndexer = (ITripleStore)user.mbot.TripleStore;
            string       userName             = user.UserID;

            if (!shouldSet)
            {
                writeToLog("!shouldSet ERROR {0} name={1} value={2} old={3}", dict, realName, value, resultGet);
                bool shouldSet2 = ShouldSet(templateNode, dict, realName, value, resultGet, query);
                return(ReturnSetSetting(dict, name, setReturn));
            }
            if (IsIncomplete(value))
            {
                if (UseLuceneForSet && userbotLuceneIndexer != null)
                {
                    userbotLuceneIndexer.retractAllTriple(userName, name);
                }
                SettingsDictionaryReal.removeSettingWithUndoCommit(query, dict, name);
                if (!IsNullOrEmpty(gName))
                {
                    SettingsDictionaryReal.removeSettingWithUndoCommit(query, gUser, gName);
                }
            }
            else
            {
                if (UseLuceneForSet && userbotLuceneIndexer != null)
                {
                    userbotLuceneIndexer.updateTriple(userName, name, value);
                }
                if (!String.IsNullOrEmpty(gName))
                {
                    SettingsDictionaryReal.addSettingWithUndoCommit(query, gUser.Predicates, gUser.addSetting, gName, value);
                }
                query.SetDictValue++;
                SettingsDictionaryReal.addSettingWithUndoCommit(query, dict, dict.addSetting, name, value);
            }
            var retVal = ReturnSetSetting(dict, name, setReturn);

            if (!IsIncomplete(retVal) || !IsNullOrEmpty(retVal))
            {
                string comment = null;
                //if (query.LastTagHandler!=null) comment = query.LastTagHandler.Succeed(" setting " + name);
                return(retVal);// +comment;
            }
            return(retVal);
        }
        public static Unifiable ReturnSetSetting(ISettingsDictionary dict, string name, string setReturn)
        {
            string defRet;
            string realName;

            if (setReturn == null)
            {
                setReturn = SettingsDictionaryReal.ToSettingsDictionary(dict).GetSetReturn(name, out realName);
            }
            if (string.IsNullOrEmpty(setReturn))
            {
                defRet = "value";
            }
            else
            {
                defRet = setReturn.ToLower();
            }
            if (defRet == "name")
            {
                return(name);
            }
            if (defRet == "value")
            {
                Unifiable resultGet = SettingsDictionaryReal.grabSettingDefaultDict(dict, name, out realName);
                return(resultGet);
            }
            return(setReturn);
        }
Ejemplo n.º 5
0
        private static Unifiable GetValue(SubQuery query, ISettingsDictionary dict2, string str, out bool rfound)
        {
            Unifiable value;

            value  = dict2.grabSetting(str);
            rfound = !IsNull(value);
            return(value);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// This resynchronizes the settings from the native settings dictionary
 /// </summary>
 /// <param name="dictionary"></param>
 /// 
 public void Resync() {
     if (this.all != null) {
         this.all.CollectionChanged -= this.OnCollectionChanged;
         this.all = null;
     }
     var settings = this.GetNativeSettings();
     this.all = new SettingsDictionary(settings);
     this.all.CollectionChanged += this.OnCollectionChanged;
 }
Ejemplo n.º 7
0
        protected override ISettingsDictionary GetDictionary()
        {
            ISettingsDictionary dict = request.GetSubstitutions(templateNode.Name.ToLower(), false);

            if (dict != null)
            {
                return(dict);
            }
            return(substs.PersonSubstitutions);
        }
Ejemplo n.º 8
0
        public int assertDictionary(string subject, ISettingsDictionary dictionary)
        {
            int asserts = 0;

            foreach (var relation in dictionary.SettingNames(TheBot.ObjectRequester, 1))
            {
                Unifiable value = dictionary.grabSetting(relation);
                asserts += updateTriple(subject, relation, value);
            }
            return(asserts);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 protected override Unifiable ProcessChangeU()
 {
     if (CheckNode("li,if"))
     {
         ResetValues(false);
         ISettingsDictionary dist = query;
         var vv = IfSucceed(templateNode, dist, query, ProcessSucceed, Failure);
         return(vv);
     }
     return(ProcessSucceed());
 }
Ejemplo n.º 11
0
        /// <summary>
        /// This resynchronizes the settings from the native settings dictionary
        /// </summary>
        /// <param name="dictionary"></param>
        ///
        public void Resync()
        {
            if (this.all != null)
            {
                this.all.CollectionChanged -= this.OnCollectionChanged;
                this.all = null;
            }
            var settings = this.GetNativeSettings();

            this.all = new SettingsDictionary(settings);
            this.all.CollectionChanged += this.OnCollectionChanged;
        }
Ejemplo n.º 12
0
 public bool UseDictionaryForSet(ISettingsDictionary dictionary)
 {
     if (dictionary == null)
     {
         return(false);
     }
     if (!CanSetDict)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Static helper that applies replacements from the passed dictionary object to the
        /// target Unifiable
        /// </summary>
        /// <param name="bot">The bot for whom this is being processed</param>
        /// <param name="dictionary">The dictionary containing the substitutions</param>
        /// <param name="target">the target Unifiable to which the substitutions are to be applied</param>
        /// <returns>The processed Unifiable</returns>
        public static string Substitute(ISettingsDictionary dictionary, string target)
        {
            if (dictionary == null)
            {
                return(target);
            }
            string marker   = ApplySubstitutions.getMarker(5);
            string markerSP = ApplySubstitutions.getMarker(3);
            string result   = " " + Unifiable.ToVMString(target) + " ";

            result = SubstituteResults(dictionary, marker, markerSP, result, false);
            return(result.Replace(marker, "").Replace(markerSP, " "));
        }
Ejemplo n.º 14
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));
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
        public DataUnifiable grabSetting(string name)
        {
            string realName;
            ISettingsDictionary dict = Request.TargetSettings;
            bool      succeed;
            Unifiable v;

            if (!UseLuceneForGet)
            {
                v = SettingsDictionaryReal.grabSettingDefaultDict(dict, name, out realName);
            }
            else
            {
                v = NamedValuesFromSettings.GetSettingForType(dict.NameSpace, this, dict, name, out realName, name, null,
                                                              out succeed, null);
            }
            return(v);
        }
        private static bool ShouldSet(XmlNode templateNode, ISettingsDictionary dictionary, string name, object newValue, Unifiable oldValue, SubQuery query)
        {
            if (templateNode == null)
            {
                return(true);
            }
            bool canSet = query.UseDictionaryForSet(dictionary);

            ;
            bool onlyIfUnknown;

            if (StaticXMLUtils.TryParseBool(templateNode, "ifUnknown", out onlyIfUnknown))
            {
                if (onlyIfUnknown)
                {
                    return((Unifiable.IsMissing(oldValue) || IsIncomplete(oldValue)) && canSet);
                }
            }

            bool overwriteExisting;

            if (StaticXMLUtils.TryParseBool(templateNode, "overwriteExisting", out overwriteExisting))
            {
                if (!overwriteExisting)
                {
                    return((Unifiable.IsNullOrEmpty(oldValue) || IsIncomplete(oldValue)) && canSet);
                }
                //if (overwriteExisting)
                return(true);
            }

            string oldMatch  = StaticXMLUtils.GetAttribValue(templateNode, "existing", null);
            bool   shouldSet = true;

            if (oldMatch != null)
            {
                if (!StaticAIMLUtils.IsPredMatch(oldMatch, oldValue, null))
                {
                    shouldSet = false;
                }
            }
            var    newValueU = Unifiable.Create(newValue);
            string newMatch  = StaticXMLUtils.GetAttribValue(templateNode, "matches", null);

            if (newMatch != null)
            {
                if (!StaticAIMLUtils.IsPredMatch(newMatch, newValueU, null))
                {
                    shouldSet = false;
                }
            }
            string wontvalue = StaticXMLUtils.GetAttribValue(templateNode, "wontvalue", null);

            if (wontvalue != null)
            {
                if (StaticAIMLUtils.IsPredMatch(wontvalue, newValueU, null))
                {
                    shouldSet = false;
                }
            }
            return(shouldSet && canSet);
        }
        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;
        }
Ejemplo n.º 19
0
        private static string SubstituteResults(ISettingsDictionary dictionary, string marker, string markerSP, string result, bool backwards)
        {
            System.Collections.Generic.IEnumerable <string> dictionarySettingNames = dictionary.SettingNames(null, 0);

            int did = 0;

            foreach (string settingName in dictionarySettingNames)
            {
                if (did++ > 200)
                {
                    break;
                }
                var grabSetting = dictionary.grabSetting(settingName);
                if (grabSetting == null)
                {
                    continue;
                }
                string fromValue = settingName;
                string toValue   = grabSetting.ToString();

                // reverse the to/from
                if (backwards)
                {
                    string swapValue = toValue;
                    toValue   = fromValue;
                    fromValue = swapValue;
                }

                if (string.IsNullOrEmpty(Trim(fromValue)))
                {
                    continue;
                }

                toValue = GetToValue(toValue);

                string replacement;
                if (Trim(toValue).Length == 0)
                {
                    replacement = " ";
                }
                else
                {
                    toValue = Trim(toValue);
                    if (Trim(toValue.ToLower().Replace("\\b", " ")) == Trim(fromValue.ToLower().Replace("\\b", " ")))
                    {
                        continue;
                    }
                    replacement = marker + toValue.Replace(" ", markerSP) + marker;
                }


                string finalPattern = GetFinalPattern(fromValue);
                //if (Regex.IsMatch(result, finalPattern, RegexOptions.IgnoreCase))
                {
                    string testResult = Regex.Replace(result, finalPattern, replacement, RegexOptions.IgnoreCase);
                    if (testResult != result)
                    {
                        if (!Regex.IsMatch(result, finalPattern, RegexOptions.IgnoreCase))
                        {
                            OutputDelegate to = DLRConsole.DebugWriteLine;
                            to("\n  SUBST :");
                            to("   R: '{0}'", result);
                            to("  PT: '{0}'", fromValue);
                            to("   V: '{0}'", toValue);
                            to("   M: '{0}'", finalPattern);
                            to("  PR: '{0}'", replacement);
                            to("  RS: '{0}'", testResult);
                            to("  TS: '{0}'", testResult.Replace(marker, "").Replace(markerSP, " "));
                        }
                    }
                    result = testResult;
                }
            }
            return(result);
        }
Ejemplo n.º 20
0
        protected override Unifiable ProcessChangeU()
        {
            //    if (this.TemplateNodeName == "condition")
            {
                if (!templateNode.HasChildNodesNonText())
                {
                    return(ProcessChangeU0Childs());
                }
                // heuristically work out the type of condition being processed
                string name           = GetAttribValue("name,var", null);
                string conditionValue = GetAttribValue("value", null);


                string type = GetAttribValue("type,dict", null);
                ISettingsDictionary dict = type == null ? null : request.GetDictionary(type);

                // old style name=value test
                if (name != null && conditionValue != null)
                {
                    dict = dict ?? request.TargetSettings;
                    string actualValue = dict.grabSetting(name);
                    if (IsPredMatch(conditionValue, actualValue))
                    {
                        return(this.TemplateNodeInnerXml);
                    }
                    return(string.Empty);
                }


                // multi-predicate
                foreach (XmlNode childLINode in this.templateNode.ChildNodes)
                {
                    if (childLINode.Name.ToLower() == "li")
                    {
                        // check the PEParser query for trueness
                        string isTrueQuery = GetAttribValue <string>(childLINode, "istrue", null);
                        if (isTrueQuery != null)
                        {
                            bool     valid = false;
                            Sentence sen   = (Sentence) new PEParser().Parse(isTrueQuery);
                            try
                            {
                                valid = bot.myActiveModel.IsTrue(sen);
                            }
                            catch
                            {
                                valid = false;
                            }
                            if (valid)
                            {
                                return(childLINode.InnerXml);
                            }
                            continue;
                        }

                        // check for the <li> name and value
                        string cvalue = GetAttribValue <string>(childLINode, "value", null);
                        string cname  = GetAttribValue <string>(childLINode, "name,var", null);
                        // if <li> name=""  pretend it doesnt exist
                        if (cname != null && cname.Trim() == "")
                        {
                            cname = null;
                        }

                        // the fallthru (ussually the last <li> element)
                        if (cname == null && cvalue == null)
                        {
                            return(childLINode.InnerXml);
                        }

                        // compare <condition> name to <li> value
                        if (cname == null && cvalue != null)
                        {
                            string ctype = GetAttribValue("type,dict", null);
                            ISettingsDictionary cdict = (ctype == null ? dict : request.GetDictionary(ctype)) ??
                                                        request.TargetSettings;
                            conditionValue = conditionValue ?? cdict.grabSetting(name) ?? String.Empty;
                            if (IsPredMatch(cvalue, conditionValue))
                            {
                                return(childLINode.InnerXml);
                            }
                            continue;
                        }

                        // compare <li> name to <li> value
                        if (cname != null && cvalue != null)
                        {
                            string ctype = GetAttribValue("type,dict", null);
                            ISettingsDictionary cdict = (ctype == null ? dict : request.GetDictionary(ctype)) ??
                                                        request.TargetSettings;
                            string actualValue = cdict.grabSetting(cname);
                            if (IsPredMatch(cvalue, actualValue))
                            {
                                return(childLINode.InnerXml);
                            }
                            continue;
                        }
                    }
                }
            }
            return(string.Empty);
        }
Ejemplo n.º 21
0
        static Unifiable IfSucceed(XmlNode templateNode, ISettingsDictionary dict, SubQuery query0, Func <Unifiable> Succeed, Func <string, Unifiable> Failure)
        {
            lock (templateNode)
            {
                //  ISettingsDictionary dict = query;
                //if (GetAttribValue(templateNode, "type", "") == "bot" || GetAttribValue(templateNode,"bot", "").ToLower() == "true")
                //   dict = query0.TargetBot.GlobalSettings;

                bool yesInherit = true;
                bool noLocal    = false;

                string locally = GetAttribValue(templateNode, "scope,type,exists", "");
                if (locally.Contains("local"))
                {
                    yesInherit = false;
                }
                if (locally.Contains("inher"))
                {
                    noLocal    = true;
                    yesInherit = true;
                }

                Func <IConvertible, string> query = query0.ReduceStarAttribute <string>;

                string    name       = GetAttribValue <string>(templateNode, "name,var,if", NullStringFunct, query);
                string    expression = GetAttribValue <string>(templateNode, "expr", NullStringFunct, query);
                string    exists     = GetAttribValue(templateNode, "exists", NullStringFunct, query);
                string    contains   = GetAttribValue(templateNode, "contains", NullStringFunct, query);
                Unifiable value      = GetAttribValue(templateNode, "value", NullStringFunct, query);
                string    scope      = GetAttribValue(templateNode, "local,scope", NullStringFunct, query);
                string    realName0;
                string    type = GetAttribValue(templateNode, "type,user,bot", out realName0, NullStringFunct, query);
                if (type == null)
                {
                    type = realName0;
                }

                string varname      = name ?? exists;
                bool   mustNotExist = false;
                if (contains != null)
                {
                    value = ".*" + contains + ".*";
                }

                if (exists != null)
                {
                    bool locallyContains = dict.containsLocalCalled(name);
                    bool anyContains     = dict.containsSettingCalled(name);

                    if (exists.StartsWith("local"))
                    {
                        if (!locallyContains)
                        {
                            return(Unifiable.Empty);
                        }
                        return(Succeed());
                    }

                    exists = exists.ToLower();
                    if (exists == "inherited")
                    {
                        if (!locallyContains && anyContains)
                        {
                            return(Succeed());
                        }
                        return(Failure("" + name + "=" + anyContains + " !inherited "));
                    }
                    if (exists == "false")
                    {
                        if (!yesInherit)
                        {
                            if (!locallyContains)
                            {
                                return(Succeed());
                            }
                        }
                        if (noLocal)
                        {
                            if (anyContains && !locallyContains)
                            {
                                return(Succeed());
                            }
                        }
                        return(Failure("" + name + "=" + anyContains + " !false "));
                    }
                    if (exists == "true")
                    {
                        if (!yesInherit)
                        {
                            if (locallyContains)
                            {
                                return(Succeed());
                            }
                        }
                        if (noLocal)
                        {
                            if (!locallyContains && anyContains)
                            {
                                return(Succeed());
                            }
                        }
                        return(Failure("" + name + "=" + anyContains + "  " + templateNode.OuterXml));
                    }
                    string realName;
                    bool   succeed;
                    //ReduceStar0 query = query0.ReduceStarAttribute;
                    Unifiable actualValue = GetActualValue(templateNode, name, type ?? dict.NameSpace, out succeed, query0);
                    if (IsPredMatch(value, actualValue, query0))
                    {
                        return(Succeed());
                    }
                    else
                    {
                        return(Failure("" + name + "=" + anyContains + "  " + templateNode.OuterXml));
                    }
                }
                return(Succeed());
            }
        }
Ejemplo n.º 22
0
 public Settings(ISettingsDictionary settingsDictionary)
 {
     _properties = Application.Current?.Properties ?? settingsDictionary.Dictionary;
 }
Ejemplo n.º 23
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();
                    }
                });
            }
        }
Ejemplo n.º 24
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);
     }
 }
Ejemplo n.º 25
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);
        }