Skip to main content

module util::UUID

rascal-Not specified

Usage

import util::UUID;

function uuid

generates a unique identifier shaped as a loc

loc uuid()

This function generates a UUID, see http://en.wikipedia.org/wiki/Universally_unique_identifier. Since UUIDs are useful to assign an opaque and unique identity to data, the function returns a location (which is the preferred representation for encoding identities in Rascal)

Examples

rascal>import util::UUID;
ok

The uuid() function generates a location with the authority showing the literal canonical UUID string

rascal>uuid()
loc: |uuid://1c6bd14d-1885-482e-9406-707ccec5000c|

Use it to relate identities to data objects, as in this example which adds a field to a relation:

rascal>myData = { <i,i*i> | i <- [1..11] }; 
rel[int,int]: {
<10,100>,
<7,49>,
<1,1>,
<3,9>,
<9,81>,
<2,4>,
<4,16>,
<6,36>,
<5,25>,
<8,64>
}
rascal>rel[int n, int square, loc id] myUniqueData = { <i,j,uuid()> | <i,j> <- myData };
rel[int n,int square,loc id]: {
<4,16,|uuid://44a9eaaf-97f1-4ed6-8376-2023faac9959|>,
<2,4,|uuid://33837baf-2ba1-463c-9434-c5c8b366486b|>,
<6,36,|uuid://c85b1cc8-134d-438e-9441-684243f4e7ff|>,
<7,49,|uuid://ae11ccc0-36ac-413e-9b2b-3e89ac50e04e|>,
<5,25,|uuid://57353c40-e478-41fb-a2a4-eca9281fa38d|>,
<9,81,|uuid://03c11e48-ba1f-4965-baef-fc07dc5ed334|>,
<8,64,|uuid://3b0f0051-eac6-43bd-b43a-1975f5d725ef|>,
<10,100,|uuid://d881b90b-2aa1-41b8-b6b8-1d3513cf6d03|>,
<3,9,|uuid://5fc0ec9c-e750-4f18-964d-d92be584d494|>,
<1,1,|uuid://7dc0c8ec-d35b-4819-a9d7-8c179e8b00c6|>
}
rascal>map[tuple[int i, int j] t, loc id] myUniqueMap = (<i,j>:uuid() | <i,j> <- myData );
map[tuple[int i,int j] t, loc id]: (
<6,36>:|uuid://0858dabc-c8f5-403f-8df8-979a3c63ca1e|,
<2,4>:|uuid://2c0f3f3c-7cfc-46c3-aa46-3d88d16c75b8|,
<7,49>:|uuid://ba190a72-5e8a-43b8-a42b-cbb47addfdba|,
<9,81>:|uuid://685d4acb-cf3f-4b48-a4e0-a80c00d838fc|,
<8,64>:|uuid://dbd56a4a-deb2-4479-b2aa-73fb6fe4781e|,
<5,25>:|uuid://5163afb0-91e6-4103-aaf7-2c9cad4a3095|,
<4,16>:|uuid://fee53c79-fd43-4ac8-ab7d-8b71223c58ea|,
<1,1>:|uuid://11b79e1b-bbb9-4900-a2a7-387aabce0b8a|,
<10,100>:|uuid://fc3b6435-6bca-4c90-b0bb-7d46ebdde655|,
<3,9>:|uuid://cdf055e9-ca7f-497b-b4a6-84094d7e2b66|
)

Note how uuid() should always generate a fresh value:

rascal>assert uuid() != uuid(); 
bool: true

Benefits

  • Locations are used for identifying program elements or model elements in Rascal. The uuid() function provides an quick-and-easy way of acquiring such an identity without having to design a naming scheme.

Pitfalls

  • UUIDs are a quick and dirty way of identifying data which may lead to hard to debug code. A naming scheme for locations is better because it generates human readable locations which carry meaning. For example consider the difference in readability between these two values: |uuid://47fdcd64-4fd0-41a1-8aa3-61c5b272c3fc| and |java+class:///java/lang/Object|. Both may lead to the same results in your computation, but if we print either of them out, one of them is opaque and the other is transparent. A transparent naming scheme is preferable for debugging purposes.

function uuidi

see [uuid], this function does the same except return the UUID as an int.

int uuidi()

Pitfalls

  • beware that this integer is almost guaranteed to use 128 bits, so communicating it outside of Rascal should not be done via a Java 32-bit integer.