Package com.tnsai.agents.cognitive
Class CognitiveEngine
java.lang.Object
com.tnsai.agents.cognitive.CognitiveEngine
Manages the BDI (Belief-Desire-Intention) cognitive state of an agent.
The CognitiveEngine encapsulates all BDI-related state and operations, providing a clean separation from the main Agent class. It handles:
- Static beliefs, desires, and intentions (immutable, from configuration)
- Dynamic beliefs, desires, and intentions (mutable, runtime modifications)
- Belief revision strategies for handling conflicting beliefs
- Desire conflict resolution
Thread Safety
This class uses CopyOnWriteArrayList for dynamic collections, making it thread-safe for concurrent read/write operations.
Usage
CognitiveEngine engine = CognitiveEngine.builder()
.beliefs(List.of(new Belief("The sky is blue")))
.desires(List.of(Desire.goal("Help the user")))
.beliefRevisionStrategy(BeliefRevisionStrategy.RECENCY_BASED)
.build();
// Add dynamic belief
engine.addBelief(new Belief("User prefers concise answers"));
// Query all beliefs
List<Belief> all = engine.allBeliefs();
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordSnapshot of BDI state for serialization.static classBuilder for CognitiveEngine. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a belief dynamically at runtime.voidAdds a desire dynamically at runtime.voidaddIntention(Intention intention) Adds an intention dynamically at runtime.Returns all beliefs (static + dynamic).Returns all desires (static + dynamic).Returns all intentions (static + dynamic).static CognitiveEngine.Builderbuilder()Creates a new builder for CognitiveEngine.voidClears all dynamic BDI components (beliefs, desires, intentions).voidClears all dynamic BDI components (beliefs, desires, intentions).voidClears all dynamic beliefs.voidClears all dynamic desires.voidClears all dynamic intentions.intRemoves all expired beliefs from the dynamic belief list.booleancompleteIntention(Intention intention) Marks an intention as completed by removing it.Returns the dynamic beliefs (added at runtime).Returns the dynamic desires (added at runtime).Returns the dynamic intentions (added at runtime).Finds desires that might conflict with each other.Returns the current belief revision strategy.Returns the current desire conflict resolver.booleanremoveBelief(Belief belief) Removes a dynamically added belief.booleanremoveBeliefByContent(String content) Removes a belief by content.booleanremoveDesire(Desire desire) Removes a dynamically added desire.booleanremoveDesireByObjective(String objective) Removes a desire by objective.booleanremoveIntention(Intention intention) Removes a dynamically added intention.booleanremoveIntentionByTask(String taskDescription) Removes an intention by task description.voidRestores dynamic BDI state from a snapshot.voidSets the belief revision strategy.voidSets the desire conflict resolver.snapshot()Returns a snapshot of the current BDI state for serialization/debugging.Returns the static beliefs (from configuration).Returns the static desires (from configuration).Returns the static intentions (from configuration).booleanupdateBeliefConfidence(String content, double newConfidence) Updates the confidence of a belief with matching content.
-
Method Details
-
builder
Creates a new builder for CognitiveEngine.- Returns:
- a new builder instance
-
staticBeliefs
Returns the static beliefs (from configuration).- Returns:
- unmodifiable list of static beliefs
-
dynamicBeliefs
Returns the dynamic beliefs (added at runtime).- Returns:
- unmodifiable snapshot of dynamic beliefs
-
allBeliefs
Returns all beliefs (static + dynamic).- Returns:
- combined unmodifiable list
-
addBelief
Adds a belief dynamically at runtime.If a belief revision strategy is configured, it will be used to handle conflicts with existing beliefs.
- Parameters:
belief- the belief to add- Throws:
IllegalArgumentException- if belief is null
-
removeBelief
Removes a dynamically added belief.- Parameters:
belief- the belief to remove- Returns:
- true if the belief was removed
-
removeBeliefByContent
Removes a belief by content.- Parameters:
content- the content of the belief to remove- Returns:
- true if a belief was removed
-
updateBeliefConfidence
Updates the confidence of a belief with matching content.- Parameters:
content- the content to matchnewConfidence- the new confidence level (0.0 to 1.0)- Returns:
- true if a belief was updated
-
clearExpiredBeliefs
public int clearExpiredBeliefs()Removes all expired beliefs from the dynamic belief list.- Returns:
- number of beliefs removed
-
clearDynamicBeliefs
public void clearDynamicBeliefs()Clears all dynamic beliefs. -
setBeliefRevisionStrategy
Sets the belief revision strategy.- Parameters:
strategy- the strategy to use (null to disable)
-
getBeliefRevisionStrategy
Returns the current belief revision strategy.- Returns:
- the current strategy, or null if none set
-
staticDesires
Returns the static desires (from configuration).- Returns:
- unmodifiable list of static desires
-
dynamicDesires
Returns the dynamic desires (added at runtime).- Returns:
- unmodifiable snapshot of dynamic desires
-
allDesires
Returns all desires (static + dynamic).- Returns:
- combined unmodifiable list
-
addDesire
Adds a desire dynamically at runtime.If a conflict resolver is configured and conflicts are detected, the resolver is used to determine which desire(s) to keep.
- Parameters:
desire- the desire to add- Throws:
IllegalArgumentException- if desire is null
-
removeDesire
Removes a dynamically added desire.- Parameters:
desire- the desire to remove- Returns:
- true if the desire was removed
-
removeDesireByObjective
Removes a desire by objective.- Parameters:
objective- the objective to match- Returns:
- true if a desire was removed
-
findConflictingDesires
Finds desires that might conflict with each other.- Returns:
- list of potentially conflicting desire pairs
-
clearDynamicDesires
public void clearDynamicDesires()Clears all dynamic desires. -
setDesireConflictResolver
Sets the desire conflict resolver.- Parameters:
resolver- the resolver to use (null to disable)
-
getDesireConflictResolver
Returns the current desire conflict resolver.- Returns:
- the current resolver, or null if none set
-
staticIntentions
Returns the static intentions (from configuration).- Returns:
- unmodifiable list of static intentions
-
dynamicIntentions
Returns the dynamic intentions (added at runtime).- Returns:
- unmodifiable snapshot of dynamic intentions
-
allIntentions
Returns all intentions (static + dynamic).- Returns:
- combined unmodifiable list
-
addIntention
Adds an intention dynamically at runtime.- Parameters:
intention- the intention to add- Throws:
IllegalArgumentException- if intention is null
-
removeIntention
Removes a dynamically added intention.- Parameters:
intention- the intention to remove- Returns:
- true if the intention was removed
-
removeIntentionByTask
Removes an intention by task description.- Parameters:
taskDescription- the task description to match- Returns:
- true if an intention was removed
-
completeIntention
Marks an intention as completed by removing it.- Parameters:
intention- the intention that was completed- Returns:
- true if the intention was removed
-
clearDynamicIntentions
public void clearDynamicIntentions()Clears all dynamic intentions. -
clearAllDynamic
public void clearAllDynamic()Clears all dynamic BDI components (beliefs, desires, intentions). -
clearAllDynamicBDI
public void clearAllDynamicBDI()Clears all dynamic BDI components (beliefs, desires, intentions).Alias for
clearAllDynamic()for API compatibility. -
snapshot
Returns a snapshot of the current BDI state for serialization/debugging.- Returns:
- BDI state snapshot
-
restoreFromSnapshot
Restores dynamic BDI state from a snapshot.This clears current dynamic state and adds all items from the snapshot that are not in the static lists.
- Parameters:
snapshot- the snapshot to restore from
-