Пример #1
0
        static void Remember(Replacements replacements, string tokenString, QueryToken token)
        {
            List <QueryToken> tokenList = token.Follow(a => a.Parent).Reverse().ToList();

            string[] oldParts = tokenString.Split('.');
            string[] newParts = token.FullKey().Split('.');

            List <string> oldPartsList = oldParts.ToList();
            List <string> newPartsList = newParts.ToList();

            Func <string, string> rep = str =>
            {
                if (Replacements.AutoReplacement == null)
                {
                    return(null);
                }

                Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext {
                    ReplacementKey = "QueryToken", OldValue = str, NewValues = null
                });

                if (sel == null || sel.Value.NewValue == null)
                {
                    return(null);
                }

                return(sel.Value.NewValue);
            };

            int pos = -1;

            while (oldPartsList.Count > 0 && newPartsList.Count > 0 &&
                   (oldPartsList[0] == newPartsList[0] ||
                    rep(oldPartsList[0]) == newPartsList[0]))
            {
                oldPartsList.RemoveAt(0);
                newPartsList.RemoveAt(0);
                pos++;
            }

            while (oldPartsList.Count > 0 && newPartsList.Count > 0 &&
                   (oldPartsList[oldPartsList.Count - 1] == newPartsList[newPartsList.Count - 1] ||
                    rep(oldPartsList[oldPartsList.Count - 1]) == newPartsList[newPartsList.Count - 1]))
            {
                oldPartsList.RemoveAt(oldPartsList.Count - 1);
                newPartsList.RemoveAt(newPartsList.Count - 1);
            }

            string key = pos == -1 ? QueryKey(tokenList[0].QueryName) : TypeKey(tokenList[pos].Type);

            replacements.GetOrCreate(key)[oldPartsList.ToString(".")] = newPartsList.ToString(".");
        }
Пример #2
0
        static bool TryParseRemember(Replacements replacements, string tokenString, QueryDescription qd, SubTokensOptions options, out QueryToken result)
        {
            string[] parts = tokenString.Split('.');

            result = null;
            for (int i = 0; i < parts.Length; i++)
            {
                string part = parts[i];

                QueryToken newResult = QueryUtils.SubToken(result, qd, options, part);

                if (newResult != null)
                {
                    result = newResult;
                }
                else
                {
                    if (i == 0)
                    {
                        var        entity       = QueryUtils.SubToken(result, qd, options, "Entity");
                        QueryToken newSubResult = QueryUtils.SubToken(entity, qd, options, part);

                        if (newSubResult != null)
                        {
                            result = newSubResult;
                            continue;
                        }
                    }


                    if (Replacements.AutoReplacement != null)
                    {
                        Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext {
                            ReplacementKey = "QueryToken", OldValue = part, NewValues = result.SubTokens(qd, options).Select(a => a.Key).ToList()
                        });

                        if (sel != null && sel.Value.NewValue != null)
                        {
                            newResult = QueryUtils.SubToken(result, qd, options, sel.Value.NewValue);

                            if (newResult != null)
                            {
                                result = newResult;
                                continue;
                            }
                        }
                    }

                    string key = result == null?QueryKey(qd.QueryName) : TypeKey(result.Type);

                    Dictionary <string, string> dic = replacements.TryGetC(key);

                    if (dic == null)
                    {
                        return(false);
                    }

                    string remainging = parts.Skip(i).ToString(".");

                    string old = dic.Keys.OrderByDescending(a => a.Length).FirstOrDefault(s => remainging.StartsWith(s));

                    if (old == null)
                    {
                        return(false);
                    }

                    var subParts = dic[old].Let(s => s.HasText() ? s.Split('.') : new string[0]);

                    for (int j = 0; j < subParts.Length; j++)
                    {
                        string subPart = subParts[j];

                        QueryToken subNewResult = QueryUtils.SubToken(result, qd, options, subPart);

                        if (subNewResult == null)
                        {
                            return(false);
                        }

                        result = subNewResult;
                    }

                    i += (old == "" ? 0 : old.Split('.').Length) - 1;
                }
            }

            return(true);
        }
