4RDF


4RDF API API 4RDF RDF Application Programming Interface for 4RDF

The 4RDF API

There is no standard API for RDF processors. We put together a simple, clean API that suits our uses of RDF very well. There are other APIs such as SIRPaC, RAX, none of which are supported by 4RDF now.

Module Ft.Rdf.Model

Module Summary

Classes

Class Summary
Model

Class Model

Method Summary
__init__ Initializer for an RDF model instance
add Add a statement to the model
addContainer Add a container to the model. This involves adding a statement for the container type, and a container statement for each item in the collection
extractContainer Build a container object from the container item relationships in the model (_1, _2, etc.).
contains Check if a statement is in the model
statements Returns all the statments in the model
remove Remove a statement from the model
removeAll Remove all statements from the model, or those with a given URI
size Returns the number of statements in the model
complete Return all the statements in the model that match a simple pattern.
completeRegex Return all the statements in the model that match a regular expression pattern.
generateUri Generates URIs for "anonymous" resources, such as reified statements. The default method is to generate a UUID URN, but this can be easily overriden.

Method Details

__init__

__init__(sourceUri, driver, schemaHandler)
      

Initializer for an RDF model instance

Parameters
sourceUri of type string

The URI used as the base for all contents of this model. For example, it could be the actual URL from where the serialization of the model is accessible.

driver of type Any object meeting the protocol of Ft.Rdf.Drivers.Memory.Memory

This is the object used to abstract the storage and management of the RDF statements. It could be a memory buffer, a DBMS connection, or even some code around a UserList

schemaHandler of type Ft.Rdf.SchemaHandler.SchemaHandler

This object is used to provide RDF schema support. If None, no schema support will be available for the resulting model. The default is None

Return Value
None


add

add(stmt, checkSchema)
      

Add a statement to the model

Parameters
stmt of type Rdf.Statement.Statement, or list thereof

The new statement or statements.

checkSchema of type integer

1 is it is desired to check that the addition does not violate the RDF schema, otherwise 0. Default is 1.

Return Value
None


addContainer

addContainer(container)
      

Add a container to the model. This involves adding a statement for the container type, and a container statement for each item in the collection

Parameters
container of type Ft.Rdf.Container.Container

The container instance

Return Value
None


extractContainer

extractContainer(uri)
      

Build a container object from the container item relationships in the model (_1, _2, etc.).

Parameters
uri of type Ft.Rdf.Container.Container

The uri of the represented container

Return Value
Ft.Rdf.Container.Container

A container object (Bag, Alt or Set) representing all the container item relationships. Note that the returned object is not live. Changes to the model after the container is extracted willl not affect the extracted container.



contains

contains(stmt)
      

Check if a statement is in the model

Parameters
stmt of type Rdf.Statement.Statement

The statement to check.

Return Value
None


statements

statements()
      

Returns all the statments in the model

Parameters
None
Return Value
list of 3-item tuples

all the statements in the model



remove

remove(stmt)
      

Remove a statement from the model

Parameters
stmt of type Rdf.Statement.Statement

The statement to remove.

Return Value
None


removeAll

removeAll(uri)
      

Remove all statements from the model, or those with a given URI

Parameters
uri of type string

If a string, only statements with this URI are removed. If None, all statements are removed. Default is None.

Return Value
None


size

size()
      

Returns the number of statements in the model

Parameters
None
Return Value
integer

The number of statements in the model. Will be equal to or greater than 0



complete

complete(subj, pred, obj)
      

Return all the statements in the model that match a simple pattern.

Parameters
subj of type string or None

If not None, statements must have the given subject to match

pred of type string or None

If not None, statements must have the given predicate to match

obj of type string or None

If not None, statements must have the given object to match

Return Value
list of 3-tuples

statements in the model that match the given pattern



completeRegex

completeRegex(subj, pred, obj, ignoreCase = 0)
      

Return all the statements in the model that match a regular expression pattern.

Parameters
subj of type string or None

If not None, statement's subject must match this regular expression.

pred of type string or None

If not None, statement's predicate must match this regular expression

obj of type string or None

If not None, statement's object must match this regular expression.

ignoreCase of type boolean

If true, case is ignored during the regular expression matches.

Return Value
list of 3-tuples

statements in the model that match the given pattern



generateUri

generateUri()
      

Generates URIs for "anonymous" resources, such as reified statements. The default method is to generate a UUID URN, but this can be easily overriden.

Parameters
None
Return Value
string

A generated URI



Module Ft.Rdf.Statement

Module Summary

Classes

Class Summary
Statement Represents an RDF triple or statement

Class Statement

Represents an RDF triple or statement

Method Summary
__init__ Initializer for an RDF statement instance
reify Prepare the statement in the model so that it can be the subject of other statements. See the RDF spec for details of how statements are expanded for reification. Note that the original statement is not removed by reification. The statement's uri is used as the subject of the added statements, or a URI is generated if the statement's uri is an empty string.
isEquivalent Statements are equivalent if their subject, predicate and object are exact string matches. Note that the statement's URI is not a factor, so this is not an equality check.

