Drools- how to find out which all rules were matched?

后端 未结 5 1617
后悔当初
后悔当初 2021-02-02 13:32

I\'ve one .DRL file which has say 10 rules. Once I insert a fact, some rules may be matched- how do I find out which rules were matched programmatically?

5条回答
  •  借酒劲吻你
    2021-02-02 13:59

    You can use a static logger factory which will log with your favorite logger the actions from your DRL file.

    For instance:

    import org.drools.runtime.rule.RuleContext;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public final class DRLLogger {
    
    private DRLLogger() {
    }
    
    protected static Logger getLogger(final RuleContext drools) {
        final String category = drools.getRule().getPackageName() + "." + drools.getRule().getName();
        final Logger logger = LoggerFactory.getLogger(category);
        return logger;
    }
    
    public static void info(final RuleContext drools, final String message, final Object... parameters) {
        final Logger logger = getLogger(drools);
        logger.info(message, parameters);
    }
    
    public static void debug(final RuleContext drools, final String message, final Object... parameters) {
        final Logger logger = getLogger(drools);
        logger.debug(message, parameters);
    }
    
    public static void error(final RuleContext drools, final String message, final Object... parameters) {
        final Logger logger = getLogger(drools);
        logger.error(message, parameters);
    }
    
    }
    

    Then from your DRL file:

    import function com.mycompany.DRLLogger.*
    
    rule "myrule"
    when
        $fact: Fact()
    then
        info(drools, "Fact:{}", $fact);
    end
    

提交回复
热议问题