Пример #1
0
            static SettingsProviderCache()
            {
                value = FetchSettingsProviders()
                        .Select(provider => new SettingsProviderInfo()
                {
                    path        = provider.settingsPath,
                    label       = provider.label,
                    scope       = provider.scope,
                    searchables = new[] { provider.settingsPath, provider.label }
                    .Concat(provider.keywords)
                    .Where(s => !string.IsNullOrEmpty(s))
                    .Select(s => Utils.FastToLower(s)).ToArray()
                })
                        .ToArray();

                var iconName    = "Filter Icon";
                var icon        = Utils.LoadIcon(iconName);
                var scopeValues = Enum.GetNames(typeof(SettingsScope)).Select(n => Utils.FastToLower(n));

                queryEngine = new QueryEngine <SettingsProviderInfo>();
                queryEngine.SetSearchDataCallback(info => info.searchables, s => Utils.FastToLower(s), StringComparison.Ordinal);
                queryEngine.SetFilter("scope", info => info.scope, new[] { ":", "=", "!=", "<", ">", "<=", ">=" })
                .SetGlobalPropositionData(category: "Scope", priority: 0, icon: icon, color: QueryColors.typeIcon)
                .AddOrUpdatePropositionData(label: "Project", replacement: "scope:" + SearchUtils.GetListMarkerReplacementText("project", scopeValues, iconName, QueryColors.typeIcon), help: "Search project settings")
                .AddOrUpdatePropositionData(label: "User", replacement: "scope:" + SearchUtils.GetListMarkerReplacementText("user", scopeValues, iconName, QueryColors.typeIcon), help: "Search user settings");

                queryEngine.AddOperatorHandler(":", (SettingsScope ev, SettingsScope fv, StringComparison sc) => ev.ToString().IndexOf(fv.ToString(), sc) != -1);
                queryEngine.AddOperatorHandler(":", (SettingsScope ev, string fv, StringComparison sc) => ev.ToString().IndexOf(fv, sc) != -1);
                queryEngine.AddOperatorHandler("=", (SettingsScope ev, SettingsScope fv) => ev == fv);
                queryEngine.AddOperatorHandler("!=", (SettingsScope ev, SettingsScope fv) => ev != fv);
                queryEngine.AddOperatorHandler("<", (SettingsScope ev, SettingsScope fv) => ev < fv);
                queryEngine.AddOperatorHandler(">", (SettingsScope ev, SettingsScope fv) => ev > fv);
                queryEngine.AddOperatorHandler("<=", (SettingsScope ev, SettingsScope fv) => ev <= fv);
                queryEngine.AddOperatorHandler(">=", (SettingsScope ev, SettingsScope fv) => ev >= fv);
            }
            static SettingsProviderCache()
            {
                value = FetchSettingsProviders()
                        .Select(provider => new SettingsProviderInfo()
                {
                    path        = provider.settingsPath,
                    label       = provider.label,
                    scope       = provider.scope,
                    searchables = new[] { provider.settingsPath, provider.label }
                    .Concat(provider.keywords)
                    .Where(s => !string.IsNullOrEmpty(s))
                    .Select(s => Utils.FastToLower(s)).ToArray()
                })
                        .ToArray();

                queryEngine = new QueryEngine <SettingsProviderInfo>();
                queryEngine.SetSearchDataCallback(info => info.searchables, s => Utils.FastToLower(s), StringComparison.Ordinal);
                queryEngine.AddFilter("scope", info => info.scope, new[] { ":", "=", "!=", "<", ">", "<=", ">=" });

                queryEngine.AddOperatorHandler(":", (SettingsScope ev, SettingsScope fv, StringComparison sc) => ev.ToString().IndexOf(fv.ToString(), sc) != -1);
                queryEngine.AddOperatorHandler(":", (SettingsScope ev, string fv, StringComparison sc) => ev.ToString().IndexOf(fv, sc) != -1);
                queryEngine.AddOperatorHandler("=", (SettingsScope ev, SettingsScope fv) => ev == fv);
                queryEngine.AddOperatorHandler("!=", (SettingsScope ev, SettingsScope fv) => ev != fv);
                queryEngine.AddOperatorHandler("<", (SettingsScope ev, SettingsScope fv) => ev < fv);
                queryEngine.AddOperatorHandler(">", (SettingsScope ev, SettingsScope fv) => ev > fv);
                queryEngine.AddOperatorHandler("<=", (SettingsScope ev, SettingsScope fv) => ev <= fv);
                queryEngine.AddOperatorHandler(">=", (SettingsScope ev, SettingsScope fv) => ev >= fv);
            }
        public SceneQueryEngine(GameObject[] gameObjects)
        {
            m_GameObjects = gameObjects;
            m_QueryEngine.AddFilter("id", GetId);
            m_QueryEngine.AddFilter("path", GetPath);
            m_QueryEngine.AddFilter("tag", GetTag);
            m_QueryEngine.AddFilter("layer", GetLayer);
            m_QueryEngine.AddFilter("size", GetSize);
            m_QueryEngine.AddFilter <string>("is", OnIsFilter, new [] { ":" });
            m_QueryEngine.AddFilter <string>("t", OnTypeFilter, new [] { "=", ":" });

            m_QueryEngine.AddFilter("p", OnPropertyFilter, s => s, StringComparison.OrdinalIgnoreCase);

            m_QueryEngine.AddOperatorHandler(":", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => r.Contains(f)));
            m_QueryEngine.AddOperatorHandler("=", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => r.Contains(f)));
            m_QueryEngine.AddOperatorHandler("!=", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => !r.Contains(f)));
            m_QueryEngine.AddOperatorHandler("<=", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f <= r.max));
            m_QueryEngine.AddOperatorHandler("<", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f < r.min));
            m_QueryEngine.AddOperatorHandler(">", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f > r.max));
            m_QueryEngine.AddOperatorHandler(">=", (object v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f >= r.min));

            m_QueryEngine.AddTypeParser(arg =>
            {
                if (arg.Length > 0 && arg.Last() == ']')
                {
                    var rangeMatches = s_RangeRx.Matches(arg);
                    if (rangeMatches.Count == 1 && rangeMatches[0].Groups.Count == 3)
                    {
                        var rg = rangeMatches[0].Groups;
                        if (float.TryParse(rg[1].Value, out var min) && float.TryParse(rg[2].Value, out var max))
                        {
                            return(new ParseResult <PropertyRange>(true, new PropertyRange(min, max)));
                        }
                    }
                }

                return(ParseResult <PropertyRange> .none);
            });

            m_QueryEngine.AddTypeParser(arg =>
            {
                if (float.TryParse(arg, out var f))
                {
                    return(new ParseResult <object>(true, f));
                }

                if (arg == "true")
                {
                    return(new ParseResult <object>(true, true));
                }
                if (arg == "false")
                {
                    return(new ParseResult <object>(true, false));
                }

                return(new ParseResult <object>(true, arg));
            });

            m_QueryEngine.SetSearchDataCallback(OnSearchData, StringComparison.Ordinal);
        }
