query
index
/Users/yosi/CVSROOT/WWW/2000/10/swap/query.py

Query for cwm architecture
 
2003-09-07 split off from llyn.py

 
Modules
       
diag
sys
types

 
Classes
       
__builtin__.list(__builtin__.object)
Queue
__builtin__.object
BecauseBuiltInWill
BecauseSupportsWill
BetterNone
Chain_Step
exceptions.Exception
BuiltInFailed
formula.Formula(term.AnonymousNode, term.CompoundTerm)
Query
formula.StoredStatement
QueryItem
CyclicSetOfRules
InferenceTask
Rule

 
class BecauseBuiltInWill(__builtin__.object)
     Methods defined here:
__init__(self, *args)

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

 
class BecauseSupportsWill(__builtin__.object)
     Methods defined here:
__init__(self, *args)

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

 
class BetterNone(__builtin__.object)
     Methods defined here:
__eq__(self, other)
__geq__ = __eq__(self, other)
__gt__ = __eq__(self, other)
__hash__(self)
__leq__ = __eq__(self, other)
__lt__ = __eq__(self, other)
__neq__ = __eq__(self, other)
__str__(self)

Static methods defined here:
__new__(cls)

Data and other attributes defined here:
__slots__ = []
__val__ = <query.BetterNone object>

 
class BuiltInFailed(exceptions.Exception)
     Methods defined here:
__init__(self, info, item, pred)
__str__(self)

Methods inherited from exceptions.Exception:
__getitem__(...)

 
class Chain_Step(__builtin__.object)
     Methods defined here:
__cmp__(self, other)
__init__(self, queue, env, parent=None, evidence=[])
__repr__(self)
copy(self)
done(self)
popBest(self)

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

 
class CyclicSetOfRules
    A set of rules which are connected
 
  Methods defined here:
__getitem__(self, i)
__init__(self, rules)
__repr__(self)
run(self)
Run a cyclic subset of the rules

 
class InferenceTask
    A task of applying rules or filters to information
 
  Methods defined here:
__init__(self, workingContext, ruleFormula=None, targetContext=None, universals=set([]), mode='', why=None, repeat=0)
Apply rules in one context to the same or another
 
A rule here is defined by log:implies, which associates the template
(aka premise, precondidtion, antecedent, body) to the conclusion
(aka postcondition, head).
gatherQueries(self, ruleFormula)
Find a set of rules in N3QL
gatherRules(self, ruleFormula)
gatherSparqlQueries(self, ruleFormula)
Find the rules in SPARQL
run(self)
Perform task.
Return number of  new facts
runLaborious(self)
Perform task.
Return number of  new facts.
Start again if new rule mayhave been generated.
runSmart(self)
Run the rules by mapping rule interactions first

 
class Query(formula.Formula)
    A query holds a hypothesis/antecedent/template which is being matched aginst (unified with)
the knowledge base.
 
 
Method resolution order:
Query
formula.Formula
term.AnonymousNode
term.Node
term.CompoundTerm
term.Term
__builtin__.object

Methods defined here:
__init__(self, store, unmatched=[], template=None, variables=set([]), existentials=set([]), workingContext=None, conclusion=None, targetContext=None, already=None, rule=None, interpretBuiltins=0, justOne=0, justReturn=0, mode='', meta=None)
checkRedirectsInAlready(self)
Kludge
conclude(self, bindings, evidence=[], extraBNodes=set([]))
When a match found in a query, add conclusions to target formula.
 
Returns the number of statements added.
matchFormula(query, queue, variables, existentials, bindings=Env({}), newBindings=Env({}), evidence=[])
Iterate on the remaining query items
bindings      collected matches already found
newBindings  matches found and not yet applied - used in recursion
 
You probably really need the state diagram to understand this
http://www.w3.org/2000/10/swap/doc/states.svg
even if it is a bit out of date.
redirect(self, redirections)
remoteQuery(query, items)
Perform remote query as client on remote store
Currently  this only goes to an SQL store, but should later use SPARQL etc
in remote HTTP/SOAP call.
resolve(self)

Methods inherited from formula.Formula:
__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, 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.
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 inherited from formula.Formula:
resetRenames(reset=True)

