formula
index
/devel/WWW/2000/10/swap/formula.py


$Id: formula.html,v 1.16 2004/10/28 17:41:59 timbl Exp $
 
Formula
See:  http://www.w3.org/DesignIssues/Notation3
 
Interfaces
==========
 
The store stores many formulae, where one formula is what in
straight RDF implementations is known as a "triple store".
So look at the Formula class for a triple store interface.
 
See also for comparison, a python RDF API for the Redland library (in C):
   http://www.redland.opensource.ac.uk/docs/api/index.html 
and the redfoot/rdflib interface, a python RDF API:
   http://rdflib.net/latest/doc/triple_store.html

 
Modules
            
StringIO
binascii
diag
md5
notation3
re
string
sys
time
types
uripath
urllib
 
Classes
            
term.AnonymousNode(term.Node)
Formula(term.AnonymousNode, term.CompoundTerm)
term.CompoundTerm(term.Term)
Formula(term.AnonymousNode, term.CompoundTerm)
StoredStatement
 
class Formula(term.AnonymousNode, term.CompoundTerm)
      A formula of a set of RDF statements, triples.
 
(The triples are actually instances of StoredStatement.)
Other systems such as jena and redland use the term "Model" for Formula.
For rdflib, this is known as a TripleStore.
Cwm and N3 extend RDF to allow a literal formula as an item in a triple.
 
A formula is either open or closed.  Initially, it is open. In this
state is may be modified - for example, triples may be added to it.
When it is closed, note that a different interned version of itself
may be returned. From then on it is a constant.
 
Only closed formulae may be mentioned in statements in other formuale.
 
There is a reopen() method but it is not recommended, and if desperate should
only be used immediately after a close().
 
  
Method resolution order:
Formula
term.AnonymousNode
term.Node
term.Term
term.CompoundTerm

Methods defined here:
__init__(self, store, uri=None)
__iter__(self)
The internal method which allows one to iterate over the statements
as though a formula were a sequence.
__len__(self)
 How many statements?
__repr__(self)
add(self, subj, pred, obj, why=None)
Add a triple to the formula.
 
The formula must be open.
subj, pred and obj must be objects as for example generated by Formula.newSymbol() and newLiteral(), or else literal values which can be interned.
why     may be a reason for use when a proof will be required.
any(self, subj=None, pred=None, obj=None)
Return None or the value filing the blank in the called parameters.
 
Specifiy exactly two of the arguments.
color = f.any(pred=pantoneColor, subj=myCar)
somethingRed = f.any(pred=pantoneColor, obj=red)
 
Note difference from the old store.any!!
Note SPO order not PSO.
To aboid confusion, use named parameters.
asPair(self)
Return an old representation. Obsolete
bind(self, prefix, uri)
Give a prefix and associated URI as a hint for output
 
The store does not use prefixes internally, but keeping track
of those usedd in the input data makes for more human-readable output.
canonicalize(F)
If this formula already exists, return the master version.
If not, record this one and return it.
Call this when the formula is in its final form, with all its statements.
Make sure no one else has a copy of the pointer to the smushed one.
 
LIMITATION: The basic Formula class does NOT canonicalize. So
it won't spot idenical formulae. The IndexedFormula will.
classOrder(self)
close(self)
No more to add. Please return interned value.
NOTE You must now use the interned one, not the original!
compareTerm(self, other)
Assume is also a Formula - see function compareTerm below
contains(self, pred=None, subj=None, obj=None)
Return boolean true iff formula contains statement(s) matching the parts given
 
For example:
if f.contains(pred=pantoneColor):
    print "We've got one statement about something being some color"
declareExistential(self, v)
declareUniversal(self, v)
doesNodeAppear(self, symbol)
Does that particular node appear anywhere in this formula
 
This function is necessarily recursive, and is useful for the pretty printer
It will also be useful for the flattener, when we write it.
each(self, subj=None, pred=None, obj=None)
Return a list of values value filing the blank in the called parameters
 