Method Details

__init__

__init__(subject, predicate, object, uri)
      

Initializer for an RDF statement instance

Parameters
subject of type string

The subject of the statement

predicate of type string

The predicate of the statement

object of type string

The object of the statement

uri of type string

The URI of the statement. This is an empty string except for very sophisticated use.

Return Value
None


reify

reify(model, uri)
      

Prepare the statement in the model so that it can be the subject of other statements. See the RDF spec for details of how statements are expanded for reification. Note that the original statement is not removed by reification. The statement's uri is used as the subject of the added statements, or a URI is generated if the statement's uri is an empty string.

Parameters
model of type Ft.Rdf.Model

The model that is used to add the additional statements generated bu reification.

uri of type string or None

URI to be assigned to the statement. If None, then first the statement object is checked for a URI, otherwise the model generates a URI for the reified statement. Default is None.

Return Value
None


isEquivalent

isEquivalent(other)
      

Statements are equivalent if their subject, predicate and object are exact string matches. Note that the statement's URI is not a factor, so this is not an equality check.

Parameters
other of type Ft.Rdf.Statement.Statement

The statement to be compared

Return Value
integer

1 if the statements are equivalent, else 0



Module Ft.Rdf.SchemaHandler

Module Summary

Classes

Class Summary
SchemaHandler Processing of RDF schema information on behalf of a model instance.

Class SchemaHandler

Processing of RDF schema information on behalf of a model instance.

Method Summary
__init__ Initializer for a SchemaHandler. There are no parameters.
isCoreRdfs Checks whether a statement comes from the core RDF meta-model.
isInstance Checks whether a resource is an instance of a class. Note that this is also true if the resource is an instance of any subclass of the given class.
isSubClass Checks whether a class is an instance of another class.

Method Details

__init__

__init__()
      

Initializer for a SchemaHandler. There are no parameters.

Parameters
None
Return Value
None


isCoreRdfs

isCoreRdfs(stmt)
      

Checks whether a statement comes from the core RDF meta-model.

Parameters
stmt of type Rdf.Statement.Statement

The statement to check.

Return Value
boolean/integer

1 if the statement comes from the core RDF meta-model, otherwise 0.



isInstance

isInstance(model, obj, class_)
      

Checks whether a resource is an instance of a class. Note that this is also true if the resource is an instance of any subclass of the given class.

Parameters
model of type Rdf.Model.Model

The model in which the condition is be checked

obj of type string

The URI of the resource to check for class membership.

class_ of type string

The URI of the class to be checked.

Return Value
boolean/integer

1 if the statement is an instance of the class, otherwise 0.



isSubClass

isSubClass(model, class1, class2)
      

Checks whether a class is an instance of another class.

Parameters
model of type Rdf.Model.Model

The model in which the condition is be checked

class1 of type string

The URI of the class to check as descendant.

class2 of type string

The URI of the class to check as ancestor.

Return Value
boolean/integer

1 if the first class is a descendant of the second, otherwise 0.



Serializers create a serialized representation from an RDF model, or populate a model with statements from a serialized form. Currently the only built-in serializer for 4RDF handles a DOM form representing the XML serialization from the RDF Model and syntax recommendation.

Module Ft.Rdf.Serializers.Dom

Serialize or deserialize a model based on the XML serialization in the RDF Model and Syntax recommendation.

Module Summary

Classes

Class Summary
Serializer Serialize or deserialize a model based on the XML serialization in the RDF Model and Syntax recommendation.

Class Serializer

Serialize or deserialize a model based on the XML serialization in the RDF Model and Syntax recommendation.

Method Summary
serialize Construct a DOM representing statements in the model.
deserialize Generate RDF statements from an XML serialization and insert these into a Model.

Method Details

serialize

serialize(model, nsMap, selectUri)
      

Construct a DOM representing statements in the model.

Parameters
model of type Ft.Rdf.Model

The model from which the serialization is to be extracted

nsMap of type dictionary with string keys and string values, or None

A mapping from URIs that are found in the model to prefixes that will be used for the corresponding XML namespaces in the serialization. If none, then the empty dictionary. default is None.

selectUri of type string or None

If a string, only statements with this URI will be selected for serialization. If None, all statements will be serialized. Default is None. Note that if the model is set up for schema validation, the RDFS schema statements are suppressed from the output.

Return Value
xml.dom.Document.Document

The DOM Node that represents the serialized document.



deserialize

deserialize(model, node, sourceUri)
      

Generate RDF statements from an XML serialization and insert these into a Model.

Parameters
model of type Ft.Rdf.Model

The model into which the statements generated by the deserialization is to be entered.

node of type xml.dom.Node

The DOM node representing the serialization from which the statements are extracted.

sourceUri of type string

The base URI of the serialization. Usually this would be the URI of the document where the serialization is found.

Return Value