diff --git a/editor/EvaluationRoute.go b/editor/EvaluationRoute.go index 39544040..2cdb30c9 100644 --- a/editor/EvaluationRoute.go +++ b/editor/EvaluationRoute.go @@ -80,7 +80,13 @@ func InitializeEvaluationRoute(router *mux.HyperMux) { } eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := knowledgeLibrary.NewKnowledgeBaseInstance("Evaluator", "0.0.1") + kb, err := knowledgeLibrary.NewKnowledgeBaseInstance("Evaluator", "0.0.1") + if err != nil { + writer.WriteHeader(http.StatusBadRequest) + _, _ = writer.Write([]byte(fmt.Sprintf("Grule Error : %s", err.Error()))) + + return + } err = eng1.Execute(dataContext, kb) if err != nil { writer.WriteHeader(http.StatusBadRequest) diff --git a/editor/StaticRoute.go b/editor/StaticRoute.go index 8f22fb98..2374ea07 100644 --- a/editor/StaticRoute.go +++ b/editor/StaticRoute.go @@ -2,9 +2,9 @@ package editor import ( "embed" + "github.com/hyperjumptech/grule-rule-engine/editor/mime" mux "github.com/hyperjumptech/hyper-mux" "github.com/sirupsen/logrus" - "grule-rule-engine/editor/mime" "net/http" "os" "strings" diff --git a/editor/cmd/Main.go b/editor/cmd/Main.go index 463114fc..cefd4490 100644 --- a/editor/cmd/Main.go +++ b/editor/cmd/Main.go @@ -1,8 +1,8 @@ package main import ( + "github.com/hyperjumptech/grule-rule-engine/editor" "github.com/sirupsen/logrus" - "grule-rule-engine/editor" ) func main() { diff --git a/engine/GruleEngine_test.go b/engine/GruleEngine_test.go index 1aa569bc..38089235 100755 --- a/engine/GruleEngine_test.go +++ b/engine/GruleEngine_test.go @@ -122,7 +122,8 @@ func TestGrule_Execute(t *testing.T) { err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(rules))) assert.NoError(t, err) engine := NewGruleEngine() - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) start := time.Now() err = engine.Execute(dctx, kb) assert.NoError(t, err) @@ -158,7 +159,8 @@ func TestEngine_ExecuteErr(t *testing.T) { engine := NewGruleEngine() engine.ReturnErrOnFailedRuleEvaluation = true - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) err = engine.Execute(dctx, kb) assert.Error(t, err) } @@ -215,7 +217,8 @@ func TestEngine_ComplexRule1(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule1))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) engine := NewGruleEngine() err = engine.Execute(dctx, kb) @@ -250,7 +253,8 @@ func TestEngine_ComplexRule2(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule2))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) engine := NewGruleEngine() err = engine.Execute(dctx, kb) @@ -286,7 +290,8 @@ func TestEngine_ComplexRule3(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule3))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) engine := NewGruleEngine() err = engine.Execute(dctx, kb) @@ -323,7 +328,8 @@ func TestEngine_ComplexRule4(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(complexRule4))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) engine := NewGruleEngine() err = engine.Execute(dctx, kb) @@ -352,7 +358,8 @@ func TestEngine_OperatorPrecedence(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(OpPresedenceRule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) engine := NewGruleEngine() err = engine.Execute(dctx, kb) @@ -398,7 +405,8 @@ And another` rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(escapedRules))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) assert.False(t, es.Result1) assert.False(t, es.Result2) @@ -439,7 +447,8 @@ rule KeepSleep "test string escaping" salience 10 { } `))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("TestTimer", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("TestTimer", "0.1.1") + assert.NoError(t, err) engine := NewGruleEngine() ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() @@ -514,7 +523,8 @@ func TestGruleEngine_FetchMatchingRules_Having_Same_Salience(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("conflict_rules_test", "0.1.1", pkg.NewBytesResource([]byte(duplicateRules))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1") + assert.NoError(t, err) //When engine := NewGruleEngine() @@ -537,7 +547,8 @@ func TestEngine_FetchMatchingRulesErr(t *testing.T) { engine := NewGruleEngine() engine.ReturnErrOnFailedRuleEvaluation = true - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) mr, err := engine.FetchMatchingRules(dctx, kb) assert.Error(t, err) assert.Nil(t, mr) @@ -598,7 +609,8 @@ func TestGruleEngine_FetchMatchingRules_Having_Diff_Salience(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("conflict_rules_test", "0.1.1", pkg.NewBytesResource([]byte(duplicateRulesWithDiffSalience))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1") + assert.NoError(t, err) //When engine := NewGruleEngine() @@ -661,7 +673,8 @@ func TestGruleEngine_Follows_logical_operator_precedence(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("logical_operator_rules_test", "0.1.1", pkg.NewBytesResource([]byte(logicalOperatorPrecedenceRules))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("logical_operator_rules_test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("logical_operator_rules_test", "0.1.1") + assert.NoError(t, err) //When engine := NewGruleEngine() diff --git a/examples/AgeCheckSample_test.go b/examples/AgeCheckSample_test.go index 0343a7f6..2fa5eb6d 100755 --- a/examples/AgeCheckSample_test.go +++ b/examples/AgeCheckSample_test.go @@ -82,7 +82,8 @@ func TestMyPoGo_GetStringLength(t *testing.T) { ruleBuilder := builder.NewRuleBuilder(lib) err = ruleBuilder.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(rule2))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) @@ -111,7 +112,8 @@ func TestMyPoGo_Compare(t *testing.T) { err = ruleBuilder.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(rule3))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 100} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) diff --git a/examples/ArraySliceMap_test.go b/examples/ArraySliceMap_test.go index ea91faf6..9792a6c3 100755 --- a/examples/ArraySliceMap_test.go +++ b/examples/ArraySliceMap_test.go @@ -103,7 +103,8 @@ rule SetTreeName "Set the top most tree name" { ruleBuilder := builder.NewRuleBuilder(lib) err = ruleBuilder.BuildRuleFromResource("TestFuncChaining", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) diff --git a/examples/CallingFactFunctionExample_test.go b/examples/CallingFactFunctionExample_test.go index b8f01c81..7c652a40 100755 --- a/examples/CallingFactFunctionExample_test.go +++ b/examples/CallingFactFunctionExample_test.go @@ -87,7 +87,9 @@ func TestCallingFactFunction(t *testing.T) { lib := ast.NewKnowledgeLibrary() ruleBuilder := builder.NewRuleBuilder(lib) err = ruleBuilder.BuildRuleFromResource("CallingFactFunction", "0.1.1", pkg.NewBytesResource([]byte(CallFactFuncGRL))) - knowledgeBase := lib.NewKnowledgeBaseInstance("CallingFactFunction", "0.1.1") + assert.NoError(t, err) + + knowledgeBase, err := lib.NewKnowledgeBaseInstance("CallingFactFunction", "0.1.1") assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 500} err = eng1.Execute(dataContext, knowledgeBase) diff --git a/examples/CallingLogExample_test.go b/examples/CallingLogExample_test.go index 93584c59..bbe29476 100755 --- a/examples/CallingLogExample_test.go +++ b/examples/CallingLogExample_test.go @@ -43,7 +43,8 @@ func TestCallingLog(t *testing.T) { err := ruleBuilder.BuildRuleFromResource("CallingLog", "0.1.1", pkg.NewBytesResource([]byte(GRL))) assert.NoError(t, err) - knowledgeBase := lib.NewKnowledgeBaseInstance("CallingLog", "0.1.1") + knowledgeBase, err := lib.NewKnowledgeBaseInstance("CallingLog", "0.1.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, knowledgeBase) diff --git a/examples/CloneTableIssue_test.go b/examples/CloneTableIssue_test.go index 38aaf4e7..809b2a44 100755 --- a/examples/CloneTableIssue_test.go +++ b/examples/CloneTableIssue_test.go @@ -36,7 +36,8 @@ func TestSliceFunctionPanicTest(t *testing.T) { ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary) err = ruleBuilder.BuildRuleFromResource("test", "0.0.1", pkg.NewBytesResource([]byte(panickingRule))) assert.NoError(t, err) - knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1") + knowledgeBase, err := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1") + assert.NoError(t, err) engine := engine.NewGruleEngine() err = engine.Execute(dataContext, knowledgeBase) diff --git a/examples/Concurrency_test.go b/examples/Concurrency_test.go index 50cd03e7..9767a00c 100644 --- a/examples/Concurrency_test.go +++ b/examples/Concurrency_test.go @@ -117,7 +117,7 @@ func beginThread(threadName string, lib *ast.KnowledgeLibrary, t *testing.T) { engine := &engine.GruleEngine{MaxCycle: 100} // Get an instance of our KnowledgeBase from KnowledgeLibrary - kb := lib.NewKnowledgeBaseInstance("VibonaciTest", "0.0.1") + kb, _ := lib.NewKnowledgeBaseInstance("VibonaciTest", "0.0.1") // Execute the KnowledgeBase against DataContext err = engine.Execute(dataContext, kb) diff --git a/examples/EvaluateMissingDataContext_test.go b/examples/EvaluateMissingDataContext_test.go index 2e9f93bb..20685b3c 100755 --- a/examples/EvaluateMissingDataContext_test.go +++ b/examples/EvaluateMissingDataContext_test.go @@ -2,6 +2,7 @@ package examples import ( "fmt" + "github.com/stretchr/testify/assert" "testing" "github.com/hyperjumptech/grule-rule-engine/ast" @@ -36,7 +37,8 @@ func TestDataContextMissingFact(t *testing.T) { // add JSON fact json := []byte(`{"blabla":"bla","name":{"first":"john","last":"doe"}}`) - kb := lib.NewKnowledgeBaseInstance("Test", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.0.1") + assert.NoError(t, err) dcx := ast.NewDataContext() err = dcx.Add("R", oresult) diff --git a/examples/FunctionCallChaining_test.go b/examples/FunctionCallChaining_test.go index af70a87c..f803ebc7 100755 --- a/examples/FunctionCallChaining_test.go +++ b/examples/FunctionCallChaining_test.go @@ -74,7 +74,8 @@ rule SetTreeName "Set the top most tree name" { ruleBuilder := builder.NewRuleBuilder(lib) err = ruleBuilder.BuildRuleFromResource("TestFuncChaining", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) @@ -119,7 +120,8 @@ rule SetTreeName "Set the top most tree name" { ruleBuilder := builder.NewRuleBuilder(lib) err = ruleBuilder.BuildRuleFromResource("TestFuncChaining", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestFuncChaining", "0.0.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) diff --git a/examples/InterfaceDataContext_test.go b/examples/InterfaceDataContext_test.go index 07137255..a52f406e 100644 --- a/examples/InterfaceDataContext_test.go +++ b/examples/InterfaceDataContext_test.go @@ -59,7 +59,8 @@ then rb := builder.NewRuleBuilder(lib) err := rb.BuildRuleFromResource("CarPriceTest", "0.1.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("CarPriceTest", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("CarPriceTest", "0.1.1") + assert.NoError(t, err) eng := &engine.GruleEngine{MaxCycle: 3} dataContext := ast.NewDataContext() diff --git a/examples/Issue108_test.go b/examples/Issue108_test.go index 56bf4c1d..8c0e9234 100644 --- a/examples/Issue108_test.go +++ b/examples/Issue108_test.go @@ -74,7 +74,8 @@ func TestIssue108(t *testing.T) { err = rb.BuildRuleFromResource("Test108", "0.0.1", pkg.NewBytesResource([]byte(Rule108))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("Test108", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("Test108", "0.0.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, 3, len(Obj.Sequence)) diff --git a/examples/Issue328_test.go b/examples/Issue328_test.go index ee0cf187..42a4f772 100644 --- a/examples/Issue328_test.go +++ b/examples/Issue328_test.go @@ -41,12 +41,14 @@ func TestMethodCall_SliceOOR(t *testing.T) { assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) eng1 = &engine.GruleEngine{MaxCycle: 5, ReturnErrOnFailedRuleEvaluation: true} - kb = lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err = lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.Error(t, err) } diff --git a/examples/Issue4_test.go b/examples/Issue4_test.go index af1e27a9..3223d4db 100755 --- a/examples/Issue4_test.go +++ b/examples/Issue4_test.go @@ -70,7 +70,8 @@ func TestMethodCall_Issue4(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(Rule4))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 3} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) diff --git a/examples/Issue5_test.go b/examples/Issue5_test.go index bafb2eb1..ee170f33 100755 --- a/examples/Issue5_test.go +++ b/examples/Issue5_test.go @@ -62,7 +62,8 @@ func TestMethodCall_Issue5(t *testing.T) { err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(Rule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "FromRuleScope3", user.GetName()) diff --git a/examples/Issue7_test.go b/examples/Issue7_test.go index d14a9d57..e11c8b8b 100755 --- a/examples/Issue7_test.go +++ b/examples/Issue7_test.go @@ -64,7 +64,8 @@ func TestMethodCall_Issue7(t *testing.T) { err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(Rule7))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "FromRule", user.GetName()) diff --git a/examples/ItemArrayExample_test.go b/examples/ItemArrayExample_test.go index 0c171b72..7bc6d0ec 100755 --- a/examples/ItemArrayExample_test.go +++ b/examples/ItemArrayExample_test.go @@ -87,7 +87,8 @@ func (cf *ItemPriceChecker) CheckPrices(t *testing.T) { err := rb.BuildRuleFromResource("PriceCheck", "0.0.1", pkg.NewBytesResource([]byte(PriceCheckRule1))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("PriceCheck", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("PriceCheck", "0.0.1") + assert.NoError(t, err) // Prepare the engine eng := engine.NewGruleEngine() @@ -168,7 +169,8 @@ func (cf *ItemPriceChecker) CheckCart(t *testing.T) { err := rb.BuildRuleFromResource("Cart Check Rules", "0.0.1", pkg.NewBytesResource([]byte(PriceCheckRule2))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Cart Check Rules", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("Cart Check Rules", "0.0.1") + assert.NoError(t, err) // Prepare the engine eng := engine.NewGruleEngine() diff --git a/examples/JSONDataContext_test.go b/examples/JSONDataContext_test.go index 6bd65425..85bb5b33 100755 --- a/examples/JSONDataContext_test.go +++ b/examples/JSONDataContext_test.go @@ -65,7 +65,8 @@ rule CheckIfJSONIntWorks { err := rb.BuildRuleFromResource("TestJSONSimple", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "PERFECT", oresult.Result) @@ -84,7 +85,8 @@ rule CheckIfJSONStringWorks { err := rb.BuildRuleFromResource("TestJSONSimple", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "PERFECT", oresult.Result) @@ -103,7 +105,8 @@ rule CheckIfJSONArrayWorks { err := rb.BuildRuleFromResource("TestJSONSimple", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "PERFECT", oresult.Result) @@ -122,7 +125,8 @@ rule CheckIfJSONMapWorks { err := rb.BuildRuleFromResource("TestJSONSimple", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestJSONSimple", "0.0.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "PERFECT", oresult.Result) @@ -165,7 +169,8 @@ rule CheckIfJSONIntWorks { err = rb.BuildRuleFromResource("TestJSONBitComplex", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestJSONBitComplex", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("TestJSONBitComplex", "0.0.1") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "PERFECT", oresult.Result) diff --git a/examples/KnowledgeBaseInstancePanic_test.go b/examples/KnowledgeBaseInstancePanic_test.go index dfa895b8..8a97b216 100755 --- a/examples/KnowledgeBaseInstancePanic_test.go +++ b/examples/KnowledgeBaseInstancePanic_test.go @@ -28,5 +28,6 @@ func TestNoPanicForNoDescription(t *testing.T) { ruleBuilder := builder.NewRuleBuilder(lib) err := ruleBuilder.BuildRuleFromResource("CallingLog", "0.1.1", pkg.NewBytesResource([]byte(GRL))) assert.NoError(t, err) - _ = lib.NewKnowledgeBaseInstance("CallingLog", "0.1.1") + _, err = lib.NewKnowledgeBaseInstance("CallingLog", "0.1.1") + assert.NoError(t, err) } diff --git a/examples/MatchingRules_test.go b/examples/MatchingRules_test.go index 744f317a..df8476d2 100644 --- a/examples/MatchingRules_test.go +++ b/examples/MatchingRules_test.go @@ -87,7 +87,8 @@ func TestGruleEngine_FetchMatchingRules_Having_Diff_Salience(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("conflict_rules_test", "0.1.1", pkg.NewBytesResource([]byte(duplicateRulesWithDiffSalience))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("conflict_rules_test", "0.1.1") + assert.NoError(t, err) //When e := engine.NewGruleEngine() diff --git a/examples/MemoizeSliceFunction_test.go b/examples/MemoizeSliceFunction_test.go index 99d40045..e8741b1a 100755 --- a/examples/MemoizeSliceFunction_test.go +++ b/examples/MemoizeSliceFunction_test.go @@ -40,7 +40,8 @@ func TestSliceFunctionTest(t *testing.T) { ruleBuilder := builder.NewRuleBuilder(knowledgeLibrary) err = ruleBuilder.BuildRuleFromResource("test", "0.0.1", pkg.NewBytesResource([]byte(rule))) assert.NoError(t, err) - knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1") + knowledgeBase, err := knowledgeLibrary.NewKnowledgeBaseInstance("test", "0.0.1") + assert.NoError(t, err) engine := engine.NewGruleEngine() err = engine.Execute(dataContext, knowledgeBase) diff --git a/examples/Multipe_knowledgebases_test.go b/examples/Multipe_knowledgebases_test.go index 65dd39b5..148655e9 100644 --- a/examples/Multipe_knowledgebases_test.go +++ b/examples/Multipe_knowledgebases_test.go @@ -19,6 +19,7 @@ import ( "github.com/hyperjumptech/grule-rule-engine/builder" "github.com/hyperjumptech/grule-rule-engine/engine" "github.com/hyperjumptech/grule-rule-engine/pkg" + "github.com/stretchr/testify/assert" "testing" ) @@ -98,8 +99,10 @@ func TestGruleEngine_Support_Multiple_KnowledgeBases(t *testing.T) { if err != nil { t.Fatal(err) } - userKnowledgeBase := lib.NewKnowledgeBaseInstance("UserRules", "0.1.1") - rideKnowledgeBase := lib.NewKnowledgeBaseInstance("RideRules", "0.1.1") + userKnowledgeBase, err := lib.NewKnowledgeBaseInstance("UserRules", "0.1.1") + assert.NoError(t, err) + rideKnowledgeBase, err := lib.NewKnowledgeBaseInstance("RideRules", "0.1.1") + assert.NoError(t, err) eng1 := engine.NewGruleEngine() err = eng1.Execute(rideDataContext, rideKnowledgeBase) if err != nil { diff --git a/examples/NegationSymbolExample_test.go b/examples/NegationSymbolExample_test.go index a2f8f0d6..f1443206 100644 --- a/examples/NegationSymbolExample_test.go +++ b/examples/NegationSymbolExample_test.go @@ -65,7 +65,8 @@ func TestNegationSymbolExpressionAtom(t *testing.T) { err = rb.BuildRuleFromResource("TestNegation", "1.0.0", pkg.NewBytesResource([]byte(NegationRule2))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestNegation", "1.0.0") + kb, err := lib.NewKnowledgeBaseInstance("TestNegation", "1.0.0") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "YES ITS NOT", structTest.StringValue) @@ -86,7 +87,8 @@ func TestNegationSymbolExpression(t *testing.T) { err = rb.BuildRuleFromResource("TestNegation", "1.0.0", pkg.NewBytesResource([]byte(NegationRule1))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestNegation", "1.0.0") + kb, err := lib.NewKnowledgeBaseInstance("TestNegation", "1.0.0") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, "ITS ABC", structTest.StringValue) diff --git a/examples/NumberExponentExample_test.go b/examples/NumberExponentExample_test.go index 84d561d8..6641af73 100644 --- a/examples/NumberExponentExample_test.go +++ b/examples/NumberExponentExample_test.go @@ -54,7 +54,8 @@ func TestEvaluateAndAssignExponentNumber(t *testing.T) { err = rb.BuildRuleFromResource("TestExponent", "1.0.0", pkg.NewBytesResource([]byte(ExponentRule))) assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 5} - kb := lib.NewKnowledgeBaseInstance("TestExponent", "1.0.0") + kb, err := lib.NewKnowledgeBaseInstance("TestExponent", "1.0.0") + assert.NoError(t, err) err = eng1.Execute(dataContext, kb) assert.NoError(t, err) assert.Equal(t, .12345e+5, exponent.Set) diff --git a/examples/PurcasingTaxSample_test.go b/examples/PurcasingTaxSample_test.go index 5cc3e142..1dbb1824 100755 --- a/examples/PurcasingTaxSample_test.go +++ b/examples/PurcasingTaxSample_test.go @@ -133,7 +133,8 @@ func (cf *CashFlowCalculator) CalculatePurchases(t *testing.T) { engine := engine2.NewGruleEngine() - kb := lib.NewKnowledgeBaseInstance("Purchase Calculator", "0.0.1") + kb, err := lib.NewKnowledgeBaseInstance("Purchase Calculator", "0.0.1") + assert.NoError(t, err) buff := &bytes.Buffer{} cat := kb.MakeCatalog() @@ -143,7 +144,8 @@ func (cf *CashFlowCalculator) CalculatePurchases(t *testing.T) { buff2 := bytes.NewBuffer(buff.Bytes()) cat2 := &ast.Catalog{} cat2.ReadCatalogFromReader(buff2) - nkb := cat2.BuildKnowledgeBase() + nkb, err := cat2.BuildKnowledgeBase() + assert.NoError(t, err) for _, purchase := range Purchases { dctx := ast.NewDataContext() diff --git a/examples/RemoveRuleEntry_test.go b/examples/RemoveRuleEntry_test.go index 1d873e46..e3dde412 100644 --- a/examples/RemoveRuleEntry_test.go +++ b/examples/RemoveRuleEntry_test.go @@ -64,7 +64,8 @@ func TestRemoveRuleEntry(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(RuleA))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng := &engine.GruleEngine{MaxCycle: 1} err = eng.Execute(dataContext, kb) assert.NoError(t, err) @@ -77,7 +78,8 @@ func TestRemoveRuleEntry(t *testing.T) { //Add RuleB again, which is similar to RuleA except its output err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(RuleB))) assert.NoError(t, err) - kb = lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err = lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng = &engine.GruleEngine{MaxCycle: 1} err = eng.Execute(dataContext, kb) assert.NoError(t, err) diff --git a/examples/SalienceExample_test.go b/examples/SalienceExample_test.go index d026b85f..3ef25ee4 100644 --- a/examples/SalienceExample_test.go +++ b/examples/SalienceExample_test.go @@ -123,7 +123,8 @@ func TestSalience(t *testing.T) { engine := engine.NewGruleEngine() - knowledgeBase := lib.NewKnowledgeBaseInstance("Tutorial", "0.0.1") + knowledgeBase, err := lib.NewKnowledgeBaseInstance("Tutorial", "0.0.1") + assert.NoError(t, err) for _, td := range testData { dataCtx := ast.NewDataContext() diff --git a/examples/Serialization_test.go b/examples/Serialization_test.go index dda3be0c..3ab36230 100755 --- a/examples/Serialization_test.go +++ b/examples/Serialization_test.go @@ -45,7 +45,8 @@ func TestSerialization(t *testing.T) { assert.Nil(t, err) } - kb2 := cat2.BuildKnowledgeBase() + kb2, err := cat2.BuildKnowledgeBase() + assert.NoError(t, err) assert.True(t, kb.IsIdentical(kb2)) } diff --git a/examples/StringFunctions_test.go b/examples/StringFunctions_test.go index 645cf0fe..94606b24 100644 --- a/examples/StringFunctions_test.go +++ b/examples/StringFunctions_test.go @@ -63,7 +63,8 @@ func TestStringInExample(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(strInConditionRule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) @@ -85,7 +86,8 @@ func TestStringMatchStringExample(t *testing.T) { rb := builder.NewRuleBuilder(lib) err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(strMatchStringConditionRule))) assert.NoError(t, err) - kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + kb, err := lib.NewKnowledgeBaseInstance("Test", "0.1.1") + assert.NoError(t, err) eng1 := &engine.GruleEngine{MaxCycle: 1} err = eng1.Execute(dataContext, kb) assert.NoError(t, err) diff --git a/examples/TutorialExample_test.go b/examples/TutorialExample_test.go index ce914316..0cda1063 100755 --- a/examples/TutorialExample_test.go +++ b/examples/TutorialExample_test.go @@ -67,7 +67,8 @@ rule CheckValues "Check the default values" salience 10 { err = ruleBuilder.BuildRuleFromResource("Tutorial", "0.0.1", byteArr) assert.NoError(t, err) - knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("Tutorial", "0.0.1") + knowledgeBase, err := knowledgeLibrary.NewKnowledgeBaseInstance("Tutorial", "0.0.1") + assert.NoError(t, err) engine := engine.NewGruleEngine() err = engine.Execute(dataCtx, knowledgeBase) diff --git a/examples/UnicodeRule_test.go b/examples/UnicodeRule_test.go index c6ee2705..d994d971 100644 --- a/examples/UnicodeRule_test.go +++ b/examples/UnicodeRule_test.go @@ -67,7 +67,8 @@ rule ChĕckValuĕs "Check the default values" salience 10 { err = ruleBuilder.BuildRuleFromResource("Tutorial", "0.0.1", byteArr) assert.NoError(t, err) - knowledgeBase := knowledgeLibrary.NewKnowledgeBaseInstance("Tutorial", "0.0.1") + knowledgeBase, err := knowledgeLibrary.NewKnowledgeBaseInstance("Tutorial", "0.0.1") + assert.NoError(t, err) engine := engine.NewGruleEngine() err = engine.Execute(dataCtx, knowledgeBase) diff --git a/examples/benchmark/ExecRules_benchmark_test.go b/examples/benchmark/ExecRules_benchmark_test.go index 1fed5a69..092e584b 100644 --- a/examples/benchmark/ExecRules_benchmark_test.go +++ b/examples/benchmark/ExecRules_benchmark_test.go @@ -78,7 +78,8 @@ func load100RulesIntoKnowledgebase() { lib := ast.NewKnowledgeLibrary() rb := builder.NewRuleBuilder(lib) _ = rb.BuildRuleFromResource("exec_rules_test", "0.1.1", pkg.NewBytesResource([]byte(rules))) - knowledgeBase = lib.NewKnowledgeBaseInstance("exec_rules_test", "0.1.1") + knowledgeBase, _ = lib.NewKnowledgeBaseInstance("exec_rules_test", "0.1.1") + } func load1000RulesIntoKnowledgebase() { @@ -94,5 +95,5 @@ func load1000RulesIntoKnowledgebase() { lib := ast.NewKnowledgeLibrary() rb := builder.NewRuleBuilder(lib) _ = rb.BuildRuleFromResource("exec_rules_test", "0.1.1", pkg.NewBytesResource([]byte(rules))) - knowledgeBase = lib.NewKnowledgeBaseInstance("exec_rules_test", "0.1.1") + knowledgeBase, _ = lib.NewKnowledgeBaseInstance("exec_rules_test", "0.1.1") } diff --git a/examples/benchmark/LoadRules_benchmark_test.go b/examples/benchmark/LoadRules_benchmark_test.go index ecda0b32..0d757a6d 100755 --- a/examples/benchmark/LoadRules_benchmark_test.go +++ b/examples/benchmark/LoadRules_benchmark_test.go @@ -70,7 +70,7 @@ func load100RulesIntoKnowledgeBase() { lib := ast.NewKnowledgeLibrary() rb := builder.NewRuleBuilder(lib) _ = rb.BuildRuleFromResource("load_rules_test", "0.1.1", pkg.NewBytesResource([]byte(rules))) - _ = lib.NewKnowledgeBaseInstance("load_rules_test", "0.1.1") + _, _ = lib.NewKnowledgeBaseInstance("load_rules_test", "0.1.1") } func load1000RulesIntoKnowledgeBase() { @@ -86,5 +86,5 @@ func load1000RulesIntoKnowledgeBase() { lib := ast.NewKnowledgeLibrary() rb := builder.NewRuleBuilder(lib) _ = rb.BuildRuleFromResource("load_rules_test", "0.1.1", pkg.NewBytesResource([]byte(rules))) - _ = lib.NewKnowledgeBaseInstance("load_rules_test", "0.1.1") + _, _ = lib.NewKnowledgeBaseInstance("load_rules_test", "0.1.1") } diff --git a/go.mod b/go.mod index aeed7bb9..8b8429f5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module grule-rule-engine +module github.com/hyperjumptech/grule-rule-engine go 1.20 @@ -6,7 +6,6 @@ require ( github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 github.com/bmatcuk/doublestar v1.3.4 github.com/google/uuid v1.3.0 - github.com/hyperjumptech/grule-rule-engine v1.13.0 github.com/hyperjumptech/hyper-mux v1.1.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index cb0219f3..f72f9c1a 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,6 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hyperjumptech/grule-rule-engine v1.13.0 h1:YwPlzazuDSD+gQsOvWINTSJ+EP/rIqCrfSMHhqPwPqI= -github.com/hyperjumptech/grule-rule-engine v1.13.0/go.mod h1:kLuSOPGiB3U1FPvsThGaQhniDlWpWIvdIvCNI9hvCxU= github.com/hyperjumptech/hyper-mux v1.1.0 h1:HIM37If0OBOiiToOw5zZJLzOhZuhMObHwVCnTKiRyGw= github.com/hyperjumptech/hyper-mux v1.1.0/go.mod h1:qdok3j+/VEtFvJ+YOotTNskGg2BXg3UJTbycU2xFDvE= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= diff --git a/pkg/embeddedResource.go b/pkg/embeddedResource.go index 1a868969..6489bcb1 100644 --- a/pkg/embeddedResource.go +++ b/pkg/embeddedResource.go @@ -20,6 +20,7 @@ package pkg import ( "embed" "path/filepath" + "runtime" "strings" "github.com/bmatcuk/doublestar" @@ -84,6 +85,15 @@ type EmbeddedResourceBundle struct { // This will accept all *.grl files under /some/base/path and its directories. func NewEmbeddedResourceBundle(source embed.FS, basePath string, pathPattern ...string) *EmbeddedResourceBundle { + if runtime.GOOS == "windows" { + + return &EmbeddedResourceBundle{ + Source: source, + BasePath: strings.TrimLeft(basePath, "\\"), + PathPattern: pathPattern, + } + } + return &EmbeddedResourceBundle{ Source: source, BasePath: strings.TrimLeft(basePath, "/"), @@ -116,8 +126,14 @@ func (bundle *EmbeddedResourceBundle) loadPath(path string) ([]Resource, error) return nil, err } - if path == "." || path == "./" { - path = "" + if runtime.GOOS == "windows" { + if path == "." || path == "./" { + path = "" + } + } else { + if path == "." || path == ".\\" { + path = "" + } } ret := make([]Resource, 0) for _, finfo := range finfos {