All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netgrif.application.engine.configuration.drools.RefreshableKieBase Maven / Gradle / Ivy

Go to download

System provides workflow management functions including user, role and data management.

There is a newer version: 6.3.3
Show newest version
package com.netgrif.application.engine.configuration.drools;

import com.netgrif.application.engine.configuration.drools.interfaces.IKnowledgeBaseInitializer;
import com.netgrif.application.engine.configuration.drools.interfaces.IRefreshableKieBase;
import com.netgrif.application.engine.rules.domain.RuleRepository;
import org.kie.api.KieBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class RefreshableKieBase implements IRefreshableKieBase {

    public static final Logger log = LoggerFactory.getLogger(RefreshableKieBase.class);

    private final RuleRepository ruleRepository;

    private final IKnowledgeBaseInitializer knowledgeBaseInitializer;

    @Value("${drools.knowBase.auto-refresh:#{true}}")
    private boolean autoRefresh;

    private LocalDateTime lastRefresh;

    private KieBase kieBase;


    public RefreshableKieBase(@Autowired RuleRepository ruleRepository, @Autowired IKnowledgeBaseInitializer knowledgeBaseInitializer) {
        this.ruleRepository = ruleRepository;
        this.knowledgeBaseInitializer = knowledgeBaseInitializer;
    }

    public KieBase kieBase() {
        return kieBase;
    }

    public boolean shouldRefresh() {
        return autoRefresh && ruleRepository.existsByLastUpdateAfter(lastRefresh);
    }

    public void refresh() {
        this.lastRefresh = LocalDateTime.now();
        log.info("Refreshing kieBase");
        this.kieBase = knowledgeBaseInitializer.constructKieBase();
        log.info("KieBase refreshed");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy