Skip to content

router::rules::RuleEngine

More...

Public Functions

Name
None init(self self, Optional rules_path[Path] =None, Optional rules_str[str] =None, float confidence_threshold =0.5)
ExecutionPlan classify(self self, str query)
None reload(self self, Optional rules_str[str] =None, Optional rules_path[Path] =None)

Protected Functions

Name
None _load_from_file(self self, Path path)
None _load_from_string(self self, str yaml_str)
None _load_from_yaml(self self, Any data, str source)
List _collect_matches(self self, str query)
float _compute_confidence(self self, Dict rule[str, Any], str query)
None _validate_rule(Dict rule[str, Any], int idx, str source)
None _validate_priorities(List] rules[Dict[str, Any])
float _keyword_confidence(Dict rule[str, Any], str query)
float _regex_confidence(Dict rule[str, Any], str query)
float _syntax_density_confidence(Dict rule[str, Any], str query)
None _validate_input_size(str query)

Protected Attributes

Name
_rules
_default_specialist

Detailed Description

class router::rules::RuleEngine;
Loads YAML rules and classifies queries via deterministic matching.

Construct from a YAML file path or a YAML string.  Validation runs at
construction time — a ``RouterConfigError`` is raised for any schema
violation (missing fields, duplicate priorities, etc.).

Args:
    rules_path: Path to a ``router_rules.yaml`` file on disk.
    rules_str: Inline YAML string with rule definitions (for testing).
        Mutually exclusive with *rules_path*.
    confidence_threshold: Minimum confidence before fallback chaining
        is triggered (default 0.5).

Raises:
    RouterConfigError: If the rules YAML fails schema validation.

Public Functions Documentation

function init

None __init__(
    self self,
    Optional rules_path[Path] =None,
    Optional rules_str[str] =None,
    float confidence_threshold =0.5
)

function classify

ExecutionPlan classify(
    self self,
    str query
)
Classify *query* and return an ExecutionPlan.

The classification pipeline:
1. Input size validation (100 KB limit)
2. Run all non-default rules in priority order (lower = higher pri)
3. First match with confidence >= threshold → return immediately
4. If no match passes threshold, run fallback chain over remaining
   matches (bounded to 5 iterations)
5. If fallback exhausted or nothing matched, return default specialist

Args:
    query: The raw input text to classify.

Returns:
    ExecutionPlan with the selected specialist and routing metadata.

Raises:
    RouterConfigError: If the input exceeds the 100 KB size limit.

function reload

None reload(
    self self,
    Optional rules_str[str] =None,
    Optional rules_path[Path] =None
)
Reload rules from a new YAML source without reinstantiating.

Useful for YAML-driven-rule tests that rewrite config and re-classify.

Protected Functions Documentation

function _load_from_file

None _load_from_file(
    self self,
    Path path
)

function _load_from_string

None _load_from_string(
    self self,
    str yaml_str
)

function _load_from_yaml

None _load_from_yaml(
    self self,
    Any data,
    str source
)

function _collect_matches

List _collect_matches(
    self self,
    str query
)
Return list of (rule, confidence) sorted by priority ascending.

Default rules (is_default=True) are excluded.

function _compute_confidence

float _compute_confidence(
    self self,
    Dict rule[str, Any],
    str query
)

function _validate_rule

static None _validate_rule(
    Dict rule[str, Any],
    int idx,
    str source
)

function _validate_priorities

static None _validate_priorities(
    List] rules[Dict[str, Any]
)
Ensure no duplicate priorities across non-default rules (T-02-12).```


### function _keyword_confidence

```python
static float _keyword_confidence(
    Dict rule[str, Any],
    str query
)

function _regex_confidence

static float _regex_confidence(
    Dict rule[str, Any],
    str query
)

function _syntax_density_confidence

static float _syntax_density_confidence(
    Dict rule[str, Any],
    str query
)

function _validate_input_size

static None _validate_input_size(
    str query
)

Protected Attributes Documentation

variable _rules

_rules;

variable _default_specialist

_default_specialist;

Updated on 2026-10-09 at 03:59:43 +0000