// FetchMatchingRules function is responsible to fetch all the rules that matches to a fact against all rule entries
// Returns []*ast.RuleEntry order by salience
FetchMatchingRules function is responsible to fetch all the rules that matches to a fact against all rule entries
Returns []*ast.RuleEntry order by salience
// 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) {
	log.Debugf("Starting rule matching using knowledge '%s' version %s. Contains %d rule entries", knowledge.Name, knowledge.Version, len(knowledge.RuleEntries))
	defunc := &ast.BuiltInFunctions{
		Knowledge:     knowledge,
		WorkingMemory: knowledge.WorkingMemory,
		DataContext:   dataCtx,
	}
	dataCtx.Add("DEFUNC", defunc)
	log.Debugf("Resetting Working memory")
	knowledge.WorkingMemory.ResetAll()
	log.Debugf("Initializing Context")
	knowledge.InitializeContext(dataCtx)
	log.Tracef("Select all rule entry that can be executed.")
	runnable := make([]*ast.RuleEntry, 0)
	for _, v := range knowledge.RuleEntries {
		can, err := v.Evaluate(dataCtx, knowledge.WorkingMemory)
		if err != nil {
			log.Errorf("Failed testing condition for rule : %s. Got error %v", v.RuleName, err)
		}
		if can {
			runnable = append(runnable, v)
		}
	}
	log.Debugf("Matching rules length %d.", len(runnable))
	if len(runnable) > 1 {
		sort.SliceStable(runnable, func(i, j int) bool {
			return runnable[i].Salience > runnable[j].Salience
		})
	}
	return runnable, nil
}
func ( g * GruleEngine ) FetchMatchingRules ( dataCtx ast . IDataContext , knowledge * ast . KnowledgeBase ) ( [ ] * ast . RuleEntry , error ) { log . Debugf ( " " , knowledge . Name , knowledge . Version , len ( knowledge . RuleEntries ) ) defunc := & ast . BuiltInFunctions { Knowledge : knowledge , WorkingMemory : knowledge . WorkingMemory , DataContext : dataCtx , } dataCtx . Add ( " " , defunc ) log . Debugf ( " " ) knowledge . WorkingMemory . ResetAll ( ) log . Debugf ( " " ) knowledge . InitializeContext ( dataCtx ) log . Tracef ( " " ) runnable := make ( [ ] * ast . RuleEntry , 0 ) for _ , v := range knowledge . RuleEntries { can , err := v . Evaluate ( dataCtx , knowledge . WorkingMemory ) if err != nil { log . Errorf ( " " , v . RuleName , err ) } if can { runnable = append ( runnable , v ) } } log . Debugf ( " " , len ( runnable ) ) if len ( runnable ) > 1 { sort . SliceStable ( runnable , func ( i , j int ) bool { return runnable [ i ] . Salience > runnable [ j ] . Salience } ) } return runnable , nil }
FetchMatchingRules function is responsible to fetch all the rules that matches to a fact against all rule entries
Returns []*ast.RuleEntry order by salience
FetchMatchingRules function is responsible to fetch all the rules that matches to a fact against all rule entries Returns [] * ast . RuleEntry order by salience
// Prepare the build-in function and add to datacontext. // Working memory need to be resetted. all Expression will be set as not evaluated. // Initialize all AST with datacontext and working memory //Loop through all the rule entries available in the knowledge base and add to the response list if it is able to evaluate // Select all rule entry that can be executed. // test if this rule entry v can execute. // No longer return error, since unavailability of variable or fact in context might be intentional. // if can, add into runnable array