storeFact method
Stores or reinforces a subject–predicate–object fact.
If an identical triple already exists it is reinforced. Returns the SemanticFact (new or updated).
Implementation
SemanticFact storeFact({
required String subject,
required String predicate,
required String object,
double confidence = 0.5,
String? episodeId,
}) {
final subjectKey = subject.toLowerCase().trim();
final facts = _bySubject[subjectKey] ??= [];
// Check for duplicate
final existing = facts.cast<SemanticFact?>().firstWhere(
(f) =>
f!.predicate.toLowerCase() == predicate.toLowerCase() &&
f.object.toLowerCase() == object.toLowerCase(),
orElse: () => null,
);
if (existing != null) {
existing.reinforce(amount: confidence * 0.1);
if (episodeId != null &&
!existing.supportingEpisodeIds.contains(episodeId)) {
existing.supportingEpisodeIds.add(episodeId);
}
_logger.debug('Reinforced fact: "${existing.statement}"');
return existing;
}
final fact = SemanticFact(
id: _uuid.v4(),
subject: subjectKey,
predicate: predicate.toLowerCase().trim(),
object: object.toLowerCase().trim(),
confidence: confidence,
supportingEpisodeIds: episodeId != null ? [episodeId] : [],
);
facts.add(fact);
_byId[fact.id] = fact;
_logger.debug(
'Stored fact: "${fact.statement}" '
'(conf: ${confidence.toStringAsFixed(2)})');
return fact;
}