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: function for returning the KB key in KL #447

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 14 additions & 8 deletions ast/KnowledgeBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package ast
import (
"bytes"
"fmt"
"github.com/google/uuid"
"io"
"sort"
"strings"
"sync"

"github.com/google/uuid"

"github.com/hyperjumptech/grule-rule-engine/pkg"
)

Expand All @@ -43,7 +44,7 @@ type KnowledgeLibrary struct {
// Although this KnowledgeBase blueprint works, It SHOULD NOT be used directly in the engine.
// You should obtain KnowledgeBase instance by calling NewKnowledgeBaseInstance
func (lib *KnowledgeLibrary) GetKnowledgeBase(name, version string) *KnowledgeBase {
knowledgeBase, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)]
knowledgeBase, ok := lib.Library[GetKnowledgeBaseKey(name, version)]
if ok {

return knowledgeBase
Expand All @@ -54,14 +55,14 @@ func (lib *KnowledgeLibrary) GetKnowledgeBase(name, version string) *KnowledgeBa
RuleEntries: make(map[string]*RuleEntry),
WorkingMemory: NewWorkingMemory(name, version),
}
lib.Library[fmt.Sprintf("%s:%s", name, version)] = knowledgeBase
lib.Library[GetKnowledgeBaseKey(name, version)] = knowledgeBase

return knowledgeBase
}

// RemoveRuleEntry mark the rule entry as deleted
func (lib *KnowledgeLibrary) RemoveRuleEntry(ruleName, name string, version string) {
nameVersion := fmt.Sprintf("%s:%s", name, version)
nameVersion := GetKnowledgeBaseKey(name, version)
_, ok := lib.Library[nameVersion]
if ok {
ruleEntry, ok := lib.Library[nameVersion].RuleEntries[ruleName]
Expand Down Expand Up @@ -97,12 +98,12 @@ func (lib *KnowledgeLibrary) LoadKnowledgeBaseFromReader(reader io.Reader, overw
return nil, err
}
if overwrite {
lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase
lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name,knowledgeBase.Version)] = knowledgeBase

return knowledgeBase, nil
}
if _, ok := lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)]; !ok {
lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase
if _, ok := lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name,knowledgeBase.Version)]; !ok {
lib.Library[GetKnowledgeBaseKey(knowledgeBase.Name,knowledgeBase.Version)] = knowledgeBase

return knowledgeBase, nil
}
Expand All @@ -129,7 +130,7 @@ func (lib *KnowledgeLibrary) StoreKnowledgeBaseToWriter(writer io.Writer, name,
// NewKnowledgeBaseInstance will create a new instance based on KnowledgeBase blue print
// identified by its name and version
func (lib *KnowledgeLibrary) NewKnowledgeBaseInstance(name, version string) (*KnowledgeBase, error) {
knowledgeBase, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)]
knowledgeBase, ok := lib.Library[GetKnowledgeBaseKey(name, version)]
if ok {
newClone, err := knowledgeBase.Clone(pkg.NewCloneTable())
if err != nil {
Expand Down Expand Up @@ -313,3 +314,8 @@ func (e *KnowledgeBase) Reset() {
}
}
}

// GetKnowledgeBaseKey returns the key corresponding to the knowledgeBase in the KnowledgeLibrary
func GetKnowledgeBaseKey(name, version string) string {
return fmt.Sprintf("%s:%s", name, version)
}