booldog.boolean.modifications

Module containing functions to manipulate Boolean networks. It is not intended to be used directly, but rather as a mixin for other classes.

Attributes

logger

Classes

ModificationTypes

Types of modifications to the Boolean network.

Modification

Represents a single modification made (or to be made) to a Boolean

BooleanNetworkModificationMixin

Mixin class to modify a Boolean network.

Module Contents

booldog.boolean.modifications.logger
class booldog.boolean.modifications.ModificationTypes(*args, **kwds)

Bases: booldog.utils.ExtendedEnum

Types of modifications to the Boolean network.

ADD = 'add_node'
REMOVE = 'remove_node'
UPDATE = 'update'
class booldog.boolean.modifications.Modification(modification_type, node_id, rule=None)

Represents a single modification made (or to be made) to a Boolean network. Used both as the audit-trail record appended to BoolDogModel.modifications by _track_network_modifications, and as the input format accepted by modify_network.

type

the type of modification (add, remove, or update).

Type:

ModificationTypes

node_id

identifier(s) of the node(s) affected.

Type:

str or list of str

rule = None

the rule (bnet form) associated with the modification, if any.

Type:

str or None

__repr__()
class booldog.boolean.modifications.BooleanNetworkModificationMixin

Mixin class to modify a Boolean network. This class is not intended to be used directly, but rather as a mixin.

_track_network_modifications(modification_type, node_id, rule=None)

Append a Modification record to self.modifications, the model’s audit trail of changes made via add_node/remove_node(s)/ update_node.

Parameters:
  • modification_type (ModificationTypes) – Type of modification performed.

  • node_id (str or list of str) – Identifier(s) of the node(s) affected.

  • rule (str, optional) – The rule (bnet form) associated with the modification, if any.

_update_model_object(uncache_primes=True)

Refresh the model’s derived bookkeeping after a node addition, removal, or rule update.

Recomputes self.node_ids/self.index (via BoolDogModel._set_node_ids_and_index) and, unless the caller has already updated self.primes in place (e.g. via pyboolnet.prime_implicants.create_variables/remove_variables), invalidates the cached primes so they are recomputed from the node rules the next time primes is accessed.

Parameters:

uncache_primes (bool, optional) – Whether to invalidate the primes cache (default True). Pass False when the primes have already been updated in place by the caller.

modify_network(modifications)

Modify the network according to the given modifications.

Parameters:

modifications (list of Modification) – List of Modification objects, applied in the order given.

Returns:

The network is modified in place.

Return type:

None

Notes

Each Modification object has the following attributes:

  • type : ModificationTypes Type of modification. One of ModificationTypes.ADD (“add_node”), ModificationTypes.REMOVE (“remove_node”), or ModificationTypes.UPDATE (“update”).

  • node_id : str or list Identifier(s) of node(s) to modify.

  • rule : str or None Rule to define the update function of node_id, in bnet form. All nodes in rule need to be defined in Network.

The modifications are applied in the order they are given, by dispatching each one to add_node, remove_nodes, or (for “update”) the equivalent of update_node. A node cannot be removed if other nodes depend on it (i.e. it occurs in their update logic). To remove such a node, either also remove all of its dependants, or first update the logic rule of its dependants to remove dependency.

This is a wrapper for pyboolnet.prime_implicants.create_variables and pyboolnet.prime_implicants.remove_variables.

remove_node(node_id)

Removes node_id from the network. Convenience wrapper around remove_nodes for a single node.

Parameters:

node_id (str) – Identifier of the node to remove.

Return type:

None

Notes

A node cannot be removed if other nodes depend on it (i.e. it occurs in their update logic). To remove such a node, either also remove all of its dependants, or first update the logic rule of its dependants to remove dependency.

This is a wrapper for pyboolnet.prime_implicants.remove_variables.

_test_node_removabilty(node_ids)

Check that removing all of node_ids from the network at once is valid, i.e. that no node outside node_ids depends on (is a successor of) any node in node_ids.

Parameters:

node_ids (list of str) – Identifiers of the nodes to be removed together.

Return type:

None

Raises:

ValueError – If any node in node_ids has a dependant (successor, via pyboolnet.prime_implicants.find_successors) that is not itself also being removed. The message lists, for each such node, which of its dependants block the removal.

remove_nodes(node_ids)

Removes all nodes in node_ids from the network.

Parameters:

node_ids (str or list of str) – Identifier, or list of identifiers, of nodes to remove. A single string is treated as one node identifier.

Return type:

None

Raises:

ValueError – If any of node_ids is not present in the network, or if removing all of node_ids together would leave a dependant node whose rule still refers to a removed node (see _test_node_removabilty).

Notes

A node cannot be removed if other nodes depend on it (i.e. it occurs in their update logic). To remove such a node, either also remove all of its dependants, or first update the logic rule of its dependants to remove dependency.

This is a wrapper for pyboolnet.prime_implicants.remove_variables.

add_node(node_id, rule, name=None)

Add a new node to Network.

Parameters:
  • node_id (str) – Identifier of the new node. Must not already be present in the network.

  • rule (str) – Rule to define update of node_id, in bnet form. All nodes in rule need to be defined in Network.

  • name (str, optional) – Not currently used by this method (the created BoolDogNode is constructed without a name, so it falls back to node_id; see BoolDogNode.__post_init__).

Return type:

None

Raises:

ValueError – If node_id is already present in the network.

Notes

This is a wrapper for pyboolnet.prime_implicants.create_variables.

update_node(node_id, rule, modification_type=ModificationTypes.UPDATE)

Update (overwrite) the logic rule defining the update of node_id. node_id must already exist in the network; use add_node to add a new node instead.

Parameters:
  • node_id (str) – Identifier of the (existing) node whose rule is to be updated.

  • rule (str) – New rule to define update of node_id, in bnet form. All nodes in rule need to be defined in Network.

  • modification_type (ModificationTypes, optional) – The modification type recorded in the audit trail (self.modifications) for this change. Defaults to ModificationTypes.UPDATE.

Return type:

None

Raises:

ValueError – If node_id is not already present in the network.

Notes

If rule is identical to the node’s current rule, a warning is logged and no update (and no audit-trail entry) is made.

This is a wrapper for pyboolnet.prime_implicants.create_variables.