/// <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); } }
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); }
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); }
internal Unifiable SystemExecute(Unifiable cmd, Unifiable langu, Request user) { if (IsNullOrEmpty(langu)) { langu = GlobalSettings.grabSetting("systemlang") ?? "bot"; } else { langu = ToLower(Trim(langu)); } Unifiable s = "The system tag should be doing '" + cmd + "' lang=" + langu; writeToLog(s.AsString()); SystemExecHandler handler; if (SettingsDictionaryReal.TryGetValue(ExecuteHandlers, langu, out handler)) { try { object o = handler(cmd, user); return(Unifiable.Create(o)); } catch (Exception e) { writeToLog(e); return(Unifiable.Empty); } } else { try { object self = user; ScriptInterpreter parent = null; ScriptInterpreter si = ScriptManager.LoadScriptInterpreter(langu, self, parent); object o = ScriptManager.EvalScriptInterpreter(cmd.ToValue(user.CurrentQuery), langu, self, parent, writeToLog); string siStr = si.Str(o); return(Unifiable.Create(siStr)); } catch (Exception e) { writeToLog(e); } } writeToLog(s); return(Unifiable.Empty); }
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); }
protected override Unifiable ProcessChangeU() { if (CheckNode("root,vars,substitutions,substitutions,bots,properties,predicates")) { // Process each of these child "settings"? nodes var prevDict = request.TargetSettings; try { SettingsDictionaryReal.loadSettingNode((ISettingsDictionary)GetTargetSettings(), templateNode, SettingsPolicy.Default, request); } finally { request.TargetSettings = prevDict; } return(Unifiable.Empty); return(ProcessSucceed()); } return(Unifiable.Empty); }
static public Unifiable GetSettingForType(string subject, SubQuery query, ISettingsDictionary dict, string name, out string realName, string gName, Unifiable defaultVal, out bool succeed, XmlNode node) { Request request = query.Request; OutputDelegate writeToLog = request.writeToLog ?? TextPatternUtils.DEVNULL; AltBot TargetBot = request.TargetBot; ISettingsDictionary udict; string dictName = AIMLTagHandler.GetNameOfDict(query, subject ?? dict.NameSpace, node, out udict); // try to use a global blackboard predicate User gUser = TargetBot.ExemplarUser; defaultVal = StaticXMLUtils.GetAttribValue(node, "default,defaultValue", defaultVal); gName = StaticXMLUtils.GetAttribValue(node, "global_name", gName); string realName0; var vv = ScriptManager.GetGroup(query.TargetBot.ObjectRequester, dictName, name); { if (vv != null) { if (vv.Count == 0) { succeed = true; realName = name; return(""); } succeed = true; realName = name; foreach (var e in vv) { return(Unifiable.Create(e)); } } } Unifiable resultGet = SettingsDictionaryReal.grabSettingDefaultDict(udict, name, out realName0); if (ReferenceEquals(resultGet, null)) { realName = null; resultGet = Unifiable.NULL; } // if ((!String.IsNullOrEmpty(result)) && (!result.IsWildCard())) return result; // we have a local one String realNameG; // try to use a global blackboard predicate Unifiable gResult = SettingsDictionaryReal.grabSettingDefaultDict(gUser.Predicates, gName, out realNameG); if ((Unifiable.IsMissing(resultGet)) && (!Unifiable.IsMissing(gResult))) { // result=nothing, gResult=something => return gResult writeToLog("SETTINGS OVERRIDE " + gResult); succeed = true; realName = realNameG; // return gResult; } string sresultGet = resultGet.ToValue(query); // if Unknown or empty if (UseLuceneForGet && Unifiable.IsMissing(sresultGet)) { Unifiable userName = udict.grabSetting("id"); if (Unifiable.IsNullOrEmpty(userName)) { writeToLog("ERROR IsNullOrEmpty id in " + udict.NameSpace); } ITripleStore userbotLuceneIndexer = (ITripleStore)query.Request.TargetBot.TripleStore; string resultLucene = userbotLuceneIndexer.queryTriple(userName, name, node); if (!string.IsNullOrEmpty(resultLucene)) { succeed = true; realName = name; return(resultLucene); } } if (sresultGet != null) { if (sresultGet.ToUpper() == "UNKNOWN") { succeed = false; realName = null; return(sresultGet + " " + name); } else if (Unifiable.IsEMPTY(resultGet)) { succeed = true; realName = name; return(resultGet); } else if (Unifiable.IsUnknown(resultGet)) { succeed = false; realName = name; return(resultGet); } } if (!String.IsNullOrEmpty(sresultGet)) { succeed = true; realName = realName0; query.GetDictValue++; if (!IsNullOrEmpty(gResult)) { if (resultGet.IsWildCard) { realName = realNameG; // result=*, gResult=something => return gResult return(gResult); } // result=something, gResult=something => return result return(resultGet); } else { // result=something, gResult=nothing => return result return(resultGet); } } if (defaultVal == null) { succeed = false; realName = null; return(defaultVal); } // default => return defaultVal succeed = true; realName = realName0; return(ReturnSetSetting(udict, name, defaultVal)); //return defaultVal; }
/// <summary> /// The method that does the actual Processing of the text. /// </summary> /// <returns>The resulting Processed text</returns> protected override Unifiable ProcessChangeU() { if (templateNode.NodeType == XmlNodeType.Comment) { return(Succeed(templateNode.Value)); } if (templateNode.NodeType == XmlNodeType.Text) { string s = Trim(templateNode.InnerText); if (String.IsNullOrEmpty(s)) { return(Unifiable.Empty); } return(s); } string currentNodeName = templateNode.Name.ToLower(); if (currentNodeName == "substitutions") { var prevDict = request.TargetSettings; // Process each of these child "settings"? nodes try { request.TargetSettings = request.TargetBot.InputSubstitutions; SettingsDictionaryReal.loadSettingNode(request.TargetSettings, templateNode, SettingsPolicy.Default, request); } finally { request.TargetSettings = prevDict; } return(ProcessSucceed()); } if (currentNodeName == "genlmt") { string name = GetAttribValue(templateNode, "name,mt,to,super,into", null); string removeTo = GetAttribValue(templateNode, "remove", null); string from = GetAttribValue(templateNode, "graph,from", null); bool deleteLink = false; if (name == null) { name = Trim(templateNode.InnerText); } if (removeTo != null) { deleteLink = true; name = removeTo; } GraphMaster FROM = request.GetGraph(from); GraphMaster TO = request.GetGraph(name); if (FROM != null && TO != null) { if (deleteLink) { FROM.RemoveGenlMT(TO, writeToLog); } else { FROM.AddGenlMT(TO, writeToLog); } return(Succeed("GENLMT: " + FROM + " => " + name + " => " + TO)); } } if (currentNodeName == "sraigraph") { string name = GetAttribValue(templateNode, "name,mt,to,super,into", null); string from = GetAttribValue(templateNode, "graph,from", null); if (name == null) { name = Trim(templateNode.InnerText); } GraphMaster FROM = request.GetGraph(from); GraphMaster TO = request.GetGraph(name); if (FROM != null && TO != null) { FROM.Srai = name; return(Succeed("SRAI: " + FROM + " => " + name + " => right now " + TO)); } return(Failure("FROM '" + from + "'='" + FROM + "'" + " TO '" + name + "'='" + TO + "'")); } if (currentNodeName == "meta") { return(Succeed("UNUSED: " + templateNode.OuterXml)); } if (currentNodeName == "#comment") { return(Succeed("UNUSED: " + templateNode.OuterXml)); } if (currentNodeName == "item") { SettingsDictionaryReal.loadSettingNode(request.TargetSettings, templateNode, SettingsPolicy.Default, request); return(ProcessSucceed()); } if (currentNodeName == "bot") { SettingsDictionaryReal.loadSettingNode(request.TargetBot.Settings, templateNode, SettingsPolicy.Default, request); return(ProcessSucceed()); } string currentNodeOuterXml = templateNode.OuterXml; if (currentNodeOuterXml.Length > 280) { currentNodeOuterXml = TextFilter.ClipString(currentNodeOuterXml, 280); } writeToLog("ImmediateAiml: " + currentNodeOuterXml); /* * <TestCase name="connect"> * <Input>CONNECT</Input> * <ExpectedAnswer>Connected to test case AIML set.</ExpectedAnswer> * </TestCase> */ if (templateNode.NodeType == XmlNodeType.Comment) { return(ProcessSucceed()); } // pull from late bound sustituion dictionaries var sd = request.GetSubstitutions(currentNodeName, false); if (sd != null) { if (FinalResultValid) { return(FinalResult); } if (!Unifiable.IsIncomplete(FinalResult)) { return(FinalResult); } Func <Unifiable, Unifiable> Format = (v) => ApplySubstitutions.Substitute(sd, templateNodeInnerText); if (base.isRecursive && !ReadOnly) { FinalResult = Format(TransformAtomically(null, true)); return(finalResult.Value); } return(FinalResult = TransformAtomically(Format, false)); } if (AltBot.UnknownTagsAreBotVars) { var dict = Proc.GlobalSettings; if (dict.containsSettingCalled(currentNodeName)) { var v = dict.grabSetting(currentNodeName); if (!Unifiable.IsMissing(v)) { return(v); } } } var vs = Proc.EvalAiml(templateNode, request, request.writeToLog); StringBuilder sb = new StringBuilder(); int writeThrus = 0; int total = 0; OutputDelegate WriteLine = DLRConsole.SystemWriteLine; foreach (var node in vs) { total++; string nodeOuterXml = ToXmlValue(node); WriteLine(nodeOuterXml); string p = GetAttribValue(node, "PASSED", "FALSE"); if (p == "False") { writeThrus++; sb.Append("\n" + nodeOuterXml.Replace("\" ", "\"\n ") + "\n"); } WriteLine(""); } WriteLine(""); WriteLine(""); WriteLine("" + writeThrus); WriteLine(""); string ss = sb.ToString(); WriteLine(ss); WriteLine(""); WriteLine(""); WriteLine(""); return(Succeed("total is " + total)); }
public User RenameUser0(string oldname, string newname) { //lock (BotUsers) { oldname = oldname ?? LastUser.UserName; oldname = CleanupFromname(oldname); string oldkey = KeyFromUsername(oldname); newname = newname ?? LastUser.UserName; newname = CleanupFromname(newname); string newkey = KeyFromUsername(newname); User newuser = FindUser(newkey); User olduser = FindUser0(oldname); if (olduser == null) { writeToUserLog("Neigther acct found so creating clean: " + newname); newuser = FindOrCreateUser(newname); newuser.LoadDirectory(GetUserDir(oldkey)); return(newuser); } if (newuser == olduser) { writeToUserLog("Same accts found: " + newname); LastUser.UserName = newname; return(newuser); } if (newuser != null) { writeToUserLog("both users exists: " + newname); // remove old acct from dict lock (microBotUsersLock) BotUsers.Remove(oldkey); // grab it into new user SettingsDictionaryReal.AddMissingKeys(olduser.Predicates, newuser.Predicates, ObjectRequester); newuser = olduser; lock (microBotUsersLock) BotUsers[newkey] = newuser; newuser.IsRoleAcct = false; SetupUserWithGraph(newname, newkey, newuser); // rebuild an old one CreateNewUser(oldname, oldkey); newuser = FindOrCreateUser(newname); return(newuser); } writeToUserLog("Copying old user .. and making new: " + newuser); // remove old acct from dict lock (microBotUsersLock) BotUsers.Remove(oldkey); // grab it into new user newuser = olduser; lock (microBotUsersLock) BotUsers[newkey] = newuser; newuser.IsRoleAcct = false; SetupUserWithGraph(newname, newkey, newuser); // rebuild an old one CreateNewUser(oldname, oldkey); return(newuser); writeToUserLog("ChangeUser " + oldname + " -> " + newname); WriteUserInfo(writeToLog, " olduser='******' ", olduser); WriteUserInfo(writeToLog, " newuser='******' ", newuser); if (olduser == null) { if (newuser == null) { writeToUserLog("Neigther acct found so creating clean: " + newname); newuser = FindOrCreateUser(newname); return(newuser); } if (newuser.IsRoleAcct) { writeToUserLog("User acct IsRole: " + newname); newuser.UserName = newname; return(newuser); } writeToUserLog("User acct found: " + newname); newuser = FindOrCreateUser(newname); return(newuser); } if (newuser == olduser) { writeToUserLog("Same accts found: " + newname); LastUser.UserName = newname; return(newuser); } // old user existed if (newuser != null) { if (newuser.IsRoleAcct) { if (olduser.IsRoleAcct) { writeToUserLog( "both acct are RoleAcct .. normaly shouldnt happen but just qa boring switchusers "); return(newuser); } writeToUserLog("New acct is RoleAcct .. so rebuilding: " + newkey); // remove old "new" acct from dict lock (microBotUsersLock) BotUsers.Remove(newkey); // kill its timer! newuser.DisposeObject(); newuser = FindOrCreateUser(newname); return(newuser); } else { writeToUserLog("old acct is just some other user so just switching to: " + newname); newuser = FindOrCreateUser(newname); return(newuser); } } else { if (olduser.IsRoleAcct) { writeToUserLog("Copying old RoleAcct .. and making new: " + newuser); // remove old acct from dict lock (microBotUsersLock) BotUsers.Remove(oldkey); // grab it into new user newuser = olduser; lock (microBotUsersLock) BotUsers[newkey] = newuser; newuser.IsRoleAcct = false; SetupUserWithGraph(newname, newkey, newuser); // rebuild an old one CreateNewUser(oldname, oldkey); return(newuser); } else { writeToUserLog("old acct is just some other user so just creating: " + newname); newuser = FindOrCreateUser(newname); return(newuser); } } writeToUserLog("ERROR, Totally lost so using FindOrCreate and switching to: " + newname); newuser = FindOrCreateUser(newname); return(newuser); } }
/// <summary> /// Updates the named setting with a new value whilst retaining the position in the /// dictionary /// </summary> /// <param name="name">the name of the setting</param> /// <param name="value">the new value</param> public bool updateSetting(string name, object value) { return(SettingsDictionaryReal.addSettingWithUndoCommit(this, TargetSettings, TargetSettings.updateSetting, name, value)); }
/// <summary> /// Removes the named setting from this class /// </summary> /// <param name="name">The name of the setting to remove</param> public bool removeSetting(string name) { return(SettingsDictionaryReal.removeSettingWithUndoCommit(this, TargetSettings, name)); }