/// <inheritdoc />
        public virtual T SynchroniseGet <T>(IRegistry registry, string key)
        {
            if (registry != null)
            {
                IRegistryResolver resolver = RegistryResolvers.TryGetValue(registry, () => new RegistryResolver(registry));
                //return resolver.ResolveGetSingle<>()
                string[] emptyArrayThrowaway;

                T[] result = resolver.ResolveGet <T>(key, out emptyArrayThrowaway);

                if (result.Length != 0)
                {
                    return(result[0]);
                }
            }

            foreach (ISynchronisationSource source in Sources)
            {
                T res;
                if (source.TryGet(key, out res))
                {
                    return(res);
                }
            }

            return(default(T));
        }
示例#2
0
        /// <summary>
        /// Resolve all registry entries using a given registry resolver.
        /// </summary>
        /// <param name="registryResolver">The registry resolver to use.</param>
        /// <param name="allRegistryEntries">The registry entries to resolve.</param>
        /// <param name="resultAllResolvedRegistryEntries">The resulting resolved registry entries.</param>
        public static void ResolveAllRequiredRegistry(IRegistryResolver registryResolver, IEnumerable <string> allRegistryEntries, ISet <string> resultAllResolvedRegistryEntries)
        {
            resultAllResolvedRegistryEntries.Clear();

            foreach (string registryEntry in allRegistryEntries)
            {
                string[] resolvedEntries;

                registryResolver.ResolveGet <object>(registryEntry, out resolvedEntries, null);

                resultAllResolvedRegistryEntries.AddRange(resolvedEntries);
            }
        }
示例#3
0
        /// <inheritdoc />
        public override void SubInvoke(IRegistry registry, IRegistryResolver resolver)
        {
            IComputationHandler handler = Operator.Handler;

            string registryEntryToProcess = ParameterRegistry.Get <string>("registry_entry_to_process");
            Func <T, IComputationHandler, INumber> metricFunction = ParameterRegistry.Get <Func <T, IComputationHandler, INumber> >("metric_function");
            string metricSharedResultIdentifier = ParameterRegistry.Get <string>("metric_shared_result_identifier");

            object[] entries = resolver.ResolveGet <object>(registryEntryToProcess);

            double totalMetric = 0.0;
            int    count       = 0;

            foreach (object entry in entries)
            {
                T entryAsT = entry as T;
                IEnumerable <T>         entryAsEnumerable = entry as IEnumerable <T>;
                IDictionary <string, T> entryAsDictionary = entry as IDictionary <string, T>;

                if (entryAsDictionary != null)
                {
                    entryAsEnumerable = entryAsDictionary.Values;
                }

                if (entryAsT != null)
                {
                    totalMetric += metricFunction.Invoke(entryAsT, handler).GetValueAs <double>();
                    count++;
                }
                else if (entryAsEnumerable != null)
                {
                    foreach (T value in entryAsEnumerable)
                    {
                        totalMetric += metricFunction.Invoke(value, handler).GetValueAs <double>();
                        count++;
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Cannot process metric for entry of type {entry.GetType()} with identifier \"{registryEntryToProcess}\", must be {typeof(T)} or enumerable thereof.");
                }
            }

            double resultMetric = totalMetric / count;

            resolver.ResolveSet(metricSharedResultIdentifier, resultMetric, addIdentifierIfNotExists: true);
        }
示例#4
0
        /// <summary>
        /// Try to retrieve a value from this source (if existent).
        /// </summary>
        /// <typeparam name="T">The type of the value that will be retrieved.</typeparam>
        /// <param name="key">The key of the value.</param>
        /// <param name="val">The value itself that will be assigned if it could be retrieved, <c>null</c> otherwise.</param>
        /// <returns><c>True</c> if the source could retrieve given key, <c>false</c> otherwise.</returns>
        public bool TryGet <T>(string key, out T val)
        {
            IRegistry         values   = (IRegistry)ParameterRegistry[ValueIdentifier];
            IRegistryResolver resolver = values.Get <IRegistryResolver>(RegistryResolver);

            //TODO: validate lock requirement, probably it is required
            lock (values)
            {
                T[] vals = resolver.ResolveGet <T>(key);

                if (vals.Length > 0)
                {
                    val = vals[0];
                    return(true);
                }

                val = default(T);
                return(false);
            }
        }
示例#5
0
        /// <summary>
        /// Invoke this hook with a certain parameter registry.
        /// </summary>
        /// <param name="registry">The registry containing the required values for this hook's execution.</param>
        /// <param name="resolver">A helper resolver for complex registry entries (automatically cached).</param>
        public override void SubInvoke(IRegistry registry, IRegistryResolver resolver)
        {
            string[] valueIdentifiers = ParameterRegistry.Get <string[]>("value_identifiers");

            IDictionary <string, object> valuesByIdentifier = ParameterRegistry.Get <IDictionary <string, object> >("value_buffer");

            valuesByIdentifier.Clear();

            for (int i = 0; i < valueIdentifiers.Length; i++)
            {
                string[] resolvedIdentifiers;
                object[] values = resolver.ResolveGet <object>(valueIdentifiers[i], out resolvedIdentifiers);

                for (int y = 0; y < resolvedIdentifiers.Length; y++)
                {
                    valuesByIdentifier.Add(resolvedIdentifiers[y], values[y]);
                }
            }

            ReportValues(valuesByIdentifier, ParameterRegistry.Get <bool>("report_epoch_iteration"), registry.Get <int>("epoch"), registry.Get <int>("iteration"));
        }