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://16f51824-0cf5-442f-bda5-c4f5ce543be1|

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]: {
<7,49,|uuid://ec53bf8e-2e5e-47ef-8a48-39daca70b560|>,
<2,4,|uuid://cfd61fd9-26ca-4b4b-ae8a-c03c036bbc35|>,
<10,100,|uuid://458cb6cc-0268-4b18-8da6-4990b56418f2|>,
<9,81,|uuid://98db1391-8dfa-4334-83d6-e7391e11aad2|>,
<8,64,|uuid://c3994eb5-1998-4a06-bbb1-15e7950872ca|>,
<4,16,|uuid://4b04fa65-d318-4ca9-8f1b-49da4ddd69e9|>,
<5,25,|uuid://c91cf060-fa83-4067-80f6-a4c299010cb6|>,
<6,36,|uuid://795d394c-13db-4937-a323-31a9b808fa9a|>,
<1,1,|uuid://9a68f154-a3cc-4545-af20-0fb2d2e2c120|>,
<3,9,|uuid://137cd771-aa04-4bca-a0f6-b2d9a5fd2a11|>
}
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://41a20bf5-26f2-4a61-aee7-ef7438b4593a|,
<2,4>:|uuid://7681b1e4-ce19-4198-a1a5-9fe65f93112e|,
<7,49>:|uuid://f6c7254a-c6b0-4034-95da-1d63611714e1|,
<9,81>:|uuid://b2f56d9f-9238-4246-b038-cf0707bdb3d7|,
<8,64>:|uuid://dc70ac12-70bb-4343-91c0-a529f93e3b10|,
<5,25>:|uuid://8e3cb72c-b7cb-4c98-bee1-d798b02e7edc|,
<4,16>:|uuid://34cbd84d-2e16-405e-a6d4-9d40b1b87283|,
<1,1>:|uuid://ed698b01-2c4b-4fbb-b927-f3f54800e9e8|,
<10,100>:|uuid://090fe6f1-7859-45ee-8f59-3d9c34465e32|,
<3,9>:|uuid://4f78924f-3304-47b4-bbe2-6d9c9383464f|
)

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.