Adding Rules with dynamic WHEN statement #257
Unanswered
rishijain300900
asked this question in
Q&A
Replies: 1 comment 2 replies
-
You can reuse the package main
import (
"bufio"
"database/sql"
"fmt"
"log"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/hyperjumptech/grule-rule-engine/ast"
"github.com/hyperjumptech/grule-rule-engine/builder"
"github.com/hyperjumptech/grule-rule-engine/engine"
"github.com/hyperjumptech/grule-rule-engine/pkg"
)
type order struct {
turnover int64
cusID string
status string
spl_permission bool
}
func (o *order) SetStatus(str string) {
o.status = str
fmt.Println("User Status: ", o.status)
}
type limit struct {
turnoverLimit int64
}
func SqlConnect(limit *limit) {
//connects to a SQL DB and populates the limit struct instance
}
func main() {
reader := bufio.NewReader(os.Stdin)
var limit limit
SqlConnect(&limit)
// Prepare grule rule engine
knowledgeLibrary := ast.NewKnowledgeLibrary()
ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary)
fileRes := pkg.NewFileResource("./rules.grl")
err = ruleBuilder.BuildRuleFromResource("TutorialRules", "0.0.1", fileRes)
if err != nil {
panic(err)
}
knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("TutorialRules", "0.0.1")
engine := engine.NewGruleEngine()
// Take CLI Input
for {
user := &order{
status: "ND",
}
fmt.Println("\nEnter ID and Current Turnover: ")
text, _ := reader.ReadString('\n')
user.cusID = text
var z int
_, err := fmt.Scanf("%d", &z)
if err != nil {
panic(err)
}
user.turnover = (int64)(z)
fmt.Println("\nDoes User have speical permission(Y/N): ")
text, _ = reader.ReadString('\n')
text = text[:1]
if text == "y" || text == "Y" {
user.spl_permission = true
} else {
user.spl_permission = false
}
// Prepare the context
dataCtx := ast.NewDataContext()
err = dataCtx.Add("LIMIT", limit)
if err != nil {
panic(err)
}
err = dataCtx.Add("CUS", user)
if err != nil {
panic(err)
}
// Just execute/reuse the engine
err = engine.Execute(dataCtx, knowledgeBase)
if err != nil {
panic(err)
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am working on a POC.
What the Code Does:
Idea is to take CUS(Customer) Turnover and Spl_permission from CLI and the LIMIT turnover limit from SQL DB, and check all the values of customer against the LIMIT.turnoverLimit (in the follwing code i read only 1 LIMIT.turnoverLimit from the SQL and use it against all the customer values that i get from CLI).
Rules.grl:
What is the problem that i am facing:
Refer the
main.go
to understand the problem.You can notice that after taking the CLI input from the infinite for loop, i have to repeat the following steps everytime:
making the code slow and very much redundant, and as soon as i shift these line before the for loop, the output is not desired one.
Need to optimise this, and also if any suggestion on how my usage of grule can improved will be more than helpful
main.go:
Beta Was this translation helpful? Give feedback.
All reactions