mirror of
https://github.com/slhaf/Partner.git
synced 2026-06-27 17:49:16 +08:00
Compare commits
2 Commits
f3213675ff
...
8323f8ed13
| Author | SHA1 | Date | |
|---|---|---|---|
| 8323f8ed13 | |||
| 0e1201253d |
@@ -9,16 +9,19 @@ class ActiveEntity @JvmOverloads constructor(
|
||||
timestamp: Long = System.currentTimeMillis(),
|
||||
private val _evidences: MutableList<String> = mutableListOf(),
|
||||
) : BlockContent("active_entity_$timestamp", "impression") {
|
||||
val evidences: List<String> get() = _evidences
|
||||
val evidences: List<String>
|
||||
get() = synchronized(_evidences) { _evidences.toList() }
|
||||
|
||||
private val _subject = AtomicReference("UNKNOWN")
|
||||
val subject: String get() = _subject.get()
|
||||
|
||||
private val _projectedFeatures: MutableMap<String, Double> = mutableMapOf()
|
||||
val projectedFeatures: Map<String, Double> get() = _projectedFeatures
|
||||
val projectedFeatures: Map<String, Double>
|
||||
get() = synchronized(_projectedFeatures) { _projectedFeatures.toMap() }
|
||||
|
||||
private val _projectedImpressions: MutableMap<String, Double> = mutableMapOf()
|
||||
val projectedImpressions: Map<String, Double> get() = _projectedImpressions
|
||||
val projectedImpressions: Map<String, Double>
|
||||
get() = synchronized(_projectedImpressions) { _projectedImpressions.toMap() }
|
||||
|
||||
fun addEvidence(evidence: String) = synchronized(_evidences) {
|
||||
_evidences.add(evidence)
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
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.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
@@ -7,11 +11,11 @@ import kotlin.concurrent.withLock
|
||||
|
||||
class Entity @JvmOverloads constructor(
|
||||
val uuid: String = UUID.randomUUID().toString(),
|
||||
private val subject: String,
|
||||
val subject: String,
|
||||
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
|
||||
private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
|
||||
private val features: MutableMap<String, IndexableData> = mutableMapOf()
|
||||
) {
|
||||
) : StateSerializable {
|
||||
|
||||
private val impressionLock = ReentrantLock()
|
||||
private val relationLock = ReentrantLock()
|
||||
@@ -105,6 +109,28 @@ class Entity @JvmOverloads constructor(
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
|
||||
fun showFeatures(): Set<FeatureView> = featureLock.withLock {
|
||||
features.map {
|
||||
FeatureView(
|
||||
it.key,
|
||||
it.value.confidence
|
||||
)
|
||||
}.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(
|
||||
var confidence: Double
|
||||
) {
|
||||
@@ -127,6 +153,11 @@ class Entity @JvmOverloads constructor(
|
||||
val relations: Map<String, Double>
|
||||
)
|
||||
|
||||
data class FeatureView(
|
||||
val feature: String,
|
||||
val confidence: Double
|
||||
)
|
||||
|
||||
@Suppress("ArrayInDataClass")
|
||||
data class ImpressionView(
|
||||
val impression: String,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package work.slhaf.partner.core.cognition.impression;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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.state.State;
|
||||
import work.slhaf.partner.framework.agent.state.StateSerializable;
|
||||
import work.slhaf.partner.framework.agent.state.StateValue;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@CapabilityCore(value = "cognition")
|
||||
public class ImpressionCore implements StateSerializable {
|
||||
|
||||
/**
|
||||
* 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
|
||||
public void updateRelation() {
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void updateImpression() {
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void showImpressions() {
|
||||
}
|
||||
|
||||
@CapabilityMethod
|
||||
public void projectEntity(Set<ActiveEntity> activeEntities) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Path statePath() {
|
||||
return Path.of("core", "impression.json");
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
public @NotNull State convert() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user