コード例 #1
0
        /// <summary>
        /// Equals method of Dimension
        /// Two instances are equal if they share the same value
        /// </summary>
        /// <param name="obj">object that is being compared</param>
        /// <returns>true if the objects are equal, false if otherwise</returns>
        public override bool Equals(object obj)
        {
            if (obj == null || !obj.GetType().ToString().Equals("core.domain.SingleValueDimension"))
            {
                return(false);
            }

            if (this == obj)
            {
                return(true);
            }

            SingleValueDimension other = (SingleValueDimension)obj;

            return(Double.Equals(this.value, other.value));
        }
コード例 #2
0
        /// <summary>
        /// Applies the algorithm that restricts a component's width to a certain percentage of the customized father product's width
        /// </summary>
        /// <param name="customProduct">customized product</param>
        /// <param name="product">component product</param>
        /// <returns>component with restricted dimensions, null if the component is not compatible with any of the allowed dimensions</returns>
        public override Product apply(CustomizedProduct customProduct, Product originalProduct)
        {
            ready();
            List <Material> materials = new List <Material>();

            foreach (ProductMaterial productMaterial in originalProduct.productMaterials)
            {
                materials.Add(productMaterial.material);
            }
            List <Measurement> measurements = new List <Measurement>();

            foreach (ProductMeasurement productMeasurement in originalProduct.productMeasurements)
            {
                measurements.Add(productMeasurement.measurement);
            }

            Product copyProduct = null;

            if (originalProduct.components.Any())
            {
                List <Product> components = new List <Product>();
                foreach (Component component in originalProduct.components)
                {
                    components.Add(component.complementaryProduct);
                }
                copyProduct = new Product(originalProduct.reference, originalProduct.designation, originalProduct.modelFilename, originalProduct.productCategory, materials, measurements, components, originalProduct.slotWidths);
            }
            else
            {
                copyProduct = new Product(originalProduct.reference, originalProduct.designation, originalProduct.modelFilename, originalProduct.productCategory, materials, measurements, originalProduct.slotWidths);
            }
            double width = customProduct.customizedDimensions.width;
            double min   = getMinPercentage();

            if (min < 0)
            {
                throw new ArgumentException(MIN_PERCENTAGE_INVALID);
            }
            double max = getMaxPercentage();

            if (max < 0)
            {
                throw new ArgumentException(MAX_PERCENTAGE_INVALID);
            }
            double minWidth = width * getMinPercentage();
            double maxWidth = width * getMaxPercentage();

            List <Measurement> measurementsToRemove = new List <Measurement>();

            List <Measurement> productMeasurements = copyProduct.productMeasurements.Select(m => m.measurement).ToList();

            foreach (Measurement measurement in productMeasurements)
            {
                Dimension dimension = measurement.width;

                if (dimension.GetType() == typeof(SingleValueDimension))
                {
                    SingleValueDimension single = (SingleValueDimension)dimension;
                    if (single.value < minWidth || single.value > maxWidth)
                    {
                        measurementsToRemove.Add(measurement);
                    }
                }
                else if (dimension.GetType() == typeof(DiscreteDimensionInterval))
                {
                    DiscreteDimensionInterval discrete    = (DiscreteDimensionInterval)dimension;
                    List <DoubleValue>        valToRemove = new List <DoubleValue>();
                    foreach (double value in discrete.values)
                    {
                        if (value < minWidth || value > maxWidth)
                        {
                            valToRemove.Add(value);
                        }
                    }
                    foreach (double val in valToRemove)
                    {
                        discrete.values.Remove(val);
                    }
                    if (discrete.values.Count == 0)
                    {
                        measurementsToRemove.Add(measurement);
                    }
                    else if (discrete.values.Count == 1)
                    {
                        measurement.changeWidthDimension(new SingleValueDimension(discrete.values[0]));
                    }
                }
                else if (dimension.GetType() == typeof(ContinuousDimensionInterval))
                {
                    ContinuousDimensionInterval continuous = (ContinuousDimensionInterval)dimension;
                    if (continuous.minValue > maxWidth || continuous.maxValue < minWidth)
                    {
                        measurementsToRemove.Add(measurement);
                    }
                    else
                    {
                        double currentMin = continuous.minValue, currentMax = continuous.maxValue;
                        if (continuous.minValue < minWidth)
                        {
                            currentMin = minWidth;
                        }
                        if (continuous.maxValue > maxWidth)
                        {
                            currentMax = maxWidth;
                        }
                        if (currentMin == currentMax)
                        {
                            SingleValueDimension single = new SingleValueDimension(currentMax);
                            measurement.changeWidthDimension(single);
                        }
                        else
                        {
                            double newIncrement = continuous.increment;
                            if (currentMax - currentMin < continuous.increment)
                            {
                                newIncrement = currentMax - currentMin;
                            }
                            ContinuousDimensionInterval newContinuous = new ContinuousDimensionInterval(currentMin, currentMax, newIncrement);

                            measurement.changeWidthDimension(newContinuous);
                        }
                    }
                }
            }
            foreach (Measurement measurement in measurementsToRemove)
            {
                try {
                    copyProduct.removeMeasurement(measurement);
                } catch (InvalidOperationException) {
                    return(null);
                } catch (ArgumentException) {
                    return(null);
                }
            }
            return(copyProduct);
        }