class chart_indirect(dict):
def __missing__(self, key):
arity = int(key.split('/')[-1])
- c = self[key] = Chart(name = key, ncols = arity + 1) # +1 for value
+ c = self[key] = Chart(name = key, arity = arity)
return c
class Chart(object):
- def __init__(self, name, ncols):
+ def __init__(self, name, arity):
self.name = name
- self.ncols = ncols
+ self.arity = arity
self.intern = {} # args -> term
- self.ix = [defaultdict(set) for _ in xrange(ncols-1)] # TODO: no index on values yet.
+ self.ix = [defaultdict(set) for _ in xrange(arity)]
def __repr__(self):
rows = [term for term in self.intern.values() if term.value is not None]
return '%s\n=================\n%s' % (self.name, x)
def __getitem__(self, s):
-
- assert isinstance(s, tuple) and len(s) == self.ncols, \
- 'item width mismatch: ncols %s, item %s' % (self.ncols, len(s))
+ assert len(s) == self.arity + 1, \
+ 'item width mismatch: arity %s, item %s' % (self.arity, len(s))
args, val = s[:-1], s[-1]
assert val is not None
+ # filter set of candidates by each bound argument
candidates = None
-
for (ix, x) in zip(self.ix, args):
if isinstance(x, slice):
continue
else:
candidates &= ix[x]
if not len(candidates):
+ # no candidates left
break
- # all arguments must be bound.
if candidates is None:
+ # This happens when all arguments are free.
candidates = self.intern.values()
# handle the value column separately because we don't index it yet.
def lookup(self, args):
"find index for these args"
- assert len(args) == self.ncols - 1
+ assert len(args) == self.arity
try:
return self.intern[args]
e()
+def emit(item, val, ruleix, variables, delete):
+
+ print >> trace, (red % 'delete' if delete else green % 'update'), \
+ '%s (val %s; curr: %s)' % (item, val, item.value)
+
+ if delete:
+ item.aggregator.dec(val, ruleix, variables)
+ else:
+ item.aggregator.inc(val, ruleix, variables)
+
+ agenda[item] = 0 # everything is high priority
+
+
def peel(fn, item):
"""
Find item's args in the appropriate chart. Assert that idx matches
return item.args
-def emit(item, val, ruleix, variables, delete):
-
- print >> trace, (red % 'delete' if delete else green % 'update'), \
- '%s (val %s; curr: %s)' % (item, val, item.value)
-
- if delete:
- item.aggregator.dec(val, ruleix, variables)
- else:
- item.aggregator.inc(val, ruleix, variables)
-
- agenda[item] = 0 # everything is high priority
-
-
changed = {}
def _go():