示例#1
0
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_Power_Length_LinearPowerLoss)))
            {
                return;
            }
            e.Result.Comment = e.Input.DebugIs;
            var input  = e.Input;
            var result = e.Result;

            if (input.Is <ValueA, ValueB, ValueC>("/"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment += " " + nameof(Def_Power_Length_LinearPowerLoss);
                result.WithResultUnit <UnitC>(
                    $"{ValueA.FirstLower()}.Unit",
                    $"{ValueB.FirstLower()}.Unit");
                return;
            }

            if (input.Is <ValueC, ValueB, ValueA>("*"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment += " " + nameof(Def_Power_Length_LinearPowerLoss);
                result.AddVariable("tmp", $"{ValueC.FirstLower()}.Unit");
                result.AddVariable("resultUnit", "tmp.CounterUnit");
                result.AddVariable("lengthUnit", "tmp.DenominatorUnit");
                result.ConvertRight("lengthUnit");
                result.ResultUnit = "resultUnit";
            }
        }
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_Force_Length_LinearForce)))
            {
                return;
            }
            e.Result.Comment = e.Input.DebugIs;
            var input  = e.Input;
            var result = e.Result;

            if (input.Is <Force, Length, LinearForce>("/"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment   += " " + nameof(Def_Force_Length_LinearForce);
                result.ResultUnit = "new LinearForceUnit(force.Unit, length.Unit)";
                return;
            }

            if (input.Is <LinearForce, Length, Force>("*"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment += " " + nameof(Def_Force_Length_LinearForce);
                result.ConvertRight("linearForce.Unit.DenominatorUnit");
                result.ResultUnit = "linearForce.Unit.CounterUnit";
                return;
            }
        }
示例#3
0
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_Power_Length_LinearPowerLoss)))
            {
                return;
            }
            e.Result.Comment = e.Input.DebugIs;
            var input  = e.Input;
            var result = e.Result;

            if (input.Is <UnitA, UnitB, UnitC>("/"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment   += " " + nameof(Def_Power_Length_LinearPowerLoss);
                result.ResultUnit = $"new {ValueC}Unit({ValueA.FirstLower()}.Unit, {ValueB.FirstLower()}.Unit)";
                return;
            }

            if (input.Is <UnitC, UnitB, UnitA>("*"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment += " " + nameof(Def_Power_Length_LinearPowerLoss);
                result.ConvertRight($"{ValueC.FirstLower()}.Unit.DenominatorUnit");
                result.ResultUnit = $"{ValueC.FirstLower()}.Unit.CounterUnit";
            }
        }
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_Power_EnergyMassDensity_MassStream)))
            {
                return;
            }
            var input  = e.Input;
            var result = e.Result;

            if (input.Is <Power, EnergyMassDensity, MassStream>("/"))
            {
                e.SetHandled();
                result.AddVariable("ru", "$(right).Unit");
                result.AddVariable <Energy>("energy", "$(right).Value", "ru.CounterUnit");
                result.AddVariable("time", "energy / power");
                result.WithResultUnit <MassStreamUnit>("ru.DenominatorUnit", "time.Unit");
                result.UseValueExpression = "1 / time.Value";
                return;
            }

            if (input.Is <Power, MassStream, EnergyMassDensity>("/"))
            {
                e.SetHandled();
                result.AddVariable("massUnit", "$(right).Unit.CounterUnit");
                result.AddVariable("leftConverted", "$(left).ConvertTo(PowerUnits.Watt)");
                result.AddVariable("rightConverted", "$(right).ConvertTo(new MassStreamUnit(massUnit, TimeUnits.Second))");
                result.WithResultUnit <EnergyMassDensityUnit>("EnergyUnits.Joule", "massUnit");
                result.UseValueExpression = "leftConverted.Value / rightConverted.Value";
                return;
            }

            if (input.Is <EnergyMassDensity, MassStream, Power>("*"))
            {
                e.SetHandled();
                result.AddVariable("massUnit", "$(right).Unit.CounterUnit");
                result.AddVariable("leftConverted", "$(left).ConvertTo(new EnergyMassDensityUnit(EnergyUnits.Joule, massUnit))");
                result.AddVariable("rightConverted", "$(right).ConvertTo(new MassStreamUnit(massUnit, TimeUnits.Second))");
                result.ResultUnit         = "PowerUnits.Watt";
                result.UseValueExpression = "leftConverted.Value * rightConverted.Value";
                return;
            }

            if (input.Is <MassStream, EnergyMassDensity, Power>("*"))
            {
                e.SetHandled();
                result.AddVariable("massUnit", "$(left).Unit.CounterUnit");
                result.AddVariable("leftConverted", "$(right).ConvertTo(new EnergyMassDensityUnit(EnergyUnits.Joule, massUnit))");
                result.AddVariable("rightConverted", "$(left).ConvertTo(new MassStreamUnit(massUnit, TimeUnits.Second))");
                result.ResultUnit         = "PowerUnits.Watt";
                result.UseValueExpression = "leftConverted.Value * rightConverted.Value";
                return;
            }

            result.SetThrow();
        }
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_LinearForce_Length_Pressure)))
            {
                return;
            }

            var input  = e.Input;
            var result = e.Result;


            if (input.Is <Length, Pressure, LinearForce>("*"))
            {
                e.SetHandled();
                result.Comment += " " + nameof(Def_LinearForce_Length_Pressure);
                AddAB(result, U_Length, U_Pressure, U_LinearForce, input.Oper);
                return;
            }

            if (input.Is <Length, Pressure, LinearForce>("*"))
            {
                e.SetHandled();
                result.Comment += " " + nameof(Def_LinearForce_Length_Pressure);
                AddAB(result, U_Length, U_Pressure, U_LinearForce, input.Oper);
                return;
            }

            if (input.Is <Pressure, Length, LinearForce>("*"))
            {
                e.SetHandled();
                result.Comment += " " + nameof(Def_LinearForce_Length_Pressure);
                AddAB(result, U_Pressure, U_Length, U_LinearForce, input.Oper);
                return;
            }

            if (input.Is <LinearForce, Pressure, Length>("/"))
            {
                e.SetHandled();
                result.Comment += " " + nameof(Def_LinearForce_Length_Pressure);
                AddAB(result, U_LinearForce, U_Pressure, U_Length, input.Oper);
                return;
            }
            if (input.Is <LinearForce, Length, Pressure>("/"))
            {
                e.SetHandled();
                result.Comment += " " + nameof(Def_LinearForce_Length_Pressure);
                AddAB(result, U_LinearForce, U_Length, U_Pressure, input.Oper);
                return;
            }
        }
        public static void TryCreate(OperatorHints.CreateOperatorCodeEventArgs args, ClrTypesResolver resolver)
        {
            var instance = new HeuristicOperatorGenerator(args, resolver);

            try
            {
                instance.TryCreateInternal();
            }
            catch
            {
                args.Result.SetComment();
                args.Result.Comment += " Exception !!!";
                args.SetHandled();
            }
        }
