欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Drools 将DSLR转化为DRL

程序员文章站 2022-05-07 21:55:28
...
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