Skip to main content

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

Pure-Dart structural precedence engine runtime and annotations.

example/main.dart

import 'package:strux_pr/strux_pr.dart';

part 'main.g.dart';

/// Demonstrates structural rule precedence using [PrecedenceEngine].
@PrecedenceEngine()
class PaymentProcessor implements PrecedenceRuleContainer<String> {
  /// Base rule.
  @Rule(id: 'base_card_payment')
  String processStandardCard() {
    return 'Processed Standard Card Payment';
  }

  /// More specific rule that takes precedence over [base_card_payment].
  @Rule(id: 'bounds_check', subscope: 'base_card_payment')
  String processLimitViolation() {
    return 'Payment Denied: Exceeds allowed limit';
  }

  /// Explicit override of [bounds_check].
  ///
  /// Because this rule targets [bounds_check], it is evaluated before the
  /// rule it overrides.
  @Rule(id: 'emergency_system_override', overrideTarget: 'bounds_check')
  String processEmergencyHalt() {
    return 'Emergency Stop: All transactions suspended';
  }

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

      case 'bounds_check':
        return context == 'EXCEEDS_LIMIT';

      case 'base_card_payment':
        return context != 'UNSUPPORTED_CONTEXT';

      default:
        return false;
    }
  }
}

void main() {
  final processor = PaymentProcessor();

  // Base rule.
  print(
    'Context: NORMAL -> '
    '${processor.dispatchRule('NORMAL')}',
  );

  // `bounds_check` is a subscope of `base_card_payment`, so it is evaluated
  // before the base rule.
  print(
    'Context: EXCEEDS_LIMIT -> '
    '${processor.dispatchRule('EXCEEDS_LIMIT')}',
  );

  // `emergency_system_override` explicitly overrides `bounds_check`, so it
  // is evaluated first when the emergency condition is satisfied.
  print(
    'Context: SYSTEM_EMERGENCY -> '
    '${processor.dispatchRule('SYSTEM_EMERGENCY')}',
  );

  // No rule matches this context, so the generated dispatcher throws
  // StateError.
  try {
    processor.dispatchRule('UNSUPPORTED_CONTEXT');
  } on StateError catch (error) {
    print('Context: UNSUPPORTED_CONTEXT -> $error');
  }
}
0
likes
0
points
1.74k
downloads

Publisher

unverified uploader

Weekly Downloads

Pure-Dart structural precedence engine runtime and annotations.

Repository (GitLab)
View/report issues

Topics

#structural-precedence #rule-engine #precedence-engine #annotations

License

unknown (license)

More

Packages that depend on strux_pr