Пример #1
0
        public void ImportRules(ProviderRuleSet mapRuleSet)
        {
            ClearRules(mapRuleSet.provider);
            if (mapRuleSet.rules != null)
            {
                foreach (QRMarketDataMap rule in mapRuleSet.rules)
                {
                    MDSDictionaryType dictType = rule.dictType;
                    switch (dictType)
                    {
                    case MDSDictionaryType.FieldName:
                        AddFieldMap(rule.priority, rule.requestType, rule.sourcePattern, rule.outputValue);
                        break;

                    case MDSDictionaryType.Instrument:
                        AddInstrMap(rule.priority, rule.sourcePattern, rule.outputValue);
                        break;

                    case MDSDictionaryType.QuoteUnits:
                        AddUnitsMap(rule.priority, rule.requestType, rule.sourcePattern, PriceQuoteUnitsScheme.ParseEnumString(rule.outputValue));
                        break;

                    default:
                        throw new NotSupportedException("DictionaryType: " + dictType.ToString());
                    }
                }
            }
            // import done
            SortRules();
        }
Пример #2
0
 public string GetKey(
     MDSDictionaryType dictType, MDSRequestType requestType,
     MDSProviderId sourceProvider, MDSProviderId targetProvider, string sourceValue)
 {
     //return String.Format("{0};{1};{2}:{3};{4}:{5}",
     return
         ($"{dictType.ToString()};{requestType.ToString()}:{sourceProvider.ToString()};{sourceValue.ToUpper()}:{targetProvider.ToString()}");
 }
Пример #3
0
 public MapRule(MDSDictionaryType dictType,
                MDSProviderId sourceProvider, MDSProviderId targetProvider,
                MDSRequestType requestType,
                int priority,
                string searchValue, string outputValue)
 {
     Disabled       = false;
     DictType       = dictType;
     SourceProvider = sourceProvider;
     TargetProvider = targetProvider;
     RequestType    = requestType;
     Priority       = priority;
     SearchValue    = searchValue;
     OutputValue    = outputValue;
     Regex          = new Regex("^" + searchValue + "$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
 }
Пример #4
0
        public string Convert(
            MDSDictionaryType dictType, MDSRequestType requestType,
            MDSProviderId sourceProvider, MDSProviderId targetProvider,
            string sourceValue, ConvertFailMode failMode)
        {
            // method:
            // 1. check for cached result
            // 2. find relevant maps (by dicttype, reqtype, assettype, source, target)
            // 3. process maps in priority order until hit, fail if no hit
            // 4. save outbound and inbound results in cache
            if (dictType == MDSDictionaryType.Undefined)
            {
                throw new ArgumentNullException(nameof(dictType));
            }
            if (requestType == MDSRequestType.Undefined)
            {
                throw new ArgumentNullException(nameof(requestType));
            }
            //if (assetType == MDSAssetType.Undefined)
            //    throw new ArgumentNullException("assetType");
            if (sourceProvider == MDSProviderId.Undefined)
            {
                throw new ArgumentNullException(nameof(sourceProvider));
            }
            if (targetProvider == MDSProviderId.Undefined)
            {
                throw new ArgumentNullException(nameof(targetProvider));
            }
            if (sourceValue == null)
            {
                throw new ArgumentNullException(nameof(sourceValue));
            }
            sourceValue = sourceValue.Trim().ToUpper();
            if (sourceValue == "")
            {
                throw new ArgumentNullException(nameof(sourceValue));
            }
            const string cUnknownValue = "[unknown]";
            // check for cached result
            string forwardKey = GetKey(dictType, requestType, //assetType,
                                       sourceProvider, targetProvider, sourceValue);

            lock (_forwardCache)
            {
                if (_forwardCache.TryGetValue(forwardKey, out var cachedResult))
                {
                    //Logger.LogDebug("Converted {0} {1} '{2}' to {3} '{4}' (via forward cache)",
                    //    sourceProvider.ToString(), dictType.ToString(), sourceValue, targetProvider.ToString(), cachedResult);
                    return(cachedResult);
                }
            }
            // find relevant maps (by dicttype, reqtype, assettype, source, target)
            var maps = new List <MapRule>();

            _rules.Locked((rules) =>
            {
                foreach (var rule in rules)
                {
                    if ((!rule.Disabled) &&
                        (rule.DictType == MDSDictionaryType.Undefined || (rule.DictType == dictType)) &&
                        (rule.SourceProvider == MDSProviderId.Undefined || (rule.SourceProvider == sourceProvider)) &&
                        (rule.TargetProvider == MDSProviderId.Undefined || (rule.TargetProvider == targetProvider))
                        //&& (rule.AssetIdType == MDSAssetType.Undefined || (rule.AssetIdType == assetType))
                        && (rule.RequestType == MDSRequestType.Undefined || (rule.RequestType == requestType)))
                    {
                        maps.Add(rule);
                    }
                }
            });
            // process maps in priority order until hit, fail if no hit
            string result   = sourceValue;
            bool   replaced = false;

            foreach (var map in maps)
            {
                Match match = map.Regex.Match(sourceValue);
                if (match.Success)
                {
                    result   = map.Regex.Replace(sourceValue, map.OutputValue);
                    replaced = true;
                    break;
                }
            }
            if (replaced)
            {
                result = result.Trim().ToUpper();
                //_Logger.LogDebug("Converted {0} {1} '{2}' to {3} '{4}' (via mapping rules)",
                //    sourceProvider.ToString(), dictType.ToString(), sourceValue, targetProvider.ToString(), result);
                // update forward and reverse caches
                UpdateForwardCache(forwardKey, result);
                string reverseKey = GetKey(dictType, requestType, //assetType,
                                           targetProvider, sourceProvider, result);
                UpdateReverseCache(reverseKey, sourceValue);
            }
            else
            {
                // no replacement rules - try the reverse cache
                string cachedResult;
                bool   foundInReverseCache;
                lock (_reverseCache)
                {
                    foundInReverseCache = _reverseCache.TryGetValue(forwardKey, out cachedResult);
                }
                if (foundInReverseCache)
                {
                    _logger.LogDebug("Converted {0} {1} '{2}' to {3} '{4}' (via reverse cache)",
                                     sourceProvider.ToString(), dictType.ToString(), sourceValue, targetProvider.ToString(), cachedResult);
                    // update forward cache
                    UpdateForwardCache(forwardKey, cachedResult);
                    return(cachedResult);
                }
                // exhausted all conversion options
                _logger.LogWarning("Cannot convert {0} {1} '{2}' to {3} (no matching map found)",
                                   sourceProvider.ToString(), dictType.ToString(), sourceValue, targetProvider.ToString());
                switch (failMode)
                {
                case ConvertFailMode.PassThrough:
                    result = sourceValue;
                    break;

                case ConvertFailMode.ReturnNull:
                    result = null;
                    break;

                case ConvertFailMode.ReturnUnknown:
                    result = cUnknownValue;
                    break;

                default:
                    throw new ApplicationException(
                              String.Format("Cannot convert {0} {1} '{2}' to {3} ",
                                            sourceProvider.ToString(), dictType.ToString(), sourceValue, targetProvider.ToString()));
                }
            }
            return(result);
        }