3
3
* Provides type-safe rule evaluation and matching
4
4
*/
5
5
6
- import type { AttributeSchema , TypedEntity , TypedRule } from '../types/schema' ;
6
+ import type { AttributeSchema , Entity , Rule } from '../types/schema' ;
7
7
import { isValidSchemaObject } from '../types/schema' ;
8
8
9
9
/**
10
- * Configuration interface for the TypedRuleEngine
10
+ * Configuration interface for the RuleEngine
11
11
*/
12
- interface TypedRuleEngineConfig {
12
+ interface RuleEngineConfig {
13
13
maxBatchSize ?: number ;
14
14
}
15
15
16
16
/**
17
17
* Enhanced rule engine with generic type support
18
18
*/
19
- export class TypedRuleEngine < TSchema extends AttributeSchema > {
20
- private readonly config : Required < TypedRuleEngineConfig > ;
19
+ export class RuleEngine < TSchema extends AttributeSchema > {
20
+ private readonly config : Required < RuleEngineConfig > ;
21
21
22
22
constructor (
23
23
private readonly schema : TSchema ,
24
- config ?: Partial < TypedRuleEngineConfig > ,
24
+ config ?: Partial < RuleEngineConfig > ,
25
25
) {
26
26
this . config = {
27
27
maxBatchSize : 1000 ,
@@ -32,7 +32,7 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
32
32
/**
33
33
* Evaluates a single rule against an entity
34
34
*/
35
- private evaluateRule ( entity : TypedEntity < TSchema > , rule : TypedRule < TSchema > ) : boolean {
35
+ private evaluateRule ( entity : Entity < TSchema > , rule : Rule < TSchema > ) : boolean {
36
36
// Validate entity against schema
37
37
if ( ! isValidSchemaObject ( entity . attributes , this . schema ) ) {
38
38
return false ;
@@ -94,15 +94,15 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
94
94
/**
95
95
* Splits entities into optimal batch sizes based on complexity
96
96
*/
97
- private getBatchSize ( entities : TypedEntity < TSchema > [ ] , rules : TypedRule < TSchema > [ ] ) : number {
97
+ private getBatchSize ( entities : Entity < TSchema > [ ] , rules : Rule < TSchema > [ ] ) : number {
98
98
// Start with configured max batch size
99
99
const entityCount = entities . length ;
100
100
let batchSize = this . config . maxBatchSize ;
101
101
102
102
// Adjust based on rule complexity
103
103
const ruleComplexity = rules . reduce ( ( complexity , rule ) => {
104
104
// Count number of conditions in rule
105
- const countConditions = ( r : TypedRule < TSchema > ) : number => {
105
+ const countConditions = ( r : Rule < TSchema > ) : number => {
106
106
let count = 0 ;
107
107
if ( r . and ) count += r . and . reduce ( ( sum , subRule ) => sum + countConditions ( subRule ) , 0 ) ;
108
108
if ( r . or ) count += r . or . reduce ( ( sum , subRule ) => sum + countConditions ( subRule ) , 0 ) ;
@@ -123,7 +123,7 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
123
123
/**
124
124
* Processes entities in optimally-sized batches
125
125
*/
126
- private processBatch ( entities : TypedEntity < TSchema > [ ] , rules : TypedRule < TSchema > [ ] ) : boolean [ ] {
126
+ private processBatch ( entities : Entity < TSchema > [ ] , rules : Rule < TSchema > [ ] ) : boolean [ ] {
127
127
const batchSize = this . getBatchSize ( entities , rules ) ;
128
128
const results = new Array ( entities . length ) ;
129
129
const batches = Math . ceil ( entities . length / batchSize ) ;
@@ -153,10 +153,7 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
153
153
/**
154
154
* Finds entities that satisfy all provided 'from' rules
155
155
*/
156
- findMatchingFrom (
157
- entities : TypedEntity < TSchema > [ ] ,
158
- rules : TypedRule < TSchema > [ ] ,
159
- ) : TypedEntity < TSchema > [ ] {
156
+ findMatchingFrom ( entities : Entity < TSchema > [ ] , rules : Rule < TSchema > [ ] ) : Entity < TSchema > [ ] {
160
157
const results = this . processBatch ( entities , rules ) ;
161
158
return entities . filter ( ( _ , index ) => results [ index ] ) ;
162
159
}
@@ -165,10 +162,10 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
165
162
* Finds target entities that can be matched with source entities based on 'to' rules
166
163
*/
167
164
findMatchingTo (
168
- fromEntities : TypedEntity < TSchema > [ ] ,
169
- toRules : TypedRule < TSchema > [ ] ,
170
- allEntities : TypedEntity < TSchema > [ ] ,
171
- ) : TypedEntity < TSchema > [ ] {
165
+ fromEntities : Entity < TSchema > [ ] ,
166
+ toRules : Rule < TSchema > [ ] ,
167
+ allEntities : Entity < TSchema > [ ] ,
168
+ ) : Entity < TSchema > [ ] {
172
169
// First, filter out 'from' entities from all entities
173
170
const candidateEntities = allEntities . filter (
174
171
entity => ! fromEntities . some ( from => from . id === entity . id ) ,
0 commit comments