Class InteractionGraph
java.lang.Object
com.tnsai.coordination.observability.InteractionGraph
Directed weighted graph representing inter-agent interactions.
Nodes represent agents and edges represent interactions such as messages, task assignments, or other communication events between agents. Edge weights indicate the frequency or intensity of interactions.
This class is thread-safe and uses ConcurrentHashMap for internal storage.
Usage Example
InteractionGraph graph = new InteractionGraph();
graph.addNode("agent-1");
graph.addNode("agent-2");
graph.addEdge("agent-1", "agent-2", "message");
graph.incrementEdge("agent-1", "agent-2", "message");
double outDegree = graph.getOutDegree("agent-1"); // 2.0
String dot = graph.toDotFormat();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA directed, weighted edge in the interaction graph.static final recordA node in the interaction graph representing an agent. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an edge with default weight of 1.0.voidAdds or replaces an edge with specified weight.voidAdds a node with no attributes.voidAdds a node with attributes.voidclear()Removes all nodes and edges from the graph.intReturns the total number of edges in the graph.Gets a specific edge between two agents of a given type.getEdges()Gets all edges in the graph.getEdgesFrom(String agentId) Gets all edges originating from a specific agent.getEdgesTo(String agentId) Gets all edges targeting a specific agent.doublegetInDegree(String agentId) Gets the in-degree of an agent (sum of incoming edge weights).getNeighbors(String agentId) Gets all agents connected to a given agent (both incoming and outgoing).Gets a node by agent ID.Gets all node IDs in the graph.doublegetOutDegree(String agentId) Gets the out-degree of an agent (sum of outgoing edge weights).booleanChecks if a node exists in the graph.voidincrementEdge(String from, String to, String type) Increments the weight of an existing edge by 1.0, or creates it with weight 1.0.intReturns the number of nodes in the graph.Exports the graph in Graphviz DOT format.toMap()Exports the graph as a JSON-friendly map representation.
-
Constructor Details
-
InteractionGraph
public InteractionGraph()
-
-
Method Details
-
addNode
Adds a node with no attributes.- Parameters:
agentId- unique agent identifier
-
addNode
Adds a node with attributes. If the node already exists, attributes are merged.- Parameters:
agentId- unique agent identifierattributes- node attributes
-
hasNode
Checks if a node exists in the graph.- Parameters:
agentId- agent identifier to check- Returns:
- true if the node exists
-
getNode
Gets a node by agent ID.- Parameters:
agentId- agent identifier- Returns:
- the node if found
-
getNodeIds
Gets all node IDs in the graph.- Returns:
- unmodifiable set of agent IDs
-
nodeCount
public int nodeCount()Returns the number of nodes in the graph.- Returns:
- node count
-
addEdge
Adds an edge with default weight of 1.0.- Parameters:
from- source agent IDto- target agent IDtype- interaction type
-
addEdge
Adds or replaces an edge with specified weight. Automatically adds nodes if they don't exist.- Parameters:
from- source agent IDto- target agent IDtype- interaction typeweight- edge weight
-
incrementEdge
Increments the weight of an existing edge by 1.0, or creates it with weight 1.0.- Parameters:
from- source agent IDto- target agent IDtype- interaction type
-
getEdges
Gets all edges in the graph.- Returns:
- list of all edges
-
getEdgesFrom
Gets all edges originating from a specific agent.- Parameters:
agentId- source agent ID- Returns:
- list of outgoing edges
-
getEdgesTo
Gets all edges targeting a specific agent.- Parameters:
agentId- target agent ID- Returns:
- list of incoming edges
-
getEdge
Gets a specific edge between two agents of a given type.- Parameters:
from- source agent IDto- target agent IDtype- interaction type- Returns:
- the edge if found
-
edgeCount
public int edgeCount()Returns the total number of edges in the graph.- Returns:
- edge count
-
getInDegree
Gets the in-degree of an agent (sum of incoming edge weights).- Parameters:
agentId- agent identifier- Returns:
- sum of incoming edge weights, 0.0 if no incoming edges
-
getOutDegree
Gets the out-degree of an agent (sum of outgoing edge weights).- Parameters:
agentId- agent identifier- Returns:
- sum of outgoing edge weights, 0.0 if no outgoing edges
-
getNeighbors
Gets all agents connected to a given agent (both incoming and outgoing).- Parameters:
agentId- agent identifier- Returns:
- set of connected agent IDs
-
toDotFormat
Exports the graph in Graphviz DOT format.Edge pen widths are scaled relative to the maximum edge weight.
- Returns:
- DOT format string
-
toMap
Exports the graph as a JSON-friendly map representation.- Returns:
- map with "nodes" and "edges" keys
-
clear
public void clear()Removes all nodes and edges from the graph.
-