简体   繁体   English

我需要同时产生几个布尔

[英]I need to gen several bools simultaneously

I have: 我有:

struct MyStruct {
     !Ipv4En : bool;
     !Ipv6En : bool;
     keep Ipv4En == TRUE or Ipv6En == TRUE;

     MyMethod() is {
         gen Ipv4En;
         gen Ipv6En;
     };
 };

I always get Ipv4En TRUE, because those 2 bools are not generated together. 我总是将Ipv4En设为TRUE,因为这两个布尔值不会一起生成。 They can't be generated when I gen MyStruct. 我生成MyStruct时无法生成它们。 How can I generate them together? 我如何一起生成它们?

Meanwhile I patched it (don't like the solution): I've deleted ! 同时我打了补丁(不喜欢解决方案):我已经删除了! in the definition. 在定义中。

temp : MyStruct;
gen temp;

Ipv4En = temp.Ipv4En;
Ipv6En = temp.Ipv6En;

Since a gen action only takes a single gen-item , you have to group the two variables you want solved together. 由于gen动作只需要一个gen-item ,因此您必须将要求解的两个变量组合在一起。 You can do this by defining a new struct : 您可以通过定义一个新的struct来做到这一点:

struct IpVersionInfo {
  Ipv4En: bool;
  Ipv6En: bool;

  keep Ipv4En == TRUE or Ipv6En == TRUE;
};

Instead of having two variables for your enables, you use a single variable of this new struct type: 而不是为您的启用使用两个变量,而是使用这个新的struct类型的单个变量:

struct MyStruct {
    !ipVersionInfo: IpVersionInfo;

    MyMethod() is {
        gen ipVersionInfo;
    };
};

If you run a couple of iterations, you'll see that you can reach all combinations: 如果运行几次迭代,您将看到可以达到所有组合:

extend sys {

    run() is also {
        for i from 1 to 20 {
            var s: MyStruct = new;
            s.MyMethod();
            outf(
                    "Ipv4En = %s, Ipv6En = %s\n",
                    s.ipVersionInfo.Ipv4En,
                    s.ipVersionInfo.Ipv6En);
        };
    };

};

This requires an API change, though, so you'll need to update all code that referenced Ipv4En and Ipv6En . 但是,这需要更改API,因此您需要更新所有引用Ipv4EnIpv6En代码。

Aside from grouping the two fields in a struct , there are also other ways of solving your problem. 除了将两个字段组合到一个struct ,还有其他解决问题的方法。 Another example would be to define an enum that contains values for your legal cases: 另一个示例是定义一个包含您的法律案件值的enum

type ip_version_info_e: [ V4, V6, BOTH ];

As before, you can use a variable of this type in your struct : 和以前一样,您可以在struct使用这种类型的变量:

struct MyStruct {
    !ip_version_info: ip_version_info_e;

    MyMethod() is {
        gen ip_version_info;
    };
};

If you run a couple of iterations of the new code you'll see that it also reaches all possible combinations: 如果运行新代码的几次迭代,您会发现它也可以达到所有可能的组合:

extend sys {

    run() is also {
        for i from 1 to 20 {
            var s: MyStruct = new;
            s.MyMethod();
            outf(
                    "Ipv4En = %s, Ipv6En = %s\n",
                    s.ip_version_info in [ V4, BOTH ],
                    s.ip_version_info in [ V6, BOTH ]);
        };
    };

};

Regardless of how you decide to solve the problem, you should hide the internal implementation from any client code that uses MyStruct . 无论您决定如何解决该问题,都应该对使用MyStruct任何客户端代码隐藏内部实现。 Notice that the out statements from above look different depending on how we chose to handle the IP version settings. 注意,根据我们选择处理IP版本设置的方式,上面的out语句看起来有所不同。 Instead of having client code look too deep inside MyStruct you should define the following two methods: 不应让客户端代码在MyStruct内看起来太深, MyStruct应定义以下两种方法:

isIpv4Enabled(): bool;
isIpv6Enabled(): bool;

In the case where we grouped the two Boolean variables in a struct , the implementations of these two methods are: 在将两个布尔变量分组到一个struct中的情况下,这两个方法的实现是:

struct MyStruct {
    // ...

    isIpv4Enabled(): bool is {
        result = ipVersionInfo.Ipv4En;
    };

    isIpv6Enabled(): bool is {
        result = ipVersionInfo.Ipv6En;
    };
};

In the case where we defined an enum , we have: 在定义enum的情况下,我们有:

struct MyStruct {
    // ...

    isIpv4Enabled(): bool is {
        result = ip_version_info in [ V4, BOTH ];
    };

    isIpv6Enabled(): bool is {
        result = ip_version_info in [ V6, BOTH ];
    };
};

In both cases, though, the client code for printing would be the same: 但是,在两种情况下,用于打印的客户端代码都是相同的:

extend sys {

    run() is also {
        for i from 1 to 20 {
            var s: MyStruct = new;
            s.MyMethod();
            outf(
                    "Ipv4En = %s, Ipv6En = %s\n",
                    s.isIpv4Enabled(),
                    s.isIpv4Enabled());
        };
    };

};

This is a major advantage. 这是一个主要优势。 This way you are free to change your implementation of MyStruct in the future and not have to touch any other code that uses it. 这样,您将来就可以自由更改MyStruct的实现,而不必接触使用它的任何其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM