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)  2003-2017, University of Amsterdam
    7                              VU University Amsterdam
    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(rdf_db,
   37          [ rdf_version/1,              % -Version
   38
   39            rdf/3,                      % ?Subject, ?Predicate, ?Object
   40            rdf/4,                      % ?Subject, ?Predicate, ?Object, ?DB
   41            rdf_has/3,                  % ?Subject, +Pred, ?Obj
   42            rdf_has/4,                  % ?Subject, +Pred, ?Obj, -RealPred
   43            rdf_reachable/3,            % ?Subject, +Pred, ?Object
   44            rdf_reachable/5,            % ?Subject, +Pred, ?Object, +MaxD, ?D
   45            rdf_resource/1,             % ?Resource
   46            rdf_subject/1,              % ?Subject
   47
   48            rdf_member_property/2,      % ?Property, ?Index
   49
   50            rdf_assert/3,               % +Subject, +Predicate, +Object
   51            rdf_assert/4,               % +Subject, +Predicate, +Object, +DB
   52            rdf_retractall/3,           % ?Subject, ?Predicate, ?Object
   53            rdf_retractall/4,           % ?Subject, ?Predicate, ?Object, +DB
   54            rdf_update/4,               % +Subject, +Predicate, +Object, +Act
   55            rdf_update/5,               % +Subject, +Predicate, +Object, +Src, +Act
   56            rdf_set_predicate/2,        % +Predicate, +Property
   57            rdf_predicate_property/2,   % +Predicate, ?Property
   58            rdf_current_predicate/1,    % -Predicate
   59            rdf_current_literal/1,      % -Literal
   60            rdf_transaction/1,          % :Goal
   61            rdf_transaction/2,          % :Goal, +Id
   62            rdf_transaction/3,          % :Goal, +Id, +Options
   63            rdf_active_transaction/1,   % ?Id
   64
   65            rdf_monitor/2,              % :Goal, +Options
   66
   67            rdf_save_db/1,              % +File
   68            rdf_save_db/2,              % +File, +DB
   69            rdf_load_db/1,              % +File
   70            rdf_reset_db/0,
   71
   72            rdf_node/1,                 % -Id
   73            rdf_bnode/1,                % -Id
   74            rdf_is_bnode/1,             % +Id
   75
   76            rdf_is_resource/1,          % +Term
   77            rdf_is_literal/1,           % +Term
   78            rdf_literal_value/2,        % +Term, -Value
   79
   80            rdf_load/1,                 % +File
   81            rdf_load/2,                 % +File, +Options
   82            rdf_save/1,                 % +File
   83            rdf_save/2,                 % +File, +Options
   84            rdf_unload/1,               % +File
   85            rdf_unload_graph/1,         % +Graph
   86
   87            rdf_md5/2,                  % +DB, -MD5
   88            rdf_atom_md5/3,             % +Text, +Times, -MD5
   89
   90            rdf_create_graph/1,         % ?Graph
   91            rdf_graph_property/2,       % ?Graph, ?Property
   92            rdf_set_graph/2,            % +Graph, +Property
   93            rdf_graph/1,                % ?Graph
   94            rdf_source/1,               % ?File
   95            rdf_source/2,               % ?DB, ?SourceURL
   96            rdf_make/0,                 % Reload modified databases
   97            rdf_gc/0,                   % Garbage collection
   98
   99            rdf_source_location/2,      % +Subject, -Source
  100            rdf_statistics/1,           % -Key
  101            rdf_set/1,                  % +Term
  102            rdf_generation/1,           % -Generation
  103            rdf_snapshot/1,             % -Snapshot
  104            rdf_delete_snapshot/1,      % +Snapshot
  105            rdf_current_snapshot/1,     % +Snapshot
  106            rdf_estimate_complexity/4,  % +S,+P,+O,-Count
  107
  108            rdf_save_subject/3,         % +Stream, +Subject, +DB
  109            rdf_save_header/2,          % +Out, +Options
  110            rdf_save_footer/1,          % +Out
  111
  112            rdf_equal/2,                % ?Resource, ?Resource
  113            lang_equal/2,               % +Lang1, +Lang2
  114            lang_matches/2,             % +Lang, +Pattern
  115
  116            rdf_prefix/2,               % :Alias, +URI
  117            rdf_current_prefix/2,       % :Alias, ?URI
  118            rdf_register_prefix/2,      % +Alias, +URI
  119            rdf_register_prefix/3,      % +Alias, +URI, +Options
  120            rdf_current_ns/2,           % :Alias, ?URI
  121            rdf_register_ns/2,          % +Alias, +URI
  122            rdf_register_ns/3,          % +Alias, +URI, +Options
  123            rdf_global_id/2,            % ?NS:Name, :Global
  124            rdf_global_object/2,        % +Object, :NSExpandedObject
  125            rdf_global_term/2,          % +Term, :WithExpandedNS
  126
  127            rdf_compare/3,              % -Dif, +Object1, +Object2
  128            rdf_match_label/3,          % +How, +String, +Label
  129            rdf_split_url/3,            % ?Base, ?Local, ?URL
  130            rdf_url_namespace/2,        % +URL, ?Base
  131
  132            rdf_warm_indexes/0,
  133            rdf_warm_indexes/1,         % +Indexed
  134            rdf_update_duplicates/0,
  135
  136            rdf_debug/1,                % Set verbosity
  137
  138            rdf_new_literal_map/1,      % -Handle
  139            rdf_destroy_literal_map/1,  % +Handle
  140            rdf_reset_literal_map/1,    % +Handle
  141            rdf_insert_literal_map/3,   % +Handle, +Key, +Literal
  142            rdf_insert_literal_map/4,   % +Handle, +Key, +Literal, -NewKeys
  143            rdf_delete_literal_map/3,   % +Handle, +Key, +Literal
  144            rdf_delete_literal_map/2,   % +Handle, +Key
  145            rdf_find_literal_map/3,     % +Handle, +KeyList, -Literals
  146            rdf_keys_in_literal_map/3,  % +Handle, +Spec, -Keys
  147            rdf_statistics_literal_map/2, % +Handle, +Name(-Arg...)
  148
  149            rdf_graph_prefixes/2,       % ?Graph, -Prefixes
  150            rdf_graph_prefixes/3,       % ?Graph, -Prefixes, :Filter
  151
  152            (rdf_meta)/1,               % +Heads
  153            op(1150, fx, (rdf_meta))
  154          ]).  155:- use_module(library(rdf)).  156:- use_module(library(lists)).  157:- use_module(library(shlib)).  158:- use_module(library(gensym)).  159:- use_module(library(sgml)).  160:- use_module(library(sgml_write)).  161:- use_module(library(option)).  162:- use_module(library(error)).  163:- use_module(library(uri)).  164:- use_module(library(debug)).  165:- use_module(library(apply)).  166:- use_module(library(xsdp_types)).  167:- if(exists_source(library(thread))).  168:- use_module(library(thread)).  169:- endif.  170:- use_module(library(semweb/rdf_cache)).  171:- use_module(library(semweb/rdf_prefixes)).  172
  173:- use_foreign_library(foreign(rdf_db)).  174:- public rdf_print_predicate_cloud/2.  % print matrix of reachable predicates
  175
  176:- meta_predicate
  177    rdf_transaction(0),
  178    rdf_transaction(0, +),
  179    rdf_transaction(0, +, +),
  180    rdf_monitor(1, +),
  181    rdf_save(+, :),
  182    rdf_load(+, :).  183
  184:- predicate_options(rdf_graph_prefixes/3, 3,
  185                     [expand(callable), filter(callable), min_count(nonneg)]).  186:- predicate_options(rdf_load/2, 2,
  187                     [ base_uri(atom),
  188                       cache(boolean),
  189                       concurrent(positive_integer),
  190                       db(atom),
  191                       format(oneof([xml,triples,turtle,trig,nquads,ntriples])),
  192                       graph(atom),
  193                       if(oneof([true,changed,not_loaded])),
  194                       modified(-float),
  195                       silent(boolean),
  196                       register_namespaces(boolean)
  197                     ]).  198:- predicate_options(rdf_save/2, 2,
  199                     [ graph(atom),
  200                       db(atom),
  201                       anon(boolean),
  202                       base_uri(atom),
  203                       write_xml_base(boolean),
  204                       convert_typed_literal(callable),
  205                       encoding(encoding),
  206                       document_language(atom),
  207                       namespaces(list(atom)),
  208                       xml_attributes(boolean),
  209                       inline(boolean)
  210                     ]).  211:- predicate_options(rdf_save_header/2, 2,
  212                     [ graph(atom),
  213                       db(atom),
  214                       namespaces(list(atom))
  215                     ]).  216:- predicate_options(rdf_save_subject/3, 3,
  217                     [ graph(atom),
  218                       base_uri(atom),
  219                       convert_typed_literal(callable),
  220                       document_language(atom)
  221                     ]).  222:- predicate_options(rdf_transaction/3, 3,
  223                     [ snapshot(any)
  224                     ]).  225
  226:- discontiguous
  227    term_expansion/2.

Core RDF database

The file library(semweb/rdf_db) provides the core of the SWI-Prolog RDF store.

deprecated
-
New applications should use library(semweb/rdf11), which provides a much more intuitive API to the RDF store, notably for handling literals. The library(semweb/rdf11) runs currently on top of this library and both can run side-by-side in the same application. Terms retrieved from the database however have a different shape and can not be exchanged without precautions. */
  243		 /*******************************
  244		 *            PREFIXES		*
  245		 *******************************/
  246
  247% the ns/2 predicate is historically defined  in this module. We'll keep
  248% that for compatibility reasons.
  249
  250:- multifile ns/2.  251:- dynamic   ns/2.                      % ID, URL
  252
  253:- multifile
  254    rdf_prefixes:rdf_empty_prefix_cache/2.  255
  256rdf_prefixes:rdf_empty_prefix_cache(_Prefix, _IRI) :-
  257    rdf_empty_prefix_cache.
  258
  259:- rdf_meta
  260    rdf(r,r,o),
  261    rdf_has(r,r,o,r),
  262    rdf_has(r,r,o),
  263    rdf_assert(r,r,o),
  264    rdf_retractall(r,r,o),
  265    rdf(r,r,o,?),
  266    rdf_assert(r,r,o,+),
  267    rdf_retractall(r,r,o,?),
  268    rdf_reachable(r,r,o),
  269    rdf_reachable(r,r,o,+,?),
  270    rdf_update(r,r,o,t),
  271    rdf_update(r,r,o,+,t),
  272    rdf_equal(o,o),
  273    rdf_source_location(r,-),
  274    rdf_resource(r),
  275    rdf_subject(r),
  276    rdf_create_graph(r),
  277    rdf_graph(r),
  278    rdf_graph_property(r,?),
  279    rdf_set_graph(r,+),
  280    rdf_unload_graph(r),
  281    rdf_set_predicate(r, t),
  282    rdf_predicate_property(r, -),
  283    rdf_estimate_complexity(r,r,r,-),
  284    rdf_print_predicate_cloud(r,+).
 rdf_equal(?Resource1, ?Resource2)
Simple equality test to exploit goal-expansion
  290rdf_equal(Resource, Resource).
 lang_equal(+Lang1, +Lang2) is semidet
True if two RFC language specifiers denote the same language
See also
- lang_matches/2.
  298lang_equal(Lang, Lang) :- !.
  299lang_equal(Lang1, Lang2) :-
  300    downcase_atom(Lang1, LangCannon),
  301    downcase_atom(Lang2, LangCannon).
 lang_matches(+Lang, +Pattern) is semidet
True if Lang matches Pattern. This implements XML language matching conform RFC 4647. Both Lang and Pattern are dash-separated strings of identifiers or (for Pattern) the wildcart *. Identifiers are matched case-insensitive and a * matches any number of identifiers. A short pattern is the same as *.
  313                 /*******************************
  314                 *     BASIC TRIPLE QUERIES     *
  315                 *******************************/
 rdf(?Subject, ?Predicate, ?Object) is nondet
Elementary query for triples. Subject and Predicate are atoms representing the fully qualified URL of the resource. Object is either an atom representing a resource or literal(Value) if the object is a literal value. If a value of the form NameSpaceID:LocalName is provided it is expanded to a ground atom using expand_goal/2. This implies you can use this construct in compiled code without paying a performance penalty. Literal values take one of the following forms:
Atom
If the value is a simple atom it is the textual representation of a string literal without explicit type or language qualifier.
lang(LangID, Atom)
Atom represents the text of a string literal qualified with the given language.
type(TypeID, Value)
Used for attributes qualified using the rdf:datatype TypeID. The Value is either the textual representation or a natural Prolog representation. See the option convert_typed_literal(:Convertor) of the parser. The storage layer provides efficient handling of atoms, integers (64-bit) and floats (native C-doubles). All other data is represented as a Prolog record.

For literal querying purposes, Object can be of the form literal(+Query, -Value), where Query is one of the terms below. If the Query takes a literal argument and the value has a numeric type numerical comparison is performed.

plain(+Text)
Perform exact match and demand the language or type qualifiers to match. This query is fully indexed.
icase(+Text)
Perform a full but case-insensitive match. This query is fully indexed.
exact(+Text)
Same as icase(Text). Backward compatibility.
substring(+Text)
Match any literal that contains Text as a case-insensitive substring. The query is not indexed on Object.
word(+Text)
Match any literal that contains Text delimited by a non alpha-numeric character, the start or end of the string. The query is not indexed on Object.
prefix(+Text)
Match any literal that starts with Text. This call is intended for completion. The query is indexed using the skip list of literals.
ge(+Literal)
Match any literal that is equal or larger then Literal in the ordered set of literals.
gt(+Literal)
Match any literal that is larger then Literal in the ordered set of literals.
eq(+Literal)
Match any literal that is equal to Literal in the ordered set of literals.
le(+Literal)
Match any literal that is equal or smaller then Literal in the ordered set of literals.
lt(+Literal)
Match any literal that is smaller then Literal in the ordered set of literals.
between(+Literal1, +Literal2)
Match any literal that is between Literal1 and Literal2 in the ordered set of literals. This may include both Literal1 and Literal2.
like(+Pattern)
Match any literal that matches Pattern case insensitively, where the `*' character in Pattern matches zero or more characters.

Backtracking never returns duplicate triples. Duplicates can be retrieved using rdf/4. The predicate rdf/3 raises a type-error if called with improper arguments. If rdf/3 is called with a term literal(_) as Subject or Predicate object it fails silently. This allows for graph matching goals like rdf(S,P,O),rdf(O,P2,O2) to proceed without errors.

 rdf(?Subject, ?Predicate, ?Object, ?Source) is nondet
As rdf/3 but in addition query the graph to which the triple belongs. Unlike rdf/3, this predicate does not remove duplicates from the result set.
Arguments:
Source- is a term Graph:Line. If Source is instatiated, passing an atom is the same as passing Atom:_.
 rdf_has(?Subject, +Predicate, ?Object) is nondet
Succeeds if the triple rdf(Subject, Predicate, Object) is true exploiting the rdfs:subPropertyOf predicate as well as inverse predicates declared using rdf_set_predicate/2 with the inverse_of property.
 rdf_has(?Subject, +Predicate, ?Object, -RealPredicate) is nondet
Same as rdf_has/3, but RealPredicate is unified to the actual predicate that makes this relation true. RealPredicate must be Predicate or an rdfs:subPropertyOf Predicate. If an inverse match is found, RealPredicate is the term inverse_of(Pred).
 rdf_reachable(?Subject, +Predicate, ?Object) is nondet
Is true if Object can be reached from Subject following the transitive predicate Predicate or a sub-property thereof, while repecting the symetric(true) or inverse_of(P2) properties.

If used with either Subject or Object unbound, it first returns the origin, followed by the reachable nodes in breath-first search-order. The implementation internally looks one solution ahead and succeeds deterministically on the last solution. This predicate never generates the same node twice and is robust against cycles in the transitive relation.

With all arguments instantiated, it succeeds deterministically if a path can be found from Subject to Object. Searching starts at Subject, assuming the branching factor is normally lower. A call with both Subject and Object unbound raises an instantiation error. The following example generates all subclasses of rdfs:Resource:

?- rdf_reachable(X, rdfs:subClassOf, rdfs:'Resource').
X = 'http://www.w3.org/2000/01/rdf-schema#Resource' ;
X = 'http://www.w3.org/2000/01/rdf-schema#Class' ;
X = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property' ;
...
 rdf_reachable(?Subject, +Predicate, ?Object, +MaxD, -D) is nondet
Same as rdf_reachable/3, but in addition, MaxD limits the number of edges expanded and D is unified with the `distance' between Subject and Object. Distance 0 means Subject and Object are the same resource. MaxD can be the constant infinite to impose no distance-limit.
 rdf_subject(?Resource) is nondet
