Exemplo n.º 1
0
        public void AddNewCompositUnit(string leftUnit, string symbol, string rightUnit, string newUnitName)
        {
            var u1 = GetUnit(leftUnit);

            if (u1 is null)
            {
                throw new HandleException($"Can't find unit with name {leftUnit}", 1124);
            }
            var u2 = GetUnit(rightUnit);

            if (u2 is null)
            {
                throw new HandleException($"Can't find unit with name {rightUnit}", 1124);
            }
            var op = GetOperator(symbol);

            if (op is null)
            {
                throw new HandleException($"Can't find operator {symbol}", 1125);
            }
            if (op is BinaryOperator opb)
            {
                var unit = new BinaryCompositUnit(u1, opb, u2, newUnitName);
                Units.Add(unit);
                AddEdges(op, unit, u1, u2);
            }
            else
            {
                throw new HandleException($"Can't use unary operator on 2 parameters", 1900);
            }
        }
Exemplo n.º 2
0
 public void AddNewCompositUnit(IUnit left, IOperator @operator, IUnit right, bool isInfered = false, bool fast = false)
 {
     if (@operator is BinaryOperator opb)
     {
         var tmp = new BinaryCompositUnit(left, opb, right, null, isInfered);
         var(unit, depth) = fast ? Helpers.ShortestUnitFast(tmp, this) : Helpers.ShortestUnit(tmp, this);
         if (!Units.Add(unit))
         {
             unit = Units.FirstOrDefault(i => i.Equals(unit));
         }
         if (!Units.Add(left))
         {
             left = Units.FirstOrDefault(i => i.Equals(left));
         }
         if (!Units.Add(right))
         {
             right = Units.FirstOrDefault(i => i.Equals(right));
         }
         AddEdges(@operator, unit, left, right);
     }
     else
     {
         throw new HandleException("Can't use unary operator on 2 parameters", 1903);
     }
 }
Exemplo n.º 3
0
        internal void AddUnitsNetDescription(UnitsNetDescription description, string unitsCoreName = null)
        {
            lock (_lock)
            {
                var unitsCoreUnit = GetUnit(unitsCoreName ?? description.Name);
                if (unitsCoreUnit is null)
                {
                    IUnit uppers = null, downers = null;
                    foreach (var bd in description.BaseDimensions?.Where(i => i.Value < 0) ?? Enumerable.Empty <KeyValuePair <string, int> >())
                    {
                        var a = Metadata.Basedimensions.Bases[bd.Key];
                        var n = bd.Value * -1;
                        while (--n >= 0)
                        {
                            if (downers is null)
                            {
                                downers = a;
                            }
                            else
                            {
                                downers = new BinaryCompositUnit(downers, BinaryOperator.TIMES, a);
                            }
                        }
                    }
                    foreach (var bd in description.BaseDimensions?.Where(i => i.Value > 0) ?? Enumerable.Empty <KeyValuePair <string, int> >())
                    {
                        var a = Metadata.Basedimensions.Bases[bd.Key];
                        var n = bd.Value;
                        while (--n >= 0)
                        {
                            if (uppers is null)
                            {
                                uppers = a;
                            }
                            else
                            {
                                uppers = new BinaryCompositUnit(uppers, BinaryOperator.TIMES, a);
                            }
                        }
                    }
                    unitsCoreUnit = downers is null?
                                    uppers?.Rename(unitsCoreName ?? description.Name) ?? Scalar.Get:
                                    new BinaryCompositUnit(uppers ?? Scalar.Get, BinaryOperator.OVER, downers, unitsCoreName ?? description.Name, false);

                    if (!Units.Add(unitsCoreUnit))
                    {
                        var old = Units.First(i => i.Equals(unitsCoreUnit));
                        if (old.Name.Length > unitsCoreUnit.Name.Length)
                        {
                            Units.Remove(old);
                            Units.Add(unitsCoreUnit);
                        }
                    }
                }
                if (Descriptions.ContainsKey(unitsCoreUnit))
                {
                    Descriptions[unitsCoreUnit].Add(description);
                }
                else
                {
                    Descriptions.Add(unitsCoreUnit, new List <UnitsNetDescription> {
                        description
                    });
                }
                var adds = description.Units
                           .ToMesurmentUnits(unitsCoreUnit)
                           .Select(this.MesurmentUnits.Add)
                           .ToList();
            }
        }