# Struggling with multi relations

**URL:** <https://alloytools.discourse.group/t/struggling-with-multi-relations/109>\
**Category:** Questions\
**Created:** [September 23, 2020, 8:56am UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109 "2020-09-23T08:56:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 23, 2020, 8:56am UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/1 "2020-09-23T08:56:48Z")

</div>

I’m sure this is all very stupid, but…

Trying to model a protocol, in particular that a message is associated with a correlation ID. The cut-down version is below.

If I use `, var correlationIDs : Message -> one CorrelationID` then I get the dreaded _No instance found_  
If I use `, var correlationIDs : Message -> CorrelationID` then I get this sort of thing, and I don’t understand how the Receiver can be associated  
 ![image](https://global.discourse-cdn.com/free1/uploads/alloytools/original/1X/a2622a51347c36fcdaf0e40ff218b370b4f354da.png)

Thanks all

```
abstract sig Subscriber {
	, var correlationIDs : Message -> one CorrelationID
}
one sig Sender extends Subscriber {}
one sig Receiver extends Subscriber {}

enum Status { New, Started }
sig CorrelationID {}
sig Message {
	, var status : one Status
}

pred start[message: Message] {
	  one id: CorrelationID - Subscriber.correlationIDs[Message] | {
			Sender.correlationIDs' = Sender.correlationIDs ++ (message -> id)
    }
	message.status = Started

	all s: Subscriber - Sender | s.correlationIDs' = s.correlationIDs
}

pred skip {
  status' = status
  correlationIDs' = correlationIDs
}

pred init {
  no correlationIDs
	some Message
	some CorrelationID
  Message.status = New
}

fact traces {
	init
  always (lone message: Message | start[message] or skip)
}

pred correlationIDs_used_once {
 	 all message: Message | lone Subscriber.correlationIDs[message]
}

check correlationIDs_used_once {
	always correlationIDs_used_once
}

pred show {
	eventually some m: Message | m.status = Started and one Sender.correlationIDs[m]
}

run show {}
```

---

<div class="post-metadata">

**Author:** ![alcino](https://yyz2.discourse-cdn.com/free1/user_avatar/alloytools.discourse.group/alcino/32/25_2.png) [@alcino](https://alloytools.discourse.group/u/alcino)\
**Post date:** [September 23, 2020, 9:36am UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/2 "2020-09-23T09:36:50Z")

</div>

Hi,

> [@steve.freeman](#):
>
> ```auto
> , var correlationIDs : Message -> one CorrelationID
> 
> ```

This `one` gives you a no instance found because it requires that at all times all subscribers (including receivers) have `one CorrelationID` for every possible `Message`. This is not possible because your `init` predicate directly contradicts that. I think the multiplicity you want is `lone`.

> [@steve.freeman](#):
>
> ```auto
> always (lone message: Message | start[message] or skip)
> 
> ```

I believe this `lone` is responsible for the second problem. This constraint can be made true by having no `Message` satisfying `start`, which will happen in a “random” trace where the first step does not satisfy `start` and leads to a bogus second state, like the one you depict. You should use `some` instead.

Btw, I also don’t think you specification of `start` is not correct:

- it allows `status` to change freely.
- the `correlationIDs` can change freely between senders. Notice that `Sender.correlationIDs' = Sender.correlationIDs` would still be true in a state where `Message0->CorrelationIDs0` is associated with `Sender0` and in the next state is associated with `Sender1`.

Here is a specification of `start` I believe captures your intention:

```
pred start[message: Message] {
	  some id: CorrelationID - Subscriber.correlationIDs[Message], s : Sender {
			correlationIDs' = correlationIDs + (s -> message -> id)
    }
	status' = status ++ message->Started

	all s: Subscriber - Sender | s.correlationIDs' = s.correlationIDs
}

```

Best,  
Alcino

---

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 23, 2020, 12:04pm UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/3 "2020-09-23T12:04:24Z")

</div>

Thanks for responding. I applied your changes, which made the code better but I’m still not there.

> [@alcino](#):
>
> This `one` gives you a no instance found because it requires that at all times all subscribers (including receivers) have `one CorrelationID` for every possible `Message` .

I find this confusing, as I was hoping that this relationship would say that a Subscriber has some Messages, each associated with a single CorrelationID.

I’m now wondering if I should model this differently: either by reversing to Subscriber → CorrelationID → Message, or by introducing an Exchange, with a CorrelationID and some messages.

---

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 23, 2020, 4:50pm UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/4 "2020-09-23T16:50:12Z")

</div>

Reworked. Once again, coding in relationships improves things. But I still can’t force the creation of a correlation relationship.

```
abstract sig Subscriber {
	, var correlationIDs : CorrelationID -> Message
}
one sig Sender extends Subscriber {}
one sig Receiver extends Subscriber {}

enum Status { New, Started }
sig CorrelationID {}
sig Message {
	, var status : one Status
}

fun freeCorrelationIDs[] : set CorrelationID {
 CorrelationID - ternary/mid[correlationIDs]
}

pred start[message: Message] {
	some freeCorrelationIDs

  one id: freeCorrelationIDs | correlationIDs' = correlationIDs + (Sender -> id -> message)
	status' = status ++ message->Started
}

pred skip {
  status' = status
  correlationIDs' = correlationIDs
}

pred init {
  no correlationIDs
	some Message
	some CorrelationID
  Message.status = New
}

fact traces {
	init
  always (some message: Message | start[message] or skip)
}
```

---

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 25, 2020, 2:04pm UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/5 "2020-09-25T14:04:26Z")

</div>

So, after some experimentation, this works:

```
one id: freeCorrelationIDs | {
  Sender.correlationIDs' = Sender.correlationIDs + id -> m
}
Receiver.correlationIDs' = Receiver.correlationIDs

```

but this doesn’t find an instance

```
one id: freeCorrelationIDs | {
  correlationIDs' = correlationIDs + Sender->id -> m
}

```

Can anyone explain why?

Thx  
S

---

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 25, 2020, 4:44pm UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/6 "2020-09-25T16:44:00Z")

</div>

Woah! This works.

`Subscriber <: correlationIDs' = Subscriber <: correlationIDs + Sender -> id -> m`

Confused.

---

<div class="post-metadata">

**Author:** ![alcino](https://yyz2.discourse-cdn.com/free1/user_avatar/alloytools.discourse.group/alcino/32/25_2.png) [@alcino](https://alloytools.discourse.group/u/alcino)\
**Post date:** [September 25, 2020, 6:20pm UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/7 "2020-09-25T18:20:50Z")

</div>

Hi,

I can create `correlationIDs` with this model, so I don’t understand what you mean by

> [@steve.freeman](#):
>
> can’t force the creation of a correlation relationship

Here is an example of trace where two correlation ids were added to relation `correlationIDs`.

 ![Screenshot 2020-09-25 at 19.18.41](https://global.discourse-cdn.com/free1/uploads/alloytools/original/1X/5f96f430fcb27fb27f90105a44aa4706d2cc0333.png)

Best,  
Alcino

---

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 26, 2020, 8:47am UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/8 "2020-09-26T08:47:38Z")

</div>

I think I may have found it. There’s

`run pred` and `run { constraint }`.

What happens with `run pred { constraint }`?

I had assumed it would apply the predicate and add the constraint, but the Green Book and @hwayne’s docs imply that it’s one _or_ the other.

---

<div class="post-metadata">

**Author:** ![nmacedo](https://yyz2.discourse-cdn.com/free1/user_avatar/alloytools.discourse.group/nmacedo/32/74_2.png) [@nmacedo](https://alloytools.discourse.group/u/nmacedo)\
**Post date:** [September 28, 2020, 8:11am UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/9 "2020-09-28T08:11:52Z")

</div>

Hi.

Yes, once you pass a formula to a `run` command the first parameter becomes just a label for that command (the same applies with `check`s and `assert`s).

---

<div class="post-metadata">

**Author:** ![steve.freeman](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@steve.freeman](https://alloytools.discourse.group/u/steve.freeman)\
**Post date:** [September 28, 2020, 8:26am UTC](https://alloytools.discourse.group/t/struggling-with-multi-relations/109/10 "2020-09-28T08:26:00Z")

</div>

That seems like a serious usability flaw to me. At least there should be a warning if the label is the same as a predicate name.

S

Written with thumbs. Please excuse autocorrections and errors