True if Resource appears as a subject. This query respects the visibility rules implied by the logical update view.
See also
- rdf_resource/1.
  481rdf_subject(Resource) :-
  482    rdf_resource(Resource),
  483    ( rdf(Resource, _, _) -> true ).
 rdf_resource(?Resource) is nondet
True when Resource is a resource used as a subject or object in a triple.

This predicate is primarily intended as a way to process all resources without processing resources twice. The user must be aware that some of the returned resources may not appear in any visible triple.

  496                 /*******************************
  497                 *     TRIPLE MODIFICATIONS     *
  498                 *******************************/
 rdf_assert(+Subject, +Predicate, +Object) is det
Assert a new triple into the database. This is equivalent to rdf_assert/4 using Graph user. Subject and Predicate are resources. Object is either a resource or a term literal(Value). See rdf/3 for an explanation of Value for typed and language qualified literals. All arguments are subject to name-space expansion. Complete duplicates (including the same graph and `line' and with a compatible `lifespan') are not added to the database.
 rdf_assert(+Subject, +Predicate, +Object, +Graph) is det
As rdf_assert/3, adding the predicate to the indicated named graph.
Arguments:
Graph- is either the name of a graph (an atom) or a term Graph:Line, where Line is an integer that denotes a line number.
 rdf_retractall(?Subject, ?Predicate, ?Object) is det
Remove all matching triples from the database. As rdf_retractall/4 using an unbound graph.
 rdf_retractall(?Subject, ?Predicate, ?Object, ?Graph) is det
As rdf_retractall/3, also matching Graph. This is particulary useful to remove all triples coming from a loaded file. See also rdf_unload/1.
 rdf_update(+Subject, +Predicate, +Object, +Action) is det
Replaces one of the three fields on the matching triples depending on Action:
subject(Resource)
Changes the first field of the triple.
predicate(Resource)
Changes the second field of the triple.
object(Object)
Changes the last field of the triple to the given resource or literal(Value).
graph(Graph)
Moves the triple from its current named graph to Graph.
 rdf_update(+Subject, +Predicate, +Object, +Graph, +Action) is det
As rdf_update/4 but allows for specifying the graph.
  550                 /*******************************
  551                 *          COLLECTIONS         *
  552                 *******************************/
 rdf_member_property(?Prop, ?Index)
Deal with the rdf:_1, ... properties.
  558term_expansion(member_prefix(x),
  559               member_prefix(Prefix)) :-
  560    rdf_db:ns(rdf, NS),
  561    atom_concat(NS, '_', Prefix).
  562member_prefix(x).
  563
  564rdf_member_property(P, N) :-
  565    integer(N),
  566    !,
  567    member_prefix(Prefix),
  568    atom_concat(Prefix, N, P).
  569rdf_member_property(P, N) :-
  570    member_prefix(Prefix),
  571    atom_concat(Prefix, Sub, P),
  572    atom_number(Sub, N).
  573
  574
  575                 /*******************************
  576                 *      ANONYMOUS SUBJECTS      *
  577                 *******************************/
 rdf_node(-Id)
Generate a unique blank node identifier for a subject.
deprecated
- New code should use rdf_bnode/1.
  585rdf_node(Resource) :-
  586    rdf_bnode(Resource).
 rdf_bnode(-Id)
Generate a unique anonymous identifier for a subject.
  592rdf_bnode(Value) :-
  593    repeat,
  594    gensym('_:genid', Value),
  595    \+ rdf(Value, _, _),
  596    \+ rdf(_, _, Value),
  597    \+ rdf(_, Value, _),
  598    !.
  599
  600
  601
  602                 /*******************************
  603                 *             TYPES            *
  604                 *******************************/
 rdf_is_bnode(+Id)
Tests if a resource is a blank node (i.e. is an anonymous resource). A blank node is represented as an atom that starts with _:. For backward compatibility reason, __ is also considered to be a blank node.
See also
- rdf_bnode/1.
 rdf_is_resource(@Term) is semidet
True if Term is an RDF resource. Note that this is merely a type-test; it does not mean this resource is involved in any triple. Blank nodes are also considered resources.
See also
- rdf_is_bnode/1
  623rdf_is_resource(Term) :-
  624    atom(Term).
 rdf_is_literal(@Term) is semidet
True if Term is an RDF literal object. Currently only checks for groundness and the literal functor.
  631rdf_is_literal(literal(Value)) :-
  632    ground(Value).
  633
  634                 /*******************************
  635                 *             LITERALS         *
  636                 *******************************/
 rdf_current_literal(-Literal) is nondet
True when Literal is a currently known literal. Enumerates each unique literal exactly once. Note that it is possible that the literal only appears in already deleted triples. Deleted triples may be locked due to active queries, transactions or snapshots or may not yet be reclaimed by the garbage collector.
 rdf_literal_value(+Literal, -Value) is semidet
True when value is the appropriate Prolog representation of Literal in the RDF value space. Current mapping:
Plain literalsAtom
Language tagged literalAtom holding plain text
xsd:stringAtom
rdf:XMLLiteralXML DOM Tree
Numeric XSD typeNumber
To be done
- Well, this is the long-term idea.
- Add mode (-,+)
  661:- rdf_meta
  662    rdf_literal_value(o, -),
  663    typed_value(r, +, -),
  664    numeric_value(r, +, -).  665
  666rdf_literal_value(literal(String), Value) :-
  667    atom(String),
  668    !,
  669    Value = String.
  670rdf_literal_value(literal(lang(_Lang, String)), String).
  671rdf_literal_value(literal(type(Type, String)), Value) :-
  672    typed_value(Type, String, Value).
  673
  674typed_value(Numeric, String, Value) :-
  675    xsdp_numeric_uri(Numeric, NumType),
  676    !,
  677    numeric_value(NumType, String, Value).
  678typed_value(xsd:string, String, String).
  679typed_value(rdf:'XMLLiteral', Value, DOM) :-
  680    (   atom(Value)
  681    ->  setup_call_cleanup(
  682            ( atom_to_memory_file(Value, MF),
  683              open_memory_file(MF, read, In, [free_on_close(true)])
  684            ),
  685            load_structure(stream(In), DOM, [dialect(xml)]),
  686            close(In))
  687    ;   DOM = Value
  688    ).
  689
  690numeric_value(xsd:integer, String, Value) :-
  691    atom_number(String, Value),
  692    integer(Value).
  693numeric_value(xsd:float, String, Value) :-
  694    atom_number(String, Number),
  695    Value is float(Number).
  696numeric_value(xsd:double, String, Value) :-
  697    atom_number(String, Number),
  698    Value is float(Number).
  699numeric_value(xsd:decimal, String, Value) :-
  700    atom_number(String, Value).
  701
  702
  703                 /*******************************
  704                 *            SOURCE            *
  705                 *******************************/
 rdf_source_location(+Subject, -Location) is nondet
True when triples for Subject are loaded from Location.
Arguments:
Location- is a term File:Line.
  713rdf_source_location(Subject, Source) :-
  714    findall(Source, rdf(Subject, _, _, Source), Sources),
  715    sort(Sources, Unique),
  716    member(Source, Unique).
  717
  718
  719                 /*******************************
  720                 *       GARBAGE COLLECT        *
  721                 *******************************/
 rdf_create_gc_thread
Create the garbage collection thread.
  727:- public
  728    rdf_create_gc_thread/0.  729
  730rdf_create_gc_thread :-
  731    thread_create(rdf_gc_loop, _,
  732                  [ alias('__rdf_GC')
  733                  ]).
 rdf_gc_loop
Take care of running the RDF garbage collection. This predicate is called from a thread started by creating the RDF DB.
  740rdf_gc_loop :-
  741    catch(rdf_gc_loop(0), E, recover_gc(E)).
  742
  743recover_gc('$aborted') :-
  744    !,
  745    thread_self(Me),
  746    thread_detach(Me).
  747recover_gc(Error) :-
  748    print_message(error, Error),
  749    rdf_gc_loop.
  750
  751rdf_gc_loop(CPU) :-
  752    repeat,
  753    (   consider_gc(CPU)
  754    ->  rdf_gc(CPU1),
  755        sleep(CPU1)
  756    ;   sleep(0.1)
  757    ),
  758    fail.
 rdf_gc(-CPU) is det
Run RDF GC one time. CPU is the amount of CPU time spent. We update this in Prolog because portable access to thread specific CPU is really hard in C.
  766rdf_gc(CPU) :-
  767    statistics(cputime, CPU0),
  768    (   rdf_gc_
  769    ->  statistics(cputime, CPU1),
  770        CPU is CPU1-CPU0,
  771        rdf_add_gc_time(CPU)
  772    ;   CPU = 0.0
  773    ).
 rdf_gc is det
Run the RDF-DB garbage collector until no garbage is left and all tables are fully optimized. Under normal operation a seperate thread with identifier =__rdf_GC= performs garbage collection as long as it is considered `useful'.

Using rdf_gc/0 should only be needed to ensure a fully clean database for analysis purposes such as leak detection.

  785rdf_gc :-
  786    has_garbage,
  787    !,
  788    rdf_gc(_),
  789    rdf_gc.
  790rdf_gc.
 has_garbage is semidet
True if there is something to gain using GC.
  796has_garbage :-
  797    rdf_gc_info_(Info),
  798    has_garbage(Info),
  799    !.
  800
  801has_garbage(Info) :- arg(2, Info, Garbage),     Garbage > 0.
  802has_garbage(Info) :- arg(3, Info, Reindexed),   Reindexed > 0.
  803has_garbage(Info) :- arg(4, Info, Optimizable), Optimizable > 0.
 consider_gc(+CPU) is semidet
Arguments:
CPU- is the amount of CPU time spent in the most recent GC.
  810consider_gc(_CPU) :-
  811    (   rdf_gc_info_(gc_info(Triples,       % Total #triples in DB
  812                             Garbage,       % Garbage triples in DB
  813                             Reindexed,     % Reindexed & not reclaimed
  814                             Optimizable,   % Non-optimized tables
  815                             _KeepGen,      % Oldest active generation
  816                             _LastGCGen,    % Oldest active gen at last GC
  817                             _ReindexGen,
  818                             _LastGCReindexGen))
  819    ->  (   (Garbage+Reindexed) * 5 > Triples
  820        ;   Optimizable > 4
  821        )
  822    ;   print_message(error, rdf(invalid_gc_info)),
  823        sleep(10)
  824    ),
  825    !.
  826
  827
  828                 /*******************************
  829                 *           STATISTICS         *
  830                 *******************************/
 rdf_statistics(?KeyValue) is nondet
Obtain statistics on the RDF database. Defined statistics are:
graphs(-Count)
Number of named graphs
triples(-Count)
Total number of triples in the database. This is the number of asserted triples minus the number of retracted ones. The number of visible triples in a particular context may be different due to visibility rules defined by the logical update view and transaction isolation.
resources(-Count)
Number of resources that appear as subject or object in a triple. See rdf_resource/1.
properties(-Count)
Number of current predicates. See rdf_current_predicate/1.
literals(-Count)
Number of current literals. See rdf_current_literal/1.
gc(GCCount, ReclaimedTriples, ReindexedTriples, Time)
Information about the garbage collector.
searched_nodes(-Count)
Number of nodes expanded by rdf_reachable/3 and rdf_reachable/5.
lookup(rdf(S, P, O, G), Count)
Number of queries that have been performed for this particular instantiation pattern. Each of S,P,O,G is either + or -. Fails in case the number of performed queries is zero.
hash_quality(rdf(S, P, O, G), Buckets, Quality, PendingResize)
Statistics on the index for this pattern. Indices are created lazily on the first relevant query.
triples_by_graph(Graph, Count)
This statistics is produced for each named graph. See triples for the interpretation of this value.
  876rdf_statistics(graphs(Count)) :-
  877    rdf_statistics_(graphs(Count)).
  878rdf_statistics(triples(Count)) :-
  879    rdf_statistics_(triples(Count)).
  880rdf_statistics(duplicates(Count)) :-
  881    rdf_statistics_(duplicates(Count)).
  882rdf_statistics(lingering(Count)) :-
  883    rdf_statistics_(lingering(Count)).
  884rdf_statistics(resources(Count)) :-
  885    rdf_statistics_(resources(Count)).
  886rdf_statistics(properties(Count)) :-
  887    rdf_statistics_(predicates(Count)).
  888rdf_statistics(literals(Count)) :-
  889    rdf_statistics_(literals(Count)).
  890rdf_statistics(gc(Count, Reclaimed, Reindexed, Time)) :-
  891    rdf_statistics_(gc(Count, Reclaimed, Reindexed, Time)).
  892rdf_statistics(searched_nodes(Count)) :-
  893    rdf_statistics_(searched_nodes(Count)).
  894rdf_statistics(lookup(Index, Count)) :-
  895    functor(Indexed, indexed, 16),
  896    rdf_statistics_(Indexed),
  897    index(Index, I),
  898    Arg is I + 1,
  899    arg(Arg, Indexed, Count),
  900    Count \== 0.
  901rdf_statistics(hash_quality(Index, Size, Quality,Optimize)) :-
  902    rdf_statistics_(hash_quality(List)),
  903    member(hash(Place,Size,Quality,Optimize), List),
  904    index(Index, Place).
  905rdf_statistics(triples_by_graph(Graph, Count)) :-
  906    rdf_graph_(Graph, Count).
  907
  908index(rdf(-,-,-,-), 0).
  909index(rdf(+,-,-,-), 1).
  910index(rdf(-,+,-,-), 2).
  911index(rdf(+,+,-,-), 3).
  912index(rdf(-,-,+,-), 4).
  913index(rdf(+,-,+,-), 5).
  914index(rdf(-,+,+,-), 6).
  915index(rdf(+,+,+,-), 7).
  916
  917index(rdf(-,-,-,+), 8).
  918index(rdf(+,-,-,+), 9).
  919index(rdf(-,+,-,+), 10).
  920index(rdf(+,+,-,+), 11).
  921index(rdf(-,-,+,+), 12).
  922index(rdf(+,-,+,+), 13).
  923index(rdf(-,+,+,+), 14).
  924index(rdf(+,+,+,+), 15).
  925
  926
  927                 /*******************************
  928                 *           PREDICATES         *
  929                 *******************************/
 rdf_current_predicate(?Predicate) is nondet
True when Predicate is a currently known predicate. Predicates are created if a triples is created that uses this predicate or a property of the predicate is set using rdf_set_predicate/2. The predicate may (no longer) have triples associated with it.

Note that resources that have rdf:type rdf:Property are not automatically included in the result-set of this predicate, while all resources that appear as the second argument of a triple are included.

See also
- rdf_predicate_property/2.
  945rdf_current_predicate(P, DB) :-
  946    rdf_current_predicate(P),
  947    (   rdf(_,P,_,DB)
  948    ->  true
  949    ).
 rdf_predicate_property(?Predicate, ?Property)
Query properties of a defined predicate. Currently defined properties are given below.
symmetric(Bool)
True if the predicate is defined to be symetric. I.e., {A} P {B} implies {B} P {A}. Setting symmetric is equivalent to inverse_of(Self).
inverse_of(Inverse)
True if this predicate is the inverse of Inverse. This property is used by rdf_has/3, rdf_has/4, rdf_reachable/3 and rdf_reachable/5.
transitive(Bool)
True if this predicate is transitive. This predicate is currently not used. It might be used to make rdf_has/3 imply rdf_reachable/3 for transitive predicates.
triples(Triples)
Unify Triples with the number of existing triples using this predicate as second argument. Reporting the number of triples is intended to support query optimization.
rdf_subject_branch_factor(-Float)
Unify Float with the average number of triples associated with each unique value for the subject-side of this relation. If there are no triples the value 0.0 is returned. This value is cached with the predicate and recomputed only after substantial changes to the triple set associated to this relation. This property is intended for path optimalisation when solving conjunctions of rdf/3 goals.
rdf_object_branch_factor(-Float)
Unify Float with the average number of triples associated with each unique value for the object-side of this relation. In addition to the comments with the subject_branch_factor property, uniqueness of the object value is computed from the hash key rather than the actual values.
rdfs_subject_branch_factor(-Float)
Same as rdf_subject_branch_factor, but also considering triples of `subPropertyOf' this relation. See also rdf_has/3.
rdfs_object_branch_factor(-Float)
Same as rdf_object_branch_factor, but also considering triples of `subPropertyOf' this relation. See also rdf_has/3.
See also
- rdf_set_predicate/2.
 1002rdf_predicate_property(P, Prop) :-
 1003    var(P),
 1004    !,
 1005    rdf_current_predicate(P),
 1006    rdf_predicate_property_(P, Prop).
 1007rdf_predicate_property(P, Prop) :-
 1008    rdf_predicate_property_(P, Prop).
 rdf_set_predicate(+Predicate, +Property) is det
Define a property of the predicate. This predicate currently supports the following properties:
symmetric(+Boolean)
Set/unset the predicate as being symmetric. Using symmetric(true) is the same as inverse_of(Predicate), i.e., creating a predicate that is the inverse of itself.
transitive(+Boolean)
Sets the transitive property.
inverse_of(+Predicate2)
Define Predicate as the inverse of Predicate2. An inverse relation is deleted using inverse_of([]).

The transitive property is currently not used. The symmetric and inverse_of properties are considered by rdf_has/3,4 and rdf_reachable/3.

To be done
- Maintain these properties based on OWL triples.
 1033                 /*******************************
 1034                 *            SNAPSHOTS         *
 1035                 *******************************/
 rdf_snapshot(-Snapshot) is det
Take a snapshot of the current state of the RDF store. Later, goals may be executed in the context of the database at this moment using rdf_transaction/3 with the snapshot option. A snapshot created outside a transaction exists until it is deleted. Snapshots taken inside a transaction can only be used inside this transaction.
 rdf_delete_snapshot(+Snapshot) is det
Delete a snapshot as obtained from rdf_snapshot/1. After this call, resources used for maintaining the snapshot become subject to garbage collection.
 rdf_current_snapshot(?Term) is nondet
True when Term is a currently known snapshot.
bug
- Enumeration of snapshots is slow.
 1058rdf_current_snapshot(Term) :-
 1059    current_blob(Term, rdf_snapshot).
 1060
 1061
 1062                 /*******************************
 1063                 *          TRANSACTION         *
 1064                 *******************************/
 rdf_transaction(:Goal) is semidet
Same as rdf_transaction(Goal, user, []). See rdf_transaction/3.
 rdf_transaction(:Goal, +Id) is semidet
Same as rdf_transaction(Goal, Id, []). See rdf_transaction/3.
 rdf_transaction(:Goal, +Id, +Options) is semidet
Run Goal in an RDF transaction. Compared to the ACID model, RDF transactions have the following properties:
  1. Modifications inside the transactions become all atomically visible to the outside world if Goal succeeds or remain invisible if Goal fails or throws an exception. I.e., the atomicy property is fully supported.
  2. Consistency is not guaranteed. Later versions may implement consistency constraints that will be checked serialized just before the actual commit of a transaction.
  3. Concurrently executing transactions do not infuence each other. I.e., the isolation property is fully supported.
  4. Durability can be activated by loading library(semweb/rdf_persistency).

Processed options are:

snapshot(+Snapshot)
Execute Goal using the state of the RDF store as stored in Snapshot. See rdf_snapshot/1. Snapshot can also be the atom true, which implies that an anonymous snapshot is created at the current state of the store. Modifications due to executing Goal are only visible to Goal.
 1100rdf_transaction(Goal) :-
 1101    rdf_transaction(Goal, user, []).
 1102rdf_transaction(Goal, Id) :-
 1103    rdf_transaction(Goal, Id, []).
 rdf_active_transaction(?Id) is nondet
True if Id is the identifier of a transaction in the context of which this call is executed. If Id is not instantiated, backtracking yields transaction identifiers starting with the innermost nested transaction. Transaction identifier terms are not copied, need not be ground and can be instantiated during the transaction.
 1114rdf_active_transaction(Id) :-
 1115    rdf_active_transactions_(List),
 1116    member(Id, List).
 rdf_monitor(:Goal, +Options)
Call Goal if specified actions occur on the database.
 1122rdf_monitor(Goal, Options) :-
 1123    monitor_mask(Options, 0xffff, Mask),
 1124    rdf_monitor_(Goal, Mask).
 1125
 1126monitor_mask([], Mask, Mask).
 1127monitor_mask([H|T], Mask0, Mask) :-
 1128    update_mask(H, Mask0, Mask1),
 1129    monitor_mask(T, Mask1, Mask).
 1130
 1131update_mask(-X, Mask0, Mask) :-
 1132    !,
 1133    monitor_mask(X, M),
 1134    Mask is Mask0 /\ \M.
 1135update_mask(+X, Mask0, Mask) :-
 1136    !,
 1137    monitor_mask(X, M),
 1138    Mask is Mask0 \/ M.
 1139update_mask(X, Mask0, Mask) :-
 1140    monitor_mask(X, M),
 1141    Mask is Mask0 \/ M.
 monitor_mask(Name, Mask)
Mask bit for the monitor events. Note that this must be kept consistent with the enum broadcast_id defined in rdf_db.c
 1148                                        % C-defined broadcasts
 1149monitor_mask(assert,       0x0001).
 1150monitor_mask(assert(load), 0x0002).
 1151monitor_mask(retract,      0x0004).
 1152monitor_mask(update,       0x0008).
 1153monitor_mask(new_literal,  0x0010).
 1154monitor_mask(old_literal,  0x0020).
 1155monitor_mask(transaction,  0x0040).
 1156monitor_mask(load,         0x0080).
 1157monitor_mask(create_graph, 0x0100).
 1158monitor_mask(reset,        0x0200).
 1159                                        % prolog defined broadcasts
 1160monitor_mask(parse,        0x1000).
 1161monitor_mask(unload,       0x1000).     % FIXME: Duplicate
 1162                                        % mask for all
 1163monitor_mask(all,          0xffff).
 1164
 1165%rdf_broadcast(Term, MaskName) :-
 1166%%      monitor_mask(MaskName, Mask),
 1167%%      rdf_broadcast_(Term, Mask).
 1168
 1169
 1170                 /*******************************
 1171                 *            WARM              *
 1172                 *******************************/
 rdf_warm_indexes
Warm all indexes. See rdf_warm_indexes/1.
 1178rdf_warm_indexes :-
 1179    findall(Index, rdf_index(Index), Indexes),
 1180    rdf_warm_indexes(Indexes).
 1181
 1182rdf_index(s).
 1183rdf_index(p).
 1184rdf_index(o).
 1185rdf_index(sp).
 1186rdf_index(o).
 1187rdf_index(po).
 1188rdf_index(spo).
 1189rdf_index(g).
 1190rdf_index(sg).
 1191rdf_index(pg).
 rdf_warm_indexes(+Indexes) is det
Create the named indexes. Normally, the RDF database creates indexes on lazily the first time they are needed. This predicate serves two purposes: it provides an explicit way to make sure that the required indexes are present and creating multiple indexes at the same time is more efficient.
 1202                 /*******************************
 1203                 *          DUPLICATES          *
 1204                 *******************************/
 rdf_update_duplicates is det
Update the duplicate administration of the RDF store. This marks every triple that is potentionally a duplicate of another as duplicate. Being potentially a duplicate means that subject, predicate and object are equivalent and the life-times of the two triples overlap.

The duplicates marks are used to reduce the administrative load of avoiding duplicate answers. Normally, the duplicates are marked using a background thread that is started on the first query that produces a substantial amount of duplicates.

 1219:- public
 1220    rdf_update_duplicates_thread/0.
 rdf_update_duplicates_thread
Start a thread to initialize the duplicate administration.
 1226rdf_update_duplicates_thread :-
 1227    thread_create(rdf_update_duplicates, _,
 1228                  [ detached(true),
 1229                    alias('__rdf_duplicate_detecter')
 1230                  ]).
 rdf_update_duplicates is det
Update the duplicate administration. If this adminstration is up-to-date, each triples that may have a duplicate is flagged. The predicate rdf/3 uses this administration to speedup checking for duplicate answers.

This predicate is normally executed from a background thread named =__rdf_duplicate_detecter= which is created when a query discovers that checking for duplicates becomes too expensive.

 1244                 /*******************************
 1245                 *    QUICK BINARY LOAD/SAVE    *
 1246                 *******************************/
 rdf_save_db(+File) is det
 rdf_save_db(+File, +Graph) is det
Save triples into File in a quick-to-load binary format. If Graph is supplied only triples flagged to originate from that database are added. Files created this way can be loaded using rdf_load_db/1.
 1256:- create_prolog_flag(rdf_triple_format, 3, [type(integer)]). 1257
 1258rdf_save_db(File) :-
 1259    current_prolog_flag(rdf_triple_format, Version),
 1260    setup_call_cleanup(
 1261        open(File, write, Out, [type(binary)]),
 1262        ( set_stream(Out, record_position(false)),
 1263          rdf_save_db_(Out, _, Version)
 1264        ),
 1265        close(Out)).
 1266
 1267
 1268rdf_save_db(File, Graph) :-
 1269    current_prolog_flag(rdf_triple_format, Version),
 1270    setup_call_cleanup(
 1271        open(File, write, Out, [type(binary)]),
 1272        ( set_stream(Out, record_position(false)),
 1273          rdf_save_db_(Out, Graph, Version)
 1274        ),
 1275        close(Out)).
 rdf_load_db_no_admin(+File, +Id, -Graphs) is det
Load triples from a .trp file without updating the source administration. Id is handled to monitor action. Graphs is a list of graph-names encountered in File.
 1284rdf_load_db_no_admin(File, Id, Graphs) :-
 1285    open(File, read, In, [type(binary)]),
 1286    set_stream(In, record_position(false)),
 1287    call_cleanup(rdf_load_db_(In, Id, Graphs), close(In)).
 check_loaded_cache(+Graph, +Graphs, +Modified) is det
Verify the loaded cache file and optionally fix the modification time (new versions save this along with the snapshot).
To be done
- What to do if there is a cache mismatch? Delete the loaded graphs and fail?
 1298check_loaded_cache(DB, [DB], _Modified) :- !.
 1299check_loaded_cache(DB, Graphs, _) :-
 1300    print_message(warning, rdf(inconsistent_cache(DB, Graphs))).
 rdf_load_db(+File) is det
Load triples from a file created using rdf_save_db/2.
 1307rdf_load_db(File) :-
 1308    uri_file_name(URL, File),
 1309    rdf_load_db_no_admin(File, URL, _Graphs).
 1310
 1311
 1312                 /*******************************
 1313                 *          LOADING RDF         *
 1314                 *******************************/
 1315
 1316:- multifile
 1317    rdf_open_hook/8,
 1318    rdf_open_decode/4,              % +Encoding, +File, -Stream, -Cleanup
 1319    rdf_load_stream/3,              % +Format, +Stream, +Options
 1320    rdf_file_type/2,                % ?Extension, ?Format
 1321    rdf_storage_encoding/2,         % ?Extension, ?Encoding
 1322    url_protocol/1.                 % ?Protocol
 rdf_load(+FileOrList) is det
Same as rdf_load(FileOrList, []). See rdf_load/2.
 rdf_load(+FileOrList, :Options) is det
Load RDF data. Options provides additional processing options. Defined options are:
blank_nodes(+ShareMode)
How to handle equivalent blank nodes. If share (default), equivalent blank nodes are shared in the same resource.
base_uri(+URI)
URI that is used for rdf:about="" and other RDF constructs that are relative to the base uri. Default is the source URL.
concurrent(+Jobs)
If FileOrList is a list of files, process the input files using Jobs threads concurrently. Default is the mininum of the number of cores and the number of inputs. Higher values can be useful when loading inputs from (slow) network connections. Using 1 (one) does not use separate worker threads.
format(+Format)
Specify the source format explicitly. Normally this is deduced from the filename extension or the mime-type. The core library understands the formats xml (RDF/XML) and triples (internal quick load and cache format). Plugins, such as library(semweb/turtle) extend the set of recognised extensions.
graph(?Graph)
Named graph in which to load the data. It is not allowed to load two sources into the same named graph. If Graph is unbound, it is unified to the graph into which the data is loaded. The default graph is a =file://= URL when loading a file or, if the specification is a URL, its normalized version without the optional #fragment.
if(Condition)
When to load the file. One of true, changed (default) or not_loaded.
modified(-Modified)
Unify Modified with one of not_modified, cached(File), last_modified(Stamp) or unknown.
cache(Bool)
If false, do not use or create a cache file.
register_namespaces(Bool)
If true (default false), register xmlns namespace declarations or Turtle @prefix prefixes using rdf_register_prefix/3 if there is no conflict.
silent(+Bool)
If true, the message reporting completion is printed using level silent. Otherwise the level is informational. See also print_message/2.

Other options are forwarded to process_rdf/3. By default, rdf_load/2 only loads RDF/XML from files. It can be extended to load data from other formats and locations using plugins. The full set of plugins relevant to support different formats and locations is below:

:- use_module(library(semweb/turtle)).        % Turtle and TRiG
:- use_module(library(semweb/rdf_ntriples)).
:- use_module(library(semweb/rdf_zlib_plugin)).
:- use_module(library(semweb/rdf_http_plugin)).
:- use_module(library(http/http_ssl_plugin)).
See also
- rdf_open_hook/3, library(semweb/rdf_persistency) and library(semweb/rdf_cache)
 1404:- dynamic
 1405    rdf_loading/3.                          % Graph, Queue, Thread
 1406
 1407rdf_load(Spec) :-
 1408    rdf_load(Spec, []).
 1409
 1410:- if(\+current_predicate(concurrent/3)). 1411concurrent(_, Goals, _) :-
 1412    forall(member(G, Goals), call(G)).
 1413:- endif. 1414
 1415% Note that we kill atom garbage collection.  This improves performance
 1416% with about 15% loading the LUBM Univ_50 benchmark.
 1417
 1418rdf_load(Spec, M:Options) :-
 1419    must_be(list, Options),
 1420    current_prolog_flag(agc_margin, Old),
 1421    setup_call_cleanup(
 1422        set_prolog_flag(agc_margin, 0),
 1423        rdf_load_noagc(Spec, M, Options),
 1424        set_prolog_flag(agc_margin, Old)).
 1425
 1426rdf_load_noagc(List, M, Options) :-
 1427    is_list(List),
 1428    !,
 1429    flatten(List, Inputs),          % Compatibility: allow nested lists
 1430    maplist(must_be(ground), Inputs),
 1431    length(Inputs, Count),
 1432    load_jobs(Count, Jobs, Options),
 1433    (   Jobs =:= 1
 1434    ->  forall(member(Spec, Inputs),
 1435               rdf_load_one(Spec, M, Options))
 1436    ;   maplist(load_goal(Options, M), Inputs, Goals),
 1437        concurrent(Jobs, Goals, [])
 1438    ).
 1439rdf_load_noagc(One, M, Options) :-
 1440    must_be(ground, One),
 1441    rdf_load_one(One, M, Options).
 1442
 1443load_goal(Options, M, Spec, rdf_load_one(Spec, M, Options)).
 1444
 1445load_jobs(_, Jobs, Options) :-
 1446    option(concurrent(Jobs), Options),
 1447    !,
 1448    must_be(positive_integer, Jobs).
 1449load_jobs(Count, Jobs, _) :-
 1450    current_prolog_flag(cpu_count, CPUs),
 1451    CPUs > 0,
 1452    !,
 1453    Jobs is max(1, min(CPUs, Count)).
 1454load_jobs(_, 1, _).
 1455
 1456
 1457rdf_load_one(Spec, M, Options) :-
 1458    source_url(Spec, Protocol, SourceURL),
 1459    load_graph(SourceURL, Graph, Options),
 1460    setup_call_cleanup(
 1461        with_mutex(rdf_load_file,
 1462                   rdf_start_load(SourceURL, Loading)),
 1463        rdf_load_file(Loading, Spec, SourceURL, Protocol,
 1464                      Graph, M, Options),
 1465        rdf_end_load(Loading)).
 rdf_start_load(+SourceURL, -WhatToDo) is det
 rdf_end_load(+WhatToDo) is det
 rdf_load_file(+WhatToDo, +Spec, +SourceURL, +Protocol, +Graph, +Module, +Options) is det
Of these three predicates, rdf_load_file/7 does the real work. The others deal with the possibility that the graph is being loaded by another thread. In that case, we wait for the other thread to complete the work.
See also
- Code is modelled closely after how concurrent loading is handled in SWI-Prolog's boot/init.pl
To be done
- What if both threads disagree on what is loaded into the graph?
 1482rdf_start_load(SourceURL, queue(Queue)) :-
 1483    rdf_loading(SourceURL, Queue, LoadThread),
 1484    \+ thread_self(LoadThread),
 1485    !,
 1486    debug(rdf(load), '~p is being loaded by thread ~w; waiting ...',
 1487          [ SourceURL, LoadThread]).
 1488rdf_start_load(SourceURL, Ref) :-
 1489    thread_self(Me),
 1490    message_queue_create(Queue),
 1491    assertz(rdf_loading(SourceURL, Queue, Me), Ref).
 1492
 1493rdf_end_load(queue(_)) :- !.
 1494rdf_end_load(Ref) :-
 1495    clause(rdf_loading(_, Queue, _), _, Ref),
 1496    erase(Ref),
 1497    thread_send_message(Queue, done),
 1498    message_queue_destroy(Queue).
 1499
 1500rdf_load_file(queue(Queue), _Spec, _SourceURL, _Protocol, _Graph, _M, _Options) :-
 1501    !,
 1502    catch(thread_get_message(Queue, _), _, true).
 1503rdf_load_file(_Ref, _Spec, SourceURL, Protocol, Graph, M, Options) :-
 1504    debug(rdf(load), 'RDF: Loading ~q into ~q', [SourceURL, Graph]),
 1505    statistics(cputime, T0),
 1506    rdf_open_input(SourceURL, Protocol, Graph,
 1507                   In, Cleanup, Modified, Format, Options),
 1508    supported_format(Format, Cleanup),
 1509    return_modified(Modified, Options),
 1510    (   Modified == not_modified
 1511    ->  Action = none
 1512    ;   Modified = cached(CacheFile)
 1513    ->  do_unload(Graph),
 1514        catch(rdf_load_db_no_admin(CacheFile, cache(Graph), Graphs), _, fail),
 1515        check_loaded_cache(Graph, Graphs, Modified),
 1516        Action = load
 1517    ;   option(base_uri(BaseURI), Options, Graph),
 1518        (   var(BaseURI)
 1519        ->  BaseURI = SourceURL
 1520        ;   true
 1521        ),
 1522        once(phrase(derived_options(Options, NSList), Extra)),
 1523        merge_options([ base_uri(BaseURI),
 1524                        graph(Graph),
 1525                        format(Format)
 1526                      | Extra
 1527                      ], Options, RDFOptions),
 1528        do_unload(Graph),
 1529        graph_modified(Modified, ModifiedStamp),
 1530        rdf_set_graph_source(Graph, SourceURL, ModifiedStamp),
 1531        call_cleanup(rdf_load_stream(Format, In, M:RDFOptions),
 1532                     Cleanup),
 1533        save_cache(Graph, SourceURL, Options),
 1534        register_file_prefixes(NSList),
 1535        format_action(Format, Action)
 1536    ),
 1537    rdf_statistics_(triples(Graph, Triples)),
 1538    report_loaded(Action, SourceURL, Graph, Triples, T0, Options).
 1539
 1540supported_format(Format, _Cleanup) :-
 1541    rdf_file_type(_, Format),
 1542    !.
 1543supported_format(Format, Cleanup) :-
 1544    call(Cleanup),
 1545    existence_error(rdf_format_plugin, Format).
 1546
 1547format_action(triples, load) :- !.
 1548format_action(_, parsed).
 1549
 1550save_cache(Graph, SourceURL, Options) :-
 1551    option(cache(true), Options, true),
 1552    rdf_cache_file(SourceURL, write, CacheFile),
 1553    !,
 1554    catch(save_cache(Graph, CacheFile), E,
 1555          print_message(warning, E)).
 1556save_cache(_, _, _).
 1557
 1558derived_options([], _) -->
 1559    [].
 1560derived_options([H|T], NSList) -->
 1561    (   {   H == register_namespaces(true)
 1562        ;   H == (register_namespaces = true)
 1563        }
 1564    ->  [ namespaces(NSList) ]
 1565    ;   []
 1566    ),
 1567    derived_options(T, NSList).
 1568
 1569graph_modified(last_modified(Stamp), Stamp).
 1570graph_modified(unknown, Stamp) :-
 1571    get_time(Stamp).
 1572
 1573return_modified(Modified, Options) :-
 1574    option(modified(M0), Options),
 1575    !,
 1576    M0 = Modified.
 1577return_modified(_, _).
 1578
 1579
 1580                 /*******************************
 1581                 *        INPUT HANDLING        *
 1582                 *******************************/
 1583
 1584/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 1585This section deals with pluggable input sources.  The task of the input
 1586layer is
 1587
 1588    * Decide on the graph-name
 1589    * Decide on the source-location
 1590    * Decide whether loading is needed (if-modified)
 1591    * Decide on the serialization in the input
 1592
 1593The protocol must ensure minimal  overhead,   in  particular for network
 1594protocols. E.g. for HTTP we want to make a single call on the server and
 1595use If-modified-since to verify that we need not reloading this file.
 1596- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 rdf_open_input(+SourceURL, +Protocol, +Graph, -Stream, -Cleanup, -Modified, -Format, +Options)
Open an input source.

Options processed:

Arguments:
Modified- is one of not_modified, last_modified(Time), cached(CacheFile) or unknown
 1614rdf_open_input(SourceURL, Protocol, Graph,
 1615               Stream, Cleanup, Modified, Format, Options) :-
 1616    option(if(If), Options, changed),
 1617    (   If == true
 1618    ->  true
 1619    ;   rdf_graph_source_(Graph, SourceURL, HaveModified)
 1620    ->  true
 1621    ;   option(cache(true), Options, true),
 1622        rdf_cache_file(SourceURL, read, CacheFile)
 1623    ->  time_file(CacheFile, HaveModified)
 1624    ;   true
 1625    ),
 1626    option(format(Format), Options, _),
 1627    open_input_if_modified(Protocol, SourceURL, HaveModified,
 1628                           Stream, Cleanup, Modified0, Format, Options),
 1629    (   Modified0 == not_modified
 1630    ->  (   nonvar(CacheFile)
 1631        ->  Modified = cached(CacheFile)
 1632        ;   Modified = not_modified
 1633        )
 1634    ;   Modified = Modified0
 1635    ).
 source_url(+Spec, -Class, -SourceURL) is det
Determine class and url of the source. Class is one of
 1646source_url(stream(In), stream(In), SourceURL) :-
 1647    !,
 1648    (   stream_property(In, file_name(File))
 1649    ->  to_url(File, SourceURL)
 1650    ;   gensym('stream://', SourceURL)
 1651    ).
 1652source_url(Stream, Class, SourceURL) :-
 1653    is_stream(Stream),
 1654    !,
 1655    source_url(stream(Stream), Class, SourceURL).
 1656source_url(Spec, Protocol, SourceURL) :-
 1657    compound(Spec),
 1658    !,
 1659    source_file(Spec, Protocol, SourceURL).
 1660source_url(FileURL, Protocol, SourceURL) :-             % or return FileURL?
 1661    uri_file_name(FileURL, File),
 1662    !,
 1663    source_file(File, Protocol, SourceURL).
 1664source_url(SourceURL0, Protocol, SourceURL) :-
 1665    is_url(SourceURL0, Protocol, SourceURL),
 1666    !.
 1667source_url(File, Protocol, SourceURL) :-
 1668    source_file(File, Protocol, SourceURL).
 1669
 1670source_file(Spec, file(SExt), SourceURL) :-
 1671    findall(Ext, valid_extension(Ext), Exts),
 1672    absolute_file_name(Spec, File, [access(read), extensions([''|Exts])]),
 1673    storage_extension(_Plain, SExt, File),
 1674    uri_file_name(SourceURL, File).
 1675
 1676to_url(URL, URL) :-
 1677    uri_is_global(URL),
 1678    !.
 1679to_url(File, URL) :-
 1680    absolute_file_name(File, Path),
 1681    uri_file_name(URL, Path).
 1682
 1683storage_extension(Plain, SExt, File) :-
 1684    file_name_extension(Plain, SExt, File),
 1685    SExt \== '',
 1686    rdf_storage_encoding(SExt, _),
 1687    !.
 1688storage_extension(File, '', File).
 load_graph(+SourceURL, -Graph, +Options) is det
Graph is the graph into which we load the data. Tries these options:
  1. The graph(Graph) option
  2. The db(Graph) option (backward compatibility)
  3. The base_uri(BaseURI) option
  4. The source URL
 1700load_graph(Source, Graph, Options) :-
 1701    (   option(graph(Graph), Options)
 1702    ;   option(db(Graph), Options)
 1703    ),
 1704    !,
 1705    load_graph2(Source, Graph, Options).
 1706load_graph(Source, Graph, Options) :-
 1707    load_graph2(Source, Graph, Options).
 1708
 1709load_graph2(_, Graph, _) :-
 1710    ground(Graph),
 1711    !.
 1712load_graph2(_Source, Graph, Options) :-
 1713    option(base_uri(Graph), Options),
 1714    Graph \== [],
 1715    ground(Graph),
 1716    !.
 1717load_graph2(Source, Graph, _) :-
 1718    load_graph(Source, Graph).
 1719
 1720load_graph(SourceURL, BaseURI) :-
 1721    file_name_extension(BaseURI, Ext, SourceURL),
 1722    rdf_storage_encoding(Ext, _),
 1723    !.
 1724load_graph(SourceURL, SourceURL).
 1725
 1726
 1727open_input_if_modified(stream(In), SourceURL, _, In, true,
 1728                       unknown, Format, _) :-
 1729    !,
 1730    (   var(Format)
 1731    ->  guess_format(SourceURL, Format)
 1732    ;   true
 1733    ).
 1734open_input_if_modified(file(SExt), SourceURL, HaveModified, Stream, Cleanup,
 1735                       Modified, Format, _) :-
 1736    !,
 1737    uri_file_name(SourceURL, File),
 1738    (   SExt == '' -> Plain = File; file_name_extension(Plain, SExt, File)),
 1739    time_file(File, LastModified),
 1740    (   nonvar(HaveModified),
 1741        HaveModified >= LastModified
 1742    ->  Modified = not_modified,
 1743        Cleanup = true
 1744    ;   storage_open(SExt, File, Stream, Cleanup),
 1745        Modified = last_modified(LastModified),
 1746        (   var(Format)
 1747        ->  guess_format(Plain, Format)
 1748        ;   true
 1749        )
 1750    ).
 1751open_input_if_modified(file, SourceURL, HaveModified, Stream, Cleanup,
 1752                       Modified, Format, Options) :-
 1753    !,
 1754    open_input_if_modified(file(''), SourceURL, HaveModified,
 1755                           Stream, Cleanup,
 1756                           Modified, Format, Options).
 1757open_input_if_modified(Protocol, SourceURL, HaveModified, Stream, Cleanup,
 1758                       Modified, Format, Options) :-
 1759    rdf_open_hook(Protocol, SourceURL, HaveModified, Stream, Cleanup,
 1760                  Modified, Format, Options).
 1761
 1762guess_format(File, Format) :-
 1763    file_name_extension(_, Ext, File),
 1764    (   rdf_file_type(Ext, Format)
 1765    ->  true
 1766    ;   Format = xml,
 1767        print_message(warning, rdf(guess_format(Ext)))
 1768    ).
 storage_open(+Extension, +File, -Stream, -Cleanup)
Open the low-level storage. Note that the file is opened as binary. This is the same as for HTTP resources. The correct encoding will be set by the XML parser or the Turtle parser.
 1776storage_open('', File, Stream, close(Stream)) :-
 1777    !,
 1778    open(File, read, Stream, [type(binary)]).
 1779storage_open(Ext, File, Stream, Cleanup) :-
 1780    rdf_storage_encoding(Ext, Encoding),
 1781    rdf_open_decode(Encoding, File, Stream, Cleanup).
 1782
 1783valid_extension(Ext) :-
 1784    rdf_file_type(Ext, _).
 1785valid_extension(Ext) :-
 1786    rdf_storage_encoding(Ext, _).
 is_url(@Term, -Scheme, -URL) is semidet
True if Term is an atom denoting URL of the given Scheme. URL is normalized (see uri_normalized/2) and a possible fragment identifier (#fragment) is removed. This predicate only succeeds if the scheme is registered using the multifile hook url_protocol/1.
 1796is_url(URL, Scheme, FetchURL) :-
 1797    atom(URL),
 1798    uri_is_global(URL),
 1799    uri_normalized(URL, URL1),              % case normalization
 1800    uri_components(URL1, Components),
 1801    uri_data(scheme, Components, Scheme0),
 1802    url_protocol(Scheme0),
 1803    !,
 1804    Scheme = Scheme0,
 1805    uri_data(fragment, Components, _, Components1),
 1806    uri_components(FetchURL, Components1).
 1807
 1808url_protocol(file).                     % built-in
 rdf_file_type(+Extension, -Format) is semidet
True if Format is the format belonging to the given file extension. This predicate is multifile and can thus be extended by plugins.
 1816rdf_file_type(xml,   xml).
 1817rdf_file_type(rdf,   xml).
 1818rdf_file_type(rdfs,  xml).
 1819rdf_file_type(owl,   xml).
 1820rdf_file_type(htm,   xhtml).
 1821rdf_file_type(html,  xhtml).
 1822rdf_file_type(xhtml, xhtml).
 1823rdf_file_type(trp,   triples).
 rdf_file_encoding(+Extension, -Format) is semidet
True if Format describes the storage encoding of file.
 1830rdf_storage_encoding('', plain).
 rdf_load_stream(+Format, +Stream, :Options)
Load RDF data from Stream.
To be done
- Handle mime-types?
 1839rdf_load_stream(xml, Stream, Options) :-
 1840    !,
 1841    graph(Options, Graph),
 1842    rdf_transaction(load_stream(Stream, Options),
 1843                    parse(Graph)).
 1844rdf_load_stream(xhtml, Stream, M:Options) :-
 1845    !,
 1846    graph(Options, Graph),
 1847    rdf_transaction(load_stream(Stream, M:[embedded(true)|Options]),
 1848                    parse(Graph)).
 1849rdf_load_stream(triples, Stream, Options) :-
 1850    !,
 1851    graph(Options, Graph),
 1852    rdf_load_db_(Stream, Graph, _Graphs).
 1853
 1854load_stream(Stream, M:Options) :-
 1855    process_rdf(Stream, assert_triples, M:Options),
 1856    option(graph(Graph), Options),
 1857    rdf_graph_clear_modified_(Graph).
 report_loaded(+Action, +Source, +DB, +Triples, +StartCPU, +Options)
 1862report_loaded(none, _, _, _, _, _) :- !.
 1863report_loaded(Action, Source, DB, Triples, T0, Options) :-
 1864    statistics(cputime, T1),
 1865    Time is T1 - T0,
 1866    (   option(silent(true), Options)
 1867    ->  Level = silent
 1868    ;   Level = informational
 1869    ),
 1870    print_message(Level,
 1871                  rdf(loaded(Action, Source, DB, Triples, Time))).
 rdf_unload(+Source) is det
Identify the graph loaded from Source and use rdf_unload_graph/1 to erase this graph.
deprecated
- For compatibility, this predicate also accepts a graph name instead of a source specification. Please update your code to use rdf_unload_graph/1.
 1884rdf_unload(Spec) :-
 1885    source_url(Spec, _Protocol, SourceURL),
 1886    rdf_graph_source_(Graph, SourceURL, _),
 1887    !,
 1888    rdf_unload_graph(Graph).
 1889rdf_unload(Graph) :-
 1890    atom(Graph),
 1891    rdf_graph(Graph),
 1892    !,
 1893    warn_deprecated_unload(Graph),
 1894    rdf_unload_graph(Graph).
 1895rdf_unload(_).
 1896
 1897:- dynamic
 1898    warned/0. 1899
 1900warn_deprecated_unload(_) :-
 1901    warned,
 1902    !.
 1903warn_deprecated_unload(Graph) :-
 1904    assertz(warned),
 1905    print_message(warning, rdf(deprecated(rdf_unload(Graph)))).
 rdf_unload_graph(+Graph) is det
Remove Graph from the RDF store. Succeeds silently if the named graph does not exist.
 1913rdf_unload_graph(Graph) :-
 1914    must_be(atom, Graph),
 1915    (   rdf_graph(Graph)
 1916    ->  rdf_transaction(do_unload(Graph), unload(Graph))
 1917    ;   true
 1918    ).
 1919
 1920do_unload(Graph) :-
 1921    (   rdf_graph_(Graph, Triples),
 1922        Triples > 0
 1923    ->  rdf_retractall(_,_,_,Graph)
 1924    ;   true
 1925    ),
 1926    rdf_destroy_graph(Graph).
 1927
 1928                 /*******************************
 1929                 *         GRAPH QUERIES        *
 1930                 *******************************/
 rdf_create_graph(+Graph) is det
Create an RDF graph without triples. Succeeds silently if the graph already exists.
 rdf_graph(?Graph) is nondet
True when Graph is an existing graph.
 1942rdf_graph(Graph) :-
 1943    rdf_graph_(Graph, _Triples).
 rdf_source(?Graph, ?SourceURL) is nondet
True if named Graph is loaded from SourceURL.
deprecated
- Use rdf_graph_property(Graph, source(SourceURL)).
 1951rdf_source(Graph, SourceURL) :-
 1952    rdf_graph(Graph),
 1953    rdf_graph_source_(Graph, SourceURL, _Modified).
 rdf_source(?Source)
True if Source is a loaded source.
deprecated
- Use rdf_graph/1 or rdf_source/2.
 1961rdf_source(SourceURL) :-
 1962    rdf_source(_Graph, SourceURL).
 rdf_make
Reload all loaded files that have been modified since the last time they were loaded.
 1969rdf_make :-
 1970    findall(Source-Graph, modified_graph(Source, Graph), Modified),
 1971    forall(member(Source-Graph, Modified),
 1972           catch(rdf_load(Source, [graph(Graph), if(changed)]), E,
 1973                 print_message(error, E))).
 1974
 1975modified_graph(SourceURL, Graph) :-
 1976    rdf_graph(Graph),
 1977    rdf_graph_source_(Graph, SourceURL, Modified),
 1978    \+ sub_atom(SourceURL, 0, _, _, 'stream://'),
 1979    Modified > 0.
 rdf_graph_property(?Graph, ?Property) is nondet
True when Property is a property of Graph. Defined properties are:
hash(Hash)
Hash is the (MD5-)hash for the content of Graph.
modified(Boolean)
True if the graph is modified since it was loaded or rdf_set_graph/2 was called with modified(false).
source(Source)
The graph is loaded from the Source (a URL)
source_last_modified(?Time)
Time is the last-modified timestamp of Source at the moment that the graph was loaded from Source.
triples(Count)
True when Count is the number of triples in Graph.

Additional graph properties can be added by defining rules for the multifile predicate property_of_graph/2. Currently, the following extensions are defined:

 2007rdf_graph_property(Graph, Property) :-
 2008    rdf_graph(Graph),
 2009    property_of_graph(Property, Graph).
 2010
 2011:- multifile
 2012    property_of_graph/2. 2013
 2014property_of_graph(hash(Hash), Graph) :-
 2015    rdf_md5(Graph, Hash).
 2016property_of_graph(modified(Boolean), Graph) :-
 2017    rdf_graph_modified_(Graph, Boolean, _).
 2018property_of_graph(source(URL), Graph) :-
 2019    rdf_graph_source_(Graph, URL, _).
 2020property_of_graph(source_last_modified(Time), Graph) :-
 2021    rdf_graph_source_(Graph, _, Time),
 2022    Time > 0.0.
 2023property_of_graph(triples(Count), Graph) :-
 2024    rdf_graph_(Graph, Count).
 rdf_set_graph(+Graph, +Property) is det
Set properties of Graph. Defined properties are:
modified(false)
Set the modified state of Graph to false.
 2033rdf_set_graph(Graph, modified(Modified)) :-
 2034    must_be(oneof([false]), Modified),
 2035    rdf_graph_clear_modified_(Graph).
 save_cache(+DB, +Cache) is det
Save triples belonging to DB in the file Cache.
 2042save_cache(DB, Cache) :-
 2043    current_prolog_flag(rdf_triple_format, Version),
 2044    setup_call_cleanup(
 2045        catch(open(Cache, write, CacheStream, [type(binary)]), _, fail),
 2046        rdf_save_db_(CacheStream, DB, Version),
 2047        close(CacheStream)).
 assert_triples(+Triples, +Source)
Assert a list of triples into the database. Foir security reasons we check we aren't inserting anything but nice RDF triples.
 2055assert_triples([], _).
 2056assert_triples([rdf(S,P,O)|T], DB) :-
 2057    !,
 2058    rdf_assert(S, P, O, DB),
 2059    assert_triples(T, DB).
 2060assert_triples([H|_], _) :-
 2061    throw(error(type_error(rdf_triple, H), _)).
 2062
 2063
 2064                 /*******************************
 2065                 *             RESET            *
 2066                 *******************************/
 rdf_reset_db
Remove all triples from the RDF database and reset all its statistics.
bug
- This predicate checks for active queries, but this check is not properly synchronized and therefore the use of this predicate is unsafe in multi-threaded contexts. It is mainly used to run functionality tests that need to start with an empty database.
 2079rdf_reset_db :-
 2080    reset_gensym('_:genid'),
 2081    rdf_reset_db_.
 2082
 2083
 2084                 /*******************************
 2085                 *           SAVE RDF           *
 2086                 *******************************/
 rdf_save(+Out) is det
Same as rdf_save(Out, []). See rdf_save/2 for details.
 rdf_save(+Out, :Options) is det
Write RDF data as RDF/XML. Options is a list of one or more of the following options:
graph(+Graph)
Save only triples associated to the given named Graph.
anon(Bool)
If false (default true) do not save blank nodes that do not appear (indirectly) as object of a named resource.
base_uri(URI)
BaseURI used. If present, all URIs that can be represented relative to this base are written using their shorthand. See also write_xml_base option
convert_typed_literal(:Convertor)
Call Convertor(-Type, -Content, +RDFObject), providing the opposite for the convert_typed_literal option of the RDF parser.
document_language(+Lang)
Initial xml:lang saved with rdf:RDF element
encoding(Encoding)
Encoding for the output. Either utf8 or iso_latin_1
inline(+Bool)
If true (default false), inline resources when encountered for the first time. Normally, only bnodes are handled this way.
namespaces(+List)
Explicitely specify saved namespace declarations. See rdf_save_header/2 option namespaces for details.
sorted(+Boolean)
If true (default false), emit subjects sorted on the full URI. Useful to make file comparison easier.
write_xml_base(Bool)
If false, do not include the xml:base declaration that is written normally when using the base_uri option.
xml_attributes(+Bool)
If false (default true), never use xml attributes to save plain literal attributes, i.e., always used an XML element as in <name>Joe</name>.
Arguments:
Out- Location to save the data. This can also be a file-url (file://path) or a stream wrapped in a term stream(Out).
See also
- rdf_save_db/1
 2148:- thread_local
 2149    named_anon/2,                   % +Resource, -Id
 2150    inlined/1.                      % +Resource
 2151
 2152rdf_save(File) :-
 2153    rdf_save2(File, []).
 2154
 2155rdf_save(Spec, M:Options0) :-
 2156    is_list(Options0),
 2157    !,
 2158    meta_options(save_meta_option, M:Options0, Options),
 2159    to_file(Spec, File),
 2160    rdf_save2(File, Options).
 2161rdf_save(Spec, _:DB) :-
 2162    atom(DB),                      % backward compatibility
 2163    !,
 2164    to_file(Spec, File),
 2165    rdf_save2(File, [graph(DB)]).
 2166
 2167save_meta_option(convert_typed_literal).
 2168
 2169to_file(URL, File) :-
 2170    atom(URL),
 2171    uri_file_name(URL, File),
 2172    !.
 2173to_file(File, File).
 2174
 2175rdf_save2(File, Options) :-
 2176    option(encoding(Encoding), Options, utf8),
 2177    valid_encoding(Encoding),
 2178    open_output(File, Encoding, Out, Close),
 2179    flag(rdf_db_saved_subjects, OSavedSubjects, 0),
 2180    flag(rdf_db_saved_triples, OSavedTriples, 0),
 2181    call_cleanup(rdf_do_save(Out, Options),
 2182                 Reason,
 2183                 cleanup_save(Reason,
 2184                              File,
 2185                              OSavedSubjects,
 2186                              OSavedTriples,
 2187                              Close)).
 2188
 2189open_output(stream(Out), Encoding, Out,
 2190            set_stream(Out, encoding(Old))) :-
 2191    !,
 2192    stream_property(Out, encoding(Old)),
 2193    set_stream(Out, encoding(Encoding)).
 2194open_output(File, Encoding, Out,
 2195            close(Out)) :-
 2196    open(File, write, Out, [encoding(Encoding)]).
 2197
 2198valid_encoding(Enc) :-
 2199    (   xml_encoding_name(Enc, _)
 2200    ->  true
 2201    ;   throw(error(domain_error(encoding, Enc), _))
 2202    ).
 2203
 2204
 2205cleanup_save(Reason,
 2206             File,
 2207             OSavedSubjects,
 2208             OSavedTriples,
 2209             Close) :-
 2210    call(Close),
 2211    flag(rdf_db_saved_subjects, SavedSubjects, OSavedSubjects),
 2212    flag(rdf_db_saved_triples, SavedTriples, OSavedTriples),
 2213    retractall(named_anon(_, _)),
 2214    retractall(inlined(_)),
 2215    (   Reason == exit
 2216    ->  print_message(informational,
 2217                      rdf(saved(File, SavedSubjects, SavedTriples)))
 2218    ;   format(user_error, 'Reason = ~w~n', [Reason])
 2219    ).
 2220
 2221rdf_do_save(Out, Options0) :-
 2222    rdf_save_header(Out, Options0, Options),
 2223    graph(Options, DB),
 2224    (   option(sorted(true), Options, false)
 2225    ->  (   var(DB)
 2226        ->  setof(Subject, rdf_subject(Subject), Subjects)
 2227        ;   findall(Subject, rdf(Subject, _, _, DB:_), SubjectList),
 2228            sort(SubjectList, Subjects)
 2229        ),
 2230        forall(member(Subject, Subjects),
 2231               rdf_save_non_anon_subject(Out, Subject, Options))
 2232    ;   forall(rdf_subject_in_graph(Subject, DB),
 2233               rdf_save_non_anon_subject(Out, Subject, Options))
 2234    ),
 2235    rdf_save_footer(Out),
 2236    !.                                  % dubious cut; without the
 2237                                        % cleanup handlers isn't called!?
 rdf_subject_in_graph(-Subject, ?DB) is nondet
True when Subject is a subject in the graph DB. If DB is unbound, all subjects are enumerated. Otherwise we have two options: enumerate all subjects and filter by graph or collect all triples of the graph and get the unique subjects. The first is attractive if the graph is big compared to the DB, also because it does not require memory, the second if the graph is small compared to the DB.
 2248rdf_subject_in_graph(Subject, DB) :-
 2249    var(DB),
 2250    !,
 2251    rdf_subject(Subject).
 2252rdf_subject_in_graph(Subject, DB) :-
 2253    rdf_statistics(triples(AllTriples)),
 2254    rdf_graph_property(DB, triples(DBTriples)),
 2255    DBTriples > AllTriples // 10,
 2256    !,
 2257    rdf_resource(Subject),
 2258    (   rdf(Subject, _, _, DB:_)
 2259    ->  true
 2260    ).
 2261rdf_subject_in_graph(Subject, DB) :-
 2262    findall(Subject, rdf(Subject, _, _, DB:_), SubjectList),
 2263    list_to_set(SubjectList, Subjects),
 2264    member(Subject, Subjects).
 2265
 2266
 2267graph(Options0, DB) :-
 2268    strip_module(Options0, _, Options),
 2269    (   memberchk(graph(DB0), Options)
 2270    ->  DB = DB0
 2271    ;   memberchk(db(DB0), Options)
 2272    ->  DB = DB0
 2273    ;   true                            % leave unbound
 2274    ).
 rdf_save_header(+Fd, +Options)
Save XML document header, doctype and open the RDF environment. This predicate also sets up the namespace notation.

Save an RDF header, with the XML header, DOCTYPE, ENTITY and opening the rdf:RDF element with appropriate namespace declarations. It uses the primitives from section 3.5 to generate the required namespaces and desired short-name. Options is one of:

graph(+URI)
Only search for namespaces used in triples that belong to the given named graph.
namespaces(+List)
Where List is a list of namespace abbreviations. With this option, the expensive search for all namespaces that may be used by your data is omitted. The namespaces rdf and rdfs are added to the provided List. If a namespace is not declared, the resource is emitted in non-abreviated form.
 2299rdf_save_header(Out, Options) :-
 2300    rdf_save_header(Out, Options, _).
 2301
 2302rdf_save_header(Out, Options, OptionsOut) :-
 2303    is_list(Options),
 2304    !,
 2305    stream_property(Out, encoding(Enc)),
 2306    xml_encoding(Enc, Encoding),
 2307    format(Out, '<?xml version=\'1.0\' encoding=\'~w\'?>~n', [Encoding]),
 2308    format(Out, '<!DOCTYPE rdf:RDF [', []),
 2309    header_namespaces(Options, NSIdList),
 2310    nsmap(NSIdList, NsMap),
 2311    append(Options, [nsmap(NsMap)], OptionsOut),
 2312    forall(member(Id=URI, NsMap),
 2313           (   xml_quote_attribute(URI, NSText0, Enc),
 2314               xml_escape_parameter_entity(NSText0, NSText),
 2315               format(Out, '~N    <!ENTITY ~w \'~w\'>', [Id, NSText])
 2316           )),
 2317    format(Out, '~N]>~n~n', []),
 2318    format(Out, '<rdf:RDF', []),
 2319    (   member(Id, NSIdList),
 2320        format(Out, '~N    xmlns:~w="&~w;"~n', [Id, Id]),
 2321        fail
 2322    ;   true
 2323    ),
 2324    (   option(base_uri(Base), Options),
 2325        option(write_xml_base(true), Options, true)
 2326    ->  xml_quote_attribute(Base, BaseText, Enc),
 2327        format(Out, '~N    xml:base="~w"~n', [BaseText])
 2328    ;   true
 2329    ),
 2330    (   memberchk(document_language(Lang), Options)
 2331    ->  format(Out, '~N    xml:lang="~w"', [Lang])
 2332    ;   true
 2333    ),
 2334    format(Out, '>~n', []).
 2335rdf_save_header(Out, FileRef, OptionsOut) :-    % compatibility
 2336    atom(FileRef),
 2337    rdf_save_header(Out, [graph(FileRef)], OptionsOut).
 2338
 2339xml_encoding(Enc, Encoding) :-
 2340    (   xml_encoding_name(Enc, Encoding)
 2341    ->  true
 2342    ;   throw(error(domain_error(rdf_encoding, Enc), _))
 2343    ).
 2344
 2345xml_encoding_name(ascii,       'US-ASCII').
 2346xml_encoding_name(iso_latin_1, 'ISO-8859-1').
 2347xml_encoding_name(utf8,        'UTF-8').
 nsmap(+NSIds, -Map:list(id=uri)) is det
Create a namespace-map that is compatible to xml_write/2 for dealing with XML-Literals
 2354nsmap([], []).
 2355nsmap([Id|T0], [Id=URI|T]) :-
 2356    ns(Id, URI),
 2357    nsmap(T0, T).
 xml_escape_parameter_entity(+In, -Out) is det
Escape % as &#37; for entity declarations.
 2363xml_escape_parameter_entity(In, Out) :-
 2364    sub_atom(In, _, _, _, '%'),
 2365    !,
 2366    atom_codes(In, Codes),
 2367    phrase(escape_parent(Codes), OutCodes),
 2368    atom_codes(Out, OutCodes).
 2369xml_escape_parameter_entity(In, In).
 2370
 2371escape_parent([]) --> [].
 2372escape_parent([H|T]) -->
 2373    (   { H == 37 }
 2374    ->  "&#37;"
 2375    ;   [H]
 2376    ),
 2377    escape_parent(T).
 header_namespaces(Options, -List)
Get namespaces we will define as entities
 2384header_namespaces(Options, List) :-
 2385    memberchk(namespaces(NSL0), Options),
 2386    !,
 2387    sort([rdf,rdfs|NSL0], List).
 2388header_namespaces(Options, List) :-
 2389    graph(Options, DB),
 2390    used_namespace_entities(List, DB).
 rdf_graph_prefixes(?Graph, -List:ord_set) is det
 rdf_graph_prefixes(?Graph, -List:ord_set, :Options) is det
List is a sorted list of prefixes (namepaces) in Graph. Options defined are:
filter(:Filter)
optional Filter argument is used to filter the results. It is called with 3 additional arguments:
call(Filter, Where, Prefix, URI)

The Where argument gives the location of the prefix ans is one of subject, predicate, object or type. The Prefix argument is the potentionally new prefix and URI is the full URI that is being processed.

expand(:Goal)
Hook to generate the graph. Called using
call(Goal,S,P,O,Graph)
min_count(+Count)
Only include prefixes that appear at least N times. Default is 1. Declared prefixes are always returned if found at least one time.
get_prefix(:GetPrefix)
Predicate to extract the candidate prefix from an IRI. Default is iri_xml_namespace/2.
 2428:- thread_local
 2429    graph_prefix/3. 2430:- meta_predicate
 2431    rdf_graph_prefixes(?, -, :). 2432
 2433rdf_graph_prefixes(Graph, List) :-
 2434    rdf_graph_prefixes(Graph, List, []).
 2435
 2436rdf_graph_prefixes(Graph, List, M:QOptions) :-
 2437    is_list(QOptions),
 2438    !,
 2439    meta_options(is_meta, M:QOptions, Options),
 2440    option(filter(Filter), Options, true),
 2441    option(expand(Expand), Options, rdf_db),
 2442    option(min_count(MinCount), Options, 1),
 2443    option(get_prefix(GetPrefix), Options, iri_xml_namespace),
 2444    call_cleanup(prefixes(Expand, Graph, Prefixes, Filter, MinCount, GetPrefix),
 2445                 retractall(graph_prefix(_,_,_))),
 2446    sort(Prefixes, List).
 2447rdf_graph_prefixes(Graph, List, M:Filter) :-
 2448    rdf_graph_prefixes(Graph, List, M:[filter(Filter)]).
 2449
 2450is_meta(filter).
 2451is_meta(expand).
 2452is_meta(get_prefix).
 2453
 2454
 2455prefixes(Expand, Graph, Prefixes, Filter, MinCount, GetPrefix) :-
 2456    (   call(Expand, S, P, O, Graph),
 2457        add_ns(subject, GetPrefix, Filter, S, MinCount, s(S)),
 2458        add_ns(predicate, GetPrefix, Filter, P, MinCount, sp(S,P)),
 2459        add_ns_obj(GetPrefix, Filter, O, MinCount, spo(S,P,O)),
 2460        fail
 2461    ;   true
 2462    ),
 2463    findall(Prefix, graph_prefix(Prefix, MinCount, _), Prefixes).
 2464
 2465add_ns(Where, GetPrefix, Filter, S, MinCount, Context) :-
 2466    \+ rdf_is_bnode(S),
 2467    call(GetPrefix, S, Full),
 2468    Full \== '',
 2469    !,
 2470    (   graph_prefix(Full, MinCount, _)
 2471    ->  true
 2472    ;   Filter == true
 2473    ->  add_ns(Full, Context)
 2474    ;   call(Filter, Where, Full, S)
 2475    ->  add_ns(Full, Context)
 2476    ;   true
 2477    ).
 2478add_ns(_, _, _, _, _, _).
 2479
 2480add_ns(Full, Context) :-
 2481    graph_prefix(Full, _, Contexts),
 2482    memberchk(Context, Contexts),
 2483    !.
 2484add_ns(Full, Context) :-
 2485    retract(graph_prefix(Full, C0, Contexts)),
 2486    !,
 2487    C1 is C0+1,
 2488    asserta(graph_prefix(Full, C1, [Context|Contexts])).
 2489add_ns(Full, _) :-
 2490    ns(_, Full),
 2491    !,
 2492    asserta(graph_prefix(Full, _, _)).
 2493add_ns(Full, Context) :-
 2494    asserta(graph_prefix(Full, 1, [Context])).
 2495
 2496
 2497add_ns_obj(GetPrefix, Filter, O, MinCount, Context) :-
 2498    atom(O),
 2499    !,
 2500    add_ns(object, GetPrefix, Filter, O, MinCount, Context).
 2501add_ns_obj(GetPrefix, Filter, literal(type(Type, _)), MinCount, _) :-
 2502    atom(Type),
 2503    !,
 2504    add_ns(type, GetPrefix, Filter, Type, MinCount, t(Type)).
 2505add_ns_obj(_, _, _, _, _).
 used_namespace_entities(-List, ?Graph) is det
Return the namespace aliases that are actually used in Graph. In addition, this predicate creates ns<N> aliases for namespaces used in predicates because RDF/XML cannot write predicates other than as an XML name.
 2515used_namespace_entities(List, Graph) :-
 2516    decl_used_predicate_ns(Graph),
 2517    used_namespaces(List, Graph).
 2518
 2519used_namespaces(List, DB) :-
 2520    rdf_graph_prefixes(DB, FullList),
 2521    ns_abbreviations(FullList, List0),
 2522    sort([rdf|List0], List).
 2523
 2524ns_abbreviations([], []).
 2525ns_abbreviations([H0|T0], [H|T]) :-
 2526    ns(H, H0),
 2527    !,
 2528    ns_abbreviations(T0, T).
 2529ns_abbreviations([_|T0], T) :-
 2530    ns_abbreviations(T0, T).
 2531
 2532
 2533/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 2534For every URL used as a predicate  we   *MUST*  define a namespace as we
 2535cannot use names holding /, :, etc. as XML identifiers.
 2536- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 2537
 2538:- thread_local
 2539    predicate_ns/2. 2540
 2541decl_used_predicate_ns(DB) :-
 2542    retractall(predicate_ns(_,_)),
 2543    (   rdf_current_predicate(P, DB),
 2544        decl_predicate_ns(P),
 2545        fail
 2546    ;   true
 2547    ).
 2548
 2549decl_predicate_ns(Pred) :-
 2550    predicate_ns(Pred, _),
 2551    !.
 2552decl_predicate_ns(Pred) :-
 2553    rdf_global_id(NS:Local, Pred),
 2554    xml_name(Local),
 2555    !,
 2556    assert(predicate_ns(Pred, NS)).
 2557decl_predicate_ns(Pred) :-
 2558    atom_codes(Pred, Codes),
 2559    append(NSCodes, LocalCodes, Codes),
 2560    xml_codes(LocalCodes),
 2561    !,
 2562    (   NSCodes \== []
 2563    ->  atom_codes(NS, NSCodes),
 2564        (   ns(Id, NS)
 2565        ->  assert(predicate_ns(Pred, Id))
 2566        ;   between(1, infinite, N),
 2567            atom_concat(ns, N, Id),
 2568            \+ ns(Id, _)
 2569        ->  rdf_register_ns(Id, NS),
 2570            print_message(informational,
 2571                          rdf(using_namespace(Id, NS)))
 2572        ),
 2573        assert(predicate_ns(Pred, Id))
 2574    ;   assert(predicate_ns(Pred, -)) % no namespace used
 2575    ).
 2576
 2577xml_codes([]).
 2578xml_codes([H|T]) :-
 2579    xml_code(H),
 2580    xml_codes(T).
 2581
 2582xml_code(X) :-
 2583    code_type(X, csym),
 2584    !.
 2585xml_code(0'-).                          % Match 0'-
 rdf_save_footer(Out:stream) is det
Finish XML generation and write the document footer.
See also
- rdf_save_header/2, rdf_save_subject/3.
 2594rdf_save_footer(Out) :-
 2595    retractall(named_anon(_, _)),
 2596    retractall(inlined(_)),
 2597    format(Out, '</rdf:RDF>~n', []).
 rdf_save_non_anon_subject(+Out, +Subject, +Options)
Save an object. Anonymous objects not saved if anon(false) is present in the Options list.
 2604rdf_save_non_anon_subject(_Out, Subject, Options) :-
 2605    rdf_is_bnode(Subject),
 2606    (   memberchk(anon(false), Options)
 2607    ;   graph(Options, DB),
 2608        rdf_db(_, _, Subject, DB)
 2609    ),
 2610    !.
 2611rdf_save_non_anon_subject(Out, Subject, Options) :-
 2612    rdf_save_subject(Out, Subject, Options),
 2613    flag(rdf_db_saved_subjects, X, X+1).
 rdf_save_subject(+Out, +Subject:resource, +Options) is det
Save the triples associated to Subject to Out. Options:
graph(+Graph)
Only save properties from Graph.
base_uri(+URI)
convert_typed_literal(:Goal)
document_language(+XMLLang)
See also
- rdf_save/2 for a description of these options.
 2628rdf_save_subject(Out, Subject, Options) :-
 2629    is_list(Options),
 2630    !,
 2631    option(base_uri(BaseURI), Options, '-'),
 2632    (   rdf_save_subject(Out, Subject, BaseURI, 0, Options)
 2633    ->  format(Out, '~n', [])
 2634    ;   throw(error(rdf_save_failed(Subject), 'Internal error'))
 2635    ).
 2636rdf_save_subject(Out, Subject, DB) :-
 2637    (   var(DB)
 2638    ->  rdf_save_subject(Out, Subject, [])
 2639    ;   rdf_save_subject(Out, Subject, [graph(DB)])
 2640    ).
 rdf_save_subject(+Out:stream, +Subject:resource, +BaseURI, +Indent:int, +Options) is det
Save properties of Subject.
Arguments:
Indent- Current indentation
 2650rdf_save_subject(_, Subject, _, _, _) :-
 2651    inlined(Subject),
 2652    !.
 2653rdf_save_subject(Out, Subject, BaseURI, Indent, Options) :-
 2654    do_save_subject(Out, Subject, BaseURI, Indent, Options).
 2655
 2656do_save_subject(Out, Subject, BaseURI, Indent, Options) :-
 2657    graph(Options, DB),
 2658    findall(Pred=Object, rdf_db(Subject, Pred, Object, DB), Atts0),
 2659    sort(Atts0, Atts),              % remove duplicates
 2660    length(Atts, L),
 2661    (   length(Atts0, L0),
 2662        Del is L0-L,
 2663        Del > 0
 2664    ->  print_message(informational,
 2665                      rdf(save_removed_duplicates(Del, Subject)))
 2666    ;   true
 2667    ),
 2668    rdf_save_subject(Out, Subject, BaseURI, Atts, Indent, Options),
 2669    flag(rdf_db_saved_triples, X, X+L).
 2670
 2671rdf_db(Subject, Pred, Object, DB) :-
 2672    var(DB),
 2673    !,
 2674    rdf(Subject, Pred, Object).
 2675rdf_db(Subject, Pred, Object, DB) :-
 2676    rdf(Subject, Pred, Object, DB:_).
 rdf_save_subject(+Out:stream, +Subject:resource, +BaseURI, +Atts:list(Pred=Obj), +Indent:int, +Options) is det
Save triples defined by Atts on Subject.
 2683rdf_save_subject(Out, Subject, BaseURI, Atts, Indent, Options) :-
 2684    rdf_equal(rdf:type, RdfType),
 2685    select(RdfType=Type, Atts, Atts1),
 2686    \+ rdf_is_bnode(Type),
 2687    rdf_id(Type, BaseURI, TypeId),
 2688    xml_is_name(TypeId),
 2689    !,
 2690    format(Out, '~*|<', [Indent]),
 2691    rdf_write_id(Out, TypeId),
 2692    save_about(Out, BaseURI, Subject),
 2693    save_attributes(Atts1, BaseURI, Out, TypeId, Indent, Options).
 2694rdf_save_subject(Out, Subject, BaseURI, Atts, Indent, Options) :-
 2695    format(Out, '~*|<rdf:Description', [Indent]),
 2696    save_about(Out, BaseURI, Subject),
 2697    save_attributes(Atts, BaseURI, Out, rdf:'Description', Indent, Options).
 2698
 2699xml_is_name(_NS:Atom) :-
 2700    !,
 2701    xml_name(Atom).
 2702xml_is_name(Atom) :-
 2703    xml_name(Atom).
 save_about(+Out, +BaseURI, +Subject) is det
Save the rdf:about. If Subject is a blank node, save the nodeID if any.
 2710save_about(Out, _, Subject) :-
 2711    rdf_is_bnode(Subject),
 2712    !,
 2713    (   named_anon(Subject, NodeID)
 2714    ->  format(Out, ' rdf:nodeID="~w"', [NodeID])
 2715    ;   true
 2716    ).
 2717save_about(Out, BaseURI, Subject) :-
 2718    stream_property(Out, encoding(Encoding)),
 2719    rdf_value(Subject, BaseURI, QSubject, Encoding),
 2720    format(Out, ' rdf:about="~w"', [QSubject]).
 save_attributes(+List, +BaseURI, +Stream, +Element, +Indent, +Options)
Save the attributes. Short literal attributes are saved in the tag. Others as the content of the description element. The begin tag has already been filled.
 2728save_attributes(Atts, BaseURI, Out, Element, Indent, Options) :-
 2729    split_attributes(Atts, InTag, InBody, Options),
 2730    SubIndent is Indent + 2,
 2731    save_attributes2(InTag, BaseURI, tag, Out, SubIndent, Options),
 2732    (   InBody == []
 2733    ->  format(Out, '/>~n', [])
 2734    ;   format(Out, '>~n', []),
 2735        save_attributes2(InBody, BaseURI, body, Out, SubIndent, Options),
 2736        format(Out, '~N~*|</', [Indent]),
 2737        rdf_write_id(Out, Element),
 2738        format(Out, '>~n', [])
 2739    ).
 split_attributes(+Attributes, -HeadAttrs, -BodyAttr, Options)
Split attribute (Name=Value) list into attributes for the head and body. Attributes can only be in the head if they are literal and appear only one time in the attribute list.
 2747split_attributes(Atts, [], Atts, Options) :-
 2748    option(xml_attributes(false), Options),
 2749    !.
 2750split_attributes(Atts, HeadAttr, BodyAttr, _) :-
 2751    duplicate_attributes(Atts, Dupls, Singles),
 2752    simple_literal_attributes(Singles, HeadAttr, Rest),
 2753    append(Dupls, Rest, BodyAttr).
 duplicate_attributes(+Attrs, -Duplicates, -Singles)
Extract attributes that appear more than onces as we cannot dublicate an attribute in the head according to the XML rules.
 2760duplicate_attributes([], [], []).
 2761duplicate_attributes([H|T], Dupls, Singles) :-
 2762    H = (Name=_),
 2763    named_attributes(Name, T, D, R),
 2764    D \== [],
 2765    append([H|D], Dupls2, Dupls),
 2766    !,
 2767    duplicate_attributes(R, Dupls2, Singles).
 2768duplicate_attributes([H|T], Dupls2, [H|Singles]) :-
 2769    duplicate_attributes(T, Dupls2, Singles).
 2770
 2771named_attributes(_, [], [], []) :- !.
 2772named_attributes(Name, [H|T], D, R) :-
 2773    (   H = (Name=_)
 2774    ->  D = [H|DT],
 2775        named_attributes(Name, T, DT, R)
 2776    ;   R = [H|RT],
 2777        named_attributes(Name, T, D, RT)
 2778    ).
 simple_literal_attributes(+Attributes, -Inline, -Body)
Split attributes for (literal) attributes to be used in the begin-tag and ones that have to go into the body of the description.
 2785simple_literal_attributes([], [], []).
 2786simple_literal_attributes([H|TA], [H|TI], B) :-
 2787    in_tag_attribute(H),
 2788    !,
 2789    simple_literal_attributes(TA, TI, B).
 2790simple_literal_attributes([H|TA], I, [H|TB]) :-
 2791    simple_literal_attributes(TA, I, TB).
 2792
 2793in_tag_attribute(_=literal(Text)) :-
 2794    atom(Text),                     % may not have lang qualifier
 2795    atom_length(Text, Len),
 2796    Len < 60.
 save_attributes(+List, +BaseURI, +TagOrBody, +Stream)
Save a list of attributes.
 2802save_attributes2([], _, _, _, _, _).
 2803save_attributes2([H|T], BaseURI, Where, Out, Indent, Options) :-
 2804    save_attribute(Where, H, BaseURI, Out, Indent, Options),
 2805    save_attributes2(T, BaseURI, Where, Out, Indent, Options).
 2806
 2807save_attribute(tag, Name=literal(Value), BaseURI, Out, Indent, _DB) :-
 2808    AttIndent is Indent + 2,
 2809    rdf_id(Name, BaseURI, NameText),
 2810    stream_property(Out, encoding(Encoding)),
 2811    xml_quote_attribute(Value, QVal, Encoding),
 2812    format(Out, '~N~*|', [AttIndent]),
 2813    rdf_write_id(Out, NameText),
 2814    format(Out, '="~w"', [QVal]).
 2815save_attribute(body, Name=literal(Literal0), BaseURI, Out, Indent, Options) :-
 2816    !,
 2817    rdf_id(Name, BaseURI, NameText),
 2818    (   memberchk(convert_typed_literal(Converter), Options),
 2819        call(Converter, Type, Content, Literal0)
 2820    ->  Literal = type(Type, Content)
 2821    ;   Literal = Literal0
 2822    ),
 2823    save_body_literal(Literal, NameText, BaseURI, Out, Indent, Options).
 2824save_attribute(body, Name=Value, BaseURI, Out, Indent, Options) :-
 2825    rdf_is_bnode(Value),
 2826    !,
 2827    rdf_id(Name, BaseURI, NameText),
 2828    format(Out, '~N~*|<', [Indent]),
 2829    rdf_write_id(Out, NameText),
 2830    (   named_anon(Value, NodeID)
 2831    ->  format(Out, ' rdf:nodeID="~w"/>', [NodeID])
 2832    ;   (   rdf(S1, Name, Value),
 2833            rdf(S2, P2, Value),
 2834            (S1 \== S2 ; Name \== P2)
 2835        ->  predicate_property(named_anon(_,_), number_of_clauses(N)),
 2836            atom_concat('bn', N, NodeID),
 2837            assertz(named_anon(Value, NodeID))
 2838        ;   true
 2839        ),
 2840        SubIndent is Indent + 2,
 2841        (   rdf_collection(Value)
 2842        ->  save_about(Out, BaseURI, Value),
 2843            format(Out, ' rdf:parseType="Collection">~n', []),
 2844            rdf_save_list(Out, Value, BaseURI, SubIndent, Options)
 2845        ;   format(Out, '>~n', []),
 2846            rdf_save_subject(Out, Value, BaseURI, SubIndent, Options)
 2847        ),
 2848        format(Out, '~N~*|</', [Indent]),
 2849        rdf_write_id(Out, NameText),
 2850        format(Out, '>~n', [])
 2851    ).
 2852save_attribute(body, Name=Value, BaseURI, Out, Indent, Options) :-
 2853    option(inline(true), Options),
 2854    has_attributes(Value, Options),
 2855    \+ inlined(Value),
 2856    !,
 2857    assertz(inlined(Value)),
 2858    rdf_id(Name, BaseURI, NameText),
 2859    format(Out, '~N~*|<', [Indent]),
 2860    rdf_write_id(Out, NameText),
 2861    SubIndent is Indent + 2,
 2862    (   rdf_collection(Value)
 2863    ->  save_about(Out, BaseURI, Value),
 2864        format(Out, ' rdf:parseType="Collection">~n', []),
 2865        rdf_save_list(Out, Value, BaseURI, SubIndent, Options)
 2866    ;   format(Out, '>~n', []),
 2867        do_save_subject(Out, Value, BaseURI, SubIndent, Options)
 2868    ),
 2869    format(Out, '~N~*|</', [Indent]),
 2870    rdf_write_id(Out, NameText),
 2871    format(Out, '>~n', []).
 2872save_attribute(body, Name=Value, BaseURI, Out, Indent, _DB) :-
 2873    stream_property(Out, encoding(Encoding)),
 2874    rdf_value(Value, BaseURI, QVal, Encoding),
 2875    rdf_id(Name, BaseURI, NameText),
 2876    format(Out, '~N~*|<', [Indent]),
 2877    rdf_write_id(Out, NameText),
 2878    format(Out, ' rdf:resource="~w"/>', [QVal]).
 2879
 2880has_attributes(URI, Options) :-
 2881    graph(Options, DB),
 2882    rdf_db(URI, _, _, DB),
 2883    !.
 save_body_literal(+Literal, +NameText, +BaseURI, +Out, +Indent, +Options)
 2888save_body_literal(lang(Lang, Value),
 2889                  NameText, BaseURI, Out, Indent, Options) :-
 2890    !,
 2891    format(Out, '~N~*|<', [Indent]),
 2892    rdf_write_id(Out, NameText),
 2893    (   memberchk(document_language(Lang), Options)
 2894    ->  write(Out, '>')
 2895    ;   rdf_id(Lang, BaseURI, LangText),
 2896        format(Out, ' xml:lang="~w">', [LangText])
 2897    ),
 2898    save_attribute_value(Value, Out, Indent),
 2899    write(Out, '</'), rdf_write_id(Out, NameText), write(Out, '>').
 2900save_body_literal(type(Type, DOM),
 2901                  NameText, _BaseURI, Out, Indent, Options) :-
 2902    rdf_equal(Type, rdf:'XMLLiteral'),
 2903    !,
 2904    (   atom(DOM)
 2905    ->  format(Out, '~N~*|<', [Indent]),
 2906        rdf_write_id(Out, NameText),
 2907        format(Out, ' rdf:parseType="Literal">~w</', [DOM]),
 2908        rdf_write_id(Out, NameText), write(Out, '>')
 2909    ;   save_xml_literal(DOM, NameText, Out, Indent, Options)
 2910    ).
 2911save_body_literal(type(Type, Value),
 2912                  NameText, BaseURI, Out, Indent, _) :-
 2913    !,
 2914    format(Out, '~N~*|<', [Indent]),
 2915    rdf_write_id(Out, NameText),
 2916    stream_property(Out, encoding(Encoding)),
 2917    rdf_value(Type, BaseURI, QVal, Encoding),
 2918    format(Out, ' rdf:datatype="~w">', [QVal]),
 2919    save_attribute_value(Value, Out, Indent),
 2920    write(Out, '</'), rdf_write_id(Out, NameText), write(Out, '>').
 2921save_body_literal(Literal,
 2922                  NameText, _, Out, Indent, _) :-
 2923    atomic(Literal),
 2924    !,
 2925    format(Out, '~N~*|<', [Indent]),
 2926    rdf_write_id(Out, NameText),
 2927    write(Out, '>'),
 2928    save_attribute_value(Literal, Out, Indent),
 2929    write(Out, '</'), rdf_write_id(Out, NameText), write(Out, '>').
 2930save_body_literal(DOM,
 2931                  NameText, BaseURI, Out, Indent, Options) :-
 2932    rdf_equal(Type, rdf:'XMLLiteral'),
 2933    save_body_literal(type(Type, DOM),
 2934                      NameText, BaseURI, Out, Indent, Options).
 2935
 2936save_attribute_value(Value, Out, _) :-  % strings
 2937    atom(Value),
 2938    !,
 2939    stream_property(Out, encoding(Encoding)),
 2940    xml_quote_cdata(Value, QVal, Encoding),
 2941    write(Out, QVal).
 2942save_attribute_value(Value, Out, _) :-  % numbers
 2943    number(Value),
 2944    !,
 2945    writeq(Out, Value).             % quoted: preserve floats
 2946save_attribute_value(Value, _Out, _) :-
 2947    throw(error(save_attribute_value(Value), _)).
 save_xml_literal(+DOM, +Attr, +Out, +Indent, +Options) is det
Save an XMLLiteral value. We already emitted
<prop parseType="literal"

but not the terminating >. We need to establish the namespaces used in the DOM. The namespaces in the rdf document are in the nsmap-option of Options.

 2961save_xml_literal(DOM, Attr, Out, Indent, Options) :-
 2962    xml_is_dom(DOM),
 2963    !,
 2964    memberchk(nsmap(NsMap), Options),
 2965    id_to_atom(Attr, Atom),
 2966    xml_write(Out,
 2967              element(Atom, ['rdf:parseType'='Literal'], DOM),
 2968              [ header(false),
 2969                indent(Indent),
 2970                nsmap(NsMap)
 2971              ]).
 2972save_xml_literal(NoDOM, _, _, _, _) :-
 2973    must_be(xml_dom, NoDOM).
 2974
 2975id_to_atom(NS:Local, Atom) :-
 2976    !,
 2977    atomic_list_concat([NS,Local], :, Atom).
 2978id_to_atom(ID, ID).
 rdf_collection(+URI) is semidet
True if URI represents an RDF list that fits the RDF parseType=collection syntax. This means it is a linked list of bnode-cells with a rdf:first that is a resource, optionally a rdf:type that is an rdf:list and the list ends in an rdf:nil.
 2988:- rdf_meta
 2989    rdf_collection(r),
 2990    collection_p(r,r). 2991
 2992rdf_collection(rdf:nil) :- !.
 2993rdf_collection(Cell) :-
 2994    rdf_is_bnode(Cell),
 2995    findall(F, rdf(Cell, rdf:first, F), [_]),
 2996    findall(F, rdf(Cell, rdf:rest, F), [Rest]),
 2997    forall(rdf(Cell, P, V),
 2998           collection_p(P, V)),
 2999    rdf_collection(Rest).
 3000
 3001collection_p(rdf:first, V) :- atom(V).
 3002collection_p(rdf:rest, _).
 3003collection_p(rdf:type, rdf:'List').
 rdf_save_list(+Out, +List, +BaseURI, +Indent, +Options)
 3008rdf_save_list(_, List, _, _, _) :-
 3009    rdf_equal(List, rdf:nil),
 3010    !.
 3011rdf_save_list(Out, List, BaseURI, Indent, Options) :-
 3012    rdf_has(List, rdf:first, First),
 3013    (   rdf_is_bnode(First)
 3014    ->  nl(Out),
 3015        rdf_save_subject(Out, First, BaseURI, Indent, Options)
 3016    ;   stream_property(Out, encoding(Encoding)),
 3017        rdf_value(First, BaseURI, QVal, Encoding),
 3018        format(Out, '~N~*|<rdf:Description rdf:about="~w"/>',
 3019               [Indent, QVal])
 3020    ),
 3021    flag(rdf_db_saved_triples, X, X+3),
 3022    (   rdf_has(List, rdf:rest, List2),
 3023        \+ rdf_equal(List2, rdf:nil)
 3024    ->  rdf_save_list(Out, List2, BaseURI, Indent, Options)
 3025    ;   true
 3026    ).
 rdf_id(+Resource, +BaseURI, -NSLocal)
Generate a NS:Local name for Resource given the indicated default namespace. This call is used for elements.
 3034rdf_id(Id, BaseURI, Local) :-
 3035    assertion(atom(BaseURI)),
 3036    atom_concat(BaseURI, Local, Id),
 3037    sub_atom(Local, 0, 1, _, #),
 3038    !.
 3039rdf_id(Id, _, NS:Local) :-
 3040    iri_xml_namespace(Id, Full, Local),
 3041    ns(NS, Full),
 3042    !.
 3043rdf_id(Id, _, NS:Local) :-
 3044    ns(NS, Full),
 3045    Full \== '',
 3046    atom_concat(Full, Local, Id),
 3047    !.
 3048rdf_id(Id, _, Id).
 rdf_write_id(+Out, +NSLocal) is det
Write an identifier. We cannot use native write on it as both NS and Local can be operators.
 3056rdf_write_id(Out, NS:Local) :-
 3057    !,
 3058    format(Out, '~w:~w', [NS, Local]).
 3059rdf_write_id(Out, Atom) :-
 3060    write(Out, Atom).
 rdf_value(+Resource, +BaseURI, -Text, +Encoding)
According to "6.4 RDF URI References" of the RDF Syntax specification, a URI reference is UNICODE string not containing control sequences, represented as UTF-8 and then as escaped US-ASCII.
 3069rdf_value(Base, Base, '', _) :- !.
 3070rdf_value(V, Base, Text, Encoding) :-
 3071    atom_concat(Base, Local, V),
 3072    sub_atom(Local, 0, _, _, #),
 3073    !,
 3074    xml_quote_attribute(Local, Text, Encoding).
 3075rdf_value(V, _, Text, Encoding) :-
 3076    ns(NS, Full),
 3077    atom_concat(Full, Local, V),
 3078    xml_is_name(Local),
 3079    !,
 3080    xml_quote_attribute(Local, QLocal, Encoding),
 3081    atomic_list_concat(['&', NS, (';'), QLocal], Text).
 3082rdf_value(V, _, Q, Encoding) :-
 3083    xml_quote_attribute(V, Q, Encoding).
 3084
 3085
 3086                 /*******************************
 3087                 *       MATCH AND COMPARE      *
 3088                 *******************************/
 rdf_compare(-Dif, +Object1, +Object2) is det
Compare two object terms. Where SPARQL defines a partial ordering, we define a complete ordering of terms. The ordering is defines as:
 rdf_match_label(+How, +Pattern, +Label) is semidet
True if Label matches Pattern according to How. How is one of icase, substring, word, prefix or like. For backward compatibility, exact is a synonym for icase.
 3111                 /*******************************
 3112                 *      DEPRECATED MATERIAL     *
 3113                 *******************************/
 rdf_split_url(+Prefix, +Local, -URL) is det
rdf_split_url(-Prefix, -Local, +URL) is det
Split/join a URL. This functionality is moved to library(sgml).
deprecated
- Use iri_xml_namespace/3. Note that the argument order is iri_xml_namespace(+IRI, -Namespace, -Localname).
 3123rdf_split_url(Prefix, Local, URL) :-
 3124    atomic(URL),
 3125    !,
 3126    iri_xml_namespace(URL, Prefix, Local).
 3127rdf_split_url(Prefix, Local, URL) :-
 3128    atom_concat(Prefix, Local, URL).
 rdf_url_namespace(+URL, -Namespace)
Namespace is the namespace of URL.
deprecated
- Use iri_xml_namespace/2
 3136rdf_url_namespace(URL, Prefix) :-
 3137    iri_xml_namespace(URL, Prefix).
 3138
 3139
 3140                 /*******************************
 3141                 *            LITERALS          *
 3142                 *******************************/
 rdf_new_literal_map(-Map) is det
Create a new literal map, returning an opaque handle.
 rdf_destroy_literal_map(+Map) is det
Destroy a literal map. After this call, further use of the Map handle is illegal. Additional synchronisation is needed if maps that are shared between threads are destroyed to guarantee the handle is no longer used. In some scenarios rdf_reset_literal_map/1 provides a safe alternative.
 rdf_reset_literal_map(+Map) is det
Delete all content from the literal map.
 rdf_insert_literal_map(+Map, +Key, +Value) is det
Add a relation between Key and Value to the map. If this relation already exists no action is performed.
 rdf_insert_literal_map(+Map, +Key, +Value, -KeyCount) is det
As rdf_insert_literal_map/3. In addition, if Key is a new key in Map, unify KeyCount with the number of keys in Map. This serves two purposes. Derived maps, such as the stem and metaphone maps need to know about new keys and it avoids additional foreign calls for doing the progress in rdf_litindex.pl.
 rdf_delete_literal_map(+Map, +Key) is det
Delete Key and all associated values from the map.
 rdf_delete_literal_map(+Map, +Key, +Value) is det
Delete the association between Key and Value from the map.
 rdf_find_literal_map(+Map, +KeyList, -ValueList) is det
Unify ValueList with an ordered set of values associated to all keys from KeyList. Each key in KeyList is either an atom, an integer or a term not(Key). If not-terms are provided, there must be at least one positive keywords. The negations are tested after establishing the positive matches.
 rdf_keys_in_literal_map(+Map, +Spec, -Answer) is det
Realises various queries on the key-set:
 rdf_statistics_literal_map(+Map, -KeyValue)
Query some statistics of the map. Provides KeyValue are:
size(-Keys, -Relations)
Unify Keys with the total key-count of the index and Relation with the total Key-Value count.
 3230                 /*******************************
 3231                 *             MISC             *
 3232                 *******************************/
 rdf_version(-Version) is det
True when Version is the numerical version-id of this library. The version is computed as
Major*10000 + Minor*100 + Patch.
 rdf_set(+Term) is det
Set properties of the RDF store. Currently defines:
hash(+Hash, +Parameter, +Value)
Set properties for a triple index. Hash is one of s, p, sp, o, po, spo, g, sg or pg. Parameter is one of:
size
Value defines the number of entries in the hash-table. Value is rounded down to a power of 2. After setting the size explicitly, auto-sizing for this table is disabled. Setting the size smaller than the current size results in a permission_error exception.
average_chain_len
Set maximum average collision number for the hash.
optimize_threshold
Related to resizing hash-tables. If 0, all triples are moved to the new size by the garbage collector. If more then zero, those of the last Value resize steps remain at their current location. Leaving cells at their current location reduces memory fragmentation and slows down access.
 rdf_md5(+Graph, -MD5) is det
True when MD5 is the MD5 hash for all triples in graph. The MD5 digest itself is represented as an atom holding a 32-character hexadecimal string. The library maintains the digest incrementally on rdf_load/[1,2], rdf_load_db/1, rdf_assert/[3,4] and rdf_retractall/[3,4]. Checking whether the digest has changed since the last rdf_load/[1,2] call provides a practical means for checking whether the file needs to be saved.
deprecated
- New code should use rdf_graph_property(Graph, hash(Hash)).
 rdf_generation(-Generation) is det
True when Generation is the current generation of the database. Each modification to the database increments the generation. It can be used to check the validity of cached results deduced from the database. Committing a non-empty transaction increments the generation by one.

When inside a transaction, Generation is unified to a term TransactionStartGen + InsideTransactionGen. E.g., 4+3 means that the transaction was started at generation 4 of the global database and we have created 3 new generations inside the transaction. Note that this choice of representation allows for comparing generations using Prolog arithmetic. Comparing a generation in one transaction with a generation in another transaction is meaningless.

 rdf_estimate_complexity(?Subject, ?Predicate, ?Object, -Complexity)
Return the number of alternatives as indicated by the database internal hashed indexing. This is a rough measure for the number of alternatives we can expect for an rdf_has/3 call using the given three arguments. When called with three variables, the total number of triples is returned. This estimate is used in query optimisation. See also rdf_predicate_property/2 and rdf_statistics/1 for additional information to help optimizers.
 rdf_debug(+Level) is det
Set debugging to Level. Level is an integer 0..9. Default is 0 no debugging.
 rdf_atom_md5(+Text, +Times, -MD5) is det
Computes the MD5 hash from Text, which is an atom, string or list of character codes. Times is an integer >= 1. When > 0, the MD5 algorithm is repeated Times times on the generated hash. This can be used for password encryption algorithms to make generate-and-test loops slow.
deprecated
- Obviously, password hash primitives do not belong in this library. The library(crypto) from the \const{ssl} package provides extensive support for hashes. The \const{clib} package provides library(crypt) to access the OS (Unix) password hash implementation as well as lightweight implementations of several popular hashes.
 3329                 /*******************************
 3330                 *             MESSAGES         *
 3331                 *******************************/
 3332
 3333:- multifile
 3334    prolog:message//1. 3335
 3336prolog:message(rdf(Term)) -->
 3337    message(Term).
 3338
 3339message(loaded(How, What, BaseURI, Triples, Time)) -->
 3340    how(How),
 3341    source(What),
 3342    into(What, BaseURI),
 3343    in_time(Triples, Time).
 3344message(save_removed_duplicates(N, Subject)) -->
 3345    [ 'Removed ~d duplicate triples about "~p"'-[N,Subject] ].
 3346message(saved(File, SavedSubjects, SavedTriples)) -->
 3347    [ 'Saved ~D triples about ~D subjects into ~p'-
 3348      [SavedTriples, SavedSubjects, File]
 3349    ].
 3350message(using_namespace(Id, NS)) -->
 3351    [ 'Using namespace id ~w for ~w'-[Id, NS] ].
 3352message(inconsistent_cache(DB, Graphs)) -->
 3353    [ 'RDF cache file for ~w contains the following graphs'-[DB], nl,
 3354      '~t~8|~p'-[Graphs]
 3355    ].
 3356message(guess_format(Ext)) -->
 3357    [ 'Unknown file-extension: ~w.  Assuming RDF/XML'-[Ext] ].
 3358message(meta(not_expanded(G))) -->
 3359    [ 'rdf_meta/1: ~p is not expanded'-[G] ].
 3360message(deprecated(rdf_unload(Graph))) -->
 3361    [ 'rdf_unload/1: Use ~q'-[rdf_unload_graph(Graph)] ].
 3362
 3363
 3364how(load)   --> [ 'Loaded' ].
 3365how(parsed) --> [ 'Parsed' ].
 3366
 3367source(SourceURL) -->
 3368    { uri_file_name(SourceURL, File),
 3369      !,
 3370      file_base_name(File, Base)    % TBD: relative file?
 3371    },
 3372    [ ' "~w"'-[Base] ].
 3373source(SourceURL) -->
 3374    [ ' "~w"'-[SourceURL] ].
 3375
 3376into(_, _) --> [].                      % TBD
 3377
 3378in_time(Triples, ParseTime) -->
 3379    [ ' in ~2f sec; ~D triples'-[ParseTime, Triples]
 3380    ]