rdf11.pl -- RDF 1.1 API
This library provides a new API on top of library(semweb/rdf_db). The new API follows the RDF 1.1 terminology and notation as much as possible. It runs on top of the old API, which implies that applications can use the new API in one file and the other in another one. Once the new API is considered stable and robust the old API will be deprecated.
In a nutshell, the following issues are addressed:
- Literals are now represented by Value^^Type or Text@Lang. Plain
literals no longer exist. Value is a Prolog representation of
the value for known types. In particular:
- xsd:double, xsd:float and xsd:decimal are represented by a Prolog float
- Integer types are represented by a Prolog integer
- The date/time types are presented by Prolog terms
- Literal matching and comparison operations are represented as
Prolog constraints. This replaces the
literal(+Search,-Value)
construct used by library(semweb/rdf_db). For example, the following query returns literals with prefix "ams", exploiting the RDF literal index.{ prefix(Name, "ams") }, rdf(S,P,Name).
- Graphs are always identified by the graph name only, i.e., the notation Graph:Line is no longer supported. If a graph name is an IRI then RDF prefix notation can now be used.
- The enumeration and type-testing predicates are now more closely based on the RDF 1.1 specification and use consistent naming.
- rdf(?S, ?P, ?O) is nondet
- rdf(?S, ?P, ?O, ?G) is nondet
- True if an RDF triple <S,P,O> exists, optionally in the graph G.
The object O is either a resource (atom) or one of the terms
listed below. The described types apply for the case where O is
unbound. If O is instantiated it is converted according to the
rules described with rdf_assert/3.
Triples consist of the following three terms:
- Blank nodes are encoded by atoms that start with `_:`.
- IRIs appear in two notations:
- Full IRIs are encoded by atoms that do not start with `_:`. Specifically, an IRI term is not required to follow the IRI standard grammar.
- Abbreviated IRI notation that allows IRI prefix aliases
that are registered by rdf_register_prefix/[2,3] to be
used. Their notation is
Alias:Local
, where Alias and Local are atoms. Each abbreviated IRI is expanded by the system to a full IRI.
- Literals appear in two notations:
- @(String, Lang)
- A language-tagged string, where String is a Prolog string and Lang is an atom.
- ^^(Value, Type)
- A type qualified literal. For unknown types, Value is a
Prolog string. If type is known, the Prolog representations
from the table below are used.
Datatype IRI Prolog term xsd:float float xsd:double float xsd:decimal float (1) xsd:integer integer XSD integer sub-types integer xsd:boolean true
orfalse
xsd:date date(Y,M,D)
xsd:dateTime date_time(Y,M,D,HH,MM,SS)
(2,3)xsd:gDay integer xsd:gMonth integer xsd:gMonthDay month_day(M,D)
xsd:gYear integer xsd:gYearMonth year_month(Y,M)
xsd:time time(HH,MM,SS)
(2)
Notes:
(1) The current implementation of
xsd:decimal
values as floats is formally incorrect. Future versions of SWI-Prolog may introduce decimal as a subtype of rational.(2) SS fields denote the number of seconds. This can either be an integer or a float.
(3) The
date_time
structure can have a 7th field that denotes the timezone offset in seconds as an integer.In addition, a ground object value is translated into a properly typed RDF literal using rdf_canonical_literal/2.
There is a fine distinction in how duplicate statements are handled in rdf/[3,4]: backtracking over rdf/3 will never return duplicate triples that appear in multiple graphs. rdf/4 will return such duplicate triples, because their graph term differs.
- rdf_has(?S, +P, ?O) is nondet
- rdf_has(?S, +P, ?O, -RealP) is nondet
- Similar to rdf/3 and rdf/4, but P matches all predicates that
are defined as an rdfs:subPropertyOf of P. This predicate also
recognises the predicate properties
inverse_of
andsymmetric
. See rdf_set_predicate/2. - rdf_update(+S, +P, +O, ++Action) is det
- rdf_update(+S, +P, +O, +G, ++Action) is det
- Replaces one of the three (four) 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. This only works with rdf_update/5 and throws an error when used with rdf_update/4.
The argument matching Action must be ground. If this argument is equivalent to the current value, no action is performed. Otherwise, the requested action is performed on all matching triples. For example, all resources typed
rdfs:Class
can be changed toowl:Class
using?- rdf_update(_, rdf:type, rdfs:'Class', object(owl:'Class')).
- rdf_reachable(?S, +P, ?O) is nondet
- rdf_reachable(?S, +P, ?O, +MaxD, -D) is nondet
- True when O can be reached from S using the transitive closure
of P. The predicate uses (the internals of) rdf_has/3 and thus
matches both rdfs:subPropertyOf and the
inverse_of
andsymmetric
predicate properties. The version rdf_reachable/5 maximizes the steps considered and returns the number of steps taken.If both S and O are given, these predicates are
semidet
. The number of steps D is minimal because the implementation uses breadth first search. - rdf_assert(+S, +P, +O) is det
- rdf_assert(+S, +P, +O, +G) is det
- Assert a new triple. If O is a literal, certain Prolog terms are
translated to typed RDF literals. These conversions are
described with rdf_canonical_literal/2.
If a type is provided using Value^^Type syntax, additional conversions are performed. All types accept either an atom or Prolog string holding a valid RDF lexical value for the type and xsd:float and xsd:double accept a Prolog integer.
- rdf_retractall(?S, ?P, ?O) is nondet
- rdf_retractall(?S, ?P, ?O, ?G) is nondet
- Remove all matching triples from the database. Matching is performed using the same rules as rdf/3. The call does not instantiate any of its arguments.
- rdf_compare(-Diff, +Left, +Right) is det
- True if the RDF terms Left and Right are ordered according to
the comparison operator Diff. The ordering is defines as:
- Literal < BNode < IRI
- For literals
- Numeric < non-numeric
- Numeric literals are ordered by value. If both are equal, floats are ordered before integers.
- Other data types are ordered lexicographically.
- BNodes and IRIs are ordered lexicographically.
Note that this ordering is a complete ordering of RDF terms that is consistent with the partial ordering defined by SPARQL.
- {+Where} is semidet
- rdf_where(+Where) is semidet
- Formulate constraints on RDF terms, notably literals. These are
intended to be used as illustrated below. RDF constraints are
pure: they may be placed before, after or inside a graph pattern
and, provided the code contains no commit operations (!, ->),
the semantics of the goal remains the same. Preferably,
constraints are placed before the graph pattern as they often
help the RDF database to exploit its literal indexes. In the
example below, the database can choose between using the subject
and/or predicate hash or the ordered literal table.
{ Date >= "2000-01-01"^^xsd:date }, rdf(S, P, Date)
The following constraints are currently defined:
- >,>=,==,=<,<
- The comparison operators are defined between numbers (of any recognised type), typed literals of the same type and langStrings of the same language.
- prefix(String, Pattern)
- substring(String, Pattern)
- word(String, Pattern)
- like(String, Pattern)
- icase(String, Pattern)
- Text matching operators that act on both typed literals and langStrings.
- lang_matches(Term, Pattern)
- Demands a full RDF term (Text@Lang) or a plain Lang term to match the language pattern Pattern.
The predicates rdf_where/1 and {}/1 are identical. The rdf_where/1 variant is provided to avoid ambiguity in applications where {}/1 is used for other purposes. Note that it is also possible to write
rdf11:{...}
. - rdf_default_graph(-Graph) is det
- rdf_default_graph(-Old, +New) is det
- Query/set the notion of the default graph. The notion of the default graph is local to a thread. Threads created inherit the default graph from their creator. See set_prolog_flag/2.
- rdf_canonical_literal(++In, -Literal) is det
- Transform a relaxed literal specification as allowed for
rdf_assert/3 into its canonical form. The following Prolog terms
are translated:
Prolog Term Datatype IRI float xsd:double integer xsd:integer string xsd:string true
orfalse
xsd:boolean date(Y,M,D)
xsd:date date_time(Y,M,D,HH,MM,SS)
xsd:dateTime date_time(Y,M,D,HH,MM,SS,TZ)
xsd:dateTime month_day(M,D)
xsd:gMonthDay year_month(Y,M)
xsd:gYearMonth time(HH,MM,SS)
xsd:time For example:
?- rdf_canonical_literal(42, X). X = 42^^'http://www.w3.org/2001/XMLSchema#integer'.
- rdf_lexical_form(++Literal, -Lexical:compound) is det
- True when Lexical is the lexical form for the literal Literal.
Lexical is of one of the forms below. The ntriples serialization
is obtained by transforming String into a proper ntriples string
using double quotes and escaping where needed and turning Type
into a proper IRI reference.
- String^^Type
- String@Lang
- invalid_lexical_form_hook(+Type, +Lexical, -Prolog)[multifile]
- This hook is called if translation of the lexical form to the Prolog representation fails due to a syntax error. By default it is not defined, causing such invalid triples to be silently ignored.
- rdf_term(?Term) is nondet
- True if Term appears in the RDF database. Term is either an IRI, literal or blank node and may appear in any position of any triple. If Term is ground, it is pre-processed as the object argument of rdf_assert/3 and the predicate is semidet.
- rdf_literal(?Term) is nondet
- True if Term is a known literal. If Term is ground, it is pre-processed as the object argument of rdf_assert/3 and the predicate is semidet.
- rdf_bnode(?BNode) is nondet
- True if BNode is a currently known blank node. The predicate is semidet if BNode is ground.
- rdf_iri(?IRI) is nondet
- True if IRI is a current IRI. The predicate is semidet if IRI is ground.
- rdf_name(?Name) is nondet
- True if Name is a current IRI or literal. The predicate is semidet if Name is ground.
- rdf_subject(?S) is nondet
- True when S is a currently known subject, i.e. it appears in the subject position of some visible triple. The predicate is semidet if S is ground.
- rdf_predicate(?P) is nondet
- True when P is a currently known predicate, i.e. it appears in the predicate position of some visible triple. The predicate is semidet if P is ground.
- rdf_object(?O) is nondet
- True when O is a currently known object, i.e. it appears in the object position of some visible triple. If Term is ground, it is pre-processed as the object argument of rdf_assert/3 and the predicate is semidet.
- rdf_node(?T) is nondet
- True when T appears in the subject or object position of a known triple, i.e., is a node in the RDF graph.
- rdf_create_bnode(--BNode)
- Create a new BNode. A blank node is an atom starting with
_:
. Blank nodes generated by this predicate are of the form_:genid
followed by a unique integer. - rdf_is_iri(@IRI) is semidet
- True if IRI is an RDF IRI term.
For performance reasons, this does not check for compliance to the syntax defined in RFC 3987. This checks whether the term is (1) an atom and (2) not a blank node identifier.
Success of this goal does not imply that the IRI is present in the database (see rdf_iri/1 for that).
- rdf_is_bnode(@Term) is semidet
- True if Term is an RDF blank node identifier.
A blank node is represented by an atom that starts with
_:
.Success of this goal does not imply that the blank node is present in the database (see rdf_bnode/1 for that).
For backwards compatibility, atoms that are represented with an atom that starts with
__
are also considered to be a blank node. - rdf_is_literal(@Term) is semidet
- True if Term is an RDF literal term.
An RDF literal term is of the form `String@LanguageTag
or
Value^^Datatype`.Success of this goal does not imply that the literal is well-formed or that it is present in the database (see rdf_literal/1 for that).
- rdf_is_name(@Term) is semidet
- True if Term is an RDF Name, i.e., an IRI or literal.
Success of this goal does not imply that the name is well-formed or that it is present in the database (see rdf_name/1 for that).
- rdf_is_object(@Term) is semidet
- True if Term can appear in the object position of a triple.
Success of this goal does not imply that the object term in well-formed or that it is present in the database (see rdf_object/1 for that).
Since any RDF term can appear in the object position, this is equaivalent to rdf_is_term/1.
- rdf_is_predicate(@Term) is semidet
- True if Term can appear in the predicate position of a triple.
Success of this goal does not imply that the predicate term is present in the database (see rdf_predicate/1 for that).
Since only IRIs can appear in the predicate position, this is equivalent to rdf_is_iri/1.
- rdf_is_subject(@Term) is semidet
- True if Term can appear in the subject position of a triple.
Only blank nodes and IRIs can appear in the subject position.
Success of this goal does not imply that the subject term is present in the database (see rdf_subject/1 for that).
Since blank nodes are represented by atoms that start with `_:` and an IRIs are atoms as well, this is equivalent to
atom(Term)
. - rdf_is_term(@Term) is semidet
- True if Term can be used as an RDF term, i.e., if Term is
either an IRI, a blank node or an RDF literal.
Success of this goal does not imply that the RDF term is present in the database (see rdf_term/1 for that).
- rdf_list(?RDFTerm) is semidet
- True if RDFTerm is a proper RDF list. This implies that every
node in the list has an
rdf:first
andrdf:rest
property and the list ends inrdf:nil
.If RDFTerm is unbound, RDFTerm is bound to each maximal RDF list. An RDF list is maximal if there is no triple
rdf(_, rdf:rest, RDFList)
. - rdf_list(+RDFList, -PrologList) is det
- True when PrologList represents the rdf:first objects for all cells in RDFList. Note that this can be non-deterministic if cells have multiple rdf:first or rdf:rest triples.
- rdf_length(+RDFList, -Length:nonneg) is nondet
- True when Length is the number of cells in RDFList. Note that a list cell may have multiple rdf:rest triples, which makes this predicate non-deterministic. This predicate does not check whether the list cells have associated values (rdf:first). The list must end in rdf:nil.
- rdf_member(?Member, +RDFList) is nondet
- True when Member is a member of RDFList
- rdf_nextto(?X, ?Y) is nondet
- rdf_nextto(?X, ?Y, ?RdfList) is nondet
- True if Y directly follows X in RdfList.
- rdf_nth0(?Index, +RDFList, ?X) is nondet
- rdf_nth1(?Index, +RDFList, ?X) is nondet
- True when X is the Index-th element (0-based or 1-based) of RDFList. This predicate is deterministic if Index is given and the list has no multiple rdf:first or rdf:rest values.
- rdf_last(+RDFList, -Last) is det
- True when Last is the last element of RDFList. Note that if the last cell has multiple rdf:first triples, this predicate becomes nondet.
- rdf_estimate_complexity(?S, ?P, ?O, -Estimate) is det
- rdf_assert_list(+PrologList, ?RDFList) is det
- rdf_assert_list(+PrologList, ?RDFList, +Graph) is det
- Create an RDF list from the given Prolog List. PrologList must be a proper Prolog list and all members of the list must be acceptable as object for rdf_assert/3. If RDFList is unbound and PrologList is not empty, rdf_create_bnode/1 is used to create RDFList.
- rdf_retract_list(+RDFList) is det
- Retract the rdf:first, rdf:rest and rdf:type=rdf:'List' triples from all nodes reachable through rdf:rest. Note that other triples that exist on the nodes are left untouched.
Re-exported predicates
The following predicates are re-exported from other modules
- 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_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.
- rdf_transaction(:Goal, +Id) is semidet
- Same as
rdf_transaction(Goal, Id, [])
. See rdf_transaction/3. - rdf_current_ns(:Prefix, ?URI) is nondet
- rdf_current_snapshot(?Term) is nondet
- True when Term is a currently known snapshot.
- 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
(defaulttrue
) 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
(defaultfalse
), inline resources when encountered for the first time. Normally, only bnodes are handled this way. - namespaces(+List)
- Explicitly specify saved namespace declarations. See rdf_save_header/2 option namespaces for details.
- sorted(+Boolean)
- If
true
(defaultfalse
), emit subjects sorted on the full URI. Useful to make file comparison easier. - write_xml_base(Bool)
- If
false
, do not include thexml:base
declaration that is written normally when using thebase_uri
option. - xml_attributes(+Bool)
- If
false
(defaulttrue
), never use xml attributes to save plain literal attributes, i.e., always used an XML element as in<name>Joe</name>
.
- rdf_load_db(+File) is det
- Load triples from a file created using rdf_save_db/2.
- rdf_warm_indexes
- Warm all indexes. See rdf_warm_indexes/1.
- rdf_create_graph(+Graph) is det
- Create an RDF graph without triples. Succeeds silently if the graph already exists.
- rdf_register_prefix(+Prefix, +URI) is det
- rdf_register_prefix(+Prefix, +URI, +Options) is det
- Register Prefix as an abbreviation for URI. Options:
- force(Boolean)
- If
true
, replace existing namespace alias. Please note that replacing a namespace is dangerous as namespaces affect preprocessing. Make sure all code that depends on a namespace is compiled after changing the registration. - keep(Boolean)
- If
true
and Alias is already defined, keep the original binding for Prefix and succeed silently.
Without options, an attempt to redefine an alias raises a permission error.
Predefined prefixes are:
- rdf_monitor(:Goal, +Options)
- Call Goal if specified actions occur on the database.
- rdf_register_ns(:Prefix, ?URI) is det
- rdf_register_ns(:Prefix, ?URI, +Options) is det
- Register an RDF prefix.
- 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_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
rdf_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.
- rdf_current_prefix(:Alias, ?URI) is nondet
- Query predefined prefixes and prefixes defined with
rdf_register_prefix/2 and local prefixes defined with
rdf_prefix/2. If Alias is unbound and one URI is the prefix of
another, the longest is returned first. This allows turning a
resource into a prefix/local couple using the simple enumeration
below. See rdf_global_id/2.
rdf_current_prefix(Prefix, Expansion), atom_concat(Expansion, Local, URI),
- rdf_save_footer(Out:stream) is det
- Finish XML generation and write the document footer.
- rdf_meta(+Heads)
- This directive defines the argument types of the named
predicates, which will force compile time namespace expansion
for these predicates. Heads is a coma-separated list of callable
terms. Defined argument properties are:
- :
- Argument is a goal. The goal is processed using expand_goal/2, recursively applying goal transformation on the argument.
- +
- The argument is instantiated at entry. Nothing is changed.
- -
- The argument is not instantiated at entry. Nothing is changed.
- ?
- The argument is unbound or instantiated at entry. Nothing is changed.
- @
- The argument is not changed.
- r
- The argument must be a resource. If it is a term prefix:local it is translated.
- o
- The argument is an object or resource. See rdf_global_object/2.
- t
- The argument is a term that must be translated. Expansion will translate all occurences of prefix:local appearing anywhere in the term. See rdf_global_term/2.
As it is subject to term_expansion/2, the rdf_meta/1 declaration can only be used as a directive. The directive must be processed before the definition of the predicates as well as before compiling code that uses the rdf meta-predicates. The atom
rdf_meta
is declared as an operator exported from library(semweb/rdf_db). Files using rdf_meta/1 must explicitely load this library.Beginning with SWI-Prolog 7.3.17, the low-level RDF interface (rdf/3, rdf_assert/3, etc.) perform runtime expansion of
Prefix:Local
terms. This eliminates the need for rdf_meta/1 for simple cases. However, runtime expansion comes at a significant overhead and having two representations for IRIs (a plain atom and a termPrefix:Local
) implies that simple operations such as comparison of IRIs no longer map to native Prolog operations such asIRI1 == IRI2
. - rdf_transaction(:Goal, +Id, +Options) is semidet
- Run Goal in an RDF transaction. Compared to the ACID model,
RDF transactions have the following properties:
- 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.
- Consistency is not guaranteed. Later versions may implement consistency constraints that will be checked serialized just before the actual commit of a transaction.
- Concurrently executing transactions do not infuence each other. I.e., the isolation property is fully supported.
- 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.
- rdf_graph(?Graph) is nondet
- True when Graph is an existing graph.
- rdf_prefix(:Alias, +URI) is det
- Register a local prefix. This declaration takes precedence over globally defined prefixes using rdf_register_prefix/2,3. Module local prefixes are notably required to deal with SWISH, where users need to be able to have independent namespace declarations.
- rdf_source(?Source)
- True if Source is a loaded source.
- rdf_register_ns(:Prefix, ?URI) is det
- rdf_register_ns(:Prefix, ?URI, +Options) is det
- Register an RDF prefix.
- rdf_reset_db
- Remove all triples from the RDF database and reset all its statistics.
- rdf_source_location(+Subject, -Location) is nondet
- True when triples for Subject are loaded from Location.
- 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) ornot_loaded
. - modified(-Modified)
- Unify Modified with one of
not_modified
,cached(File)
,last_modified(Stamp)
orunknown
. - cache(Bool)
- If
false
, do not use or create a cache file. - register_namespaces(Bool)
- If
true
(defaultfalse
), registerxmlns
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 levelsilent
. Otherwise the level isinformational
. See also print_message/2. - prefixes(-Prefixes)
- Returns the prefixes defined in the source data file as a list of pairs.
- multifile +Boolean
- Indicate that the addressed graph may be populated with triples from multiple sources. This disables caching and avoids that an rdf_load/2 call affecting the specified graph cleans the graph.
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)).
- rdf_register_prefix(+Prefix, +URI) is det
- rdf_register_prefix(+Prefix, +URI, +Options) is det
- Register Prefix as an abbreviation for URI. Options:
- force(Boolean)
- If
true
, replace existing namespace alias. Please note that replacing a namespace is dangerous as namespaces affect preprocessing. Make sure all code that depends on a namespace is compiled after changing the registration. - keep(Boolean)
- If
true
and Alias is already defined, keep the original binding for Prefix and succeed silently.
Without options, an attempt to redefine an alias raises a permission error.
Predefined prefixes are:
- rdf_equal(?Resource1, ?Resource2)
- Simple equality test to exploit goal-expansion.
- 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_debug(+Level) is det
- Set debugging to Level. Level is an integer 0..9. Default is 0 no debugging.
- rdf_set_graph(+Graph, +Property) is det
- Set properties of Graph. Defined properties are:
- modified(false)
- Set the modified state of Graph to false.
- rdf_keys_in_literal_map(+Map, +Spec, -Answer) is det
- Realises various queries on the key-set:
- all Unify Answer with an ordered list of all keys.
key(+Key)
Succeeds if Key is a key in the map and unify Answer with the number of values associated with the key. This provides a fast test of existence without fetching the possibly large associated value set as with rdf_find_literal_map/3.prefix(+Prefix)
Unify Answer with an ordered set of all keys that have the given prefix. See section 3.1 for details on prefix matching. Prefix must be an atom. This call is intended for auto-completion in user interfaces.ge(+Min)
Unify Answer with all keys that are larger or equal to the integer Min.le(+Max)
Unify Answer with all keys that are smaller or equal to the integer Max.between(+Min, +Max)
Unify Answer with all keys between Min and Max (including).
- 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_new_literal_map(-Map) is det
- Create a new literal map, returning an opaque handle.
- 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.
- 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.
- 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 wildcard *. Identifiers are matched case-insensitive and a * matches any number of identifiers. A short pattern is the same as *.
- 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.
- rdf_delete_literal_map(+Map, +Key, +Value) is det
- Delete the association between Key and Value from the map.
- 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.
- 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.
- rdf_load(+FileOrList) is det
- Same as
rdf_load(FileOrList, [])
. See rdf_load/2. - lang_equal(+Lang1, +Lang2) is semidet
- True if two RFC language specifiers denote the same language
- 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
ortype
. 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.
- 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)
- rdf_transaction(:Goal) is semidet
- Same as
rdf_transaction(Goal, user, [])
. See rdf_transaction/3. - rdf_source(?Graph, ?SourceURL) is nondet
- True if named Graph is loaded from SourceURL.
- rdf_url_namespace(+URL, -Namespace)
- Namespace is the namespace of URL.
- rdf_member_property(?Prop, ?Index)
- Deal with the rdf:_1, ... properties.
- 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
ortype
. 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.
- 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_gc is det
- Run the RDF-DB garbage collector until no garbage is left and all
tables are fully optimized. Under normal operation a separate 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.
- 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 asinverse_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. Thesymmetric
andinverse_of
properties are considered by rdf_has/3,4 and rdf_reachable/3. - 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).
- rdf_global_object(+Object, :GlobalObject) is semidet
- rdf_global_object(-Object, :GlobalObject) is semidet
- Same as rdf_global_id/2, but intended for dealing with the object part of a triple, in particular the type for typed literals. Note that the predicate is a meta-predicate on the output argument. This is necessary to get the module context while the first argument may be of the form (:)/2.
- 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.
- rdf_save(+Out) is det
- Same as
rdf_save(Out, [])
. See rdf_save/2 for details. - 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.
- 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
orpg
. 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_make
- Reload all loaded files that have been modified since the last time they were loaded.
- rdf_match_label(+How, +Pattern, +Label) is semidet
- True if Label matches Pattern according to How. How is one of
icase
,substring
,word
,prefix
orlike
. For backward compatibility,exact
is a synonym foricase
. - rdf_global_id(?IRISpec, :IRI) is semidet
- Convert between Prefix:Local and full IRI (an atom). If IRISpec is
an atom, it is simply unified with IRI. This predicate fails
silently if IRI is an RDF literal.
Note that this predicate is a meta-predicate on its output argument. This is necessary to get the module context while the first argument may be of the form (:)/2. The above mode description is correct, but should be interpreted as (?,?).
- rdf_unregister_prefix(+Alias) is det
- Delete a prefix global registration.
- 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
andrdfs
are added to the provided List. If a namespace is not declared, the resource is emitted in non-abreviated form.
- 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_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.
- 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_unload(+Source) is det
- Identify the graph loaded from Source and use rdf_unload_graph/1 to erase this graph.
- rdf_delete_literal_map(+Map, +Key) is det
- Delete Key and all associated values from the map.
- rdf_reset_literal_map(+Map) is det
- Delete all content from the literal map.
- 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_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.
- 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 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:
- library(semweb/rdf_persistency)
- persistent(Boolean)
- Boolean is
true
if the graph is persistent.
- rdf_global_term(+TermIn, :GlobalTerm) is det
- Performs rdf_global_id/2 on predixed IRIs and rdf_global_object/2 on
RDF literals, by recursively analysing the term. Note that the
predicate is a meta-predicate on the output argument. This is
necessary to get the module context while the first argument may be
of the form (:)/2.
Terms of the form
Prefix:Local
that appear in TermIn for which Prefix is not defined are not replaced. Unlike rdf_global_id/2 and rdf_global_object/2, no error is raised. - rdf_unload_graph(+Graph) is det
- Remove Graph from the RDF store. Succeeds silently if the named graph does not exist.
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
- rdf_assert(Arg1, Arg2, Arg3, Arg4)
- rdf_update(Arg1, Arg2, Arg3, Arg4, Arg5)
- rdf_assert_list(Arg1, Arg2, Arg3)
- rdf_default_graph(Arg1, Arg2)
- rdf_retractall(Arg1, Arg2, Arg3, Arg4)
- rdf_has(Arg1, Arg2, Arg3, Arg4)
- rdf(Arg1, Arg2, Arg3, Arg4)
- rdf_reachable(Arg1, Arg2, Arg3, Arg4, Arg5)
- rdf_where(Arg1)
- rdf_nextto(Arg1, Arg2, Arg3)
- rdf_nth1(Arg1, Arg2, Arg3)