Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions README.md

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be prudent to mention this new feature at the very top of the README file also, where it defines what is drules. Also append the Feature section with the new features and similarly append the Usage section and add your fluent api usage there. My suggestion here would be to properly integrate the documentation of your changes into the existing sections and add new section if required.

Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,63 @@ Or you can use (+) operator to add a listener.
ruleEngine + print;
```

## Enhanced OOP-Friendly API (Drules)

Drules also provides a modern, type-safe, and OOP-friendly API for Dart and Flutter projects. This API supports dynamic fact management, fluent method chaining, and type-safe action definitions, making it ideal for dependency injection and reactive systems.

### Example Usage

```dart
import 'package:drules/drules.dart';

void main() async {
final drules = Drules();

// Add static and dynamic facts
drules.addFacts({
'age': 20,
'wifiConnected': () => NetworkService.isWifiConnected(),
'batteryLevel': () => BatteryService.getCurrentLevel(),
});

// Add type-safe actions
drules.addAction(
key: 'lowBatteryAlert',
condition: (facts) => facts['batteryLevel'] < 20,
onSuccess: () async {
await NotificationService.showLowBatteryAlert();
print('Low battery alert triggered');
},
onFail: () => print('Battery level sufficient'),
);

drules.addAction(
key: 'wifiConnectionHandler',
condition: (facts) => facts['wifiConnected'] == false,
onSuccess: () async {
await NetworkService.switchToMobileData();
return 'switched_to_mobile';
},
onFail: () => print('WiFi connection stable'),
);

// Non-blocking evaluation with automatic fact refresh
await drules.trigger();
}
```

### Key Features

- Fluent, OOP-friendly API with method chaining
- Type-safe, compile-time checked conditions and actions
- Dynamic fact providers (sync or async) with automatic refresh
- Action chaining and callback support
- Non-blocking, async evaluation
- Easy integration with dependency injection and clean architecture
- Backward compatible with the classic JSON API

For more advanced usage and migration tips, see the [integration tests](test/drules_enhanced_integration_test.dart) and [unit tests](test/drules_enhanced_test.dart).

## Bugs and feature requests

Please file feature requests and bugs at the [issue tracker](https://github.com/dizitart/drules/issues).
Expand Down
1 change: 1 addition & 0 deletions lib/drules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export 'src/rule.dart' hide ruleId;
export 'src/repos/index.dart';
export 'src/events/activation.dart';
export 'src/rule_context.dart' hide Facts;
export 'src/drules_enhanced.dart';
Loading