Eep3 Documentation
Eep3 is a lightweight Python API for RDF and Semantic Web processing. The
core API (of which this is the documentation for) consists of three simple
modules: the general API, an NTuples/NTriples parsing/serializing module, and
a query module.
This core API module is the basic ingredient for the larger eep3 stuff,
which includes Notation3 parsers, CWM clones, RDF diff programs, Semantic Web
IRC bots, NTriples normalizers, and so on.
api
This module contains the basic classes that form the core of the eep3 API:
from Articles to quad-oriented RDF stores.
Classes
- class Article(s)
- s is a string representation of a term in NTuples. In other words, it can
be a URI '<blargh>', a bNode '_:x', a literal '"blargh"', or a universally
quantified variable '?x'.
- The value fed to the article using the constructor is always available
as the representation of the article, so can be called using repr(article),
`article`, '%r' % article, etc.
- The value of the term passed to it will be available from
article.value. Here is the list of example NTuples terms again with their
value counterparts: '<blargh>': 'blargh'; '_:x': 'x'; '"blargh"': 'blargh';
'?x': 'x'.
- set(self, s)
- Resets the article by recalling __init__.
- class Tuple([tuple])
- This is a subclass of tuple, so it can be passed a tuple which will be
stored in itself.
- class Triple(data)
- A sub class of Tuple (see above). It must be passed a list of three
articles, which will be used as the data for the Triple (subject, predicate,
object).
- class TupleStore([tuples])
- A TupleStore is a sub class of a Python list (be careful not to confuse
the two: there are some assertions in the related eep3 modules that check for
lists being passed instead of TupleStores). It can be passed a list of tuiples
on the constructor.
- feed(store)
- This takes in another TupleStore and merges it with the current store
without local name collisions. If you don't care about local name
collisons, use the append method available for lists.
- query(q, [loose=1])
- This enables you to query with a single tuple, and get a TupleStore back.
If the optional "loose" variable is set to 0, it will not match existentially
quantified variables as if they were univars.
- remove(t)
- This removes a triple from the store by querying with it first, and then
filtering.
The following two classes are the most important from the RDF API POV. The
two super classes above are just provided for people that want to use more than
binary relationships (perhaps modelling KIF).
- class TripleStore([data])
- A sub class of TupleStore. This is a store for Triples, i.e. with no
formulae involved. Any XML RDF or NTriples document should be able to be
stored in a TripleStore and then serialized with no loss of data.
- If the data option is passed (a list of Triple instances), they will be
added to the store.
- append(t)
- Appends a triple to the TripleStore. This is a significant function since
it adds it to both an internal flat list, and a special dictionary
structure
{ predicate: { subject: [objects] } }
. This dictionary
structure allows for some extremely fast predicate (or predicate and subject)
oriented searches, as well as greatly optimising the append method in
general.
- extend(*triplelists)
- Simply calls extend for each triple of each triple list in the triple
lists.
- remove(t, [loose=1])
- This is basically a more efficient (i.e. triple oriented) version of the
remove method of the TupleStore class.
- class Store()
- A subclass of dict, using the keys as formulae labels (reprs of Articles)
and values as TripleStores.
- Note that the constructor automatically puts a blank TripleStore into
the root formula, which is given None as a key.
- formulae()
-
- The formulae of the current store.
- append(t, formula)
- Appends a triple (t) to the given formula.
query
This is the main query module. If you have a single query triple whose
predicate and (possible) subject are known, then it's much more efficient to
do a specialized query over the ._triples store in the TripleStore class
instance, but otherwise this module provides.
- query(f, q, [fmt=0])
- f and q must be instances of TupleStore (or TripleStore). query will query
the q TupleStore against f, and return either a list of bindings and tuples (if
fmt is set to 0), or a formatted list of the tuples.
- The structure of the result when fmt is 0 is:
( ([tuples],
{bindings})* )
.
Example use:-
import query, ntuples
x = ntuples.parsent("""<#Sean> <#likes> <#TheSimpsons> .
<#Sean> <#likes> <#TheSemanticWeb> .
<#Aaron> <#name> "Aaron" .
<#Aaron> <#likes> <#ThePlex> .
<#Sean> <#name> "Sean" .""")
print x
>>> [Triple(<#Sean>, <#likes>, <#TheSimpsons>),
Triple(<#Sean>, <#likes>, <#TheSemanticWeb>),
Triple(<#Aaron>, <#name>, "Aaron"),
Triple(<#Aaron>, <#likes>, <#ThePlex>),
Triple(<#Sean>, <#name>, "Sean")]
y = ntuples.parsent('?x <#name> ?y .\n?x <#likes> ?z .')
print y
>>> [Triple(?x, <#name>, ?y),
Triple(?x, <#likes>, ?z)]
print query.query(x, y)
>>> [([Triple(<#Aaron>, <#name>, "Aaron"),
Triple(<#Aaron>, <#likes>, <#ThePlex>)],
{'?z': '<#ThePlex>', '?y': '"Aaron"', '?x': '<#Aaron>'}),
([Triple(<#Sean>, <#name>, "Sean"),
Triple(<#Sean>, <#likes>, <#TheSimpsons>)],
{'?z': '<#TheSimpsons>', '?y': '"Sean"', '?x': '<#Sean>'}),
([Triple(<#Sean>, <#name>, "Sean"),
Triple(<#Sean>, <#likes>, <#TheSemanticWeb>)],
{'?z': '<#TheSemanticWeb>', '?y': '"Sean"', '?x': '<#Sean>'})]
print query.query(x, y, 1)
>>>
<#Aaron> <#name> "Aaron" .
<#Aaron> <#likes> <#ThePlex> .
<#Sean> <#name> "Sean" .
<#Sean> <#likes> <#TheSimpsons> .
<#Sean> <#name> "Sean" .
<#Sean> <#likes> <#TheSemanticWeb> .
Note that in the original Eep (i.e. Eep1), large queries used to melt the
CPU. This is no longer the case.
ntuples
- parseNTuples(s)
- Parse some NTuples into a TupleStore.
- serialize(s)
- Serialize a TupleStore, TripleStore, or Store.
- parsent(s, [t=[]])
- Parse some NTriples with a RegExp (fast), returning a TripleStore.
- parse(s)
- Parse some NQuads, returning a Store.
Sean B. Palmer