简体   繁体   English

Alloy约束如何将集合放入其子集中?

[英]How can an Alloy constraint put a set inside its subset?

The following Alloy code says that each hotel room has a set of keys: 以下Alloy代码表示每个酒店房间都有一组钥匙:

sig Key {}

sig Room {
    keys: set Key
}

The keys relation needs to be constrained. keys关系需要被约束。 As it stands, it permits instances such as this: key K1 is used in a bunch of rooms. 就目前而言,它允许这样的实例:密钥K1用于一堆房间。 Ouch! 哎哟! We don't want that. 我们不想要那个。 We want each key to be used only with one room. 我们希望每个密钥只能用于一个房间。 Here's a graphic illustrating the universe of valid instances (and the subset of instances that we actually want to allow): 这是一个图形,显示了有效实例的范围(以及我们实际上希望允许的实例子集):

房间/关键实例的宇宙

The set of instances that we actually want is nicely expressed with this Alloy code: 我们实际想要的实例集可以用以下Alloy代码很好地表达:

Room lone -> Key

The instances for that Alloy code is depicted in the above graphic by the small circle. 上图中的小圆圈表示了该Alloy代码的实例。

So, how do we constrain keys ? 那么,我们如何约束keys One answer is this: create an Alloy fact which says this: 一个答案是:创建一个合金事实,说明:

keys in Room lone -> Key

Think about what that is graphically saying. 考虑一下图形上的意思。 It is saying that the big circle must be inside the little circle (see below). 据说大圆圈必须在小圆圈内(见下文)。 Isn't that weird? 那不是很奇怪吗? How can a circle be inside its sub-circle? 圆如何在其子圆内? Can someone give me some intuition about this please? 有人可以给我一些直觉吗? It seems odd. 好像很奇怪

大圈内小圈

  • if you only have sig Room {keys: set Key} without any additional facts/constraints, the domain for the keys relation is the big circle; 如果您只有sig Room {keys: set Key}而没有任何其他事实/约束,则keys关系的域为大圆圈;

  • you can decide to add some constraints (like keys in Room lone -> Key ) exactly for the purpose of shrinking the domain for the keys relation (so that it becomes the small circle). 您可以决定完全添加一些约束(例如keys in Room lone -> Key ),以缩小keys关系的域(使其变为小圆圈)。

So the right way to think about it is not that the big circle must be inside the small circle (?!); 因此,正确的思考方法不是大圆圈一定在小圆圈内(?!); rather, think of it as using the small circle instead of the big circle as the domain (set of all valid values) for keys . 而是将其视为使用小圆圈而不是大圆圈作为keys的域(所有有效值的集合)。

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

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