简体   繁体   English

在Alloy模型中使用boolean的最佳实践

[英]The best practice to use boolean in Alloy model

I'm building a simple Alloy to generate simple Java Pojo objects and some fields of that pojo are Boolean values. 我正在构建一个简单的Alloy来生成简单的Java Pojo对象,并且该pojo的某些字段是布尔值。 I'm now using the following mechanism to achieve this function 我现在使用以下机制来实现此功能

one sig item {
    autoPay: String,
    Price: Int
}

fact boolean {
    all n: item {
        item.autoPay = "true" or
        item.autoPay = "false"
    } 
}

This will work but everytime I introduced a new boolean field I have to modify the boolean fact to make sure the value to be either "true" or "false". 这将工作,但每次我引入一个新的布尔字段时,我必须修改布尔事实,以确保该值为“true”或“false”。 Is there any best practice to do this? 有没有最好的做法呢? Like what we Alloy does for Integers? 就像我们Alloy为Integers做的那样?

It would be much better to introduce a Bool sig, and then use it for all your boolean fields, eg, 引入Bool sig会更好,然后将它用于所有布尔字段,例如,

abstract sig Bool{}
one sig True extends Bool
one sig False extends Bool

one sig item {
  autoPay: Bool,
  Price: Int
}

No additional fact is needed in this case. 在这种情况下不需要其他事实。

If you like this approach, there is a built-in "util/boolean" library which defines Bool , True , and False sigs exactly like I did above, and additionally provides some helper functions (like isTrue , And , Or , etc.) so you can simply say 如果您喜欢这种方法,那么有一个内置的“util / boolean”库,它完全像我上面那样定义BoolTrueFalse sigs,并且还提供了一些辅助函数(比如isTrueAndOr等)。所以你可以简单地说

open util/boolean 

one sig item {
  autoPay: Bool,
  Price: Int
}    

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

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