BayesNetFragment.addStructureListener
- BayesNetFragment.addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenArcAdded=None, whenArcDeleted=None)
Add the listeners in parameters to the list of existing ones.
- Parameters:
whenNodeAdded (
lambda expression) – a function for when a node is addedwhenNodeDeleted (
lambda expression) – a function for when a node is removedwhenArcAdded (
lambda expression) – a function for when an arc is addedwhenArcDeleted (
lambda expression) – a function for when an arc is removed
- Returns:
the new listener (kept alive by the model), on which other functions can be set; None if no function is given
- Return type:
pyagrum.PythonBNListener | None
Example
import pyagrum as gum
bn = gum.fastBN("A->B->C")
fragment = gum.BayesNetFragment(bn)
events = []
fragment.addStructureListener(
whenNodeAdded=lambda i, name: events.append(f"node {name} installed"),
whenArcAdded=lambda i, j: events.append(f"arc {i}->{j} installed"),
)
fragment.installNode("A")
fragment.installNode("B") # the arc A->B of bn is installed with B
events
# ['node A installed', 'node B installed', 'arc 0->1 installed']
Class