Examples:
colors = f.each(pred=pantoneColor, subj=myCar)
 
for redthing in f.each(pred=pantoneColor, obj=red): ...
existentials(self)
Return a list of existential variables with this formula as scope.
 
Implementation:
we may move to an internal storage rather than these pseudo-statements
flatten(self, sink, why=None)
generated(self)
Yes, any identifier you see for this is arbitrary.
includes(f, g, _variables=[], bindings=[])
Does this formula include the information in the other?
 
bindings is for use within a query.
intern(self, value)
loadFormulaWithSubsitution(self, old, bindings={}, why=None)
Load information from another formula, subsituting as we go
returns number of statements added (roughly)
n3String(self, base=None, flags='')
Dump the formula to an absolute string in N3
newBlankNode(self, uri=None, why=None)
Create a new unnamed node with this formula as context.
 
The URI is typically omitted, and the system will make up an internal idnetifier.
If given is used as the (arbitrary) internal identifier of the node.
newExistential(self, uri=None, why=None)
Create a named variable existentially qualified within this formula
 
If the URI is not given, an arbitrary identifier is generated.
See also: existentials().
newFormula(self, uri=None)
Create a new open, empty, formula in the same store as this one.
 
The URI is typically omitted, and the system will make up an internal idnetifier.
If given is used as the (arbitrary) internal identifier of the formula.
newList(self, list)
newLiteral(self, str, dt=None, lang=None)
Create or reuse the internal representation of the RDF literal whose string is given
 
The literal is created in the same store as the formula.
newSymbol(self, uri)
Create or reuse the internal representation of the RDF node whose uri is given
 
The symbol is created in the same store as the formula.
newUniversal(self, uri=None, why=None)
Create a named variable universally qualified within this formula
 
If the URI is not given, an arbitrary identifier is generated.
See also: universals()
objects(self, pred=None, subj=None)
Obsolete - use each(subj=..., pred=...)
occurringIn(self, vars)
Which variables in the list occur in this?
outputStrings(self, channel=None, relation=None)
Fetch output strings from store, sort and output
 
To output a string, associate (using the given relation) with a key
such that the order of the keys is the order in which you want the corresponding
strings output.
predicates(self, subj=None, obj=None)
Obsolete - use each(subj=..., obj=...)
rdfString(self, base=None, flags='')
Dump the formula to an absolute string in RDF/XML
reification(self, sink, bnodeMap={}, why=None)
Describe myself in RDF to the given context
removeStatement(self, s)
Removes a statement The formula must be open.
 
This implementation is alas slow, as removal of items from tha hash is slow.
reopen(self)
Make a formula which was once closed oopen for input again.
 
NOT Recommended.  Dangers: this formula will be, because of interning,
the same objet as a formula used elsewhere which happens to have the same content.
You mess with this one, you mess with that one.
Much better to keep teh formula open until you don't needed it open any more.
The trouble is, the parsers close it at the moment automatically. To be fixed.
searchable(self, subj=None, pred=None, obj=None)
A pair of the difficulty of searching and a statement iterator of found statements
 
The difficulty is a store-portable measure of how long the store
thinks (in arbitrary units) it will take to search.
This will only be used for choisng which part of the query to search first.
If it is 0 there is no solution to the query, we know now.
 
In this implementation, we use the length of the sequence to be searched.
size(self)
Return the number statements.
Obsolete: use len(F).
statementsMatching(self, pred=None, subj=None, obj=None)
Return a READ-ONLY list of StoredStatement objects matching the parts given
 
For example:
for s in f.statementsMatching(pred=pantoneColor):
    print "We've got one which is ", `s[OBJ]`
    
If none, returns []
subjects(self, pred=None, obj=None)
Obsolete - use each(pred=..., obj=...)
substituteEquals(self, bindings, newBindings)
Return this or a version of me with subsitution made
 
