简体   繁体   English

合金中的值分配和枚举的使用

[英]Value assignment in Alloy and use of Enum

How to assign variable in Alloy? 如何在Alloy中分配变量?

Sig ClassA{
    variable_1: String,
    variable_2: Int
}

Sig ClassB{
    variable_1: String,
    variable_2: Int
}

pred isLess[a:ClassA,b:ClassB]{
    a.variable_2 < b.variable_2
}

assert integrityTest{
    all a:ClassA,b:ClassB| isLess[a,b]

}

Now I want to check counterexample of variables when I assign some bigger value in a.variable_2 than b.variable_2. 现在,当我在a.variable_2中分配比b.variable_2更大的值时,我想检查变量的反例。 But I am not sure how to assign variable in Alloy. 但是我不确定如何在Alloy中分配变量。 Only thing I came up with is following but it is not working- 我想出的唯一东西是跟随但不能正常工作-

pred assignValue[a:ClassA]{
    a.variable_2 = Int[4]  
}

However, I believe it will only check the equality with 4 and return as false. 但是,我相信它将只检查4的相等性并返回false。 It has nothing to do with the assignment. 它与任务无关。 So my question is how should I produce counterexample when a.variable_2>b.variable_2 所以我的问题是当a.variable_2>b.variable_2时我应该如何产生反例

And How can I use Enum of Int in alloy? 以及如何在合金中使用Enum of Int? Like- Enum MetricValue {1,2,3,4,5} to assign a.variable 1. Like- Enum MetricValue {1,2,3,4,5}分配变量1。

EDIT I am still having trouble finding the counterexample, even though I can find one by manually checking when I toggle the value of variable_2 of ca and cb. 编辑我仍然很难找到反例,即使我可以通过手动检查切换ca和cb的variable_2的值来找到反例。

 sig ClassA{ 
    variable_1: String, 
    variable_2 = Int 
 } 

 sig CA extends ClassA{}{ 
    variable_2 = 1 
 } 
 sig ClassB{ 
    variable_1: String,
    variable_2 = Int 
 } 
 sig CB extends ClassB{}{ 
    variable_2 = 4 
 }

 pred checkAllConstraint [ca: ClassA, cb: ClassB] { 
    ca.variable_2 > cb.variable_2 } 

  assert checkAll{ 
    all ca: ClassA, cb: ClassB | checkAllConstraint[ca,cb] 
  } 
  check checkAll for 15

You can restrain a field to a single value through facts. 您可以通过事实将字段限制为单个值。 In your case, signature facts come handy. 在您的情况下,签名事实很方便。

It will look like this: 它看起来像这样:

sig ClassA{
  variable_1: String,
  variable_2: Int
}{
    variable_1="hello world"
    variable_2=4
}

To bound a field to one value amongst a set, you can use the "in" keyword instead of "=" as follows: 要将字段绑定到一组值中的一个值,可以使用“ in”关键字代替“ =”,如下所示:

sig ClassA{
variable_1: String,
variable_2: Int
}{
    variable_1 in ("hello" + "world")
    variable_2 in (1+2+3+4)  
}

Note that in Alloy + is a UNION operator. 注意,Alloy +中是UNION运算符。 It doesn't sum nor concatenate as you might expect. 它不像您期望的那样求和或连接。

EDIT It doesn't work for 2 reasons: 编辑它不起作用有两个原因:

  • you wrote : variable_2 = Int instead of variable_2: Int . 您写道: variable_2 = Int而不是variable_2: Int By doing so, no valid instance contains atoms typed by CA or CB, because eg ClassA.variable2 is constrained to be the set of all integers and CA.variable2 is constrained to be 1 这样做,没有有效的实例包含由CA或CB类型的原子,因为例如ClassA.variable2被约束为所有整数的集合,而CA.variable2被约束为1
  • no String atom is defined. 没有定义String原子。 That's one of Alloy's weird part. 那是合金的怪异部分之一。 If you want to use Strings in your model, you need to have string litterals somewhere specified, eg in a fact. 如果要在模型中使用字符串,则需要在特定位置(例如,事实中)指定字符串文本。

Here's your model, corrected: 这是您的模型,已更正:

sig ClassA{ 
    variable_1: String, 
    variable_2 : Int 
 } 

 sig CA extends ClassA{}{ 
    variable_2 = 1 
 } 
 sig ClassB{ 
    variable_1: String,
    variable_2 : Int 
 } 
 sig CB extends ClassB{}{ 
    variable_2 = 4 
 }

 pred checkAllConstraint [ca: ClassA, cb: ClassB] { 
    ca.variable_2 > cb.variable_2 } 

  assert checkAll{ 
    all ca: ClassA, cb: ClassB | checkAllConstraint[ca,cb] 
  } 
  check checkAll for 15

fact { String in ("test"+"test2"+"test3"+"test4")}

If you still have questions, please create a new one. 如果仍有问题,请重新创建一个。

- -

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

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