References and where rules

As I understand a property with a reference can have additional filters in the form of where rules, for example:

'Blue EV passenger car': text -> .'Vehicles'[]
   where '4 Wheels' ~> .'Vehicles'[] .'Number of wheels'?'Four'
   where 'At least 4 seats' ~> .'Vehicles'[] .'Number of seats'?'Four or more'
   where 'Blue' ~> .'Vehicles'[] .'Color'?'Blue'
   where 'EV' ~> .'Vehicles'[] .'Kind of propulsion'?'Electric'

My question is on how these where rules form the resulting data set. It seems the intersection of the data resulting from each rule determines the final data set, rather than the union. Is this correct?

If this is the case, then this would result in an empty set:

'Blue and red passenger cars': text -> .'Vehicles'[]
   where '4 Wheels' ~> .'Vehicles'[] .'Number of wheels'?'Four'
   where 'At least 4 seats' ~> .'Vehicles'[] .'Number of seats'?'Four or more'
   where 'Blue' ~> .'Vehicles'[] .'Color'?'Blue'
   where 'Red' ~> .'Vehicles'[] .'Color'?'Red'

… because a vehicle cannot be blue and red at the same time (at least, in this model; this would need a state 'Blue or red' in stategroup 'Color')

The where rules in your examples all specify optional references (~>). These optional reference rules do not restrict the set of collection entries that text values can reference.

What is more, you can only have one mandatory collection entry reference per text value. The main rule at a text attribute specifies that reference:

'Blue EV passenger car': text -> .'Vehicles'[] /* main collection entry reference rule */

But, you can have multiple mandatory where rules that further restrict the set of valid collection entries from the Vehicles collection (and only that collection), as follows:

'Blue EV passenger car that is New and In Stock': text -> .'Vehicles'[] as $'vehicle'
   where 'Blue'     -> $'vehicle'.'Color'?'Blue'
   where 'In Stock' -> $'vehicle'.'In Stock'?'Yes'
   where 'New'      -> $'vehicle'.'New'?'Yes'

For a text value to be valid, all mandatory reference rules have to be satisfied.

As you mention, in your last example those mandatory where rules all need to be satisfied. This means only vehicles that are blue and in stock and new are valid entries for the text property.
A combinations of where rules thus act like the intersection (not a union) of the individual where rules. This is what I’m trying to grasp.

1 Like

when I put this line in application.alan then an error message appears: unexpected 'keyword ‘as’
is this line a part of a routine?

In platform versions <= 2021.10, $ is implicitly defined. With newer (unofficial) platform versions, you have to explicitly assign to $'...' in order to use the reference. If you are using platform version 2021.10, this should work:

'Blue EV passenger car that is New and In Stock': text -> .'Vehicles'[]
   where 'Blue'     -> $ .'Color'?'Blue'
   where 'In Stock' -> $ .'In Stock'?'Yes'
   where 'New'      -> $ .'New'?'Yes'

thanks for the elucidation.