public IPluginProxy FindLoadedPlugin(Guid pluginId, bool checkCurrentlyLoading) { var p = _loadedPlugins.GetValueWithDefault(pluginId, null); if (p == null && checkCurrentlyLoading) { p = _newlyLoadedPlugins.FirstOrDefault(n => n.PluginKey.UniqueId == pluginId); } return(p); }
public void ShouldFallbackToDefaultTypeValueIfNotExists() { // Given var dictionary = new Dictionary<string, object> { { "key", "value" } }; // When var value = dictionary.GetValueWithDefault<string>("otherKey"); // Then Assert.Equal(default(string), value); }
public void GetValueWithNullKey() { // Setup var dictionary = new Dictionary <string, int> { { "one", 1 }, { "two", 2 }, { "three", 3 }, }; // Execute var value = dictionary.GetValueWithDefault(a_key: null, a_default: 50); }
public void GetValueWithNotExistingKey() { // Setup var dictionary = new Dictionary <string, int> { { "one", 1 }, { "two", 2 }, { "three", 3 } }; // Execute var value = dictionary.GetValueWithDefault("1", 50); // Assert Assert.AreEqual(50, value); }
/// <summary> /// Finds a global section. /// </summary> /// <param name="name">Name of the section.</param> /// <returns>The section or null if not found.</returns> public Section FindGlobalSection(string name) => _sections.GetValueWithDefault(name, null);
/// <summary> /// Finds a <see cref="Monitor"/> by its identifier. /// </summary> /// <param name="monitorId">The monitor's identifier.</param> /// <returns>The monitor or null if not found.</returns> public Monitor FindMonitor(Guid monitorId) { return(_monitors.GetValueWithDefault(monitorId, null)); }
/// <summary> /// Finds a subordinated route by its name. /// </summary> /// <param name="name">Name of the route.</param> /// <returns>The route or null if it does not exist.</returns> public SubRouteConfigurationResolved FindSubRouteByName(string name) { return(_namedSubRoutes.GetValueWithDefault(name, null)); }
bool CheckReference(IServiceReferenceInfo serviceRef, BitArray parseMap, ref int cost) { // If the reference is a reference to an external service if (!serviceRef.Reference.IsDynamicService) { return(true); // todo check if the service is available in the service container. } // Checks if at least one of the implementations of the service is available in the current map. bool implAvailable = serviceRef.Reference.Implementations.Count > 0; // If the service is really needed and is not available, return the reference cannot be resolved. if (serviceRef.Requirements >= RunningRequirement.MustExist && !implAvailable) { return(false); } else { Debug.Assert(serviceRef.Requirements < RunningRequirement.MustExist || implAvailable); bool isPossible = false; if (!implAvailable) { switch (serviceRef.Requirements) { // the plugin is stopped but it's optional -> whatever ! case RunningRequirement.Optional: return(true); case RunningRequirement.OptionalTryStart: cost += 10; return(true); } } else { foreach (IPluginInfo impl in serviceRef.Reference.Implementations) { PluginData data = _mappingDic.GetValueWithDefault(impl, null); if (data != null) { // if the plugin is running in this map if (parseMap[data.Index]) { isPossible = true; switch (serviceRef.Requirements) { // the plugin is started but we don't really need it -> +10 if its not currently running. case RunningRequirement.Optional: case RunningRequirement.MustExist: if (!IsPluginRunning(impl)) { cost += 10; } break; // the plugin is started and we want it -> +0 (its what we want) case RunningRequirement.OptionalTryStart: case RunningRequirement.MustExistTryStart: case RunningRequirement.MustExistAndRun: break; } } else { switch (serviceRef.Requirements) { // the plugin is stopped but it's optional -> whatever ! case RunningRequirement.Optional: case RunningRequirement.MustExist: isPossible = true; break; // the plugin is stopped but we wants it -> +10 case RunningRequirement.OptionalTryStart: cost += 10; break; // the plugin is stopped but we absolutely needs it -> impossible. case RunningRequirement.MustExistTryStart: case RunningRequirement.MustExistAndRun: isPossible = false; break; } } } else { isPossible = false; } } } return(isPossible); } }
/// <summary> /// Launches ComputeCombination for each "plugin status combination". /// Gets the lower cost among all the combinations generated. /// </summary> internal ExecutionPlan ObtainBestPlan(Dictionary <object, SolvedConfigStatus> finalConfig, bool stopLaunchedOptionals) { _finalConfig = finalConfig; if (_parseMap == null) { _parseMap = new BitArray(_discoverer.Plugins.Count); } else { _parseMap.Length = _discoverer.Plugins.Count; } _mappingArray.Clear(); _mappingDic.Clear(); int disabledCount = 0; // Locking plugins : // - If a plugin is disabled, it should not be launched, we do not add it to the map // - If a plugin needs to be started (MustExistAndRun), we lock its value to true // - If a plugin is the only implemention of a service and that this service has to be started, we lock this plugin's value to true // - If a plugin has no service references and does not implement any services as well, and that it is not asked to be // started and it is NOT running or it is running but stopLaunchedOptionals is true, we lock its value to false; int index = 0; foreach (IPluginInfo pI in _discoverer.Plugins) { // SolvedConfigStatus of the actual plugin. SolvedConfigStatus pluginStatus = _finalConfig.GetValueWithDefault(pI, SolvedConfigStatus.Optional); if (pluginStatus == SolvedConfigStatus.Disabled) { // If a plugin is disabled, it should not be launched, we do not add it to the map. disabledCount++; continue; } // SolvedConfigStatus of the implemented service if any. SolvedConfigStatus serviceStatus = pI.Service != null ? _finalConfig.GetValueWithDefault(pI.Service, SolvedConfigStatus.Optional) : SolvedConfigStatus.Optional; if (serviceStatus == SolvedConfigStatus.Disabled) { // If a plugin is disabled, it should not be launched, we do not add it to the map disabledCount++; continue; } // Here, we have no more disabled plugins. // Initializes a PluginData for this particular plugin and allocates // a new index in the bit array. Debug.Assert(index == _mappingArray.Count); PluginData pluginData = new PluginData(pI, index, IsPluginRunning(pI)); _mappingArray.Add(pluginData); _mappingDic.Add(pI, pluginData); if (pluginStatus == SolvedConfigStatus.MustExistAndRun || (serviceStatus == SolvedConfigStatus.MustExistAndRun && pI.Service.Implementations.Count == 1)) { // If a plugin needs to be started (MustExistAndRun), we lock its value to true. // If a plugin is the only implemention of a service and that this service has to be started, we lock this plugin's value to true. _parseMap.Set(index, true); pluginData.Locked = true; } else if (pI.Service == null && pI.ServiceReferences.Count == 0) // This plugin is independent. { // This is only an optimization. // The cost function gives a cost to the stop or the start of a plugin. When a plugin is independant like in this case, we lock its // status by taking into account the requirement (should it run? MustExistTryStart/OptionalTryStart) and its current status (IsPluginRunning) // and the stopLaunchedOptionals boolean. if ((pluginStatus != SolvedConfigStatus.MustExistTryStart || pluginStatus != SolvedConfigStatus.OptionalTryStart) && (!pluginData.IsRunning || stopLaunchedOptionals)) { // If a plugin has no service references and does not implement any services as well, // and that it is not asked to be started AND it is not running, we lock its value to false; _parseMap.Set(index, false); // We do not set thereWillBeNoChange to false: there will ACTUALLY be no changes // since the plugin is NOT running. } else { // If a plugin has no service references and does not implement any services as well, // and that it is asked to be started OR is currently running, we lock its value to true; _parseMap.Set(index, true); } pluginData.Locked = true; } index++; } // Trim the parseMap, to remove indexes that should have been filled by disabled plugins. Debug.Assert(_parseMap.Length >= disabledCount); _parseMap.Length -= disabledCount; // If the parseMap has a length of 0, it means either that there are no plugins or that all plugins are disabled. // In either of these cases, we don't calculate any execution plan. But we still have a valid execution plan, it just doesn't have any plugins to launch. BitArray bestCombination = _parseMap; if (_parseMap.Length > 0) { int bestCost = Int32.MaxValue; double combinationsCount = Math.Pow(2, _parseMap.Length - _mappingDic.Values.Count((e) => { return(e.Locked == true); })); for (int i = 0; i < combinationsCount; i++) { int cost = ComputeCombination(stopLaunchedOptionals); Debug.Assert(cost >= 0); // Return if the cost is equal to 0 (no better solution). if (cost == 0) { return(_lastBestPlan = GenerateExecutionPlan(_parseMap)); } if (cost < bestCost) { bestCost = cost; bestCombination = (BitArray)_parseMap.Clone(); } GenerateNextCombination(); } // If there is no valid combination, we return an impossible plan and // we do not keep it as the LastBestPlan. if (bestCost == Int32.MaxValue) { return(GenerateExecutionPlan(null)); } } return(_lastBestPlan = GenerateExecutionPlan(bestCombination)); }
public void ShouldReturnValueIfExists() { // Given var dictionary = new Dictionary<string, object> { { "key", "value" } }; // When var value = dictionary.GetValueWithDefault<string>("key"); // Then Assert.Equal("value", value); }
/// <summary> /// Gets the index of the plugin which cost to calculate, together with the current parseMap /// Returns how much launching (or not) this plugin costs. /// </summary> /// <param name="i">The index of the plugin to compute</param> /// <returns>The plugin's cost</returns> int ComputeElementCost(int i, bool stopLaunchedOptionals) { int cost = 0; // Gets the actual plugin's SolvedConfigStatus. PluginData plugin = _mappingArray[i]; IPluginInfo actualPlugin = plugin.PluginInfo; Debug.Assert(actualPlugin != null); SolvedConfigStatus status = _finalConfig.GetValueWithDefault(actualPlugin, SolvedConfigStatus.Optional); if (actualPlugin.Service != null) { var serviceStatus = _finalConfig.GetValueWithDefault(actualPlugin.Service.AssemblyQualifiedName, SolvedConfigStatus.Optional); status = status < serviceStatus ? serviceStatus : status; } // If the plugin has to be started. if (_parseMap[i]) { // Check its references. foreach (IServiceReferenceInfo serviceRef in actualPlugin.ServiceReferences) { if (!CheckReference(serviceRef, _parseMap, ref cost)) { return(int.MaxValue); } } #region Check the cost regarding the plugin's requirement, when the plugin is to be started // If the plugin needs to be started, but its not currently running. // we increase the cost only if the plugin is not absolutely needed. if (!plugin.IsRunning) { switch (status) { case SolvedConfigStatus.Optional: case SolvedConfigStatus.MustExist: cost += 10; break; case SolvedConfigStatus.Disabled: return(Int32.MaxValue); } } #endregion } // If the plugin doesn't need to be started (0 in the parseMap) else { // Here we check if the plugin can be stopped // If the plugin implements a service, and if the service has a MustExistAndRun requirement // and if this plugin is the only implementation of this service, so we return the max value. // Otherwise, if we find a substitute, the combinaison is possible. if (actualPlugin.Service != null) { bool substitue = false; if (_finalConfig.GetValueWithDefault(actualPlugin.Service, SolvedConfigStatus.Optional) == SolvedConfigStatus.MustExistAndRun) { for (int idx = 0; idx < _parseMap.Length; idx++) { if (_parseMap[idx] && _mappingArray[idx].PluginInfo.Service != null && _mappingArray[idx].PluginInfo.Service == actualPlugin.Service) { substitue = true; break; } } if (!substitue) { return(int.MaxValue); } } } else if (status == SolvedConfigStatus.MustExistAndRun) { return(int.MaxValue); } // If this plugin is already running and stopLaunchedOptionals is set to false, don't stop it. if (plugin.IsRunning && !stopLaunchedOptionals) { cost += 10; } #region Check the cost regarding the plugin's requirement when the plugin won't be started // if we wanted this plugin started ... if (status == SolvedConfigStatus.MustExistTryStart || status == SolvedConfigStatus.OptionalTryStart) { cost += 10; } #endregion } return(cost); }
public SolvedPluginConfigElement Find(Guid pluginId) { return(_cfg.GetValueWithDefault(pluginId, null)); }
internal List <SkippedFragment> GetSkippedFragments(object o) { return(_fragments.GetValueWithDefault(o, null)); }
ActionConfigurationResolved IRouteConfigurationContext.FindExisting(string name) { return(_actionsByName.GetValueWithDefault(name, null)); }
/// <summary> /// Gets the dependent solution by its name. /// </summary> /// <param name="name">The solution name.</param> /// <returns>The dependent solution or null.</returns> public DependentSolution this[string name] => _index.GetValueWithDefault(name, null);
public SolvedConfigStatus FinalRequirement(Guid pluginId) { return(_final.GetValueWithDefault(pluginId, SolvedConfigStatus.Optional)); }
/// <summary> /// Gets the <see cref="ConfigUserAction"/> for the specified PluginId /// </summary> /// <param name="pluginId">ID of the plugin</param> /// <returns>The UserAction for the specified PluginId</returns> public ConfigUserAction GetAction(Guid pluginId) { LiveUserAction action = _actions.GetValueWithDefault <Guid, LiveUserAction>(pluginId, null); return(action != null ? action.Action : ConfigUserAction.None); }
public T GetPlugin(Type t) => _mappings.GetValueWithDefault(t, null) as T;
/// <summary> /// Finds a plugin by its name. Returns null if it does not exist. /// </summary> /// <param name="pluginFullName">The plugin name.</param> /// <returns>Null if not found.</returns> public PluginData FindPlugin(string pluginFullName) { return(_plugins.GetValueWithDefault(pluginFullName, null)); }
/// <summary> /// Finds a service by its name. Returns null if it does not exist. /// </summary> /// <param name="serviceFullName">The service name.</param> /// <returns>Null if not found.</returns> public ServiceData FindService(string serviceFullName) { return(_services.GetValueWithDefault(serviceFullName, null)); }
public IUriHistory Find(Uri address) { return(_byAddress.GetValueWithDefault(address, null)); }
/// <summary> /// Finds a project by its <see cref="Project.ProjectName"/> or <see cref="Project.SolutionRelativePath"/>. /// </summary> /// <param name="key">The name or relative path.</param> /// <returns>The project or null if not found.</returns> public ProjectBase FindProject(string key) => _projectIndex.GetValueWithDefault(key, null);
/// <summary> /// Gets the command handler. Its <see cref="ICommandHandler.GetEnabled()"/> may be false. /// </summary> /// <param name="path">Path of the command.</param> /// <returns>The command handler or null.</returns> public ICommandHandler this[NormalizedPath path] => _commands.GetValueWithDefault(path, null);
ActionConfiguration IProtoRoute.FindDeclaredAction(string name) { var d = _declaredActions.GetValueWithDefault(name, null); return(d != null ? d.Action : null); }