-
Notifications
You must be signed in to change notification settings - Fork 1
Docs: Rules
- Introduction
- Installation
- Overview
-
Key Concepts
- Rule Parsing
- Rule Resolution
- Rule Evaluation
-
Core Components
RulesModule
-
Key Services
RulesParserServiceRulesResolverService
-
Usage
- Parsing Rules
- Resolving Rules
- Evaluating Rules
- API Reference
- Examples
- Testing
The Angular Rules Library provides tools for defining, parsing, resolving, and evaluating rules in applications. It helps integrate rule-based logic into Angular projects using advanced parsing mechanisms, rule evaluation engines, and nested conditions. It is lightweight and extensible, making it easy to dynamically handle rules.
Install the library along with its dependencies:
npm install @rollthecloudinc/ngx-angular-query-builder @rollthecloudinc/attributes @rollthecloudinc/context json-rules-engine uuidThe Rules Library is designed to help developers implement rule-based workflows. It provides services for the creation, parsing, and evaluation of rules in complex applications using tools like JSON rules engines and field mappings.
- Rule Parsing: Convert data structures into rules or fields.
- Rule Resolution: Resolve rules with external context or nested conditions.
- Rule Evaluation: Evaluate rule sets dynamically with facts and conditions.
The library allows parsing objects, attributes, or fields into manageable rules and conditions. Using the RulesParserService, developers can generate rule structures compatible with JSON rule engines.
Rules can be resolved with the help of inline contexts or external configuration using the RulesResolverService. It supports merging contexts and evaluating nested rule structures for complex workflows.
Rules are evaluated using factual inputs and the json-rules-engine. Rule evaluation supports nested conditions (and, or) and events, enabling dynamic workflows based on rule logic.
The RulesModule encapsulates all services provided by the library. Importing this module makes the services available throughout your Angular application.
The RulesParserService provides methods for parsing data into fields, attribute mappings, and engine-compatible rule structures. It contains logic for handling complex attribute values and nested rule conditions.
Key Responsibilities:
- Parse objects and attributes into reusable
Fieldstructures. - Convert rule sets into JSON engine-compatible rules.
- Resolve field types dynamically (e.g., number, string, boolean).
Example:
import { RulesParserService } from './services/rules-parser.service';
constructor(private rulesParser: RulesParserService) {}
ngOnInit() {
const fields = this.rulesParser.buildFields({ age: 30, name: "John" });
console.log('Parsed Fields:', fields);
}The RulesResolverService resolves rule sets dynamically with context and evaluates them using the json-rules-engine. It manages inline contexts and combines facts for complex workflows.
Key Responsibilities:
- Resolve rules with dynamic inline contexts.
- Evaluate rules using provided facts and return results.
- Handle nested rule conditions (
and/or) efficiently.
Example:
import { RulesResolverService } from './services/rules-resolver.service';
constructor(private rulesResolver: RulesResolverService) {}
ngOnInit() {
const ruleSet = { condition: "or", rules: [{ field: "age", operator: ">", value: 18 }] };
this.rulesResolver.evaluate(ruleSet).subscribe(result => {
console.log('Rule is visible:', result);
});
}Use the RulesParserService to convert data objects or attribute arrays into reusable fields or engine-compatible rules.
Example:
const parsedFields = rulesParser.buildFields({ age: 30, isActive: true });
console.log('Parsed Fields:', parsedFields);Leverage the RulesResolverService to resolve rules dynamically with inline contexts. This allows integration with external rules or nested sets.
Example:
const context = [{ name: "userContext", attributes: [{ name: "age", value: 25 }] }];
rulesResolver.evaluate(ruleSet, context).subscribe(result => {
console.log('Evaluation Result:', result);
});Evaluate rule sets with facts using the JSON rules engine, resolving conditions like and/or dynamically.
Example:
const ruleSet = { condition: "and", rules: [{ field: "user.age", operator: ">", value: 18 }] };
rulesResolver.evaluate(ruleSet).subscribe(isVisible => {
console.log('Rule Evaluation:', isVisible);
});-
RulesParserService
-
buildFields(obj: any, prefix?: string): Map<string, Field>: Parses an object into fields. -
toEngineRule(ruleSet: RuleSet): Rule: Converts a rule set into an engine-compatible rule structure. -
resolveNativeType(type: string): string: Resolves JavaScript native types to field types. -
resolveAttributeType(type: AttributeTypes): string: Resolves attribute types to field types.
-
-
RulesResolverService
-
evaluate(ruleSet: RuleSet, contexts?: Array<InlineContext>): Observable<boolean>: Evaluates a rule set with the given contexts. - Dependencies:
InlineContextResolverService,json-rules-engine.
-
import { RulesParserService } from './services/rules-parser.service';
const data = { age: 30, isActive: true };
const fields = rulesParser.buildFields(data);
console.log('Fields:', fields);import { RulesResolverService } from './services/rules-resolver.service';
const ruleSet = {
condition: "and",
rules: [
{ field: "user.age", operator: ">", value: 18 },
{ field: "user.isActive", operator: "=", value: true }
]
};
const userContext = [{ name: "userContext", attributes: [{ name: "age", value: 25 }, { name: "isActive", value: true }] }];
rulesResolver.evaluate(ruleSet, userContext).subscribe(result => {
console.log('Rule Evaluated Result:', result); // Output: true
});import { RulesParserService } from './services/rules-parser.service';
describe('RulesParserService', () => {
let parserService: RulesParserService;
beforeEach(() => {
parserService = new RulesParserService();
});
it('should parse object into fields', () => {
const fields = parserService.buildFields({ age: 30 });
expect(fields.has('age')).toBeTruthy();
expect(fields.get('age')).toEqual({ name: 'age', type: 'number', defaultValue: 30 });
});
it('should resolve native types correctly', () => {
expect(parserService.resolveNativeType('string')).toBe('string');
expect(parserService.resolveNativeType('number')).toBe('number');
expect(parserService.resolveNativeType('boolean')).toBe('boolean');
});
});import { RulesResolverService } from './services/rules-resolver.service';
describe('RulesResolverService', () => {
let resolverService: RulesResolverService;
beforeEach(() => {
resolverService = new RulesResolverService(/* dependencies */);
});
it('should evaluate rule sets', (done) => {
const ruleSet = { condition: "or", rules: [{ field: "age", operator: ">", value: 18 }] };
resolverService.evaluate(ruleSet).subscribe(result => {
expect(result).toBe(true);
done();
});
});
});The Angular Rules Library provides a robust solution for parsing, resolving, and evaluating rules using dynamic data, nested conditions, and contextual workflows. It is suitable for applications requiring decision-making logic based on rules and conditions.
For contributions, feature requests, or bug reports, please reach out!