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 breath 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:dateTime }, 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:{...}
.