Drools 将DSLR转化为DRL

Scala的代码只能用Java着色。
gist 链接如下: https://gist.github.com/2574987

class DrlGeneratorFromDsl {
    private val logger = LoggerFactory.getLogger(getClass)
    private var dslFiles = List.empty[DSLTokenizedMappingFile]

    def addDsl(resource: Resource) {
        val file = new DSLTokenizedMappingFile
        if (!file.parseAndLoad(resource.getReader)) {
            file.getErrors.asScala.foreach(e => logger.error(e.asInstanceOf[ParserError].getMessage))
        }
        dslFiles :+= file
    }

    // this is dslr type rule
    def generate(rules: Iterable[RuleData]): String = {
        val dslr = rules.map(_.toDslr).mkString("\n")
        val expander = getDslExpander
        val drl = expander.expand(dslr)
        if (expander.hasErrors) {
            expander.getErrors.asScala.foreach(e => logger.error(e.getMessage))
        }
        return drlHeader + drl
    }

    private val drlHeader = """package logistics.settlement.drools.drl.gen

import logistics.settlement.drools.OrderData;
import logistics.settlement.drools.UnitPrice;
"""

    /**
     * Returns an expander for DSLs (only if there is a DSL configured for this
     * package).
     */
    private def getDslExpander: DefaultExpander = {
        var expander: DefaultExpander = new DefaultExpander
        for (file <- this.dslFiles) {
            expander.addDSLMapping(file.getMapping)
        }
        return expander
    }
}


你可能感兴趣的:(drools)