Subsitution of = for = does NOT happen inside a formula,
as the formula is a form of quotation.
substitution(self, bindings, why=None)
Return this or a version of me with subsitution made
the(self, subj=None, pred=None, obj=None)
Return None or the value filing the blank in the called parameters
 
This is just like any() except it checks that there is only
one answer in the store. It wise to use this when you expect only one.
 
color = f.the(pred=pantoneColor, subj=myCar)
redCar = f.the(pred=pantoneColor, obj=red)
unify(self, other, vars, existentials, bindings)
See Term.unify()
universals(self)
Return a list of variables universally quantified with this formula as scope.
 
Implementation:
We may move to an internal storage rather than these statements.
variables(self)
Return a list of all variables quantified within this scope.

Data and non-method functions defined here:
__doc__ = 'A formula of a set of RDF statements, triples.\n ... only be used immediately after a close(). \n '
__module__ = 'formula'

Methods inherited from term.AnonymousNode:
unflatten(self, sink, bNodes, why=None)
uriref(self)

Methods inherited from term.Term:
compareAnyTerm(self, other)
Compare two langauge items
This is a cannoncial ordering in that is designed to allow
the same graph always to be printed in the same order.
This makes the regression tests possible.
The literals are deemed smaller than symbols, which are smaller
than formulae.  This puts the rules at the botom of a file where
they tend to take a lot of space anyway.
Formulae have to be compared as a function of their sorted contents.
 
@@ Anonymous nodes have to, within a given Formula, be compared as
a function of the sorted information about them in that context.
This is not done yet
debugString(self, already)
representation(self, base=None)
The string represnting this in N3
value(self)
As a python value - by default, none exists, use self
 
class StoredStatement
      A statememnt as an element of a formula
 
   Methods defined here:
__getitem__(self, i)
__init__(self, q)
__len__(self)
__repr__(self)
asFormula(self, why=None)
The formula which contains only a statement like this.
 
When we split the statement up, we lose information in any existentials which are
shared with other statements. So we introduce a skolem constant to tie the
statements together.  We don't have access to any enclosing formula 
so we can't express its quantification.  This @@ not ideal.
 
This extends the StoredStatement class with functionality we only need with "why" module.
comparePredObj(self, other)
Just compare P and OBJ, others the same
compareSubjPredObj(self, other)
Just compare SUBJ, Pred and OBJ, others the same
Avoid loops by spotting reference to containing formula
context(self)
Return the context of the statement
object(self)
Return the object of the statement
predicate(self)
Return the predicate of the statement
spo(self)
statements(self)
subject(self)
Return the subject of the statement

Data and non-method functions defined here:
__doc__ = 'A statememnt as an element of a formula\n '
__module__ = 'formula'
 
Data
             ALL4 = (0, 1, 2, 3)
ANONYMOUS = 3
CONTEXT = 0
FORMULA = 1
LITERAL = 2
List_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
Logic_NS = 'http://www.w3.org/2000/10/swap/log#'
N3_Empty = (0, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Empty')
N3_List = (0, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#List')
N3_first = (0, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first')
N3_nil = (0, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil')
N3_rest = (0, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest')
OBJ = 3
OWL_NS = 'http://www.w3.org/2002/07/owl#'
PARTS = (1, 2, 3)
PRED = 1
RDF_NS_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
RDF_type_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
SUBJ = 2
SYMBOL = 0
__file__ = './formula.py'
__name__ = 'formula'
becauseSubexpression = <why.BecauseSubexpression instance>
cvsRevision = '$Revision: 1.16 $'
forAllSym = 'http://www.w3.org/2000/10/swap/log#forAll'
forSomeSym = 'http://www.w3.org/2000/10/swap/log#forSome'
generators = _Feature((2, 2, 0, 'alpha', 1), (2, 3, 0, 'final', 0), 4096)
owlOneOf = 'http://www.w3.org/2002/07/owl#oneOf'
reifyNS = 'http://www.w3.org/2004/06/rei#'
tracking = 0