|
| 1 | +package editor |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/hyperjumptech/grule-rule-engine/ast" |
| 8 | + "github.com/hyperjumptech/grule-rule-engine/builder" |
| 9 | + "github.com/hyperjumptech/grule-rule-engine/engine" |
| 10 | + "github.com/hyperjumptech/grule-rule-engine/pkg" |
| 11 | + mux "github.com/hyperjumptech/hyper-mux" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | +) |
| 15 | + |
| 16 | +type JSONData struct { |
| 17 | + Name string |
| 18 | + JSONData string `json:"jsonText"` |
| 19 | +} |
| 20 | + |
| 21 | +type EvaluateRequest struct { |
| 22 | + GrlText string `json:"grlText"` |
| 23 | + Input []*JSONData `json:"jsonInput"` |
| 24 | +} |
| 25 | + |
| 26 | +func InitializeEvaluationRoute(router *mux.HyperMux) { |
| 27 | + router.AddRoute("/evaluate", http.MethodPost, func(w http.ResponseWriter, r *http.Request) { |
| 28 | + bodyBytes, err := ioutil.ReadAll(r.Body) |
| 29 | + if err != nil { |
| 30 | + w.WriteHeader(http.StatusInternalServerError) |
| 31 | + _, _ = w.Write([]byte(fmt.Sprintf("error while reading body stream. got %v", err))) |
| 32 | + return |
| 33 | + } |
| 34 | + evReq := &EvaluateRequest{} |
| 35 | + err = json.Unmarshal(bodyBytes, evReq) |
| 36 | + if err != nil { |
| 37 | + w.WriteHeader(http.StatusBadRequest) |
| 38 | + _, _ = w.Write([]byte(fmt.Sprintf("wrong json format. got %v \n\n Json : %s", err, string(bodyBytes)))) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + dataContext := ast.NewDataContext() |
| 43 | + |
| 44 | + for _, jd := range evReq.Input { |
| 45 | + jsonByte, err := base64.StdEncoding.DecodeString(jd.JSONData) |
| 46 | + if err != nil { |
| 47 | + w.WriteHeader(http.StatusBadRequest) |
| 48 | + _, _ = w.Write([]byte(fmt.Sprintf("json data named %s should be sent using base64. got %v", jd.Name, err))) |
| 49 | + return |
| 50 | + } |
| 51 | + err = dataContext.AddJSON(jd.Name, jsonByte) |
| 52 | + if err != nil { |
| 53 | + w.WriteHeader(http.StatusBadRequest) |
| 54 | + _, _ = w.Write([]byte(fmt.Sprintf("invalid JSON data named %s when add json to context got %v", jd.Name, err))) |
| 55 | + return |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + lib := ast.NewKnowledgeLibrary() |
| 60 | + rb := builder.NewRuleBuilder(lib) |
| 61 | + |
| 62 | + grlByte, err := base64.StdEncoding.DecodeString(evReq.GrlText) |
| 63 | + if err != nil { |
| 64 | + w.WriteHeader(http.StatusBadRequest) |
| 65 | + _, _ = w.Write([]byte(fmt.Sprintf("GRL data should be sent using base64. got %v", err))) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + err = rb.BuildRuleFromResource("Evaluator", "0.0.1", pkg.NewBytesResource(grlByte)) |
| 70 | + if err != nil { |
| 71 | + w.WriteHeader(http.StatusBadRequest) |
| 72 | + _, _ = w.Write([]byte(fmt.Sprintf("invalid GRL : %s", err.Error()))) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + eng1 := &engine.GruleEngine{MaxCycle: 5} |
| 77 | + kb := lib.NewKnowledgeBaseInstance("Evaluator", "0.0.1") |
| 78 | + err = eng1.Execute(dataContext, kb) |
| 79 | + if err != nil { |
| 80 | + w.WriteHeader(http.StatusBadRequest) |
| 81 | + _, _ = w.Write([]byte(fmt.Sprintf("Grule Error : %s", err.Error()))) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + respData := make(map[string]interface{}) |
| 86 | + for _, keyName := range dataContext.GetKeys() { |
| 87 | + respData[keyName] = dataContext.Get(keyName) |
| 88 | + } |
| 89 | + |
| 90 | + resultBytes, err := json.Marshal(respData) |
| 91 | + if err != nil { |
| 92 | + w.WriteHeader(http.StatusInternalServerError) |
| 93 | + _, _ = w.Write([]byte(fmt.Sprintf("Error marshaling result : %s", err.Error()))) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + w.Header().Add("Content-Type", "application/json") |
| 98 | + w.WriteHeader(http.StatusOK) |
| 99 | + _, _ = w.Write(resultBytes) |
| 100 | + }) |
| 101 | +} |
0 commit comments