mirror of
https://github.com/slhaf/Partner.git
synced 2026-06-28 01:59:17 +08:00
feat(impression): add entity overview state
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
package work.slhaf.partner.core.cognition.impression
|
package work.slhaf.partner.core.cognition.impression
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject
|
||||||
|
import work.slhaf.partner.framework.agent.state.State
|
||||||
|
import work.slhaf.partner.framework.agent.state.StateSerializable
|
||||||
|
import java.nio.file.Path
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.locks.ReentrantLock
|
import java.util.concurrent.locks.ReentrantLock
|
||||||
@@ -11,7 +15,7 @@ class Entity @JvmOverloads constructor(
|
|||||||
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
|
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
|
||||||
private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
|
private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
|
||||||
private val features: MutableMap<String, IndexableData> = mutableMapOf()
|
private val features: MutableMap<String, IndexableData> = mutableMapOf()
|
||||||
) {
|
) : StateSerializable {
|
||||||
|
|
||||||
private val impressionLock = ReentrantLock()
|
private val impressionLock = ReentrantLock()
|
||||||
private val relationLock = ReentrantLock()
|
private val relationLock = ReentrantLock()
|
||||||
@@ -115,6 +119,18 @@ class Entity @JvmOverloads constructor(
|
|||||||
}.toSet()
|
}.toSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun statePath(): Path = Path.of("core", "impression", "entity-$uuid.json")
|
||||||
|
|
||||||
|
override fun load(state: JSONObject) {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun convert(): State {
|
||||||
|
TODO("Not yet implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun autoLoadOnRegister(): Boolean = false
|
||||||
|
|
||||||
data class IndexableData(
|
data class IndexableData(
|
||||||
var confidence: Double
|
var confidence: Double
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,20 +1,27 @@
|
|||||||
package work.slhaf.partner.core.cognition.impression;
|
package work.slhaf.partner.core.cognition.impression;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityCore;
|
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityCore;
|
||||||
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityMethod;
|
import work.slhaf.partner.framework.agent.factory.capability.annotation.CapabilityMethod;
|
||||||
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.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@CapabilityCore(value = "cognition")
|
@CapabilityCore(value = "cognition")
|
||||||
public class ImpressionCore implements StateSerializable {
|
public class ImpressionCore implements StateSerializable {
|
||||||
|
|
||||||
private final ConcurrentHashMap<String, Entity> knownEntities = new ConcurrentHashMap<>();
|
/**
|
||||||
|
* Keyed by entity uuid. Subject can be revised or merged later, so it should not be used as the stable key.
|
||||||
|
*/
|
||||||
|
private final ConcurrentHashMap<String, Entity> knownEntitiesByUuid = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@CapabilityMethod
|
@CapabilityMethod
|
||||||
public void updateRelation() {
|
public void updateRelation() {
|
||||||
@@ -39,13 +46,43 @@ public class ImpressionCore implements StateSerializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load(@NotNull JSONObject state) {
|
public void load(@NotNull JSONObject state) {
|
||||||
|
JSONArray entityArray = state.getJSONArray("entities");
|
||||||
|
if (entityArray == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
knownEntitiesByUuid.clear();
|
||||||
|
for (int i = 0; i < entityArray.size(); i++) {
|
||||||
|
JSONObject entityObject = entityArray.getJSONObject(i);
|
||||||
|
if (entityObject == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String uuid = entityObject.getString("uuid");
|
||||||
|
String subject = entityObject.getString("subject");
|
||||||
|
if (uuid == null || uuid.isBlank() || subject == null || subject.isBlank()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Entity entity = new Entity(uuid, subject);
|
||||||
|
entity.load();
|
||||||
|
knownEntitiesByUuid.put(uuid, entity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull State convert() {
|
public @NotNull State convert() {
|
||||||
State state = new State();
|
State state = new State();
|
||||||
|
|
||||||
|
List<StateValue.Obj> entities = knownEntitiesByUuid.values().stream()
|
||||||
|
.map(entity -> StateValue.obj(Map.of(
|
||||||
|
"uuid", entity.getUuid(),
|
||||||
|
"subject", entity.getSubject()
|
||||||
|
)))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
state.append("entities", StateValue.arr(entities));
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user