C# 访问者模式 构建结果的表达式 度量单位

发布于 2024-11-18 08:25:47 字数 633 浏览 1 评论 0原文

我正在着手一个涉及一些计算的项目,并从基于 Cureos 框架的相当简单的 UOM(测量单位)改编开始。到目前为止一切顺利,我已经有了一些基本的单位。

我想做的一件事是建立在基本单位的基础上,以获得计算值。例如,如果我们有长度(或距离)和时间,我们可以得出速度(距离/时间)或加速度(距离/平方(时间))。

我的单位很灵活;我可以将它们显示为英制、国际单位制或系统中我需要的任何数量,无论是英尺、英寸、米、厘米等。或者以时间、毫秒、秒、分钟等形式显示

。我想做的是报告计算时的实际单位。因此,如果单位是米和秒,我想报告速度为“m/s”或加速度为“m/s^2”。或者,如果单位是英尺和毫秒,则分别报告“ft/ms”或“ft/ms^2”。

所以...以这个故事为背景,我很高兴将这些更复杂的单元表达为 Linq 表达式树

如果涉及的对象是度量(对于可量化的值)、单位(对于单位)和数量(对于单位的种类),在我看来这应该是完全可行的。

想法?

I am embarking on a project involving some calculations and have started with a fairly straightforward UOM (Unit of Measurement) adaptation based on the Cureos framework. So far so good, I've got some basic Units represented.

One of the things I would like to do is to build upon the foundational units in order to arrive at calculated values. For instance, if we have Length (or Distance) and Time, we can arrive at Velocity (Distance / Time) or Acceleration (Distance / Square(Time)).

My units are flexible; I can show them as either English, SI, or whatever the system happens to be in whatever quantity I need, whether feet, inches, meters, centimeters, etc. Or in the case of time, milliseconds, seconds, minutes, etc.

What I'd like to do is report the actual unit at the moment of calculation. So if the units are meters and seconds, I'd like to report "m/s" for Velocity or "m/s^2" for Acceleration. Or, if the units are feet and milliseconds, report "ft/ms" or "ft/ms^2", respectively.

So... With that story as the backdrop, I am entertaining the possibility of expressing these more complex units (if you will, maybe it's a unit, maybe it's not... that's TBD) as Linq Expression Trees whereby I could plausibly visit the nodes throughout the Expression Tree for its units and "compile" the unit names just in addition to the actual numeric result.

If the objects involved are Measures (for quantifiable values), Units (for the units) and Quantities (for the kind of unit), it seems to me this should be quite doable.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-11-25 08:25:47

我不确定您在这里要求什么,但是当遇到在程序中维护不同变量的单位并找到结果值的正确单位的问题时,这就是我想出的解决方案。

using System;

namespace ConsoleApplication1
{
    public interface Mass
    {
    }

    public class Kg: Mass
    {
    }

    public interface Length
    {
    }

    public interface M : Length
    {
    }

    public interface M<in T>: M where T: M, Length
    {
    }

    public interface Time
    {
    }

    public interface S : Time
    {
    }

    public interface S<in T> : S where T : S, Time
    {
    }

    public interface IUnit<in M, in L, in T> 
        where M: Mass 
        where L: Length
        where T: Time
    {
    }

    public sealed class Unit<M, L, T>: IUnit<M, L, T>
        where M : Mass
        where L : Length
        where T : Time
    {
        public static Unit<M, L, T>  operator +(Unit<M, L, T> a, Unit<M, L, T> b)
        {
            throw new NotImplementedException();
        }
    }

    public interface Nil: Mass, Length, Time
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            Unit<Nil, M, S<S>> value1 = null;
            Unit<Nil, M, S<S>> value2 = null;

            var result = value1 + value2;
        }
    }
}

这将允许您在返回其他单位的单位上定义运算符(简单的 MLT 数学)。请注意,S 实际上是 S^2,并且是我想出的实现此功能的最易读的方法。

希望这有帮助!

I'm not sure what you're asking for here, but when confronted with the problem of maintaining units of different variables in my program and finding the correct units of the resultant value, this is the solution I came up with.

using System;

namespace ConsoleApplication1
{
    public interface Mass
    {
    }

    public class Kg: Mass
    {
    }

    public interface Length
    {
    }

    public interface M : Length
    {
    }

    public interface M<in T>: M where T: M, Length
    {
    }

    public interface Time
    {
    }

    public interface S : Time
    {
    }

    public interface S<in T> : S where T : S, Time
    {
    }

    public interface IUnit<in M, in L, in T> 
        where M: Mass 
        where L: Length
        where T: Time
    {
    }

    public sealed class Unit<M, L, T>: IUnit<M, L, T>
        where M : Mass
        where L : Length
        where T : Time
    {
        public static Unit<M, L, T>  operator +(Unit<M, L, T> a, Unit<M, L, T> b)
        {
            throw new NotImplementedException();
        }
    }

    public interface Nil: Mass, Length, Time
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            Unit<Nil, M, S<S>> value1 = null;
            Unit<Nil, M, S<S>> value2 = null;

            var result = value1 + value2;
        }
    }
}

This would allow you to define operators on units which return other units (simple MLT math). Note that the S is actually S^2 and is the most readable way I came up with to implement this feature.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文