Drools 将DSLR转化为DRL
程序员文章站
2022-05-07 21:55:28
...
Scala的代码只能用Java着色。
gist 链接如下: https://gist.github.com/2574987
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--java端使用