Rule

Rule module holds the base implementation of a rule, Rule. Rules combine features and mappers to a functional body, where each feature also has a weight attached to it.

class creamas.rules.rule.Rule(rules, weights, evaluation='ave')[source]

A Rule is a treelike data structure consisting of other Rule and RuleLeaf instances. Rules can be used by agents to evaluate artifacts.

Like features, rules offer a simple interface where artifact can be evaluated by calling a rule instance with artifact as the only argument. Rules should return a float in [-1, 1] when evaluated.

from creamas.core.rule import Rule
rule = Rule([myleaf, myleaf2, myrule], [1.0, 1.0, 1.0])
res = rule(myartifact)
Parameters
  • rules (list) – Subrules for this rule. Subrule can be either an iterable of length 2, the (Feature, Mapper)-pair, or another Rule instance.

  • weights (list) – Weights for the subrules.

  • evaluation

    How rule’s internal evaluation is done. Either one of the predefined functions, or user defined callable which takes two arguments: rule and artifact (in that order). Predefined functions and their keywords are:

    • ’ave’: weighted_average()

    • ’min’: minimum()

    • ’max’: maximum()

add_subrule(subrule, weight)[source]

Add subrule to the rule.

Parameters
  • subrule – Subrule to add to this rule, an instance of Rule or RuleLeaf.

  • weight (float) – Weight of the subrule

property R

A list of subrules in this rule.

property W

A list of weights for subrules in this rule.

property domains

Rule’s acceptable artifact domains is the union of all its subrules acceptable domains. Each artifact is evaluated only with subrules that do not return None when the feature is evaluated with it.

class creamas.rules.rule.RuleLeaf(feat, mapper)[source]

Leaf implementation for rules.

A RuleLeaf combines a feature and a mapper into one functional unit. Adding two RuleLeaf instances together will result in an instance of Rule. Two instances of RuleLeaf are equal if their features are equal, mappers are not considered.

Parameters
  • feat (py:class:~creamas.core.feature.Feature) – Feature for this leaf rule.

  • mapper (py:class:~creamas.core.mapper.Mapper) – Mapper for this leaf rule

property domains

Domains for this rule leaf.

property feat

The feature for this rule leaf.

property mapper

The mapper used in this rule leaf.