-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_engine.go
39 lines (32 loc) · 1003 Bytes
/
rule_engine.go
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
package coffeemachine
import (
"io"
)
// RuleEngine is an interface that exposes a Run method to evaluate a RuleSet against
// provided parameters
type RuleEngine interface {
Run(req *RuleEngineRequest) (*RuleEngineResponse, error)
}
type ruleengine struct {
evaluator Evaluator
}
// NewRuleEngine is a constructor for RuleEngine
// It accpets an io.Reader to read the rule-set
// and returns an instance of engine which can be run with
// different set of parameter values
func NewRuleEngine(ruleSet io.Reader) (RuleEngine, error) {
ruleGraph, err := NewParser().Parse(ruleSet)
if err != nil {
return nil, err
}
return &ruleengine{
evaluator: NewEvaluator(ruleGraph),
}, nil
}
// Run method accepts a request containing parameter value and output
// criteria
// It evaluates the rule-set against provided values and returns the response
// as per the output criteria
func (r *ruleengine) Run(req *RuleEngineRequest) (*RuleEngineResponse, error) {
return r.evaluator.Evaluate(req)
}