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

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

 
Modules
            
diag
sys
types
 
Classes
            
CyclicSetOfRules
exceptions.Exception
BuiltInFailed
InferenceTask
Query
Rule
formula.StoredStatement
QueryItem
 
class BuiltInFailed(exceptions.Exception)
       
   Methods defined here:
__init__(self, info, item, pred)
__str__(self)

Data and non-method functions defined here:
__doc__ = None
__module__ = 'query'

Methods inherited from exceptions.Exception:
__getitem__(...)
 
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

Data and non-method functions defined here:
__doc__ = 'A set of rules which are connected\n '
__module__ = 'query'
 
class InferenceTask
      A task of applying rules or filters to information
 
   Methods defined here:
__init__(self, workingContext, ruleFormula=None, targetContext=None, universals=[], 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 (premise, precondidtion,
antecedent) to the conclusion (postcondition).
gatherQueries(self, ruleFormula)
Find a set of rules in N3QL
gatherRules(self, ruleFormula)
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

Data and non-method functions defined here:
__doc__ = 'A task of applying rules or filters to information'
__module__ = 'query'
 
class Query
      A query holds a hypothesis/antecedent/template which is being matched aginst (unified with)
the knowledge base.
 
   Methods defined here:
__init__(self, store, unmatched=[], template=None, variables=[], existentials=[], workingContext=None, conclusion=None, targetContext=None, already=None, rule=None, smartIn=[], justOne=0, mode='', meta=None)
checkRedirectsInAlready(self)
Kludge
conclude(self, bindings, evidence=[])
When a match found in a query, add conclusions to target formula.
 
Returns the number of statements added.
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 RDFQL/DAMLQL etc
in remote HTTP/SOAP call.
resolve(self)
unify(query, queue, variables, existentials, bindings={}, newBindings={}, 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.

Data and non-method functions defined here:
__doc__ = 'A query holds a hypothesis/antecedent/template w...hed aginst (unified with)\n the knowledge base.'
__module__ = 'query'
 
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
searchDone(self)
Search has been done: figure out next state.
setup(self, allvars, unmatched, smartIn=[], mode='')
Check how many variables in this term,
and how long it would take to search
 
Returns, [] normally or 0 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, heavy, 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)
Search the store, matching 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

Data and non-method functions defined here:
__doc__ = 'One line in a query being resolved.\n \n To ...he same server have to be coordinated again.\n '
__module__ = 'query'

Methods inherited from formula.StoredStatement:
__getitem__(self, i)
__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
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
 
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().
__repr__(self)
compareByAffects(other)
once(self)
traceForward(self, r1)

Data and non-method functions defined here:
__doc__ = None
__module__ = 'query'
 
Functions
            
_substitute(bindings, list)
Subsitustes IN-LINE into a list of quads
applyQueries(workingContext, ruleFormula=None, targetContext=None)
Once, nothing recusive, for a N3QL query
applyRules(workingContext, ruleFormula=None, targetContext=None)
Once
bindingsToString(bindings)
compareCyclics(self, other)
Note the set indirectly affected is the same for any member of a cyclic subsystem
lookupQuad(bindings, q)
Return a subsituted quad
lookupQuadRecursive(bindings, q, why=None)
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
seqToString(set)
setToString(set)
testIncludes(f, g, _variables=[], bindings={})
Return whether or nor f contains a top-level formula equvalent to g.
Just a test: no bindings returned.
think(knowledgeBase, ruleFormula=None, mode='')
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)
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#'
SUBJ = 2
S_DONE = 80
S_HEAVY_READY = 40
S_HEAVY_WAIT = 20
S_LIGHT_EARLY = 50
S_LIGHT_GO = 65
S_LIGHT_UNS_GO = 70
S_LIGHT_WAIT = 30
S_NEED_DEEP = 45
S_NOT_LIGHT = 60
S_REMOTE = 10
S_SATISFIED = 0
S_UNKNOWN = 99
__file__ = './query.py'
__name__ = 'query'
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
tracking = 0