本文整理匯總了Python中rule.Rule.add_variable方法的典型用法代碼示例。如果您正苦於以下問題:Python Rule.add_variable方法的具體用法?Python Rule.add_variable怎麽用?Python Rule.add_variable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rule.Rule
的用法示例。
在下文中一共展示了Rule.add_variable方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: AssertAggregate
# 需要導入模塊: from rule import Rule [as 別名]
# 或者: from rule.Rule import add_variable [as 別名]
class AssertAggregate(Assert):
def __init__(self, **kwargs):
self.map_reduce_values = {}
for key, value in kwargs.items():
if isinstance(value, MapReduceFunc):
del kwargs[key]
subkeys = []
for n, map_value in enumerate(value.map_values):
subkey = key if n == 0 else '{}_{}'.format(key, n+1)
kwargs[subkey] = map_value
subkeys.append(subkey)
self.map_reduce_values[key] = (value, subkeys)
super(AssertAggregate, self).__init__(**kwargs)
def prepare_rule(self, rule):
self.data.update({key : normalize_expr(value) for key, value in rule.groupby.iteritems()})
self.data[UNIQUE_INDEX_FIELD] = gensym()
super(AssertAggregate, self).prepare_rule(rule)
self._create_reduce_rule(rule)
def _create_reduce_rule(self, map_rule):
r1 = map_rule.target[0]
r2 = map_rule.target[1]
self.reduce_rule = Rule()
self.reduce_rule.set_name('_Reduce')
for field_name in map_rule.groupby:
self.reduce_rule.add_variable(getattr(r1, field_name), getattr(r2, field_name))
self.reduce_rule.add_condition(fact_index(r1) != fact_index(r2))
reduce_kwargs = {}
for field_name, (value, subfields) in self.map_reduce_values.iteritems():
v1 = [getattr(r1, field_name) for field_name in subfields]
v2 = [getattr(r2, field_name) for field_name in subfields]
if len(subfields) == 1:
v1 = v1[0]
v2 = v2[0]
reduced = value._reduce(v1, v2)
if len(subfields) == 1:
reduced = [reduced]
reduce_kwargs.update(dict(zip(subfields, reduced)))
self.reduce_rule.add_action(Delete(r2))
self.reduce_rule.add_action(Update(r1, **reduce_kwargs))
self.reduce_rule.set_salience(map_rule.salience)
map_rule.add_secondary_rule(self.reduce_rule)