public static InputDeviceMatcher FromDeviceDescription(InputDeviceDescription deviceDescription) { var matcher = new InputDeviceMatcher(); if (!string.IsNullOrEmpty(deviceDescription.interfaceName)) { matcher = matcher.WithInterface(deviceDescription.interfaceName); } if (!string.IsNullOrEmpty(deviceDescription.deviceClass)) { matcher = matcher.WithDeviceClass(deviceDescription.deviceClass); } if (!string.IsNullOrEmpty(deviceDescription.manufacturer)) { matcher = matcher.WithManufacturer(deviceDescription.manufacturer); } if (!string.IsNullOrEmpty(deviceDescription.product)) { matcher = matcher.WithProduct(deviceDescription.product); } if (!string.IsNullOrEmpty(deviceDescription.version)) { matcher = matcher.WithVersion(deviceDescription.version); } // We don't include capabilities in this conversion. return(matcher); }
private int GetNumPropertiesIn(InputDeviceDescription description) { var count = 0; if (!string.IsNullOrEmpty(description.interfaceName)) { count += 1; } if (!string.IsNullOrEmpty(description.deviceClass)) { count += 1; } if (!string.IsNullOrEmpty(description.manufacturer)) { count += 1; } if (!string.IsNullOrEmpty(description.product)) { count += 1; } if (!string.IsNullOrEmpty(description.version)) { count += 1; } if (!string.IsNullOrEmpty(description.capabilities)) { count += 1; } return(count); }
/// <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 JsonParser(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); }