franz.openrdf.query package

Submodules

franz.openrdf.query.dataset module

class franz.openrdf.query.dataset.Dataset(contexts=None)[source]

Bases: object

Records a set of default and named graphs that can restrict the scope of a query.

__init__(contexts=None)[source]
addDefaultGraph(uri)[source]
addNamedGraph(uri)[source]
asQuery(excludeNullContext)[source]
clear()[source]
getDefaultGraphs()[source]
getNamedGraphs()[source]
removeDefaultGraph(uri)[source]
removeNamedGraph(uri)[source]

franz.openrdf.query.query module

class franz.openrdf.query.query.BooleanQuery(queryLanguage=QueryLanguage.SPARQL, query=None, baseURI=None, queryString=None)[source]

Bases: franz.openrdf.query.query.Query

A query that returns a boolean.

evaluate()[source]

Execute the embedded query against the RDF store.

Returns:A boolean.
Return type:bool
class franz.openrdf.query.query.GraphQuery(queryLanguage=QueryLanguage.SPARQL, query=None, baseURI=None, queryString=None)[source]

Bases: franz.openrdf.query.query.Query

A query that returns statements (see Statement).

evaluate(output=None, output_format=RDFFormat.TABLE: Table)[source]

Execute the embedded query against the RDF store.

Parameters:
  • output (str|file|int|bool) – A file name, file descriptor or a file-like object to save the results to (optional). True can be used as a synonym for stdout.
  • output_format (RDFFormat) – Serialization format for output. The default is TABLE.
Returns:

An iterator over statements or None (if output is used)..

Return type:

GraphQueryResult

class franz.openrdf.query.query.Query(queryLanguage=QueryLanguage.SPARQL, query=None, baseURI=None, queryString=None)[source]

Bases: object

A query on a {@link Repository} that can be formulated in one of the supported query languages (for example SeRQL or SPARQL). It allows one to predefine bindings in the query to be able to reuse the same query with different bindings.

__init__(queryLanguage=QueryLanguage.SPARQL, query=None, baseURI=None, queryString=None)[source]

Initialize a query object.

Parameters:
  • queryLanguage (QueryLanguage) – Query syntax - the default is SPARQL.
  • query (string) – Query text
  • baseURI (URI) – Prefix used to relsove relative URIs. Default: chosen by the server.
  • queryString (string) – Legacy name of the ‘query’ parameter.
evaluate_generic_query(count=False, accept=None, callback=None, analyze=False, analysisTechnique=None, analysisTimeout=None, update=False)[source]

Evaluate a SPARQL or PROLOG query, which may be a ‘select’, ‘construct’, ‘describe’ or ‘ask’ query (in the SPARQL case). Return an appropriate response.

Parameters:
  • count (bool) – If True return only the count of the query results.
  • accept (string) – Set to 'application/sparql-results+xml' or 'application/sparql-results+json' to retrieve the result as a string in the specified format. The default is to return a QueryResult object.
  • analyze (bool) – If True perform query analysis for SPARQL queries.
  • analysisTechnique (string) – Control the method of analysis. Can be either “executed” (default). meaning that the query will be executed to perform dynamic analysis, or "static".
  • analysisTimeout (float) – Number of second to run the query if executed for analysis.
  • update (bool) – If True this is an update query and the result will be a boolean.
Returns:

The result can be either: - A boolean (for update queries). - A dictionary containing the results. - A string (if accept was passed). - An integer (if count=True).

Return type:

dict|string|int|bool

getBindings()[source]

Retrieve the bindings that have been set on this query.

Returns:A dictionary of bindings.
Return type:dict[string, Value]
getDataset()[source]

Get the dataset against which this query will operate.

Returns:Dataset.
Return type:Dataset
getIncludeInferred()[source]

Check whether this query will return inferred statements. :return: True if inferred triples are included, False otherwise. :rtype: bool

removeBinding(name)[source]

Removes a previously set binding on the supplied variable. Calling this method with an unbound variable name has no effect.

Parameters:name (string) – Variable name.
setBinding(name, value)[source]

