從0開始用kotlin作CRM之郵件系統--2.2引入規則引擎drools

需求:git

  • 1.當郵件標題或內容中,包含某些關鍵字時,將郵件設爲只能領導處理
  • 2.根據不一樣的關鍵字,給郵件打tag
  • 3.關鍵字,tag,項能夠交用戶配置

在上一章中,咱們已經能成功的收取gmail郵件了。在這一章中,咱們將使用drools對其進行處理。
規則引擎選擇drools,主要緣由是用的人比較多,對於我這種新手來說,能找到的資料會更多,而且看了幾個規則引擎,大可能是英文的,頭痛,因此找個方便點的。drools的缺點是比較重,你們能夠自行選擇。
咱們今天是先實現drools的介入(題外話,若是要經過視頻系統學drools7,能夠看下csdn學院的視頻教程。B站也有一個系列的,可是聲音效果不佳,文章csdn多一些,掘金不多)github

  • 引入依賴:2019.09.11查到的最新版本是7.9.0

<drools.version>7.9.0.Final</drools.version>

<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-core</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-compiler</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.kie</groupId>
			<artifactId>kie-spring</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.kie</groupId>
			<artifactId>kie-internal</artifactId>
			<version>${drools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-templates</artifactId>
			<version>${drools.version}</version>
		</dependency>
複製代碼
  • 加xml:聽說這個是必須的?我還沒試能不能不要

  • 引入工具類:讀rules的,來源於github
object KieSessionUtils {
    private var kieSession: KieSession? = null
    private val RULES_PATH = "rules/"
    /**
     * @description TODO(建立包含全部規則的對象)
     * @throws Exception
     * @return KieSession
     */
    val allRules: KieSession?
        @Throws(Exception::class)
        get() {
            try {
                disposeKieSession()
                val kieServices = KieServices.Factory.get()
                val kieFileSystem = kieServices.newKieFileSystem()
                for (file in PathMatchingResourcePatternResolver().getResources("classpath*:$RULES_PATH**/*.*")) {
                    kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.filename!!, "UTF-8"))
                }
                val kieRepository = KieServices.Factory.get().repository
                kieRepository.addKieModule { kieRepository.defaultReleaseId }
                val kieBuilder = KieServices.Factory.get().newKieBuilder(kieFileSystem)
                kieBuilder.buildAll()
                kieSession = KieServices.Factory.get().newKieContainer(kieRepository.defaultReleaseId).newKieSession().kieBase.newKieSession()
                return kieSession
            } catch (ex: Exception) {
                throw ex
            }

        }

    /**
     * @description TODO (快速新建KieSession)
     * @param classPath 絕對路徑
     * @return KieSession 有狀態
     */
    @Throws(Exception::class)
    fun newKieSession(classPath: String): KieSession {
        val kieSession = getKieBase(classPath).newKieSession()
        kieSession.addEventListener(DebugRuleRuntimeEventListener())
        return kieSession

    }

    /**
     * @description TODO (快速新建StatelessKieSession)
     * @param classPath 絕對路徑
     * @return StatelessKieSession 無狀態
     */
    @Throws(Exception::class)
    fun newStatelessKieSession(classPath: String): StatelessKieSession {
        return getKieBase(classPath).newStatelessKieSession()

    }

    /**
     * @description TODO (清空對象)
     * @title disposeKieSession 重置KieSession
     * @return void
     */
    fun disposeKieSession() {
        if (kieSession != null) {
            kieSession!!.dispose()
            kieSession = null
        }
    }

    @Throws(Exception::class)
    internal fun getKieBase(classPath: String): KieBase {
        val kieServices = KieServices.Factory.get()
        val kfs = kieServices.newKieFileSystem()
        //Resource resource = kieServices.getResources().newClassPathResource(classPath);
        val resource = ResourceFactory.newFileResource(classPath)
        kfs.write(resource)
        val kieBuilder = kieServices.newKieBuilder(kfs).buildAll()
        if (kieBuilder.results.getMessages(Message.Level.ERROR).size > 0) {
            throw Exception()
        }
        val kieContainer = kieServices.newKieContainer(kieServices.repository.defaultReleaseId)
        return kieContainer.kieBase
    }

    /**
     * 根據服務器真實路徑下的xls文件生成drl文件內容
     */
    @Throws(FileNotFoundException::class)
    fun getKieSessionFromXLS(realPath: String): KieSession {
        return createKieSessionFromDRL(getDRL(realPath))
    }

    // 把xls文件解析爲String
    @Throws(FileNotFoundException::class)
    fun getDRL(realPath: String): String {
        val file = File(realPath) // 例如:C:\\abc.xls
        val `is` = FileInputStream(file)
        val compiler = SpreadsheetCompiler()
        return compiler.compile(`is`, InputType.XLS)
    }

    // drl爲含有內容的字符串
    fun createKieSessionFromDRL(drl: String): KieSession {
        val kieHelper = KieHelper()
        kieHelper.addContent(drl, ResourceType.DRL)
        val results = kieHelper.verify()
        if (results.hasMessages(Message.Level.WARNING, Message.Level.ERROR)) {
            val messages = results.getMessages(Message.Level.WARNING, Message.Level.ERROR)
            for (message in messages) {
                println("Error: " + message.text)
            }
            throw IllegalStateException("Compilation errors were found. Check the logs.")
        }
        return kieHelper.build().newKieSession()
    }

複製代碼
  • 加rule:參見JAVA規則引擎 引入的這個系統郵件類,是不能修改的
  • 測試

相關文章
相關標籤/搜索