Skip to main content

strux_pr 0.1.2 copy "strux_pr: ^0.1.2" to clipboard
strux_pr: ^0.1.2 copied to clipboard

A structural precedence engine for Dart and Flutter. Resolves state ambiguity using compile-time DAG resolution.

strux_pr #

A deterministic structural precedence engine code generator for Dart and Flutter.

strux_pr resolves rule precedence at build time using a directed acyclic graph (DAG), then generates a deterministic dispatcher for your rule container.

Features #

  • Compile-time precedence resolution.
  • Explicit rule relationships using subscope and overrideTarget.
  • Circular dependency detection.
  • Duplicate rule ID detection.
  • Validation of rule references.
  • Validation of rule methods.
  • Deterministic declaration ordering for unrelated rules.
  • Generated dispatch code with no runtime graph traversal.
  • Support for rule IDs containing quotes, backslashes, newlines, and Unicode.
  • Compatible with Dart build_runner and source_gen.

Installation #

Add strux_pr to your dependencies and build_runner to your development dependencies.

dependencies:
  strux_pr: ^0.1.1

dev_dependencies:
  build_runner: ^2.4.0

Usage #

Annotate a class with @PrecedenceEngine() and implement PrecedenceRuleContainer<TContext>.

Annotate executable rule methods with @Rule().

import 'package:strux_pr/strux_pr.dart';

part 'my_engine.g.dart';

@PrecedenceEngine()
class RulesContainer implements PrecedenceRuleContainer<String> {
  @Rule(id: 'base')
  String executeBase() => 'BASE';

  @Rule(
    id: 'specific',
    subscope: 'base',
  )
  String executeSpecific() => 'SPECIFIC';

  @Rule(
    id: 'emergency',
    overrideTarget: 'specific',
  )
  String executeEmergency() => 'EMERGENCY';

  @override
  bool canExecuteRule(String ruleId, String context) {
    switch (ruleId) {
      case 'emergency':
        return context == 'EMERGENCY';

      case 'specific':
        return context == 'SPECIFIC';

      case 'base':
        return context == 'NORMAL';

      default:
        return false;
    }
  }
}

Run the generator with:

dart run build_runner build

The generated part provides:

final result = engine.dispatchRule(context);

The dispatcher evaluates rules in the precedence order determined at build time.

Precedence #

A rule can reference another rule using either:

  • subscope
  • overrideTarget

For example:

@Rule(id: 'base')
String base() => 'base';

@Rule(
  id: 'specific',
  subscope: 'base',
)
String specific() => 'specific';

@Rule(
  id: 'emergency',
  overrideTarget: 'specific',
)
String emergency() => 'emergency';

The relationships form a precedence graph:

emergency
    |
    v
specific
    |
    v
base

Rules with explicit precedence relationships are ordered according to the dependency graph.

Rules that are unrelated by explicit precedence relationships retain their declaration order. This provides deterministic output without inventing precedence relationships that were not specified by the user.

Rule validation #

The generator validates the rule graph and rule declarations during code generation.

Generation fails for invalid configurations such as:

  • Duplicate rule IDs.
  • Unknown subscope references.
  • Unknown overrideTarget references.
  • A rule using itself as its subscope.
  • A rule using itself as its overrideTarget.
  • Circular precedence dependencies.
  • An engine containing no @Rule methods.
  • A @PrecedenceEngine class that does not implement PrecedenceRuleContainer<TContext>.
  • Rule methods that accept parameters.
  • Static rule methods.
  • Abstract rule methods.
  • Getter, setter, and operator rule methods.
  • void-returning rule methods.
  • Empty or whitespace-only rule IDs.
  • Abstract @PrecedenceEngine classes.
  • Generic @PrecedenceEngine classes.

For example, this is invalid:

@Rule(
  id: 'specific',
  subscope: 'does_not_exist',
)
String specific() => 'specific';

The generator reports the invalid reference during the build instead of allowing an invalid precedence graph to reach runtime.

Rule methods #

Rule methods are invoked by the generated dispatcher and therefore must be instance methods with no parameters.

Valid:

@Rule(id: 'payment')
String processPayment() => 'payment';

Invalid:

@Rule(id: 'payment')
String processPayment(String value) => value;

Invalid:

@Rule(id: 'payment')
static String processPayment() => 'payment';

Invalid:

@Rule(id: 'payment')
String processPayment();

Rule IDs #

Rule IDs are strings and may contain characters that require escaping in generated Dart source, including:

@Rule(id: 'user "admin"')
String quoted() => 'quoted';

@Rule(id: r'path\to\rule')
String pathRule() => 'path';

@Rule(id: 'line\nbreak')
String newlineRule() => 'newline';

@Rule(id: '支付_规则_🚀')
String unicodeRule() => 'unicode';

The generator escapes rule IDs when emitting generated Dart code.

Runtime contract #

PrecedenceRuleContainer<TContext> provides the runtime precondition check:

abstract class PrecedenceRuleContainer<TContext> {
  bool canExecuteRule(String ruleId, TContext context);
}

The generated dispatcher calls canExecuteRule() for each rule in the resolved precedence order.

For example:

@override
bool canExecuteRule(String ruleId, String context) {
  switch (ruleId) {
    case 'emergency':
      return context == 'EMERGENCY';

    case 'specific':
      return context == 'SPECIFIC';

    case 'base':
      return context == 'NORMAL';

    default:
      return false;
  }
}

If no rule matches the supplied context, the generated dispatcher throws a StateError.

Example #

A complete example is available in:

example/strux_pr_example.dart

Run it with:

dart run example/strux_pr_example.dart

Example output:

Context: NORMAL -> Processed Standard Card Payment
Context: EXCEEDS_LIMIT -> Payment Denied: Exceeds allowed limit
Context: SYSTEM_EMERGENCY -> Emergency Stop: All transactions suspended
Context: UNSUPPORTED_CONTEXT -> Bad state: No deterministic rule matched the given context.

Known limitations #

  • Only methods declared directly in the annotated class are scanned; rules defined via mixins or superclasses are not included.
  • Rule methods must return a non-void value.

Testing #

Run the package checks with:

dart format lib test
dart analyze
dart test

To regenerate source files:

dart run build_runner clean
dart run build_runner build

License #

MIT

0
likes
0
points
1.74k
downloads

Publisher

unverified uploader

Weekly Downloads

A structural precedence engine for Dart and Flutter. Resolves state ambiguity using compile-time DAG resolution.

Repository (GitLab)
View/report issues

License

unknown (license)

Dependencies

analyzer, build, source_gen

More

Packages that depend on strux_pr