Пример #4
0
        public SceneQueryEngine(GameObject[] gameObjects)
        {
            m_GameObjects = gameObjects;
            m_QueryEngine.AddFilter("id", GetId);
            m_QueryEngine.AddFilter("path", GetPath);
            m_QueryEngine.AddFilter("tag", GetTag);
            m_QueryEngine.AddFilter("layer", GetLayer);
            m_QueryEngine.AddFilter("size", GetSize);
            m_QueryEngine.AddFilter <string>("is", OnIsFilter, new [] { ":" });
            m_QueryEngine.AddFilter <string>("t", OnTypeFilter, new [] { "=", ":" });
            m_QueryEngine.AddFilter <string>("ref", GetReferences, new [] { "=", ":" });

            m_QueryEngine.AddFilter("p", OnPropertyFilter, s => s, StringComparison.OrdinalIgnoreCase);

            m_QueryEngine.AddOperatorHandler(":", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => r.Contains(f)));
            m_QueryEngine.AddOperatorHandler("=", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => r.Contains(f)));
            m_QueryEngine.AddOperatorHandler("!=", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => !r.Contains(f)));
            m_QueryEngine.AddOperatorHandler("<=", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f <= r.max));
            m_QueryEngine.AddOperatorHandler("<", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f < r.min));
            m_QueryEngine.AddOperatorHandler(">", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f > r.max));
            m_QueryEngine.AddOperatorHandler(">=", (GOP v, PropertyRange range) => PropertyRangeCompare(v, range, (f, r) => f >= r.min));

            m_QueryEngine.AddOperatorHandler(":", (GOP v, float number, StringComparison sc) => PropertyFloatCompare(v, number, (f, r) => StringContains(f, r, sc)));
            m_QueryEngine.AddOperatorHandler("=", (GOP v, float number) => PropertyFloatCompare(v, number, (f, r) => Math.Abs(f - r) < Mathf.Epsilon));
            m_QueryEngine.AddOperatorHandler("!=", (GOP v, float number) => PropertyFloatCompare(v, number, (f, r) => Math.Abs(f - r) >= Mathf.Epsilon));
            m_QueryEngine.AddOperatorHandler("<=", (GOP v, float number) => PropertyFloatCompare(v, number, (f, r) => f <= r));
            m_QueryEngine.AddOperatorHandler("<", (GOP v, float number) => PropertyFloatCompare(v, number, (f, r) => f < r));
            m_QueryEngine.AddOperatorHandler(">", (GOP v, float number) => PropertyFloatCompare(v, number, (f, r) => f > r));
            m_QueryEngine.AddOperatorHandler(">=", (GOP v, float number) => PropertyFloatCompare(v, number, (f, r) => f >= r));

            m_QueryEngine.AddOperatorHandler("=", (GOP v, bool b) => PropertyBoolCompare(v, b, (f, r) => f == r));
            m_QueryEngine.AddOperatorHandler("!=", (GOP v, bool b) => PropertyBoolCompare(v, b, (f, r) => f != r));

            m_QueryEngine.AddOperatorHandler(":", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => StringContains(f, r, sc)));
            m_QueryEngine.AddOperatorHandler("=", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => string.Equals(f, r, sc)));
            m_QueryEngine.AddOperatorHandler("!=", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => !string.Equals(f, r, sc)));
            m_QueryEngine.AddOperatorHandler("<=", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => string.Compare(f, r, sc) <= 0));
            m_QueryEngine.AddOperatorHandler("<", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => string.Compare(f, r, sc) < 0));
            m_QueryEngine.AddOperatorHandler(">", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => string.Compare(f, r, sc) > 0));
            m_QueryEngine.AddOperatorHandler(">=", (GOP v, string s, StringComparison sc) => PropertyStringCompare(v, s, (f, r) => string.Compare(f, r, sc) >= 0));

            m_QueryEngine.AddTypeParser(arg =>
            {
                if (arg.Length > 0 && arg.Last() == ']')
                {
                    var rangeMatches = s_RangeRx.Matches(arg);
                    if (rangeMatches.Count == 1 && rangeMatches[0].Groups.Count == 3)
                    {
                        var rg = rangeMatches[0].Groups;
                        if (float.TryParse(rg[1].Value, out var min) && float.TryParse(rg[2].Value, out var max))
                        {
                            return(new ParseResult <PropertyRange>(true, new PropertyRange(min, max)));
                        }
                    }
                }

                return(ParseResult <PropertyRange> .none);
            });

            m_QueryEngine.SetSearchDataCallback(OnSearchData, s => s.ToLowerInvariant(), StringComparison.Ordinal);
            m_QueryEngine.AddFiltersFromAttribute <SceneQueryEngineFilterAttribute, SceneQueryEngineParameterTransformerAttribute>();
        }
    public QueryEngineCustomOperators()
    {
        GenerateData(1000);

        // Setup the query engine
        m_QueryEngine = new QueryEngine <MyCustomObjectType>();
        // Id supports all operators
        m_QueryEngine.AddFilter("id", myObj => myObj.Id);

        // Setup what data will be matched against search words
        m_QueryEngine.SetSearchDataCallback(myObj => new[] { myObj.Id.ToString() });

        // Extend the set of operators and type parsers

        // Modulo operator will work on all filters that are integers
        const string moduloOp = "%";

        m_QueryEngine.AddOperator(moduloOp);
        m_QueryEngine.AddOperatorHandler(moduloOp, (int ev, int fv) => ev % fv == 0);

        // List operator will work on al filters that have an integer as left hand side and
        // a list of integers as right hand side
        var listOp = "?";

        m_QueryEngine.AddOperator(listOp);
        m_QueryEngine.AddOperatorHandler(listOp, (int ev, List <int> values) => values.Contains(ev));

        // To correctly parse this new type (because it is not supported by default), we add a type parser
        m_QueryEngine.AddTypeParser(s =>
        {
            var tokens = s.Split(',');
            if (tokens.Length == 0)
            {
                return(new ParseResult <List <int> >(false, null));
            }

            var numberList = new List <int>(tokens.Length);
            foreach (var token in tokens)
            {
                if (TryConvertValue(token, out int number))
                {
                    numberList.Add(number);
                }
                else
                {
                    return(new ParseResult <List <int> >(false, null));
                }
            }

            return(new ParseResult <List <int> >(true, numberList));
        });

        // We an also extend existing operators

        // Position supports =, !=, <, >, <=, >=
        m_QueryEngine.AddFilter("p", myObj => myObj.Position, new[] { "=", "!=", "<", ">", "<=", ">=" });

        // Extend the =, !=, <, >, <=, >= operators to support comparing Vector2's magnitude
        m_QueryEngine.AddOperatorHandler("=", (Vector2 ev, Vector2 fv) => Math.Abs(ev.magnitude - fv.magnitude) < float.Epsilon);
        m_QueryEngine.AddOperatorHandler("!=", (Vector2 ev, Vector2 fv) => Math.Abs(ev.magnitude - fv.magnitude) > float.Epsilon);
        m_QueryEngine.AddOperatorHandler("<", (Vector2 ev, Vector2 fv) => ev.magnitude < fv.magnitude);
        m_QueryEngine.AddOperatorHandler(">", (Vector2 ev, Vector2 fv) => ev.magnitude > fv.magnitude);
        m_QueryEngine.AddOperatorHandler("<=", (Vector2 ev, Vector2 fv) => ev.magnitude <= fv.magnitude);
        m_QueryEngine.AddOperatorHandler(">=", (Vector2 ev, Vector2 fv) => ev.magnitude >= fv.magnitude);

        // Add a new type parser for Vector2
        m_QueryEngine.AddTypeParser(s =>
        {
            if (!s.StartsWith("[") || !s.EndsWith("]"))
            {
                return(new ParseResult <Vector2>(false, Vector2.zero));
            }

            var trimmed      = s.Trim('[', ']');
            var vectorTokens = trimmed.Split(',');
            var vectorValues = vectorTokens.Select(token => float.Parse(token, CultureInfo.InvariantCulture.NumberFormat)).ToList();
            Assert.AreEqual(vectorValues.Count, 2);
            var vector = new Vector2(vectorValues[0], vectorValues[1]);
            return(new ParseResult <Vector2>(true, vector));
        });

        // If you don't want to add a multitude of operator handlers, you can define
        // a generic filter handler that will handle all existing operators on a filter
        m_QueryEngine.AddFilter <string>("is", IsFilterResolver);
    }