mirror of
https://github.com/slhaf/Partner.git
synced 2026-10-25 19:03:48 +00:00
feat(impression): serialize entity state
This commit is contained in:
@@ -3,6 +3,7 @@ package work.slhaf.partner.core.cognition.impression
|
|||||||
import com.alibaba.fastjson2.JSONObject
|
import com.alibaba.fastjson2.JSONObject
|
||||||
import work.slhaf.partner.framework.agent.state.State
|
import work.slhaf.partner.framework.agent.state.State
|
||||||
import work.slhaf.partner.framework.agent.state.StateSerializable
|
import work.slhaf.partner.framework.agent.state.StateSerializable
|
||||||
|
import work.slhaf.partner.framework.agent.state.StateValue
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
@@ -122,15 +123,83 @@ class Entity @JvmOverloads constructor(
|
|||||||
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
|
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
|
||||||
|
|
||||||
override fun load(state: JSONObject) {
|
override fun load(state: JSONObject) {
|
||||||
TODO("Not yet implemented")
|
state.getJSONObject("relations")?.let { loadedRelations ->
|
||||||
|
relationLock.withLock {
|
||||||
|
relations.clear()
|
||||||
|
loadedRelations.forEach { (target, relationValue) ->
|
||||||
|
val relationObject = relationValue as? JSONObject ?: return@forEach
|
||||||
|
val relationMap = mutableMapOf<String, Double>()
|
||||||
|
relationObject.forEach { (relation, strengthValue) ->
|
||||||
|
numberValue(strengthValue)?.let { relationMap[relation] = it }
|
||||||
|
}
|
||||||
|
if (relationMap.isNotEmpty()) {
|
||||||
|
relations[target] = relationMap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.getJSONObject("impressions")?.let { loadedImpressions ->
|
||||||
|
impressionLock.withLock {
|
||||||
|
impressions.clear()
|
||||||
|
impressions.putAll(loadIndexableDataMap(loadedImpressions))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.getJSONObject("features")?.let { loadedFeatures ->
|
||||||
|
featureLock.withLock {
|
||||||
|
features.clear()
|
||||||
|
features.putAll(loadIndexableDataMap(loadedFeatures))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun convert(): State {
|
override fun convert(): State {
|
||||||
TODO("Not yet implemented")
|
val state = State()
|
||||||
|
state.append("uuid", StateValue.str(uuid))
|
||||||
|
state.append("subject", StateValue.str(subject))
|
||||||
|
|
||||||
|
val relationState = relationLock.withLock {
|
||||||
|
relations.mapValues { (_, relationMap) -> relationMap.toMap() }
|
||||||
|
}
|
||||||
|
state.append("relations", StateValue.obj(relationState))
|
||||||
|
|
||||||
|
val impressionState = impressionLock.withLock {
|
||||||
|
indexableDataState(impressions)
|
||||||
|
}
|
||||||
|
state.append("impressions", StateValue.obj(impressionState))
|
||||||
|
|
||||||
|
val featureState = featureLock.withLock {
|
||||||
|
indexableDataState(features)
|
||||||
|
}
|
||||||
|
state.append("features", StateValue.obj(featureState))
|
||||||
|
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun autoLoadOnRegister(): Boolean = false
|
override fun autoLoadOnRegister(): Boolean = false
|
||||||
|
|
||||||
|
private fun loadIndexableDataMap(state: JSONObject): Map<String, IndexableData> {
|
||||||
|
val loaded = mutableMapOf<String, IndexableData>()
|
||||||
|
state.forEach { (key, value) ->
|
||||||
|
val confidence = when (value) {
|
||||||
|
is JSONObject -> value.getDouble("confidence")?.toDouble() ?: 1.0
|
||||||
|
else -> numberValue(value) ?: return@forEach
|
||||||
|
}
|
||||||
|
loaded[key] = IndexableData(confidence)
|
||||||
|
}
|
||||||
|
return loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun indexableDataState(source: Map<String, IndexableData>): Map<String, Map<String, Double>> =
|
||||||
|
source.mapValues { (_, data) -> mapOf("confidence" to data.confidence) }
|
||||||
|
|
||||||
|
private fun numberValue(value: Any?): Double? = when (value) {
|
||||||
|
is Number -> value.toDouble()
|
||||||
|
is String -> value.toDoubleOrNull()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
data class IndexableData(
|
data class IndexableData(
|
||||||
var confidence: Double
|
var confidence: Double
|
||||||
) {
|
) {
|
||||||
|
|||||||
Reference in New Issue
Block a user