Methods inherited from term.AnonymousNode:
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 QueryItem(formula.StoredStatement)
    One line in a query being resolved.
 
To a large extent, each line can keep track of its own state.
When a remote query is done, query lines to the same server have to be coordinated again.
 
  Methods defined here:
__init__(self, query, quad)
__repr__(self)
Diagnostic string only
bindNew(self, newBindings)
Take into account new bindings from query processing to date
 
The search may get easier, and builtins may become ready to run.
Lists may either be matched against store by searching,
and/or may turn out to be bound and therefore ready to run.
 
Return:
    0           No way can this item ever complete.
    None        Binding done, see item.state
canRun(self)
Is this built-in ready to run?
clone(self)
Take a copy when for iterating on a query
doIncludes(item, queue, existentials, variables, bindings)
Implement log:includes by adding all the statements on the LHS
to the query items.  The plan is that this should
doSupports(item, queue, existentials, variables, bindings)
Real implementation of log:supports. Have fun.
searchDone(self)
Search has been done: figure out next state.
setup(self, allvars, unmatched, interpretBuiltins=[], mode='')
Check how many variables in this term,
and how long it would take to search
 
Returns, true normally or false if there is no way this query will work.
Only called on virgin query item.
The mode is a set of character flags about how we think.
tryBuiltin(self, queue, bindings, evidence)
Check for  built-in functions to see whether it will resolve.
Return codes:  0 - give up; 
        [] - success, no new bindings, (action has been called)
        [...] list of binding lists, each a pair of bindings and reason.
tryDeepSearch(self, queue)
Search the store, unifying nested compound structures
 
Returns lists of list of bindings, attempting if necessary to unify
any nested compound terms. Added 20030810, not sure whether it is worth the
execution time in practice. It could be left to a special magic built-in like "matches"
or something if it is only occasionnaly used.
 
Used in state S_NEED_DEEP
updateMyIndex(self, formula)

Methods inherited from formula.StoredStatement:
__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)
__len__(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)

 
class Queue(__builtin__.list)
    
Method resolution order:
Queue
__builtin__.list
__builtin__.object

Methods defined here:
__init__(self, other=[], metaSource=None)
popBest(self)

Data and other attributes defined here:
__slots__ = ['statements', 'bNodes']
bNodes = <member 'bNodes' of 'Queue' objects>
list = <type 'list'>
list() -> new list
list(sequence) -> new list initialized from sequence's items
statements = <member 'statements' of 'Queue' objects>

Methods inherited from __builtin__.list:
__add__(...)
x.__add__(y) <==> x+y
__contains__(...)
x.__contains__(y) <==> y in x
__delitem__(...)
x.__delitem__(y) <==> del x[y]
__delslice__(...)
x.__delslice__(i, j) <==> del x[i:j]
 
Use of negative indices is not supported.
__eq__(...)
x.__eq__(y) <==> x==y
__ge__(...)
x.__ge__(y) <==> x>=y
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__gt__(...)
x.__gt__(y) <==> x>y
__hash__(...)
x.__hash__() <==> hash(x)
__iadd__(...)
x.__iadd__(y) <==> x+=y
__imul__(...)
x.__imul__(y) <==> x*=y
__iter__(...)
x.__iter__() <==> iter(x)
__le__(...)
x.__le__(y) <==> x<=y
__len__(...)
x.__len__() <==> len(x)
__lt__(...)
x.__lt__(y) <==> x<y
__mul__(...)
x.__mul__(n) <==> x*n
__ne__(...)
x.__ne__(y) <==> x!=y
__repr__(...)
x.__repr__() <==> repr(x)
__reversed__(...)
L.__reversed__() -- return a reverse iterator over the list
__rmul__(...)
x.__rmul__(n) <==> n*x
__setitem__(...)
x.__setitem__(i, y) <==> x[i]=y
__setslice__(...)
x.__setslice__(i, j, y) <==> x[i:j]=y
 
Use  of negative indices is not supported.
append(...)
L.append(object) -- append object to end
count(...)
L.count(value) -> integer -- return number of occurrences of value
extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value
insert(...)
L.insert(index, object) -- insert object before index
pop(...)
L.pop([index]) -> item -- remove and return item at index (default last)
remove(...)
L.remove(value) -- remove first occurrence of value
reverse(...)
L.reverse() -- reverse *IN PLACE*
sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

