forked from project-flogo/rules
/
functions.go
70 lines (55 loc) · 1.99 KB
/
functions.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"fmt"
"github.com/project-flogo/rules/config"
"github.com/project-flogo/rules/common/model"
)
func init() {
config.RegisterConditionEvaluator("checkForGrocery", checkForGrocery)
config.RegisterActionFunction("groceryAction", groceryAction)
config.RegisterConditionEvaluator("checkForFurniture", checkForFurniture)
config.RegisterActionFunction("furnitureAction", furnitureAction)
}
func checkForGrocery(ruleName string, condName string, tuples map[model.TupleType]model.Tuple, ctx model.RuleContext) bool {
ordr := tuples["order"]
if ordr == nil {
fmt.Println("Should not get a nil tuple in FilterCondition! This is an error")
return false
}
typeVal, _ := ordr.GetString("type")
return typeVal == "grocery"
}
func groceryAction(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
fmt.Printf("Rule fired: [%s]\n", ruleName)
t1 := tuples["order"]
if t1 == nil {
fmt.Println("Should not get nil tuples here in Action! This is an error")
return
}
price, _ := t1.GetDouble("totalPrice")
if price >= 2000 {
fmt.Println("Congratulations you are eligible for Rs. 500 gift coupon")
}
}
func checkForFurniture(ruleName string, condName string, tuples map[model.TupleType]model.Tuple, ctx model.RuleContext) bool {
ordr := tuples["order"]
if ordr == nil {
fmt.Println("Should not get a nil tuple in FilterCondition! This is an error")
return false
}
typeVal, _ := ordr.GetString("type")
return typeVal == "furniture"
}
func furnitureAction(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
fmt.Printf("Rule fired: [%s]\n", ruleName)
t1 := tuples["order"]
if t1 == nil {
fmt.Println("Should not get nil tuples here in Action! This is an error")
return
}
price, _ := t1.GetDouble("totalPrice")
if price >= 20000 {
fmt.Println("Congratulations you are eligible for Rs. 1000 gift coupon")
}
}