优化:转发规则匹配的值允许传入逻辑运算符与(&&)或(||)(用于支持多个关键词) #470

This commit is contained in:
pppscn 2024-05-22 21:52:07 +08:00
parent 9dbb2572fa
commit 8cc4c76735
2 changed files with 71 additions and 68 deletions

View File

@ -214,47 +214,49 @@ data class Rule(
//内容分支 //内容分支
private fun checkValue(msgValue: String?): Boolean { private fun checkValue(msgValue: String?): Boolean {
var checked = false if (msgValue == null) return false
when (this.check) {
CHECK_IS -> checked = this.value == msgValue
CHECK_NOT_IS -> checked = this.value != msgValue
CHECK_CONTAIN -> if (msgValue != null) {
checked = msgValue.contains(this.value)
}
CHECK_NOT_CONTAIN -> if (msgValue != null) { fun evaluateCondition(condition: String): Boolean {
checked = !msgValue.contains(this.value) return when (check) {
} CHECK_IS -> msgValue == condition
CHECK_NOT_IS -> msgValue != condition
CHECK_START_WITH -> if (msgValue != null) { CHECK_CONTAIN -> msgValue.contains(condition)
checked = msgValue.startsWith(this.value) CHECK_NOT_CONTAIN -> !msgValue.contains(condition)
} CHECK_START_WITH -> msgValue.startsWith(condition)
CHECK_END_WITH -> msgValue.endsWith(condition)
CHECK_END_WITH -> if (msgValue != null) { CHECK_REGEX -> try {
checked = msgValue.endsWith(this.value) val pattern = Pattern.compile(condition, Pattern.CASE_INSENSITIVE)
}
CHECK_REGEX -> if (msgValue != null) {
try {
//checked = Pattern.matches(this.value, msgValue);
val pattern = Pattern.compile(this.value, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(msgValue) val matcher = pattern.matcher(msgValue)
while (matcher.find()) { matcher.find()
checked = true
break
}
} catch (e: PatternSyntaxException) { } catch (e: PatternSyntaxException) {
Log.d(TAG, "PatternSyntaxException: ") Log.i(TAG, "PatternSyntaxException: ${e.description}, Index: ${e.index}, Message: ${e.message}, Pattern: ${e.pattern}")
Log.d(TAG, "Description: " + e.description) false
Log.d(TAG, "Index: " + e.index) }
Log.d(TAG, "Message: " + e.message)
Log.d(TAG, "Pattern: " + e.pattern) else -> false
} }
} }
else -> {} fun parseAndEvaluate(expression: String): Boolean {
// Split by "||" and evaluate each segment joined by "&&"
val orGroups = expression.split("||")
return orGroups.any { orGroup ->
val andGroups = orGroup.split("&&")
andGroups.all { andGroup ->
val trimmedCondition = andGroup.trim()
evaluateCondition(trimmedCondition)
} }
Log.i(TAG, "checkValue " + msgValue + " " + this.check + " " + this.value + " checked:" + checked) }
}
val checked = if (value.contains("&&") || value.contains("||")) {
parseAndEvaluate(value)
} else {
evaluateCondition(value)
}
Log.i(TAG, "checkValue $msgValue $check $value checked:$checked")
return checked return checked
} }
} }

View File

@ -119,45 +119,46 @@ class RuleLine(line: String, lineNum: Int, beforeRuleLine: RuleLine?) {
//内容分支 //内容分支
private fun checkValue(msgValue: String?): Boolean { private fun checkValue(msgValue: String?): Boolean {
var checked = false if (msgValue == null) return false
when (check) {
CHECK_EQUALS -> checked = value == msgValue
CHECK_CONTAIN -> if (msgValue != null) {
checked = msgValue.contains(value)
}
CHECK_NOT_CONTAIN -> if (msgValue != null) { fun evaluateCondition(condition: String): Boolean {
checked = !msgValue.contains(value) return when (check) {
} CHECK_EQUALS -> msgValue == condition
CHECK_CONTAIN -> msgValue.contains(condition)
CHECK_START_WITH -> if (msgValue != null) { CHECK_NOT_CONTAIN -> !msgValue.contains(condition)
checked = msgValue.startsWith(value) CHECK_START_WITH -> msgValue.startsWith(condition)
} CHECK_END_WITH -> msgValue.endsWith(condition)
CHECK_REGEX -> try {
CHECK_END_WITH -> if (msgValue != null) { val pattern = Pattern.compile(condition, Pattern.CASE_INSENSITIVE)
checked = msgValue.endsWith(value)
}
CHECK_REGEX -> if (msgValue != null) {
try {
//checked = Pattern.matches(this.value, msgValue);
val pattern = Pattern.compile(value, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(msgValue) val matcher = pattern.matcher(msgValue)
while (matcher.find()) { matcher.find()
checked = true
break
}
} catch (e: PatternSyntaxException) { } catch (e: PatternSyntaxException) {
logg("PatternSyntaxException: ") logg("PatternSyntaxException: ${e.description}, Index: ${e.index}, Message: ${e.message}, Pattern: ${e.pattern}")
logg("Description: " + e.description) false
logg("Index: " + e.index) }
logg("Message: " + e.message)
logg("Pattern: " + e.pattern) else -> false
} }
} }
else -> {} fun parseAndEvaluate(expression: String): Boolean {
// Split by "||" and evaluate each segment joined by "&&"
val orGroups = expression.split("||")
return orGroups.any { orGroup ->
val andGroups = orGroup.split("&&")
andGroups.all { andGroup ->
val trimmedCondition = andGroup.trim()
evaluateCondition(trimmedCondition)
} }
}
}
val checked = if (value.contains("&&") || value.contains("||")) {
parseAndEvaluate(value)
} else {
evaluateCondition(value)
}
logg("checkValue $msgValue $check $value checked:$checked") logg("checkValue $msgValue $check $value checked:$checked")
return checked return checked
} }