示例#1
0
        /// <summary>
        /// Return the level of matching to the given <paramref name="deviceDescription">device description</paramref>.
        /// </summary>
        /// <param name="deviceDescription"></param>
        /// <returns></returns>
        /// <remarks>
        /// The algorithm computes a score of how well the matcher matches the given description. For every property
        /// that is present on t
        /// </remarks>
        public float MatchPercentage(InputDeviceDescription deviceDescription)
        {
            if (empty)
            {
                return(0);
            }

            // Go through all patterns. Score is 0 if any of the patterns
            // doesn't match.
            var numPatterns = m_Patterns.Length;

            for (var i = 0; i < numPatterns; ++i)
            {
                var key     = m_Patterns[i].Key;
                var pattern = m_Patterns[i].Value;

                if (key == kInterfaceKey)
                {
                    if (string.IsNullOrEmpty(deviceDescription.interfaceName) ||
                        !MatchSingleProperty(pattern, deviceDescription.interfaceName))
                    {
                        return(0);
                    }
                }
                else if (key == kDeviceClassKey)
                {
                    if (string.IsNullOrEmpty(deviceDescription.deviceClass) ||
                        !MatchSingleProperty(pattern, deviceDescription.deviceClass))
                    {
                        return(0);
                    }
                }
                else if (key == kManufacturerKey)
                {
                    if (string.IsNullOrEmpty(deviceDescription.manufacturer) ||
                        !MatchSingleProperty(pattern, deviceDescription.manufacturer))
                    {
                        return(0);
                    }
                }
                else if (key == kProductKey)
                {
                    if (string.IsNullOrEmpty(deviceDescription.product) ||
                        !MatchSingleProperty(pattern, deviceDescription.product))
                    {
                        return(0);
                    }
                }
                else if (key == kVersionKey)
                {
                    if (string.IsNullOrEmpty(deviceDescription.version) ||
                        !MatchSingleProperty(pattern, deviceDescription.version))
                    {
                        return(0);
                    }
                }
                else
                {
                    // Capabilities match. Take the key as a path into the JSON
                    // object and match the value found at the given path.

                    if (string.IsNullOrEmpty(deviceDescription.capabilities))
                    {
                        return(0);
                    }

                    var graph = new JsonGraph(deviceDescription.capabilities);
                    if (!graph.NavigateToProperty(key.ToString()) ||
                        !graph.CurrentPropertyHasValueEqualTo(pattern))
                    {
                        return(0);
                    }
                }
            }

            // All patterns matched. Our score is determined by the number of properties
            // we matched against.
            var propertyCountInDescription = GetNumPropertiesIn(deviceDescription);
            var scorePerProperty           = 1.0f / propertyCountInDescription;

            return(numPatterns * scorePerProperty);
        }
示例#2
0
        public Model Deserialize(string s)
        {
            var jobject = JsonGraph.GraphFromJsonString(s);

            return(JsonGraph.Create(jobject));
        }