Пример #3
0
        static string AskTypeReplacement(Replacements replacements, string type)
        {
            return(replacements.GetOrCreate("cleanNames").GetOrCreate(type, () =>
            {
                if (Replacements.AutoReplacement != null)
                {
                    Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext {
                        ReplacementKey = "FixValue.Type", OldValue = type, NewValues = null
                    });

                    if (sel != null && sel.Value.NewValue != null)
                    {
                        return sel.Value.NewValue;
                    }
                }

                Console.WriteLine("Type {0} has been renamed?".FormatWith(type));

                int startingIndex = 0;
                StringDistance sd = new StringDistance();
                var list = TypeLogic.NameToType.Keys.OrderBy(t => sd.LevenshteinDistance(t, type)).ToList();
                retry:
                int maxElements = Console.LargestWindowHeight - 11;

                list.Skip(startingIndex).Take(maxElements)
                .Select((s, i) => "- {1,2}: {2} ".FormatWith(i + startingIndex == 0 ? ">" : " ", i + startingIndex, s)).ToConsole();
                Console.WriteLine();
                SafeConsole.WriteLineColor(ConsoleColor.White, "- n: None");

                int remaining = list.Count - startingIndex - maxElements;
                if (remaining > 0)
                {
                    SafeConsole.WriteLineColor(ConsoleColor.White, "- +: Show more values ({0} remaining)", remaining);
                }

                while (true)
                {
                    string answer = Console.ReadLine();

                    if (answer == null)
                    {
                        throw new InvalidOperationException("Impossible to synchronize interactively without Console");
                    }

                    answer = answer.ToLower();

                    if (answer == "+" && remaining > 0)
                    {
                        startingIndex += maxElements;
                        goto retry;
                    }

                    if (answer == "n")
                    {
                        return null;
                    }

                    if (int.TryParse(answer, out int option))
                    {
                        return list[option];
                    }

                    Console.WriteLine("Error");
                }
            }));
        }
Пример #4
0
        public static FixTokenResult FixValue(Replacements replacements, Type type, ref string valueString, bool allowRemoveToken, bool isList)
        {
            string error = FilterValueConverter.TryParse(valueString, type, out object val, isList);

            if (error == null)
            {
                return(FixTokenResult.Nothing);
            }

            if (isList && valueString.Contains('|'))
            {
                List <string> changes = new List <string>();
                foreach (var str in valueString.Split('|'))
                {
                    string s      = str;
                    var    result = FixValue(replacements, type, ref s, allowRemoveToken, false);

                    if (result == FixTokenResult.DeleteEntity || result == FixTokenResult.SkipEntity || result == FixTokenResult.RemoveToken)
                    {
                        return(result);
                    }

                    changes.Add(s);
                }

                valueString = changes.ToString("|");
                return(FixTokenResult.Fix);
            }

            if (type.IsLite())
            {
                var m = Lite.ParseRegex.Match(valueString);
                if (m.Success)
                {
                    var typeString = m.Groups["type"].Value;

                    if (!TypeLogic.NameToType.ContainsKey(typeString))
                    {
                        string newTypeString = AskTypeReplacement(replacements, typeString);

                        if (newTypeString.HasText())
                        {
                            valueString = valueString.Replace(typeString, newTypeString);
                            return(FixTokenResult.Fix);
                        }
                    }
                }
            }

            if (Replacements.AutoReplacement != null)
            {
                Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext {
                    ReplacementKey = "FixValue", OldValue = valueString, NewValues = null
                });

                if (sel != null && sel.Value.NewValue != null)
                {
                    valueString = sel.Value.NewValue;
                    return(FixTokenResult.Fix);
                }
            }

            SafeConsole.WriteLineColor(ConsoleColor.White, "Value '{0}' not convertible to {1}.".FormatWith(valueString, type.TypeName()));
            SafeConsole.WriteLineColor(ConsoleColor.Yellow, "- s: Skip entity");
            if (allowRemoveToken)
            {
                SafeConsole.WriteLineColor(ConsoleColor.DarkRed, "- r: Remove token");
            }
            SafeConsole.WriteLineColor(ConsoleColor.Red, "- d: Delete entity");
            SafeConsole.WriteLineColor(ConsoleColor.Green, "- freeText: New value");

            string answer = Console.ReadLine();

            if (answer == null)
            {
                throw new InvalidOperationException("Impossible to synchronize interactively without Console");
            }

            string a = answer.ToLower();

            if (a == "s")
            {
                return(FixTokenResult.SkipEntity);
            }

            if (allowRemoveToken && a == "r")
            {
                return(FixTokenResult.RemoveToken);
            }

            if (a == "d")
            {
                return(FixTokenResult.DeleteEntity);
            }

            valueString = answer;
            return(FixTokenResult.Fix);
        }