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://61c1d378-507a-4207-bf38-f20512754287|

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://6d0beb08-4ff2-4b57-ac35-e5b0ded1f7ec|>,
<8,64,|uuid://653e4108-78e1-4890-903e-4dcc29337120|>,
<5,25,|uuid://ed3c63de-3ad8-45d6-9ee2-c76a1ae05b77|>,
<10,100,|uuid://9ca5a6ba-5496-4ad1-96ae-ef4fcbe0a0f5|>,
<2,4,|uuid://e299b8a8-5898-4fef-9dca-15b00cf1ca37|>,
<1,1,|uuid://572226a9-8691-4be6-9ea6-c60c199d2915|>,
<9,81,|uuid://b5d1b11f-2092-4156-8246-88f3a59b73c4|>,
<3,9,|uuid://0bfab665-e031-495b-8077-f9dd62ee5d88|>,
<6,36,|uuid://ee696345-7add-4036-add5-c27efd601167|>,
<4,16,|uuid://7a0e011b-73fc-426e-997e-0c6016a11681|>
}
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://aeb9f3ed-28fa-4586-bab0-baf529e204c5|,
<2,4>:|uuid://3e5c363a-c810-48c3-8a23-bc8181e410ba|,
<7,49>:|uuid://a00e2f0c-0e2d-47b2-a522-461d9476d7ab|,
<9,81>:|uuid://8863bd63-f345-4132-b948-9b0f2d3aaf7f|,
<8,64>:|uuid://302e3e4b-07a2-4e27-a843-7c73eeb4c305|,
<5,25>:|uuid://46f63a5f-3e26-4122-b769-46fa2934d05f|,
<4,16>:|uuid://333b6775-11ba-483a-8fa1-55a23a6a4953|,
<1,1>:|uuid://b989df49-fcef-4863-b358-f74a4179b641|,
<10,100>:|uuid://92ebe00a-1ab0-4cf5-992b-a3fe6e8420dd|,
<3,9>:|uuid://510184f1-43bd-465e-a9e6-0107c5697316|
)

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.