AllegroGraph Python API Reference¶
This is a description of the Python Application Programmer’s Interface (API) to Franz Inc.’s AllegroGraph.
The Python API offers convenient and efficient access to an AllegroGraph server from a Python-based application. This API provides methods for creating, querying and maintaining RDF data, and for managing the stored triples.
Note
The Python API deliberately emulates the Eclipse RDF4J API (formerly Aduna Sesame) to make it easier to migrate from RDF4J to AllegroGraph. The Python API has also been extended in ways that make it easier and more intuitive than the RDF4J API.
AllegroGraphServer class¶
- class franz.openrdf.sail.allegrographserver.AllegroGraphServer(host=None, port=None, user=None, password=None, cainfo=None, sslcert=None, verifyhost=None, verifypeer=None, protocol=None, proxy=None, **options)
The AllegroGraphServer object represents a remote AllegroGraph server on the network. It is used to inventory and access the catalogs of that server.
Example:
server = AllegroGraphServer(host="localhost", port=8080, user="test", password="pw")
Define the connection to the AllegroGraph HTTP server.
Creates a new role.
Grant read/write access to a role.
Grants a role a certain permission.
Add a security filter for the user.
Create or replace a sitescript.
Create a new user.
Grant read/write access to a user.
Assigns the given permission to this user.
Add a role to a user.
Add a security filter for the user.
Change the password for the given user.
Deletes a role.
Revoke read/write access for a role.
Revokes a permission for a role.
Add a security filter for the user.
Delete a sitescript.
Delete a user.
Takes the same parameters as PUT on this URL, but revokes the access instead of granting it.
Remove user data from the server.
Revokes the given permission for this user.
Remove a role from a user.
Add a security filter for the user.
Retrieve the contents of the server initialization file.
Get the body of a sitescript.
Retrieve user data value with given key.
Get the list of catalogs on this server.
Query the access granted to a role.
Lists the permission flags granted to a role.
List security filters for user.
Returns the names of all defined roles.
Return the list of Sitescripts currently on the server.
Retrieve the read/write access for a user.
Get all used keys from the user data store on the server.
Like listUserAccess, but also includes the access granted to roles that this user has.
Retrieve the permission flags assigned to the user, or any of its roles.
List the permission flags that have been assigned to a user (any of super, eval, session, replication).
Retrieves a list of role names for this user name.
List security filters for user.
Returns a list of names of all the users that have been defined.
Open a catalog by name.
Open a session that federates several repositories.
Open a session on a federated, reasoning, or filtered store.
Replace the current initialization file contents with the
content
string or remove if None.Set user data with given key.
Catalog class¶
- class franz.openrdf.sail.allegrographserver.Catalog(name, server, client)
Container of multiple repositories (triple stores).
Construct catalogs using the server object:
catalog = server.openCatalog('scratch')
Creates a new repository with the given name.
Delete a repository.
Return the catalog name.
Creates or opens a repository.
Return a list of names of repositories (triple stores) managed by this catalog.
Spec module¶
Helper functions for creating session specification strings.
See openSession()
Create a session spec for connecting to a federated store. |
|
Create a graph-filtered session spec. |
|
Create a session spec for connecting to a store on the local server. |
|
Create a session spec that adds reasoning support to another session. |
|
Create a session spec for connecting to a store on another server. |
|
Create a session spec for connecting to a remote store with known URL. |
Repository class¶
- class franz.openrdf.repository.repository.Repository(catalog, database_name, repository)
A repository contains RDF data that can be queried and updated. Access to the repository can be acquired by opening a connection to it. This connection can then be used to query and/or update the contents of the repository.
Please note that a repository needs to be initialized before it can be used and that it should be shut down before it is discarded/garbage collected. Forgetting the latter can result in loss of data (depending on the Repository implementation)!
Construct instances using
getRepository()
.with catalog.getRepository("agraph_test", Repository.ACCESS) as repo: ...
Invoke through
getRepository()
.Opens a connection to this store that can be used for querying and updating the contents of the store.
Return the name of the database (remote triple store) that this repository is interfacing with.
Return a session spec string for this repository.
Return a ValueFactory for this store.
Initializes this repository.
Checks whether this store is writable, i.e. if the data contained in this store can be changed.
Register an inlined datatype.
Shuts the store down, releasing any resources that it keeps hold of.
Utility connection functions¶
Manually constructing server, catalog and repository objects is often
tedious when only a simple connection to a single repository is needed.
In that case the connection may be created directly using
ag_connect()
.
Create a connection to an AllegroGraph repository. |
RepositoryConnection class¶
- class franz.openrdf.repository.repositoryconnection.RepositoryConnection(repository, close_repo=False, is_session=False)
The RepositoryConnection class is the main interface for updating data in and performing queries on a
Repository
. By default, a RespositoryConnection is in autoCommit mode, meaning that each operation corresponds to a single transaction on the underlying triple store. autoCommit can be switched off, in which case it is up to the user to handle transaction commit/rollback. Note that care should be taken to always properly close a RepositoryConnection after one is finished with it, to free up resources and avoid unnecessary locks.Note that concurrent access to the same connection object is explicitly forbidden. The client must perform its own synchronization to ensure non-concurrent access.
Several methods take a vararg argument that optionally specifies a set of contexts on which the method should operate. (A context is the URI of a subgraph.) Note that a vararg parameter is optional, it can be completely left out of the method call, in which case a method either operates on a provided statement’s context (if one of the method parameters is a statement or collection of statements), or operates on the repository as a whole, completely ignoring context. A vararg argument may also be
None
, meaning that the method operates on those statements which have no associated context only.RepositoryConnection
objects should be constructed usinggetConnection()
. To ensure that repository connections are closed, the best practice is to use awith
statement:with repository.getConnection() as conn: ...
Call through
getConnection()
.
General connection methods¶
This section contains the RepositoryConnection
methods that create,
maintain, search, and delete triple stores.
Calls |
|
Adds data from a string to the repository. |
|
Loads a file into the triple store. |
|
Add the supplied statement to the specified contexts in the repository. |
|
Add a single triple to the repository. |
|
Add the supplied triples or quads to this repository. |
|
Removes all statements from designated contexts in the repository. |
|
Delete all namespaces in this repository for the current user. |
|
Close the connection. |
|
Create a new blank node. |
|
Create a new literal with value |
|
Create a compound literal representing a range from lowerBound to upperBound. |
|
Create a new Statement object. |
|
Creates a new URI from the supplied string-representation(s). |
|
Delete duplicate triples from the store. |
|
Prepare and immediately evaluate a query that returns a boolean. |
|
Prepare and immediately evaluate a query that returns RDF. |
|
Prepare and immediately evaluate a query that returns tuples. |
|
Prepare and immediately evaluate a SPARQL update query. |
|
Export all explicit statements in the specified contexts to a file. |
|
Export statements to a file. |
|
Get the current value of |
|
Return a list of context resources, one for each context referenced by a quad in the triple store. |
|
Return all duplicates in the store. |
|
Get the namespace that is associated with the specified prefix, if any. |
|
Get all declared prefix/namespace pairs. |
|
Get the session specification string for this repository. |
|
Get all statements with a specific subject, predicate and/or object from the repository. |
|
Return all statements whose triple ID matches an ID in the list 'ids'. |
|
Get the |
|
Return |
|
Creates an object that allows for simple creation of URIs in given namespace. |
|
Parse |
|
Parse |
|
Parse |
|
Parse |
|
Register an inlined datatype. |
|
Call |
|
Remove a namespace declaration by removing the association between a prefix and a namespace name. |
|
Remove enumerated quads from this repository. |
|
Remove all quads with matching IDs. |
|
Remove the supplied statement from the specified contexts in the repository. |
|
Remove the statement(s) with the specified subject, predicate and object from the repository, optionally restricted to the specified contexts. |
|
Set the value of |
|
Define or redefine a namespace mapping in the repository. |
|
Returns the number of (explicit) statements that are in the specified contexts in this repository. |
|
Warm up specified parts of the repository in order to speed up access. |
|
The threshold for commit size during triple add operations. |
Vector Repository Methods¶
These methods operation on vector triple stores
Compute the embedding for the given text string and add it to the repository. |
|
Convert an existing normal repo to a vector store by setting the embedder and optionally the api-key and model. |
|
An embedding is computed for the given text string and the closest embeddings to that embedding are computed. |
|
Return the embedding for the given object_id. |
|
Return the value of the given property of the given object_id. |
|
Return the text of the object denoted by object_id. |
|
Remove zero or more objects. |
Triple Index Methods¶
These RepositoryConnection
methods support user-defined triple indices.
See AllegroGraph Triple Indices for more
information on this topic.
Return the list of the current set of triple indices. |
|
Return the list of valid index names. |
|
Add a specific type of index to the current set of triple indices. |
|
Removes a specific type of index to the current set of triple indices. |
|
Optimize indices. |
Free Text Indexing Methods¶
The following RepositoryConnection
methods support free-text indexing in
AllegroGraph.
Create a free-text index with the given parameters. |
|
Delete a free-text index from the server. |
|
Return a list of statements for the given free-text pattern search. |
|
Get the current settings of a free-text index. |
|
Get the names of currently defined free-text indices. |
|
Modify parameters of a free-text index. |
Note that text search is implemented through a SPARQL query using a
“magic” predicate called fti:search
. See the AllegroGraph
Python API Tutorial for an example of how to set up this
search.
Prolog Rule Inference Methods¶
These RepositoryConnection
methods support the use of Prolog rules in
AllegroGraph. Any use of Prolog rules requires that you create a
transaction to run them in.
Add Prolog functors to the current session. |
|
Add Prolog rules from file to the current session. |
Geospatial Reasoning Methods¶
These RepositoryConnection
methods support geospatial reasoning.
Create a rectangular search region (a box) for geospatial search. |
|
Create a circular search region for geospatial search. |
|
Create an x, y or latitude, longitude coordinate in the current coordinate system. |
|
Create a spherical coordinate system and use it as the current coordinate system. |
|
Define a polygonal region with the specified vertices. |
|
Create a Cartesian coordinate system and use it as the current coordinate system. |
|
Get the current geospatial coordinate system. |
|
Set the current geospatial coordinate system. |
Transactions¶
AllegroGraph lets you set up a special RepositoryConnection
(a
“session”) that supports transaction semantics. You can add statements
to this session until you accumulate all the triples you need for a
specific transaction. Then you can commit the triples in a single act.
Up to that moment the triples will not be visible to other users of the
repository.
If anything interrupts the accumulation of triples building to the transaction, you can roll back the session. This discards all of the uncommitted triples and resynchronizes the session with the repository as a whole.
Closing the session discards all uncommitted triples and all rules, generators, and matrices that were created in the session. Rules, generators, and matrices cannot be committed. They persist as long as the session persists.
Open a session. |
|
Close a session. |
|
A session context manager for use with the |
|
Commit changes on an open session. |
|
Roll back changes on open session. |
Subject Triples Caching¶
You can enable subject triple caching to speed up queries where the same subject URI appears in multiple patterns. The first time AllegroGraph retrieves triples for a specific resource, it caches the triples in memory. Subsequent query patterns that ask for the same subject URI can retrieve the matching triples very quickly from the cache. The cache has a size limit and automatically discards old entries as that limit is exceeded.
Maintain a cache of size |
|
Disable the subject triples cache (see |
|
Return the current size of the subject triples cache. |
Query Class (and Subclasses)¶
Note
The Query class is non-instantiable. It is an abstract class from which the three query subclasses are derived. It is included here because of its methods, which are inherited by the subclasses.
A query on a Repository
that can be formulated
in one of the supported query languages (for example SPARQL). The query can
be parameterized, to allow one to reuse the same query with different parameter
bindings.
The best practice is to allow the RepositoryConnection
object to
create an instance of one of the Query subclasses
(TupleQuery
, GraphQuery
, BooleanQuery
, UpdateQuery
).
There is no reason for the Python application programmer to create
such objects directly.
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
result = tupleQuery.evaluate()
Evaluate a SPARQL or PROLOG query, which may be a 'select', 'construct', 'describe' or 'ask' query (in the SPARQL case). |
|
Retrieve the bindings that have been set on this query. |
|
Get the dataset against which this query will operate. |
|
Check whether this query will return inferred statements. |
|
Removes a previously set binding on the supplied variable. |
|
Binds the specified variable to the supplied value. |
|
Set multiple variable bindings. |
|
Determine whether the presence of variables in the select clause not referenced in a triple is flagged. |
|
Internal call to embed the connection into the query. |
|
Assert a set of contexts (named graphs) that filter all triples. |
|
Select the dataset against which to evaluate this query, overriding any dataset that is specified in the query itself. |
|
Determine whether evaluation results of this query should include inferred statements. |
Subclass TupleQuery¶
This subclass is used with SELECT queries. Use the
prepareTupleQuery()
method to create
a TupleQuery object. The results of the query are returned in a
QueryResult
iterator that yields a sequence of
binding sets
.
TupleQuery uses all the methods of the Query
class,
plus two more:
Execute the embedded query against the RDF store. |
|
Analyze the query. |
Subclass GraphQuery¶
This subclass is used with CONSTRUCT and DESCRIBE queries. Use the
prepareGraphQuery()
method to create a
GraphQuery object. The results of the query are returned in a
GraphQueryResult
iterator that yields a
sequence of statements
.
GraphQuery
implements all the methods of the Query
class,
plus one more:
Execute the embedded query against the RDF store. |
Subclass BooleanQuery¶
This subclass is used with ASK queries. Use the
prepareBooleanQuery()
method to create
a BooleanQuery
object. The results of the query are
True
or False
.
BooleanQuery implements all the methods of the Query
class, plus one more:
Execute the embedded query against the RDF store. |
Subclass UpdateQuery¶
This subclass is used for DELETE
and INSERT
queries. The
result returned when the query is evaluated is a boolean that can be
used to tell if the store has been modified by the operation. Use the
prepareUpdate()
method to create an
UpdateQuery
object.
UpdateQuery
implements all the methods of the Query
class, plus one more:
Execute the embedded update against the RDF store. |
QueryResult Class¶
A QueryResult object is simply an iterator that also has a
close()
method that must be called to free
resources. Such objects are returned as a result of SPARQL and PROLOG
query evaluation and should not be constructed directly. Result
objects are context managers and can be used in the with
statement. The recommended usage looks like this:
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
with tupleQuery.evaluate() as results:
for result in results:
print result
It is also possible to use one of the execute*Query
methods
(e.g. executeTupleQuery()
) to prepare and execute a query in a
single call:
with conn.executeTupleQuery(queryString) as results:
for result in results:
print result
Release resources used by this query result. |
|
|
Return the next result item if there is one. |
Subclass TupleQueryResult¶
A QueryResult subclass used for queries that return tuples.
Get the names of the bindings, in order of projection. |
|
Get a nested dictionary containing query result metadata. |
Subclass GraphQueryResult¶
A QueryResult subclass used for queries that return statements. Objects
of this class are also RepositoryResult
instances.
RepositoryResult class¶
A RepositoryResult
object is a result collection of statements
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
switched on, but this should not be used lightly as the filtering
mechanism is potentially memory-intensive.
A RepositoryResult
must be closed after use to free up any resources
(open connections, read locks, etc.) it has on the underlying
repository. To make this easier it is a context manager and
can be used in the with
statement.
graphQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, queryString)
with graphQuery.evaluate() as results:
for result in results:
print result
|
Shut down the iterator to be sure the resources are freed up. |
|
Return the next Statement in the answer, if there is one. |
|
Switch on duplicate filtering while iterating over objects. |
|
Returns a list containing all objects of this RepositoryResult in order of iteration. |
|
Add all objects of this RepositoryResult to the supplied collection. |
|
Get the number of statements in this result object. |
Statement Class¶
A Statement
is a client-side triple. It encapsulates the subject,
predicate, object and context (subgraph) values of a single triple and
makes them available.
Best practice is to allow the RepositoryConnection.createStatement()
method to create and return the Statement object. There is no reason for
the Python application programmer to create a Statement object
directly.
stmt1 = conn.createStatement(alice, age, fortyTwo)
Get the graph (the fourth, optional element of the statement). |
|
Get the object (the third element of the statement). |
|
Get the predicate (the second element of the statement). |
|
Get the subject (the first element of the statement). |
ValueFactory Class¶
A ValueFactory is a factory for creating URIs, blank nodes, literals
and Statement
objects. In the AllegroGraph Python interface,
the ValueFactory
class is regarded as obsolete. Its
functions have been subsumed by the expanded capability of the
RepositoryConnection
class. It is documented here for the
convenience of users porting an application from Eclipse RDF4J.
Social Network Analysis Methods¶
The following
RepositoryConnection
methods support Social Network Analysis in AllegroGraph. The Python API to the Social Network Analysis methods of AllegroGraph requires Prolog queries, and therefore must be run in a dedicated session.registerNeighborMatrix
Construct a neighbor matrix named
name
.registerSNAGenerator
Create a new SNA generator named
name
.