Binds the specified variable to the supplied value. Any value that was previously bound to the specified value will be overwritten.

Parameters:
  • name (string) – Variable name.
  • value (Value|basestring) – New value for the variable. If a string is passed it will be converted to a literal.
setBindings(dict)[source]

Set multiple variable bindings.

Parameters:dict (dict[string, Value]) – A dictionary of bindings.
setCheckVariables(setting)[source]

Determine whether the presence of variables in the select clause not referenced in a triple is flagged.

Parameters:setting (bool) – True if variables should be checked, False otherwise.
setConnection(connection)[source]

Internal call to embed the connection into the query.

setContexts(contexts)[source]

Assert a set of contexts (named graphs) that filter all triples.

Parameters:contexts (list[URI|string]) – List of graph URIs.
setDataset(dataset)[source]

Select the dataset against which to evaluate this query, overriding any dataset that is specified in the query itself.

Parameters:dataset (Dataset) – A dataset object.
setIncludeInferred(includeInferred)[source]

Determine whether evaluation results of this query should include inferred statements.

The default setting is True.

Parameters:includeInferred (bool) – If True include inferred triples in query results.
class franz.openrdf.query.query.QueryLanguage(name)[source]

Bases: object

PROLOG = QueryLanguage.PROLOG
SPARQL = QueryLanguage.SPARQL
__init__(name)[source]
getName()[source]
registered_languages = [QueryLanguage.SPARQL, QueryLanguage.PROLOG]
static valueOf(name)[source]
static values()[source]
class franz.openrdf.query.query.TupleQuery(queryLanguage=QueryLanguage.SPARQL, query=None, baseURI=None, queryString=None)[source]

Bases: franz.openrdf.query.query.Query

A query that returns tuples (i.e. sets of variable bindings).

analyze(analysisTechnique=None, analysisTimeout=None)[source]

Analyze the query.

Analysis is only available for SPARQL queries.

Parameters:
  • analysisTechnique (string) – Control the method of analysis. Can be either “executed” (default). meaning that the query will be executed to perform dynamic analysis, or "static".
  • analysisTimeout (float) – Number of second to run the query if executed for analysis.
Returns:

Analysis result as a dictionary.

Return type:

dict

evaluate(count=False, output=None, output_format=TupleFormat.TABLE: Table)[source]

Execute the embedded query against the RDF store.

Return an iterator that produces for each step a tuple of values (resources and literals) corresponding to the variables or expressions in a ‘select’ clause (or its equivalent).

Parameters:
  • count (bool) – If True return the number of result rows instead of the usual iterator.
  • output (str|file|int|bool) – A file name, file descriptor or a file-like object to save the results to (optional). True can be used as a synonym for stdout.
  • output_format (RDFFormat) – Serialization format for output. The default is TABLE.
Returns:

Either an iterator over results, an integer (the number of results, if count is used) or None (if output is used).

Return type:

TupleQueryResult|int

class franz.openrdf.query.query.UpdateQuery(queryLanguage=QueryLanguage.SPARQL, query=None, baseURI=None, queryString=None)[source]

Bases: franz.openrdf.query.query.Query

An update query.

evaluate()[source]

Execute the embedded update against the RDF store.

Returns:True if the query changed the store, False otherwise.
Return type:bool

franz.openrdf.query.queryresult module

class franz.openrdf.query.queryresult.Binding[source]

Bases: franz.openrdf.query.queryresult.Binding

getName()[source]

Gets the name of the binding (e.g. the variable name).

getValue()[source]

Gets the value of the binding. The returned value is never equal to None, such a “binding” is considered to be unbound.

class franz.openrdf.query.queryresult.GraphQueryResult(string_tuples)[source]

Bases: franz.openrdf.query.repositoryresult.RepositoryResult, franz.openrdf.query.queryresult.QueryResult

A graph query result is an iterator over the Statements.

__init__(string_tuples)[source]
class franz.openrdf.query.queryresult.ListBindingSet(variable_names)[source]

Bases: object

