pymor.algorithms.rules

Module Contents

Classes

rule

Decorator to make a method a rule in a given RuleTable.

match_class_base

Decorator to make a method a rule in a given RuleTable.

match_class

rule that matches when obj is instance of one of the given classes.

match_class_all

rule that matches when each item of obj is instance of one of the given classes.

match_class_any

rule that matches when any item of obj is instance of one of the given classes.

match_always

rule that always matches.

match_generic

rule with matching condition given by an arbitrary function.

RuleTableMeta

Meta class for RuleTable.

RuleTable

Define algorithm by a table of match conditions and corresponding actions.

Functions

print_children

format_rules

print_rules

class pymor.algorithms.rules.rule[source]

Decorator to make a method a rule in a given RuleTable.

The decorated function will become the action to perform in case the rule matches. Matching conditions are specified by subclassing and overriding the matches method.

If an action is decorated by multiple rules, all these rules must match for the action to apply.

action[source]

Method to call in case the rule matches.

condition_description[source]
condition_type[source]
__call__(self, action)[source]
abstract _matches(self, obj)[source]

Returns True if given object matches the condition.

matches(self, obj)[source]

Returns True if given object matches the condition.

_ipython_display_(self)[source]
__repr__(self)[source]

Return repr(self).

property action_description(self)[source]
property source(self)[source]
class pymor.algorithms.rules.match_class_base(*classes)[source]

Bases: rule

Decorator to make a method a rule in a given RuleTable.

The decorated function will become the action to perform in case the rule matches. Matching conditions are specified by subclassing and overriding the matches method.

If an action is decorated by multiple rules, all these rules must match for the action to apply.

action[source]

Method to call in case the rule matches.

class pymor.algorithms.rules.match_class(*classes)[source]

Bases: match_class_base

rule that matches when obj is instance of one of the given classes.

condition_type = CLASS[source]
_matches(self, obj)[source]

Returns True if given object matches the condition.

class pymor.algorithms.rules.match_class_all(*classes)[source]

Bases: match_class_base

rule that matches when each item of obj is instance of one of the given classes.

condition_type = ALLCLASSES[source]
_matches(self, obj)[source]

Returns True if given object matches the condition.

class pymor.algorithms.rules.match_class_any(*classes)[source]

Bases: match_class_base

rule that matches when any item of obj is instance of one of the given classes.

condition_type = ANYCLASS[source]
_matches(self, obj)[source]

Returns True if given object matches the condition.

class pymor.algorithms.rules.match_always(action)[source]

Bases: rule

rule that always matches.

condition_type = ALWAYS[source]
_matches(self, obj)[source]

Returns True if given object matches the condition.

class pymor.algorithms.rules.match_generic(condition, condition_description=None)[source]

Bases: rule

rule with matching condition given by an arbitrary function.

Parameters

condition

Function of one argument which checks if given object matches condition.

condition_description

Optional string describing the condition implemented by condition.

condition_type = GENERIC[source]
_matches(self, obj)[source]

Returns True if given object matches the condition.

class pymor.algorithms.rules.RuleTableMeta(cls, name, bases, namespace)[source]

Bases: pymor.core.base.UberMeta

Meta class for RuleTable.

__str__[source]
__repr__(cls)[source]

Return repr(self).

__getitem__(cls, idx)[source]
class pymor.algorithms.rules.RuleTable(use_caching=False)[source]

Bases: pymor.core.base.BasicObject

Define algorithm by a table of match conditions and corresponding actions.

RuleTable manages a table of rules, stored in the rules attributes, which can be applied to given objects.

A new table is created by subclassing RuleTable and defining new methods which are decorated with match_class, match_generic or another rule subclass. The order of the method definitions determines the order in which the defined rules are applied.

Parameters

use_caching

If True, cache results of apply.

rules[source]

list of all defined rules.

insert_rule(cls, index, rule_)[source]
append_rule(cls, rule_)[source]
classmethod breakpoint_for_obj(cls, obj)[source]

Add a conditional breakpoint for given object.

Break execution in apply, when being applied to a certain object.

Parameters

obj

Object for which to add the conditional breakpoint.

classmethod breakpoint_for_name(cls, name)[source]

Add a conditional breakpoint for objects of given name.

Break execution in apply, when being applied to an object with a certain name.

Parameters

name

name of the object for which to add the conditional breakpoint.

apply(self, obj)[source]

Sequentially apply rules to given object.

This method iterates over all rules of the given RuleTable. For each rule, it is checked if it matches the given object. If False, the next rule in the table is considered. If True the corresponding action is executed with obj as parameter. If execution of action raises RuleNotMatchingError, the rule is considered as not matching, and execution continues with evaluation of the next rule. Otherwise, execution is stopped and the return value of rule.action is returned to the caller.

If no rule matches, a NoMatchingRuleError is raised.

Parameters

obj

The object to apply the RuleTable to.

Returns

Return value of the action of the first matching rule in the table.

Raises

NoMatchingRuleError

No rule could be applied to the given object.

apply_children(self, obj, children=None)[source]

Apply rules to all children of the given object.

This method calls apply to each child of the given object. The children of the object are either provided by the children parameter or automatically inferred by the get_children method.

Parameters

obj

The object to apply the RuleTable to.

children

None or a list of attribute names defining the children to consider.

Returns

Result of : meth:apply for all given children.

replace_children(self, obj, children=None)[source]

Replace children of object according to rule table.

Same as apply_children, but additionally calls obj.with_ to replace the children of obj with the result of the corresponding apply call.

classmethod get_children(cls, obj)[source]

Determine children of given object.

This method returns a list of the names of all attributes a, for which one of the folling is true:

  1. a is an Operator.

  2. a is a mapping and each of its values is either an Operator or None.

  3. a is an iterable and each of its elements is either an Operator or None.

__repr__(self)[source]

Return repr(self).

pymor.algorithms.rules.print_children(obj)[source]
pymor.algorithms.rules.format_rules(rules)[source]
pymor.algorithms.rules.print_rules(rules)[source]