-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.dart
More file actions
51 lines (42 loc) · 1.97 KB
/
example.dart
File metadata and controls
51 lines (42 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import 'package:boolean_rhapsody/boolean_rhapsody.dart';
void main() {
// Instantiate function registry containing boolean functions like string_equals
final functionRegistry = BooleanRhapsodyFunctionRegistry();
// Configure semantic analyser options
final RhapsodyAnalyserOptions analyserOptions = RhapsodyAnalyserOptions(
prefixes: ['env', 'config'], // Allowed variable prefixes
functions: rhapsodyFunctionNames, // List of allowed function names
variableValidator: (String variableName) {
// Validate variable naming: must start with letter, then alphanumerics
return RegExp(r'^[a-zA-Z][a-zA-Z0-9_:]*$').hasMatch(variableName);
},
functionRegistry: functionRegistry, // Attach function registry
);
// Create tokeniser and parse input rule strings into tokens
final tokeniser = RhapsodyTokeniser();
final tokens = tokeniser.parse([
'rule stop = string_equals(env:state, config:color:red) or is_present(env:alert);',
'rule orange = string_equals(env:state, config:color:orange);'
].join(''));
// Create analyser and perform semantic analysis on tokens
final RhapsodySemanticAnalyser analyser =
RhapsodySemanticAnalyser(analyserOptions);
final analysis = analyser.analyse(tokens);
// Interpret analysed rules
final interpreter = RhapsodyInterpreter(analysis);
// Define context with variable values at runtime
RhapsodyEvaluationContextBuilder builder =
RhapsodyEvaluationContextBuilder(prefixes: ['env', 'config']);
builder.setRefValue('config:color:red', 'red');
builder.setRefValue('config:color:orange', 'orange');
builder.setRefValue('env:state', 'green');
builder.setRefValue('env:alert', 'panic');
RhapsodyEvaluationContext context = builder.build();
// Evaluate rules using the interpreter
interpreter.interpret(context);
// Print final rule evaluation results
print(context.ruleState.states);
// Output:
// {stop: RhapsodicBool{value: true, certain: true},
// orange: RhapsodicBool{value: false, certain: false}}
}