View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2012-2021, VU University Amsterdam
    7                              SWI-Prolog Solutions b.v.
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(uuid,
   37          [ uuid/1,                     % -UUID
   38            uuid/2,                     % -UUID, +Options
   39            uuid_property/2             % +UUID, ?Property
   40          ]).

Universally Unique Identifier (UUID) Library

The library provides operations on UUIDs. Please consult other sources for understanding UUIDs and the implications of the different UUID versions. Some typical calls are given below:

?- uuid(X).
X = 'ea6589fa-19dd-11e2-8a49-001d92e1879d'.

?- uuid(X, [url('http://www.swi-prolog.org')]).
X = '73a07870-6a90-3f2e-ae2b-ffa538dc7c2c'.
See also
- http://www.ossp.org/pkg/lib/uuid/
- https://en.wikipedia.org/wiki/Universally_unique_identifier */
To be done
- Compare UUIDs, extract time and version from UUIDs
   61link_uuid :-
   62    catch(load_foreign_library(foreign(uuid)), _, true).
   63
   64:- initialization(link_uuid, now).
 uuid(-UUID) is det
UUID is an atom representing a new UUID. This is the same as calling uuid(UUID, []). See uuid/2 for options.
   71uuid(UUID) :-
   72    uuid(UUID, []).
 uuid(-UUID, +Options) is det
Create a new UUID according to Options. The following options are defined:
version(+Versions)
Integer in the range 1..5, which specifies the UUID version that is created. Default is 1.
dns(DNS)
url(URL)
oid(OID)
x500(X500)
Provide additional context information for UUIDs using version 3 or 5. If there is no explicit version option, UUID version 3 is used.
format(+Format)
Representation of the UUID. Default is atom, yielding atoms such as 8304efdd-bd6e-5b7c-a27f-83f3f05c64e0. The alternative is integer, returning a large integer that represents the 128 bits of the UUID.

If SWI-Prolog was not built with the OSSP UUID dependency library a simple Prolog alternative that only implements version 4 random UUIDs is provided. In this case the default version is 4 and the only admissible options are version(4) and format(atom).

  102:- if(current_predicate(ossp_uuid/2)).  103uuid(UUID, Options) :-
  104    ossp_uuid(UUID, Options).
  105
  106:- else.  107
  108uuid(UUID, []) :-
  109    !,
  110    random_uuid(UUID).
  111uuid(UUID, Options) :-
  112    option(version(4), Options, 4),
  113    option(format(atom), Options, atom),
  114    !,
  115    random_uuid(UUID).
  116uuid(_UUID, Options) :-
  117    domain_error(uuid_options, Options).
  118
  119random_uuid(UUID) :-
  120    Version = 4,
  121    A is random(0xffffffff),
  122    B is random(0xffff),
  123    C is random(0x0fff) \/ Version<<12,
  124    D is random(0xffff) \/ 0x8000,
  125    E is random(0xffffffffffff),
  126    format(atom(UUID),
  127           '~`0t~16r~8+-~|\c
  128            ~`0t~16r~4+-~|\c
  129            ~`0t~16r~4+-~|\c
  130            ~`0t~16r~4+-~|\c
  131            ~`0t~16r~12+', [A,B,C,D,E]).
  132:- endif.
 uuid_property(+UUID, ?Property)
True when UUID is a property of the given UUID. Supported properties are:
version(V)
Return the version of the UUID (1..5)
time(-Stamp)
Time using SWI-Prolog's time stamp (float with seconds since January 1, 1970, UTC). Only for version 1 and 2 UUIDs
To be done
- Implement more properties.
  148uuid_property(UUID, P) :-
  149    property_uuid(P, UUID).
  150
  151property_uuid(version(V), UUID) :-
  152    split_string(UUID, "-", "", [_A,_B,C,_D,_E]),
  153    sub_string(C, 0, 1, _, F),
  154    char_type(F, xdigit(V)).
  155property_uuid(time(Time), UUID) :-
  156    split_string(UUID, "-", "", [A,B,C,_D,_E]),
  157    sub_atom(C, 0, 1, _, F),
  158    has_time(F),
  159    sub_string(C, 1, _, 0, T),
  160    atomics_to_string(["0x",T,B,A], Hex),
  161    number_string(Nanos, Hex),
  162    Offset is 24*60*60*141427*10 000 000,
  163    Time is (Nanos-Offset)/10000000.0.
  164
  165has_time('1').
  166has_time('2')