| | |
| | | private const val OFFSET = 10 |
| | | } |
| | | |
| | | inner class Tag { |
| | | // 起始数据下标 |
| | | var sIndex = 0 |
| | | |
| | | // 起始数据对象 |
| | | var startData: BaseRealTimeData? = null |
| | | |
| | | // 末尾数据下标 |
| | | var eIndex = -1 |
| | | |
| | | // 末尾数据对象 |
| | | var endData: BaseRealTimeData? = null |
| | | |
| | | // 异常数据段 |
| | | var exceptionData = mutableListOf<BaseRealTimeData>() |
| | | |
| | | // 是否存在异常 |
| | | var exceptionExisted = false |
| | | |
| | | // 异常结果是否创建 |
| | | var exceptionCreated = false |
| | | |
| | | fun addExceptionData(data: BaseRealTimeData) { |
| | | exceptionExisted = true |
| | | exceptionData.add(data) |
| | | } |
| | | |
| | | fun refreshWithNextException(data: BaseRealTimeData) { |
| | | sIndex = eIndex |
| | | startData = data |
| | | exceptionData.clear() |
| | | exceptionExisted = false |
| | | exceptionCreated = false |
| | | } |
| | | } |
| | | |
| | | protected val tagMap = mutableMapOf<FactorType, T>() |
| | | |
| | | // 起始数据与末尾数据间隔 |
| | |
| | | |
| | | // 末尾数据对象 |
| | | protected var lastData: BaseRealTimeData? = null |
| | | |
| | | /** |
| | | * 后置判断:当相邻数据时间不连续时,或者满足自定义条件时,对之前已有的异常进行记录 |
| | | */ |
| | | open fun afterExcCheck(isContinue: Boolean, tag: T, hasException: Boolean?): Boolean { |
| | | return !isContinue || needCut(tag, hasException) |
| | | } |
| | | |
| | | /** |
| | | * 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求 |
| | | */ |
| | | open fun immeExcCheck(tag: T, factorType: FactorType): Boolean { |
| | | return false |
| | | } |
| | | |
| | | /** |
| | | * 判断相邻数据是否连续 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 判断是否满足异常条件 |
| | | * 判断数据量级在异常判断的范围内 |
| | | * 默认所有量级都在异常判断的范围内 |
| | | */ |
| | | open fun judgeDataScale(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean> { |
| | | val res = mutableMapOf<FactorType, Boolean>() |
| | | config.factorFilter.mainList().forEach { f -> res[f] = true } |
| | | return res |
| | | } |
| | | |
| | | /** |
| | | * 判断前后数据是否满足异常条件 |
| | | */ |
| | | abstract fun judgeException(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean> |
| | | |
| | |
| | | * 判断异常出现的连续个数是否满足条件 |
| | | * @param tag 异常数据对象 |
| | | */ |
| | | abstract fun judgeExceptionCount(tag: T): Boolean |
| | | abstract fun judgeExceptionCount(tag: T, factorType: FactorType?): Boolean |
| | | |
| | | /** |
| | | * 判断监测因子是否出现异常 |
| | | */ |
| | | open fun judge(p: BaseRealTimeData?, n: BaseRealTimeData): MutableMap<FactorType, Boolean> { |
| | | val jds = judgeDataScale(p, n) |
| | | val jex = judgeException(p, n) |
| | | val res = mutableMapOf<FactorType, Boolean>() |
| | | jds.forEach { (t, u) -> |
| | | res[t] = u && jex[t] ?: false |
| | | } |
| | | return res |
| | | } |
| | | |
| | | /** |
| | | * 异常数据的截取判断 |
| | | * 是否需要限制一组异常数据的长度 |
| | | * @return 默认不需要截取 |
| | | * @return |
| | | */ |
| | | open fun needCut(tag: T): Boolean { |
| | | return false |
| | | open fun needCut(tag: T, hasException: Boolean?): Boolean { |
| | | // 默认判断条件为 当异常不再重复出现时,形成异常结果 |
| | | return tag.exceptionExisted && hasException == false |
| | | } |
| | | |
| | | override fun init() { |
| | |
| | | |
| | | override fun onNextData(data: BaseRealTimeData) { |
| | | val isContinue = isContinuous(lastData, data) |
| | | val hasException = judgeException(lastData, data) |
| | | val hasException = judge(lastData, data) |
| | | config.factorFilter.selectedList.forEach { s -> |
| | | val f = s.main |
| | | tagMap[f]?.let { |
| | | it.addHistoryData(data) |
| | | |
| | | it.eIndex++ |
| | | // 起始数据 |
| | | it.endData = data |
| | | if (it.startData == null) { |
| | | it.refreshWithNextException(data) |
| | | } |
| | | // 判断相邻数据是否连续并且是否满足异常判断 |
| | | if (!isContinue || needCut(it)) { |
| | | // 数据不连续时,记录异常情况 |
| | | recordException(s, it, data) |
| | | } else { |
| | | if (hasException[f] == true) { |
| | | it.addExceptionData(data) |
| | | } else { |
| | | // 异常不再重复出现时,记录异常情况 |
| | | |
| | | // 对于异常的生成分别执行后置判断、和立即判断 |
| | | // 1. 后置判断:当相邻数据时间不连续时,或者满足自定义条件时,对之前已有的异常进行记录,形成异常结果 |
| | | // if (afterExcCheck(isContinue, it, hasException[f])) { |
| | | // // 数据不连续时或者满足主动截断条件时,记录异常情况 |
| | | // recordException(s, it, data) |
| | | // } |
| | | // 2. 立即判断:当出现异常时,缓存异常数据的同时,立即对已有异常进行判断是否满足异常结果要求 |
| | | if (hasException[f] == true) { |
| | | // afterExcCheck(isContinue, it, hasException[f]) |
| | | needCut(it, hasException[f]) |
| | | // 有异常出现时,记录异常数据 |
| | | it.addExceptionData(data) |
| | | // 当立即判断通过时,形成异常结果 |
| | | if (immeExcCheck(it, f)) { |
| | | recordException(s, it, data) |
| | | } |
| | | } |
| | | // 3. 数据正常,无任何异常时d |
| | | // TODO("2025.6.3:其他子类的此处刷新逻辑待完成“) |
| | | // else { |
| | | // it.refreshWithNextException(data) |
| | | // } |
| | | } |
| | | } |
| | | lastData = data |
| | |
| | | |
| | | /** |
| | | * 异常结束,记录异常 |
| | | * 判断已有的异常数据是否满足异常条件,满足则记录,不满足则略过 |
| | | */ |
| | | fun recordException(factor: FactorFilter.SelectedFactor, tag: T, data: BaseRealTimeData) { |
| | | checkResult(factor, ExceptionStatusType.Ended) |
| | | // if (tag.eIndex - tag.sIndex >= durationCount) { |
| | | tag.refreshWithNextException(data) |
| | | // } |
| | | } |
| | | |
| | | /** |
| | |
| | | ) { |
| | | val tag = tagMap[factor?.main] |
| | | if (factor != null && tag != null) { |
| | | if (tag.exceptionExisted && judgeExceptionCount(tag)) { |
| | | if (tag.exceptionExisted && judgeExceptionCount(tag, factor.main)) { |
| | | onNewException(tag, factor, exceptionStatus) |
| | | } |
| | | } else { |
| | | config.factorFilter.selectedList.forEach { f -> |
| | | val tag1 = tagMap[f.main] ?: return@forEach |
| | | if (tag1.exceptionExisted && judgeExceptionCount(tag1)) { |
| | | if (tag1.exceptionExisted && judgeExceptionCount(tag1, f.main)) { |
| | | onNewException(tag1, f, exceptionStatus) |
| | | } |
| | | } |
| | |
| | | */ |
| | | open fun onNewException(tag: T, factor: FactorFilter.SelectedFactor, exceptionStatus: ExceptionStatusType) { |
| | | if (tag.startData == null) return |
| | | val ex = newResult(tag.startData!!, lastData, factor, tag.exceptionData) |
| | | // val ex = newResult(tag.startData!!, tag.endData, factor, tag.exceptionData) |
| | | val ex = newResult(tag, factor) |
| | | .apply { status = exceptionStatus.value } |
| | | // 异常已创建时,更新异常信息 |
| | | if (tag.exceptionCreated) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 生成一条异常分析结果 |
| | | */ |
| | | abstract fun newResult(tag:T, factor: FactorFilter.SelectedFactor): Y |
| | | |
| | | } |