View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker and Wouter Beek
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2016, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(rdf11_containers,
   36          [ rdf_alt/3,                            % ?Alt, -Default, -Others
   37            rdf_assert_alt/3,                     % ?Alt, +Default, +Others
   38            rdf_assert_alt/4,                     % ?Alt, +Default, +Others, +G
   39            rdf_assert_bag/2,                     % ?Bag, +Set
   40            rdf_assert_bag/3,                     % ?Bag, +Set, +G
   41            rdf_assert_seq/2,                     % ?Seq, +List
   42            rdf_assert_seq/3,                     % ?Seq, +List, +G
   43            rdf_bag/2,                            % ?Bag, -List
   44            rdf_seq/2,                            % ?Seq, -List
   45            rdfs_container/2,                     % ?Container, -List
   46            rdfs_container_membership_property/1, % ?Property
   47            rdfs_container_membership_property/2, % ?Property, ?Number
   48            rdfs_member/2,                        % ?Elem, ?Container
   49            rdfs_nth0/3                           % ?N, ?Container, ?Elem
   50          ]).   51:- use_module(library(apply)).   52:- use_module(library(pairs)).   53:- use_module(library(error)).   54:- use_module(rdf11).

RDF 1.1 Containers

Implementation of the conventional human interpretation of RDF 1.1 containers.

RDF containers are open enumeration structures as opposed to RDF collections or RDF lists which are closed enumeration structures. The same resource may appear in a container more than once. A container may be contained in itself.


author
- Wouter Beek
- Jan Wielemaker
version
- 2016/01 */
See also
- http://www.w3.org/TR/2014/REC-rdf-schema-20140225/#ch_containervocab
Compatibility
- RDF 1.1
   74:- rdf_meta
   75    rdf_alt(r, -, -),
   76    rdf_assert_alt(r, r, t),
   77    rdf_assert_alt(r, r, t, r),
   78    rdf_assert_bag(r, t),
   79    rdf_assert_bag(r, t, r),
   80    rdf_assert_seq(r, t),
   81    rdf_assert_seq(r, t, r),
   82    rdf_bag(r, -),
   83    rdf_seq(r, -),
   84    rdfs_assert_container(r, t),
   85    rdfs_assert_container(r, t, r),
   86    rdfs_assert_container(r, r, t, r),
   87    rdfs_container(r, -),
   88    rdfs_container_membership_property(r),
   89    rdfs_container_membership_property(r,?),
   90    rdfs_member(r, -).
 rdf_alt(+Alt, ?Default, ?Others) is nondet
True when Alt is an instance of rdf:Alt with first member Default and remaining members Others.

Notice that this construct adds no machine-processable semantics but is conventionally used to indicate to a human reader that the numerical ordering of the container membership properties of Container is intended to only be relevant in distinguishing between the first and all non-first members.

Default denotes the default option to take when choosing one of the alternatives container in Container. Others denotes the non-default options that can be chosen from.

  108rdf_alt(Alt, Default, Others) :-
  109    rdfs_container(Alt, [Default|Others]).
 rdf_assert_alt(?Alt, +Default, +Others:list) is det
 rdf_assert_alt(?Alt, +Default, +Others:list, +Graph) is det
Create an rdf:Alt with the given Default and Others. Default and the members of Others must be valid object terms for rdf_assert/3.
  118rdf_assert_alt(Alt, H, T) :-
  119    rdf_default_graph(G),
  120    rdf_assert_alt(Alt, H, T, G).
  121
  122rdf_assert_alt(Alt, H, T, G) :-
  123    rdfs_assert_container(Alt, rdf:'Alt', [H|T], G).
 rdf_bag(+Bag, -List:list) is nondet
True when Bag is an rdf:Bag and set is the set values related through container membership properties to Bag.

Notice that this construct adds no machine-processable semantics but is conventionally used to indicate to a human reader that the numerical ordering of the container membership properties of Container is intended to not be significant.

  136rdf_bag(Bag, List) :-
  137    rdfs_container(Bag, List).
 rdf_assert_bag(?Bag, +Set:list) is det
 rdf_assert_bag(?Bag, +Set:list, +Graph) is det
Create an rdf:Bag from the given set of values. The members of Set must be valid object terms for rdf_assert/3.
  146rdf_assert_bag(Bag, L) :-
  147    rdf_default_graph(G),
  148    rdf_assert_bag(Bag, L, G).
  149
  150rdf_assert_bag(Bag, L, G) :-
  151    rdfs_assert_container(Bag, rdf:'Bag', L, G).
 rdf_seq(+Seq, -List:list) is nondet
True when Seq is an instance of rdf:Seq and List is a list of associated values, ordered according to the container membership property used.