A BindingSet is a set of named value bindings, which is used to represent a single query solution. Values are indexed by name of the binding which typically corresponds to the names of the variables used in the projection of the original query.

ListBindingSet emulates an RDF4J BindingSet, a Python dictionary and a list simultaneously. The internal datastructure is a pair of lists.

__init__(variable_names)[source]
getBinding(bindingName)[source]

Gets the binding with the specified name from this BindingSet.

getBindingNames()[source]

Gets the names of the bindings in this BindingSet.

getRow()[source]

Return a list of strings representing the values of the current row. This exists mostly for debugging (otherwise, we would return terms instead of strings).

getValue(bindingName)[source]

Gets the value of the binding with the specified name from this BindingSet. Throws exception if ‘bindingName’ is not legal.

hasBinding(bindingName)[source]

Checks whether this BindingSet has a binding with the specified name.

iterator()[source]

Creates an iterator over the bindings in this BindingSet. This only returns bindings with non-null values. An implementation is free to return the bindings in arbitrary order.

Currently, we only support Python-style iteration over BindingSet dictionaries, so this (Java-style) iterator method is not implemented.

size()[source]

Returns the number of bindings in this BindingSet.

class franz.openrdf.query.queryresult.QueryResult[source]

Bases: object

Super type of all query result types (TupleQueryResult, GraphQueryResult, etc. Evaluates as a Python iterator

close()[source]

Release resources used by this query result.

class franz.openrdf.query.queryresult.TupleQueryResult(variable_names, string_tuples)[source]

Bases: franz.openrdf.query.queryresult.QueryResult

A representation of a variable-binding query result as a sequence of BindingSet objects. Each query result consists of zero or more solutions, each of which represents a single query solution as a set of bindings. Note: take care to always close a TupleQueryResult after use to free any resources it keeps hold of.

__init__(variable_names, string_tuples)[source]
close()[source]
getBindingNames()[source]

Get the names of the bindings, in order of projection.

Returns:A list of names.
Return type:list[string]
rowCount()[source]
toPandas()[source]

franz.openrdf.query.repositoryresult module

class franz.openrdf.query.repositoryresult.RepositoryResult(string_tuples, subjectFilter=None, tripleIDs=False)[source]

Bases: object

An iterable collection of statements.

A RepositoryResult is a result collection of objects (for example org.openrdf.model.Statement, org.openrdf.model.Namespace, or org.openrdf.model.Resource objects) that can be iterated over. It keeps an open connection to the backend for lazy retrieval of individual results. Additionally it has some utility methods to fetch all results and add them to a collection.

By default, a RepositoryResult is not necessarily a (mathematical) set: it may contain duplicate objects. Duplicate filtering can be {{@link #enableDuplicateFilter() switched on}, but this should not be used lightly as the filtering mechanism is potentially memory-intensive.

A RepositoryResult needs to be {@link #close() closed} after use to free up any resources (open connections, read locks, etc.) it has on the underlying repository.

__init__(string_tuples, subjectFilter=None, tripleIDs=False)[source]
addTo(collection)[source]

Add all objects of this RepositoryResult to the supplied collection.

The RepositoryResult is fully consumed and automatically closed by this operation.

Parameters:collection (set|list) – The collection to add the results to. It can be a list or a set.
asList()[source]

Returns a list containing all objects of this RepositoryResult in order of iteration.

The RepositoryResult is fully consumed and automatically closed by this operation. :return: List of statements. :rtype: list[Statement]

close()[source]

Shut down the iterator to be sure the resources are freed up.

It is safe to call this method multiple times.

enableDuplicateFilter()[source]

Switch on duplicate filtering while iterating over objects.

The RepositoryResult will keep track of the previously returned objects in a set and on calling next() will ignore any objects that already occur in this set.

Caution: use of this filtering mechanism is potentially memory-intensive.

static normalize_quad(stringTuple)[source]
static normalize_quint(stringTuple)[source]
rowCount()[source]

Get the number of statements in this result object.

Returns:The number of results in this iterator.
toPandas(include_graph=True)[source]

Module contents