formula (version Id: formula.py,v 1.63 2007/12/16 00:22:59 syosi Exp )
index
/home/syosi/cvs-trunk/WWW/2000/10/swap/formula.py

 
Modules
       
StringIO
diag
notation3
sys
types

 
Classes
       
StoredStatement
term.AnonymousNode(term.Node)
Formula(term.AnonymousNode, term.CompoundTerm)
term.CompoundTerm(term.Term)
Formula(term.AnonymousNode, term.CompoundTerm)

 
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.CompoundTerm
term.Term
__builtin__.object

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.
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, key=None)
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
freeVariables(self)
generated(self)
Yes, any identifier you see for this is arbitrary.
intern(self, value)
loadFormulaWithSubstitution(self, old, bindings={}, why=None, cannon=False)
Load information from another formula, subsituting as we go
returns number of statements added (roughly)
n3EntailedBy(pattern, kb, vars=set([]), existentials=set([]), bindings={})
See Term.unify() and term.matchSet()
 
KB is a stronger statement han other.
Bindings map variables in pattern onto kb.
Self n3-entails other.
Criteria:  Subset of self statements must match other statements.
  Self's exisetntials must be subset of other's
  Self's universals must be superset.
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
 
See also: existentials()  and newBlankNode().
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
 
See also: universals()
newXMLLiteral(self, doc)
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.
ntString(self, base=None, flags='bravestpun')
Dump the formula to an absolute string in N3
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.
 
@@ what is this doing here??
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
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.
renameVars(self)
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 []
subSet(self, statements, why=None)
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, cannon=False, keepOpen=False)
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)
unifySecondary(self, other, env1, env2, vars, universals, existentials, n1Source, n2Source)
universals(self)
Return a set 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 set of all variables quantified within this scope.

Static methods defined here:
resetRenames(reset=True)

Methods inherited from term.AnonymousNode:
asPair(self)
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
unify(self, other, env1=Env({}), env2=Env({}), vars=set([]), universals=set([]), existentials=set([]), n1Source=32, n2Source=32)
value(self)
As a python value - by default, none exists, use self

Data and other attributes inherited from term.Term:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'Term' objects>
list of weak references to the object (if defined)

 
class StoredStatement
    A statememnt as an element of a formula
 
  Methods defined here:
__cmp__(self, other)
Just compare SUBJ, Pred and OBJ, others the same
Avoid loops by spotting reference to containing formula
__getitem__(self, i)
__hash__(self)
__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
context(self)
Return the context of the statement
existentials(self)
object(self)
Return the object of the statement
occurringIn(self, vars)
Which variables in the list occur in this?
predicate(self)
Return the predicate of the statement
spo(self)
statements(self)
subject(self)
Return the subject of the statement
unify(self, other, env1, env2, vars, universals, existentials, n1Source=32, n2Source=32)
unifySecondary(self, other, env1, env2, vars, universals, existentials, n1Source, n2Source)
universals(self)

 
Functions
       
sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

 
Data
        CONTEXT = 0
FORMULA = 1
Logic_NS = 'http://www.w3.org/2000/10/swap/log#'
OBJ = 3
PRED = 1
SUBJ = 2
SYMBOL = 0
__version__ = 'Id: formula.py,v 1.63 2007/12/16 00:22:59 syosi Exp '
generators = _Feature((2, 2, 0, 'alpha', 1), (2, 3, 0, 'final', 0), 4096)
tracking = 0