示例#7
0
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs args)
        {
            if (!args.ShouldITryToCreate(nameof(Def_Energy_Time_Power)))
            {
                return;
            }
            var input  = args.Input;
            var result = args.Result;

            result.Comment = args.Input.DebugIs;

            if (input.Is <Energy, Power, Time>("/"))
            {
                args.SetHandled();
                AlgebraDefUtils.Add1(result, "timeSeconds");
                result.AddVariable("returnType", "energy.Unit.GetSuggestedTimeUnit()");
                result.AddVariable("time", "Time.FromSecond(timeSeconds).ConvertTo(returnType)");
                result.UseReturnExpression = "time";
                return;
            }

            if (input.Is <Energy, Time, Power>("/"))
            {
                args.SetHandled();
                AlgebraDefUtils.CreateHeuristicCode(args);
                return;
            }

            if (input.Is <Time, Power, Energy>("*"))
            {
                args.SetHandled();
                AlgebraDefUtils.Add1(result, "basicEnergy");
                result.UseValueExpression = "basicEnergy";
                result.ResultUnit         = "EnergyUnits.Joule";
                return;
            }

            if (input.Is <Power, Time, Energy>("*"))
            {
                args.SetHandled();
                result.AddVariable("x", "4");
                AlgebraDefUtils.Add1(result, "basicEnergy");
                result.UseValueExpression = "basicEnergy";
                result.ResultUnit         = "EnergyUnits.Joule";
                return;
            }
        }
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_Force_Area_Pressure)))
            {
                return;
            }
            var input  = e.Input;
            var result = e.Result;

            void Simple(string leftUnit, string rightUnit, string resultUnit)
            {
                result.AddVariable("leftConverted", "$(left).ConvertTo(" + leftUnit + ")");
                result.AddVariable("rightConverted", "$(right).ConvertTo(" + rightUnit + ")");
                result.ResultUnit         = resultUnit;
                result.UseValueExpression = "leftConverted.Value " + input.Oper + " rightConverted.Value";
            }

            if (input.Is <TMul, T1, T2>("/"))
            {
                e.SetHandled();
                Simple(TMulUnit, T1Unit, T2Unit);
                return;
            }

            if (input.Is <TMul, T2, T1>("/"))
            {
                e.SetHandled();
                Simple(TMulUnit, T2Unit, T1Unit);
                return;
            }

            if (input.Is <T1, T2, TMul>("*"))
            {
                e.SetHandled();
                Simple(T1Unit, T2Unit, TMulUnit);
                return;
            }

            if (input.Is <T2, T1, TMul>("*"))
            {
                e.SetHandled();
                Simple(T2Unit, T1Unit, TMulUnit);
                return;
            }

            result.SetThrow();
        }
 private HeuristicOperatorGenerator(OperatorHints.CreateOperatorCodeEventArgs args, ClrTypesResolver resolver)
 {
     _args     = args;
     _resolver = resolver;
 }
        public static void CreateHeuristicCode(OperatorHints.CreateOperatorCodeEventArgs args)
        {
            var clrTypesResolver = new ClrTypesResolver(typeof(Length).Assembly);

            HeuristicOperatorGenerator.TryCreate(args, clrTypesResolver);
        }
        private static void HandleCreateOperatorCode(object sender, OperatorHints.CreateOperatorCodeEventArgs e)
        {
            if (!e.ShouldITryToCreate(nameof(Def_LinearPowerLoss_Length_Irradiance)))
            {
                return;
            }
            e.Result.Comment = e.Input.DebugIs;
            var input  = e.Input;
            var result = e.Result;

            if (input.Is <UnitA, UnitB, UnitC>("/"))
            {
                e.SetHandled();
                result.SetComment();

                result.AddVariable("lengthUnit", "linearPowerLoss.Unit.DenominatorUnit");
                result.AddVariable("areaUnit", "lengthUnit.GetAreaUnit()");
                result.AddVariable("powerUnit", $"{ValueA.FirstLower()}.Unit.CounterUnit");

                result.Comment += " " + nameof(Def_LinearPowerLoss_Length_Irradiance);
                result.ConvertRight("lengthUnit");
                result.WithResultUnit <UnitCUnit>("powerUnit", "areaUnit");
                return;
            }

            if (input.Is <UnitA, UnitC, UnitB>("/"))
            {
                e.SetHandled();
                result.SetComment();
                result.AddVariable("resultUnit", "linearPowerLoss.Unit.DenominatorUnit");
                result.AddVariable("areaUnit", "resultUnit.GetAreaUnit()");
                // result.AddVariable<IrradianceUnit>("irradianceUnit", "irradiance.Unit.CounterUnit", "areaUnit");
                result.Comment += " " + nameof(Def_LinearPowerLoss_Length_Irradiance);

                result.ConvertRight_WithDenominatorUnit("areaUnit");

                result.ResultUnit = "resultUnit";
                return;
            }



            if (input.Is <UnitC, UnitB, UnitA>("*"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment += " " + nameof(Def_LinearPowerLoss_Length_Irradiance);
                result.ConvertRight($"{ValueC.FirstLower()}.Unit.DenominatorUnit.GetLengthUnit()");
                result.WithResultUnit <LinearPowerLossUnit>("irradiance.Unit.CounterUnit", "lengthConverted.Unit");
            }


            if (input.Is <UnitB, UnitC, UnitA>("*"))
            {
                e.SetHandled();
                result.SetComment();
                result.Comment += " " + nameof(Def_LinearPowerLoss_Length_Irradiance);
                result.AddVariable("areaUnit", "length.Unit.GetAreaUnit()");
                result.ConvertRight_WithDenominatorUnit("areaUnit");
                result.WithResultUnit <LinearPowerLossUnit>($"{ValueC.FirstLower()}.Unit.CounterUnit", $"{ValueB.FirstLower()}.Unit");
            }
        }