表达式解析
import org.junit.Test
import scala.collection.mutable
class ExprTestCase {
private val orderSource = "source_1"
private val saleChannel = "saleChannel"
val datas = new mutable.HashMap[String, String]();
// p1, source1, sale1, source_1 & ( sale_channel_1 | sq )"
datas.put("source_1", "source_1");
datas.put("saleChannel", "saleChannel");
datas.put("orderSource", "orderSource");
val expr = s"orderSource:source_1 & ( saleChannel:saleChannel | orderSource:orderSource )"
private val tokens: Array[String] = expr.split("\s+")
private val optStack = new mutable.ArrayStack[String]()
private val memberStatck = new mutable.ArrayStack[String]()
private var flag = false;
def matchFiled(fieldName: String): String = {
fieldName match {
case "orderSource" => orderSource
case "saleChannel" => saleChannel
case _ => throw new IllegalArgumentException(s"unknown filed name: ${fieldName}, it's can only be 'orderSource' or 'saleChannel'")
}
}
@Test
def expParseTestCase() = {
var opt = ""
def exprProcess = {
if (opt == "" || opt == "(") opt = optStack.pop()
while (opt != null && opt != "(" && memberStatck.nonEmpty) {
opt match {
case "|" => {
if(memberStatck.size > 1) {
val m1 = memberStatck.pop().split(":")
val m2 = memberStatck.pop().split(":")
flag = datas.getOrElse(m2(1), "N").eq(matchFiled(m2(0))) || datas.getOrElse(m1(1), "N").eq(matchFiled(m1(0)))
println(
s"""
[&]: ${datas.getOrElse(m2(1), "N")}.eq("${matchFiled(m2(0))}") || ${datas.getOrElse(m1(1), "N")}.eq("${matchFiled(m1(0))}")
""")
} else {
val m1 = memberStatck.pop().split(":")
flag = flag || datas.getOrElse(m1(1), "N").eq(matchFiled(m1(0)))
println(
s"""
[&]: ${flag} || ${datas.getOrElse(m1(1), "N")}.eq("${matchFiled(m1(0))}
""")
}
}
case "&" => {
if (memberStatck.size > 1) {
val m1 = memberStatck.pop().split(":")
val m2 = memberStatck.pop().split(":")
flag = datas.getOrElse(m1(1), "N").equals(matchFiled(m1(0))) && datas.getOrElse(m2(1), "N").equals(matchFiled(m2(0)))
println(
s"""
[&]: ${datas.getOrElse(m1(1), "N")}.eq("${matchFiled(m1(0))}") && ${datas.getOrElse(m2(1), "N")}.eq("${matchFiled(m2(0))}")
""")
} else {
val m1 = memberStatck.pop().split(":")
flag = flag && datas.getOrElse(m1(1), "N").equals(matchFiled(m1(0)))
println(
s"""
[&]: ${false} && ${datas.get(m1(1))}.eq("$saleChannel")}.eq("${matchFiled(m1(0))}")
""")
}
}
}
if (optStack.nonEmpty) opt = optStack.pop()
}
}
tokens.foreach {
case ")" => {
exprProcess
}
case token@("|" | "&" | "(") => {
optStack.push(token)
}
case token => {
memberStatck.push(token)
}
}
println("expr match complete :) ")
if(memberStatck.nonEmpty) {
exprProcess
}
println(flag)
println(optStack)
println(memberStatck)
}
}
输出
[&]: saleChannel.eq("saleChannel") || orderSource.eq("source_1")
expr match complete :)
[&]: false && Some(source_1).eq("saleChannel")}.eq("source_1")
true
ArrayStack()
ArrayStack()
原文地址:https://blog.csdn.net/weixin_46661903/article/details/134565961
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_1987.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。