- Documentation
- Reference manual
- Packages
- SWI-Prolog Semantic Web Library 3.0
- Two RDF APIs
- library(semweb/rdf_db): The RDF database
- Query the RDF database
- Enumerating objects
- Modifying the RDF database
- Update view, transactions and snapshots
- Type checking predicates
- Loading and saving to file
- Graph manipulation
- Literal matching and indexing
- Predicate properties
- Prefix Handling
- Miscellaneous predicates
- Memory management considerations
- library(semweb/rdf_db): The RDF database
- Two RDF APIs
- SWI-Prolog Semantic Web Library 3.0
3.3.1 Query the RDF database
- [nondet]rdf(?Subject, ?Predicate, ?Object)
- 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 than Literal in the ordered set of literals.
- gt(+Literal)
- Match any literal that is larger than 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 than Literal in the ordered set of literals.
- lt(+Literal)
- Match any literal that is smaller than 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 likerdf(S,P,O)
,rdf(O,P2,O2)
to proceed without errors. - [nondet]rdf(?Subject, ?Predicate, ?Object, ?Source)
- 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.
Source is a term Graph:Line. If Source is instatiated, passing an atom is the same as passing Atom:_. - [nondet]rdf_has(?Subject, +Predicate, ?Object)
- 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 theinverse_of
property. - [nondet]rdf_has(?Subject, +Predicate, ?Object, -RealPredicate)
- 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)
. - [nondet]rdf_reachable(?Subject, +Predicate, ?Object)
- Is true if Object can be reached from Subject
following the transitive predicate Predicate or a
sub-property thereof, while repecting the
symetric(true)
orinverse_of(P2)
properties.If used with either Subject or Object unbound, it first returns the origin, followed by the reachable nodes in breadth-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' ; ...
- [nondet]rdf_reachable(?Subject, +Predicate, ?Object, +MaxD, -D)
- 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.