3.1 library(semweb/rdf11): The RDF database
The library(semweb/rdf11)
provides a new interface to
the SWI-Prolog RDF database based on the RDF 1.1 specification.
3.1.1 Query the RDF database
- [nondet]rdf(?S, ?P, ?O)
- [nondet]rdf(?S, ?P, ?O, ?G)
- 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.
S is the subject term. It is either a blank node or IRI. P is the predicate term. It is always an IRI. O is the object term. It is either a literal, a blank node or IRI (except for true
andfalse
that denote the values of datatype XSD boolean).G is the graph term. It is always an IRI. - See also
- - Triple
pattern querying
- xsd_number_string/2 and xsd_time_string/3 are used to convert between lexical representations and Prolog terms.
- [nondet]rdf_has(?S, +P, ?O)
- [nondet]rdf_has(?S, +P, ?O, -RealP)
- 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. - [nondet]rdf_reachable(?S, +P, ?O)
- [nondet]rdf_reachable(?S, +P, ?O, +MaxD, -D)
- 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.
Constraints on literal values
- [semidet]{}(+Where)
- [semidet]rdf_where(+Where)
- 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:{...}
.
3.1.2 Enumerating and testing objects
Enumerating objects by role
- [nondet]rdf_subject(?S)
- 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.
- [nondet]rdf_predicate(?P)
- 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.
- [nondet]rdf_object(?O)
- 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.
- [nondet]rdf_node(?T)
- True when T appears in the subject or object position of a known triple, i.e., is a node in the RDF graph.
- [nondet]rdf_graph(?Graph)
- True when Graph is an existing graph.
Enumerating objects by type
- [nondet]rdf_literal(?Term)
- 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.
- [nondet]rdf_bnode(?BNode)
- True if BNode is a currently known blank node. The predicate is semidet if BNode is ground.
- [nondet]rdf_iri(?IRI)
- True if IRI is a current IRI. The predicate is semidet if IRI is ground.
- [nondet]rdf_name(?Name)
- True if Name is a current IRI or literal. The predicate is semidet if Name is ground.
- [nondet]rdf_term(?Term)
- 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.
Testing objects types
- [semidet]rdf_is_iri(@IRI)
- 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).
- [semidet]rdf_is_bnode(@Term)
- 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. - [semidet]rdf_is_literal(@Term)
- True if Term is an RDF literal term.
An RDF literal term is of the form
String@LanguageTag
orValue^^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).
- [semidet]rdf_is_name(@Term)
- 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).
- [semidet]rdf_is_object(@Term)
- 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.
- [semidet]rdf_is_predicate(@Term)
- 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.
- [semidet]rdf_is_subject(@Term)
- 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)
. - [semidet]rdf_is_term(@Term)
- 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).
3.1.3 RDF literals
- [det]rdf_canonical_literal(++In, -Literal)
- 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'.
- [det]rdf_lexical_form(++Literal, -Lexical:compound)
- 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
- String
- [det]rdf_compare(-Diff, +Left, +Right)
- 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.
Diff is one of <
,=
or>
3.1.4 Accessing RDF graphs
- [det]rdf_default_graph(-Graph)
- [det]rdf_default_graph(-Old, +New)
- 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.
3.1.5 Modifying the RDF store
- [det]rdf_assert(+S, +P, +O)
- [det]rdf_assert(+S, +P, +O, +G)
- 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. - [nondet]rdf_retractall(?S, ?P, ?O)
- [nondet]rdf_retractall(?S, ?P, ?O, ?G)
- 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_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.
3.1.6 Accessing RDF collections
The following predicates are utilities to access RDF 1.1 collections.
A collection is a linked list created from rdf:first
and rdf:next
triples, ending in rdf:nil
.
- [det]rdf_last(+RDFList, -Last)
- True when Last is the last element of RDFList. Note that if the last cell has multiple rdf:first triples, this predicate becomes nondet.
- [semidet]rdf_list(?RDFTerm)
- 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)
. - [det]rdf_list(+RDFList, -PrologList)
- 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.
- [nondet]rdf_length(+RDFList, -Length:nonneg)
- 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.
- [nondet]rdf_member(?Member, +RDFList)
- True when Member is a member of RDFList
- [nondet]rdf_nth0(?Index, +RDFList, ?X)
- [nondet]rdf_nth1(?Index, +RDFList, ?X)
- 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.
- [det]rdf_assert_list(+PrologList, ?RDFList)
- [det]rdf_assert_list(+PrologList, ?RDFList, +Graph)
- 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.
- [det]rdf_retract_list(+RDFList)
- 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.