private void Split_ApplyDivisionOnChildren(ValueObject parent, IEnumerable <ValueObject> children, Queue <ValueObject> qVoMod, Dictionary <long, Tuple <ValueObject, int, string, bool> > dicAllVo, HashSet <KeyValuePair <long, double> > modifiedValues, string format)
        {
            IEnumerable <ValueObject> childrenToApply = children.Where(vo => !dicAllVo[vo.Id].Item4);
            double total = ValueObjectDomain.GetMostCurrentValue(parent);
            double subTotalUnmodified = children.Where(vo => dicAllVo[vo.Id].Item4).Select(vo => ValueObjectDomain.GetMostCurrentValue(vo))?.Sum() ?? 0;
            double realTotal          = total - subTotalUnmodified;
            double sumOldTargets      = childrenToApply.Select(vo => ValueObjectDomain.GetMostCurrentValue(vo))?.Sum() ?? 0;
            double quota = sumOldTargets == 0 ? 0 : realTotal / sumOldTargets;

            double precision = ValueObjectHelper.GetPrecisionAbs(format);

            if (precision == 0)
            {
                precision = 1;
            }

            // Split
            double totalP   = realTotal / precision;
            double deltaSum = 0;
            Dictionary <long, double> expectedValues = new Dictionary <long, double>();

            foreach (ValueObject child in childrenToApply)
            {
                if (expectedValues.ContainsKey(child.Id))
                {
                    continue;
                }

                double oldValue = ValueObjectDomain.GetMostCurrentValue(child) / precision;
                double newValue = oldValue * quota;
                double delta    = newValue;
                newValue  = Math.Floor(newValue);
                delta    -= newValue;
                deltaSum += delta;

                expectedValues.Add(child.Id, newValue);
                qVoMod.Append(child);
            }

            // Arrondi
            double deltaSumRounded = Math.Round(deltaSum);

            foreach (KeyValuePair <long, double> kvp in expectedValues.OrderByDescending(k => k.Value))
            {
                double newValue = kvp.Value;

                if (deltaSumRounded >= 1)
                {
                    newValue        += 1;
                    deltaSumRounded -= 1;
                }

                newValue = newValue * precision;
                dicAllVo[kvp.Key].Item1.FutureValue = newValue;
                modifiedValues.Add(new KeyValuePair <long, double>(kvp.Key, newValue));
            }
        }
Ejemplo n.º 2
0
 public static bool operator !=(FullName obj1, FullName obj2) =>
 ValueObjectHelper.NotEqualsOperator(obj1, obj2);
Ejemplo n.º 3
0
 public override int GetHashCode() =>
 ValueObjectHelper.GetHashCode <FullName>(this);
Ejemplo n.º 4
0
 public override bool Equals(object obj) =>
 ValueObjectHelper.Equals <FullName>(this, obj);
        private async Task <HttpResponseMessageResult> CheckTreeSum(long referenceSequence, int ordreSequence, ConstraintLevelEnum level, SelectorInstance selectorInstance, WorkflowInstance workflowInstance)
        {
            HttpResponseMessageResult res = new HttpResponseMessageResult()
            {
                IsSuccess = true
            };
            await ConfigVariableDomain.LoadVariables();

            string nomDimension = await GetConstraintParameter(referenceSequence, ordreSequence, Constant.PARAMETER_CONSTRAINT_ALIGNMENTNAME);

            Dictionary <long, Tuple <ValueObject, int, string, bool> > dicVo = await ValueObjectDomain.CreateDictionaryVO(nomDimension, selectorInstance);

            // Construction des arbres
            int topLvl    = dicVo.Values.Select(t => t.Item2).Min();
            int bottomLvl = dicVo.Values.Select(t => t.Item2).Max();
            IEnumerable <TreeValueObject> lstTree = ValueObjectDomain.BuildTreeVO(dicVo, nomDimension, topLvl, bottomLvl);

            // On parcours les arbres et on vérifie les valeurs
            HashSet <long> idsAlreadyComputed = new HashSet <long>();
            //Dictionary<long, Tuple<double, double>> outOfConstraint = new Dictionary<long, Tuple<double, double>>();
            Dictionary <long, object> outOfConstraint = new Dictionary <long, object>();

            foreach (TreeValueObject tvo in lstTree)
            {
                string format = await ValueObjectDomain.GetNumericalFormat(tvo.Node.TypeValue, workflowInstance.DataSetId);

                for (int currentLvl = bottomLvl; currentLvl > topLvl; currentLvl--)
                {
                    IEnumerable <TreeValueObject> nodes = TreeValueObject.GetNodesFromLevel(tvo, currentLvl);
                    foreach (TreeValueObject child in nodes)
                    {
                        if ((child.Parent == null) || idsAlreadyComputed.Contains(child.Parent.Node.Id))
                        {
                            continue;
                        }

                        double sumComputed = child.Parent.Children.Select(subnode => ValueObjectDomain.GetMostCurrentValue(subnode.Node)).Sum();
                        double sumParent   = ValueObjectDomain.GetMostCurrentValue(child.Parent.Node);

                        if (!ValueObjectHelper.AlmostEqual(sumComputed, sumParent, format))
                        {
                            outOfConstraint.Add(child.Parent.Node.Id, new { expected = sumParent, computed = sumComputed });
                        }

                        idsAlreadyComputed.Add(child.Parent.Node.Id);
                    }
                }
            }

            if (outOfConstraint.Count > 0)
            {
                res.Json    = JsonConvert.SerializeObject(outOfConstraint);
                res.Message = "Some values are not sum of their children.";

                if ((level == ConstraintLevelEnum.Info) || (level == ConstraintLevelEnum.Warning))
                {
                    res.IsSuccess = true;
                }
                if (level == ConstraintLevelEnum.Error)
                {
                    res.IsSuccess = false;
                }
            }

            return(res);
        }