public static CompositeUnitGeneratorInfo Make(FractionUnit cfg)
        {
            const string counterUnit     = nameof(IFractionalUnit <int, int> .CounterUnit);
            const string denominatorUnit = nameof(IFractionalUnit <int, int> .DenominatorUnit);

            return(new CompositeUnitGeneratorInfo(
                       counterUnit, cfg.CounterUnit,
                       denominatorUnit, cfg.DenominatorUnit,
                       cfg.UnitTypes));
        }
Пример #2
0
        public static string GetUnitName(this CommonFractionalUnit unitParts, ref FractionUnit fractionUnit)
        {
            if (fractionUnit is null)
            {
                fractionUnit = FractionUnitDefs.All
                               .ByValueTypeName(unitParts.Type.Value);
            }
            var u        = GetUnitname(fractionUnit.CounterUnit.Container, unitParts.CounterUnit);
            var d        = GetUnitname(fractionUnit.DenominatorUnit.Container, unitParts.DenominatorUnit);
            var unitName = u + "/" + d;

            return(unitName);
        }
        private static CsCodeWriter CreateCodeForRightFractionValue(FractionUnit rightFraction, OperatorParams p)
        {
            var cw = Ext.Create <MultiplyAlgebraGenerator>();

            Debug.WriteLine(rightFraction);
            var isCnt    = rightFraction.CounterUnit == p.Left;
            var isResult = rightFraction.CounterUnit == p.Result;

            if (!(isCnt ^ isResult))
            {
                if (TryHint(p, cw))
                {
                    return(cw);
                }
                cw.WriteLine("// scenario G");
                cw.WithThrowNotImplementedException();
                return(cw);
            }

            if (isCnt)
            {
                cw.WriteLine("// scenario A");
                cw.WriteLine(
                    $"// {p.Right.Value.ToLower()} unit will be synchronized with {p.Left.Value.ToLower()} unit");
                cw.WriteLine("var unit = new {2}({0}.Unit, {1}.Unit.DenominatorUnit);",
                             p.LeftMethodArgumentName,
                             p.RightMethodArgumentName,
                             p.Right.Unit);
                cw.WriteLine("var {1}Converted    = {1}.WithCounterUnit({0}.Unit);", p.LeftMethodArgumentName,
                             p.RightMethodArgumentName);
                cw.WriteLine($"var value = {{0}}.Value {p.Oper} {{1}}Converted.Value;", p.LeftMethodArgumentName,
                             p.RightMethodArgumentName);
                cw.WriteLine("return new {0}(value, {1}.Unit.DenominatorUnit);", p.Result.Value,
                             p.RightMethodArgumentName);
            }
            else
            {
                cw.WriteLine("// scenario B");
                cw.WriteLine("var unit = new {2}({1}.Unit.CounterUnit, {0}.Unit);",
                             p.LeftMethodArgumentName,
                             p.RightMethodArgumentName,
                             p.Right.Unit);
                cw.WriteLine("var {1}Converted    = {1}.WithDenominatorUnit({0}.Unit);", p.LeftMethodArgumentName,
                             p.RightMethodArgumentName);
                cw.WriteLine($"var value = {{0}}.Value {p.Oper} {{1}}Converted.Value;", p.LeftMethodArgumentName,
                             p.RightMethodArgumentName);
                cw.WriteLine("return new {0}(value, {1}.Unit.CounterUnit);", p.Result.Value, p.RightMethodArgumentName);
            }

            return(cw);
        }
        private static CsCodeWriter CreateCodeForLeftFractionValue(OperatorParams p,
                                                                   [NotNull] FractionUnit leftFraction)
        {
            if (leftFraction == null)
            {
                throw new ArgumentNullException(nameof(leftFraction));
            }

            var cw = Ext.Create <MultiplyAlgebraGenerator>();

            cw.WriteLine("// " + p);
            var canNormal = leftFraction.DenominatorUnit.Unit == p.Right.Unit &&
                            leftFraction.CounterUnit.Unit == p.Result.Unit;

            if (canNormal)
            {
                if (TryHint(p, cw))
                {
                    cw.WriteLine("// scenario D1");
                    return(cw);
                }
            }
            else
            {
                if (TryHint(p, cw))
                {
                    return(cw);
                }
                cw.WriteLine("// scenario D2");
                return(cw.WithThrowNotImplementedException());
            }

            var oper     = new OperatorCodeBuilderInput(p);
            var leftUnit = p.LeftMethodArgumentName + "Unit";

            oper.ConvertRight(leftUnit + ".DenominatorUnit");
            oper.ResultUnit = leftUnit + ".CounterUnit";
            oper.AddVariable(leftUnit, p.LeftMethodArgumentName + ".Unit");
            cw.WriteLine("// scenario D3");
            var builder = new OperatorCodeBuilder(oper);

            builder.WriteCode(cw);
            return(cw);
        }
Пример #5
0
        public void Generate(CommonFractionalUnitsCollection collection)
        {
            foreach (var item in collection.Items)
            {
                var cl = GetClass(item.Type.Container.GetTypename());
                cl.AddComment(item.ToString());

                FractionUnit fuDefinition = null;

                var description = string.Format("represents {0} unit '{1}'",
                                                item.Type.Value.FirstLower(), item.GetUnitName(ref fuDefinition));
                var p1 = fuDefinition.CounterUnit.Container + "." + item.CounterUnit;
                var p2 = fuDefinition.DenominatorUnit.Container + "." + item.DenominatorUnit;

                var x = new CsArguments(p1, p2).Create(item.Type.Unit);
                var f = cl.AddField(item.TargetPropertyName, item.Type.Unit.GetTypename())
                        .WithStatic()
                        .WithIsReadOnly()
                        .WithConstValue(x);

                f.Description = description;
            }
        }
Пример #6
0
        public static string GetUnitName(this CommonFractionalUnit unitParts)
        {
            FractionUnit fractionUnit = null;

            return(GetUnitName(unitParts, ref fractionUnit));
        }