strux_pr 0.1.2
strux_pr: ^0.1.2 copied to clipboard
A structural precedence engine for Dart and Flutter. Resolves state ambiguity using compile-time DAG resolution.
example/strux_pr_example.dart
import 'package:strux_pr/strux_pr.dart';
part 'strux_pr_example.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');
}
}