private ParameterViewModel(ParameterInfo info, ConstructorViewModel owner, DoubleRange range, double value)
        {
            bool isInteger = info.ParameterType == typeof(int);

            double min = range.Min;
            double max = range.Max;

            if (min < -1e+5)
            {
                min = -1e+5;
            }

            if (max > 1e+5)
            {
                max = 1e+5;
            }

            double step = 0.1;

            if (isInteger)
            {
                step = 1;
            }

            Min        = min;
            Max        = max;
            Step       = step;
            Value      = value;
            Name       = DistributionManager.Normalize(info.Name);
            Owner      = owner;
            Parameter  = info;
            IsDiscrete = isInteger;
        }
        /// <summary>
        ///   Attempts to create a distribution's constructor. If the constructor
        ///   parameter's aren't valid, this method fails and returns false.
        /// </summary>
        ///
        /// <param name="info">The constructor's reflection information.</param>
        /// <param name="distribution">The distribution that owns this constructor.</param>
        /// <param name="constructor">The created distribution constructor.</param>
        ///
        /// <returns>True if the constructor could be created; false otherwise.</returns>
        ///
        public static bool TryParse(ConstructorInfo info, DistributionViewModel distribution, out ConstructorViewModel constructor)
        {
            constructor = new ConstructorViewModel(info, distribution);

            foreach (var param in info.GetParameters())
            {
                ParameterViewModel viewModel;
                if (!ParameterViewModel.TryParse(param, constructor, out viewModel))
                {
                    return(false);
                }

                constructor.Parameters.Add(viewModel);
            }

            return(true);
        }
        /// <summary>
        ///   Attempts to create a constructor's parameter. If the parameter doesn't
        ///   qualify as a valid parameter to be used in the automatic distribution
        ///   construction, the method fails and returns false.
        /// </summary>
        ///
        /// <param name="info">The parameter's reflection information.</param>
        /// <param name="owner">The constructor that should own this parameter.</param>
        /// <param name="parameter">The created distribution parameter.</param>
        ///
        /// <returns>True if the parameter could be created; false otherwise.</returns>
        ///
        public static bool TryParse(ParameterInfo info, ConstructorViewModel owner, out ParameterViewModel parameter)
        {
            parameter = null;

            DoubleRange range;

            if (!DistributionManager.TryGetRange(info, out range))
            {
                return(false);
            }

            double value;

            if (!DistributionManager.TryGetDefault(info, out value))
            {
                return(false);
            }

            parameter = new ParameterViewModel(info, owner, range, value);

            return(true);
        }
예제 #4
0
        /// <summary>
        /// Attempts to create a new DistributionViewModel from a given type.
        /// </summary>
        public static bool TryParse(MainViewModel owner, Type type, Dictionary <string, DocumentationViewModel> doc, out DistributionViewModel distribution)
        {
            distribution = new DistributionViewModel(owner);

            if (!typeof(IUnivariateDistribution).IsAssignableFrom(type) || !doc.ContainsKey(type.Name))
            {
                return(false);
            }


            string name = DistributionManager.GetDistributionName(type);


            // Extract all properties with return value of double
            //
            var properties = new List <PropertyViewModel>();

            foreach (PropertyInfo prop in type.GetProperties())
            {
                PropertyViewModel property;
                if (PropertyViewModel.TryParse(prop, distribution, out property))
                {
                    properties.Add(property);
                }
            }

            // Extract buildable constructors. A constructor is
            // considered buildable if we can extract valid ranges
            // and default values from all of its parameters
            //
            var list = new List <ConstructorViewModel>();

            foreach (var ctor in type.GetConstructors())
            {
                ConstructorViewModel constructor;
                if (ConstructorViewModel.TryParse(ctor, distribution, out constructor))
                {
                    list.Add(constructor);
                }
            }

            if (list.Count == 0)
            {
                return(false);
            }

            // For the time being, just consider the buildable
            // constructor with the largest number of parameters.
            //
            var main = list.OrderByDescending(x => x.Parameters.Count).First();



            // Extract some documentation
            var documentation = doc[type.Name];

            documentation.Name = name;

            distribution.Constructor   = main;
            distribution.Properties    = new ObservableCollection <PropertyViewModel>(properties);
            distribution.Parameters    = main.Parameters;
            distribution.Type          = type;
            distribution.Name          = name;
            distribution.Documentation = documentation;


            foreach (var parameter in distribution.Constructor.Parameters)
            {
                parameter.ValueChanged += distribution.distribution_OnParameterChanged;
            }

            distribution.Options = DistributionManager.GetFittingOptions(distribution.Type);

            return(true);
        }
        /// <summary>
        ///   Attempts to create a distribution's constructor. If the constructor 
        ///   parameter's aren't valid, this method fails and returns false.
        /// </summary>
        /// 
        /// <param name="info">The constructor's reflection information.</param>
        /// <param name="distribution">The distribution that owns this constructor.</param>
        /// <param name="constructor">The created distribution constructor.</param>
        /// 
        /// <returns>True if the constructor could be created; false otherwise.</returns>
        /// 
        public static bool TryParse(ConstructorInfo info, DistributionViewModel distribution, out ConstructorViewModel constructor)
        {
            constructor = new ConstructorViewModel(info, distribution);

            foreach (var param in info.GetParameters())
            {
                ParameterViewModel viewModel;
                if (!ParameterViewModel.TryParse(param, constructor, out viewModel))
                    return false;

                constructor.Parameters.Add(viewModel);
            }

            return true;
        }
        private ParameterViewModel(ParameterInfo info, ConstructorViewModel owner, DoubleRange range, double value)
        {
            bool isInteger = info.ParameterType == typeof(int);

            double min = range.Min;
            double max = range.Max;

            if (min < -1e+5)
                min = -1e+5;

            if (max > 1e+5)
                max = 1e+5;

            double step = 0.1;
            if (isInteger)
                step = 1;

            Min = min;
            Max = max;
            Step = step;
            Value = value;
            Name = DistributionManager.Normalize(info.Name);
            Owner = owner;
            Parameter = info;
            IsDiscrete = isInteger;
        }
        /// <summary>
        ///   Attempts to create a constructor's parameter. If the parameter doesn't
        ///   qualify as a valid parameter to be used in the automatic distribution
        ///   construction, the method fails and returns false.
        /// </summary>
        /// 
        /// <param name="info">The parameter's reflection information.</param>
        /// <param name="owner">The constructor that should own this parameter.</param>
        /// <param name="parameter">The created distribution parameter.</param>
        /// 
        /// <returns>True if the parameter could be created; false otherwise.</returns>
        /// 
        public static bool TryParse(ParameterInfo info, ConstructorViewModel owner, out ParameterViewModel parameter)
        {
            parameter = null;

            DoubleRange range;
            if (!DistributionManager.TryGetRange(info, out range))
                return false;

            double value;
            if (!DistributionManager.TryGetDefault(info, out value))
                return false;

            parameter = new ParameterViewModel(info, owner, range, value);

            return true;
        }