Notice that this construct adds no machine-processable semantics but is conventionally used to indicate to a human reader that the numerical ordering of the container membership properties of Container is intended to be significant.

  165rdf_seq(Seq, L) :-
  166    rdfs_container(Seq, L).
 rdf_assert_seq(?Seq, +List) is det
 rdf_assert_seq(?Seq, +List, +Graph) is det
  172rdf_assert_seq(Seq, L) :-
  173    rdf_default_graph(G),
  174    rdf_assert_seq(Seq, L, G).
  175rdf_assert_seq(Seq, L, G) :-
  176    rdfs_assert_container(Seq, rdf:'Seq', L, G).
 rdfs_assert_container(?Container, +Class, +Elems, +Graph)
  181rdfs_assert_container(Container, Class, Elems, G) :-
  182    must_be(list, Elems),
  183    rdf_transaction(rdfs_assert_container_(Container, Class, Elems, G)).
  184
  185rdfs_assert_container_(Container, Class, Elems, G) :-
  186    (var(Container) -> rdf_create_bnode(Container) ; true),
  187    rdf_assert(Container, rdf:type, Class, G),
  188    rdfs_assert_members(Elems, 1, Container, G).
  189
  190rdfs_assert_members([], _, _, _).
  191rdfs_assert_members([H|T], N1, Resource, G) :-
  192    !,
  193    rdf_equal(rdf:'_', Prefix),
  194    atom_concat(Prefix, N1, P),
  195    rdf_assert(Resource, P, H, G),
  196    N2 is N1 + 1,
  197    rdfs_assert_members(T, N2, Resource, G).
 rdfs_container(+Container, -List) is nondet
True when List is the list of objects attached to Container using a container membership property (rdf:_0, rdf:_1, ...). If multiple objects are connected to the Container using the same membership property, this predicate selects one value non-deterministically.
  208rdfs_container(Container, List) :-
  209    rdf_is_subject(Container),
  210    !,
  211    findall(N-Elem, rdfs_member0(Container, N, Elem), Pairs),
  212    keysort(Pairs, Sorted),
  213    group_pairs_by_key(Sorted, GroupedPairs),
  214    pairs_values(GroupedPairs, Groups),
  215    maplist(member, List, Groups).
  216rdfs_container(Container, _) :-
  217    type_error(rdf_subject, Container).
 rdfs_container_membership_property(?Property) is nondet
True when Property is a container membership property (rdf:_1, rdf:_2, ...).
  225rdfs_container_membership_property(P) :-
  226    rdfs_container_membership_property(P, _).
 rdfs_container_membership_property(?Property, ?Number:nonneg) is nondet
True when Property is the Nth container membership property.

Success of this goal does not imply that Property is present in the database.

  236rdfs_container_membership_property(P, N) :-
  237    var(P),
  238    !,
  239    between(1, inf, N),
  240    rdf_equal(rdf:'_', Prefix),
  241    atom_concat(Prefix, N, P).
  242rdfs_container_membership_property(P, N) :-
  243    atom(P),
  244    rdf_equal(rdf:'_', Prefix),
  245    string_concat(Prefix, NumS, P),
  246    number_string(N, NumS),
  247    integer(N),
  248    N >= 0.
 rdfs_member(?Elem, ?Container) is nondet
True if rdf(Container, P, Elem) is true and P is a container membership property.
  256rdfs_member(Elem, Container) :-
  257    rdfs_member0(Container, _, Elem).
 rdfs_nth0(?N, ?Container, ?Elem) is nondet
True if rdf(Container, P, Elem) is true and P is the N-th (0-based) container membership property.
  264rdfs_nth0(N, Container, Elem) :-
  265    rdfs_member0(Container, N, Elem).
 rdfs_member0(?Container, ?N, ?Elem) is nondet
What is the most efficient way to enumerate rdfs_member(-,-)?
  1. If we enumerate over all container membership properties (= the current implementation) then it takes N steps before we get to triple `<Container, rdf:_N, Elem>`, for arbitrary N.
  2. The alternative is to enumerate over all triples and check whether the predicate term is a container membership property.
  3. The choice between (1) and (2) depends on whether the number of currently loaded triples in larger/smaller than the largest number that appears in a container membership property. This means enumerating over all predicate terms using rdf_predicate/1.
  284rdfs_member0(Container, N, Elem) :-
  285    (nonvar(Container) ; nonvar(Elem)),
  286    !,
  287    rdf_has(Container, P, Elem),
  288    rdfs_container_membership_property(P, N).
  289rdfs_member0(Container, N, Elem) :-
  290    rdfs_container_membership_property(P, N),
  291    rdf_has(Container, P, Elem)