Data and other attributes inherited from __builtin__.list:
__new__ = <built-in method __new__ of type object at 0xfe9ec>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
class Rule
     Methods defined here:
__init__(self, task, antecedent, consequent, statement, variables)
Try a rule
 
Beware lists are corrupted. Already list is updated if present.
The idea is that, for a rule which may be tried many times, the constant 
processing is done in this rather than in Query().
 
The already dictionary is used to track bindings.
This less useful when not repeating (as in --filter), but as in fact
there may be several ways in which one cane get the same bindings,
even without a repeat.
__repr__(self)
compareByAffects(other)
once(self)
traceForward(self, r1)

 
Functions
       
applyQueries(workingContext, ruleFormula=None, targetContext=None)
Once, nothing recusive, for a N3QL query
applyRules(workingContext, ruleFormula=None, targetContext=None)
Once
applySparqlQueries(workingContext, ruleFormula=None, targetContext=None)
Once, nothing recusive, for a N3QL query
bindingsToString(bindings)
buildPattern(workingContext, template)
Make a list of unmatched statements including special
builtins to check something is universally quantified
buildStrictPattern(workingContext, template)
compareCyclics(self, other)
Note the set indirectly affected is the same for any
member of a cyclic subsystem
hasFormula(l)
lookupQuad(bindings, q)
Return a subsituted quad
lookupQuadRecursive(bindings, q, why=None)
n3Equivalent(g, f, env1, env2, vars=set([]), universals=set([]), existentials=set([]), n1Source=42, n2Source=42)
Return whether or nor f contains a top-level formula equvalent to g.
Just a test: no bindings returned.
partialOrdered(cy1, pool)
Return sequence conforming to the partially order in a set of cyclic subsystems
 
Basially, we find the dependencies of a node and remove them from the pool.
Then, any node in the pool can be done earlier, because has no depndency from those done.
quadToString(q, neededToRun=[[], [], [], []], pattern=[1, 1, 1, 1])
queueToString(queue)
#   DIAGNOSTIC STRING OUTPUT
returnWrapper(f)
seqToString(set)
setToString(set)
smarterSubstitution(f, bindings, source, why=None, exception=[])
sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
testIncludes(f, g, _variables=set([]), bindings={}, interpretBuiltins=0)
Return whether or nor f contains a top-level formula equvalent to g.
Just a test: no bindings returned.
think(knowledgeBase, ruleFormula=None, mode='', why=None)
Forward-chaining inference
 
In the case in which rules are added back into the
store. The store is used for read (normally canonical) and write
(normally open) at the samne time.  It in fact has to be open.

 
Data
        ALL4 = (0, 1, 2, 3)
BNone = <query.BetterNone object>
BuiltinFeedError = (<class term.ArgumentNotLiteral>, <class term.UnknownType>)
CONTEXT = 0
INFINITY = 1000000000
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
QL_NS = 'http://www.w3.org/2004/ql#'
RDF_NS_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
SPARQL_NS = 'http://www.w3.org/2000/10/swap/sparqlCwm'
SUBJ = 2
S_DONE = 80
S_HEAVY_READY = 40
S_HEAVY_WAIT = 20
S_LIGHT_EARLY = 50
S_LIGHT_GO = 65
S_LIGHT_UNS_READY = 70
S_LIGHT_WAIT = 30
S_NEED_DEEP = 45
S_NOT_LIGHT = 60
S_REMOTE = 10
S_UNKNOWN = 99
becauseSubexpression = <why.BecauseSubexpression instance>
chatty_flag = 0
forAllSym = 'http://www.w3.org/2000/10/swap/log#forAll'
forSomeSym = 'http://www.w3.org/2000/10/swap/log#forSome'
nextRule = 0
stateName = {10: 'Remote', 20: 'HvWait', 30: 'LtWait', 40: 'HvGo', 45: 'Deep', 50: 'LtEarly', 60: 'NotLt', 65: 'LtGo', 70: 'LtUsGo', 80: 'DONE', ...}
tracking = 0