Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/nilcheck #401

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions engine/GruleEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (g *GruleEngine) notifyBeginCycle(cycle uint64) {
// The engine will evaluate context cancelation status in each cycle.
// The engine also do conflict resolution of which rule to execute.
func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataContext, knowledge *ast.KnowledgeBase) error {
if knowledge == nil || dataCtx == nil {

return fmt.Errorf("nil KnowledgeBase or DataContext is not allowed")
}

log.Debugf("Starting rule execution using knowledge '%s' version %s. Contains %d rule entries", knowledge.Name, knowledge.Version, len(knowledge.RuleEntries))

// Prepare the timer, we need to measure the processing time in debug mode.
Expand Down Expand Up @@ -241,6 +246,11 @@ func (g *GruleEngine) ExecuteWithContext(ctx context.Context, dataCtx ast.IDataC
// FetchMatchingRules function is responsible to fetch all the rules that matches to a fact against all rule entries
// Returns []*ast.RuleEntry order by salience
func (g *GruleEngine) FetchMatchingRules(dataCtx ast.IDataContext, knowledge *ast.KnowledgeBase) ([]*ast.RuleEntry, error) {
if knowledge == nil || dataCtx == nil {

return nil, fmt.Errorf("nil KnowledgeBase or DataContext is not allowed")
}

log.Debugf("Starting rule matching using knowledge '%s' version %s. Contains %d rule entries", knowledge.Name, knowledge.Version, len(knowledge.RuleEntries))
// Prepare the build-in function and add to datacontext.
defunc := &ast.BuiltInFunctions{
Expand Down
6 changes: 3 additions & 3 deletions examples/Concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ var (
type Vibonaci struct {
fmt.Stringer
Count int
A uint
B uint
C uint
A uint64
B uint64
C uint64
}

func startThread(threadName string) {
Expand Down
54 changes: 54 additions & 0 deletions examples/NilKnowledgeBasePanic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright hyperjumptech/grule-rule-engine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package examples

import (
"testing"

"github.com/hyperjumptech/grule-rule-engine/ast"
"github.com/hyperjumptech/grule-rule-engine/engine"

"github.com/stretchr/testify/assert"
)

func Test_NoPanicOnEmptyKnowledgeBase(t *testing.T) {
// create a new fact for user
user := &User{
Name: "Calo",
Age: 0,
Male: true,
}
// create an empty data context
dataContext := ast.NewDataContext()
// add the fact struct to the data context
err := dataContext.Add("User", user)
if err != nil {
t.Fatal(err)
}

t.Run("with nil knowledge base in execute", func(t *testing.T) {
eng := &engine.GruleEngine{MaxCycle: 10}
err = eng.Execute(dataContext, nil)

assert.NotNil(t, err)
})

t.Run("with nil knowledge base in FetchMatchingRules", func(t *testing.T) {
eng := &engine.GruleEngine{MaxCycle: 10}
_, err = eng.FetchMatchingRules(dataContext, nil)

assert.NotNil(t, err)
})
}
Loading