Skip to main content

module util::UUID

rascal-0.34.0

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://96d9690a-d3c6-4d9c-b958-6d9abd891388|

Use it to relate identies 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://7e466c4e-50dc-4949-8456-d6223c09a7ca|>,
<2,4,|uuid://8f6e3239-1465-4ab7-bf01-17868c8a9ee4|>,
<3,9,|uuid://6c52289d-122e-41a8-a991-004ed82c5f79|>,
<4,16,|uuid://fbb9eee9-381b-4a9f-991e-bd727556e02c|>,
<8,64,|uuid://f535ea19-49f0-4671-942f-c50fb7b99e11|>,
<10,100,|uuid://325d4501-589b-4c02-9238-7575a693fef7|>,
<5,25,|uuid://7098528a-2225-400b-bae0-bebdab0cef51|>,
<1,1,|uuid://92627179-6a4f-484a-af1a-cfe9b0a83fcc|>,
<9,81,|uuid://9f351754-1aaa-422a-ae25-2d03f7601468|>,
<6,36,|uuid://b2729b1e-5bd8-4a85-86e0-9b7f1a932424|>
}
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://9d3c190a-753a-4ff5-988e-0c3c51393327|,
<2,4>:|uuid://77641f8d-176d-401a-b8e3-3484e9b9fc62|,
<7,49>:|uuid://27e0d987-b0f0-454a-9df5-d4fcf95cd13d|,
<9,81>:|uuid://ef000881-82dc-42a7-a020-9841bf3182dc|,
<8,64>:|uuid://3687e667-0da3-4e1f-8188-6fe5e3343a92|,
<5,25>:|uuid://17e0a1bb-4120-4913-816b-d66e9069b347|,
<4,16>:|uuid://3e9bfa41-206e-49b7-bf5c-adfa5003c632|,
<1,1>:|uuid://1e8b429b-2c1c-415b-8ba2-f3750431d857|,
<10,100>:|uuid://7d513f1c-d030-4e1a-adf9-0db418358418|,
<3,9>:|uuid://4dff0100-89b5-4a71-9d0c-2ae7af7c402d|
)

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.