Node and rule vocabulary

class NodeVocabulary

class rostok.graph_grammar.node_vocabulary.NodeVocabulary

The class contains dictionary of nodes and methods to manipulate with it. This is a class to manage a dictionary of nodes. The keys are labels of the nodes and the values are Node objects. User can create or add nodes to vocabulary, get individual nodes or list of nodes.

node_dict

dictionary of all nodes.

Type:

List[Node]

terminal_node_dict

dictionary of only terminal nodes.

Type:

List[Node]

non-terminal_node_dict

dictionary of only non-terminal nodes.

Type:

List[Node]

add_node(node: Node)

Add an already created node to the vocabulary.

Parameters:

node (Node) – node to be added to vocabulary.

Raises:

Exception – Attempt to add a Node with a label that is already in dictionary!

check_node(label: str) bool

Check if the label is in the vocabulary.

Parameters:

label (str) – the label of the node that should be checked.

Returns:

True is the label is in dictionary, False otherwise.

Return type:

bool

create_node(label: str, is_terminal: bool = False, block_wrapper: BlockWrapper | None = None)

Create a node and add it to the vocabulary.

Parameters:
  • label (str) – the label of the new node.

  • is_terminal (bool, optional) – defines if the new node is a terminal node. Default is False.

  • block_wrapper (BlockWrapper, optional) – the object that contains physical properties of the node. Default is None.

Raises:

Exception – Attempt to add a Node with a label that is already in dictionary!

get_list_of_nodes(nodes: list[str]) list[rostok.graph_grammar.node.Node]

Returns list of Node objects corresponding to list of labels.

Parameters:

nodes (list[str]) – list of labels to construct a list of Node objects.

Returns:

list of Node objects corresponding to the list of passed labels.

get_node(label: str) Node

Return a node corresponding to the label.

Parameters:

label (str) – the label of the node that should be returned.

Returns:

A requested node as a Node class object.

Raises

Exception: Node with given label not found!

class RuleVocabulary

class rostok.graph_grammar.rule_vocabulary.RuleVocabulary(node_vocab: ~rostok.graph_grammar.node_vocabulary.NodeVocabulary = <rostok.graph_grammar.node_vocabulary.NodeVocabulary object>)

The class that contains the rules for building the GraphGrammar object. All rules for mechanism generation should be created with an instance of RuleVocabulary. This class provides utility methods for the rules.

node_vocab

the node vocabulary that should contain all the nodes used in the rules.

Type:

NodeVocabulary

rule_dict

the dictionary of all rules with the rule names as keys.

Type:

dict[str, Rule]

nonterminal_rule_dict

the dictionary of only non-terminal rules.

Type:

dict[str, Rule]

terminal_rule_dict

the dictionary of only terminal rules.

Type:

dict[str, Rule]

rules_nonterminal_node_set

the set of node labels that are used as the new nodes in the non-terminal rules. These are nodes that can appear in the final graph.

Type:

set(str)

rules_terminal_node_set

the set of node labels that are used as the new nodes in the terminal rules. These are nodes that can appear in the final graph.

Type:

set(str)

terminal_dict

the dictionary that contains the list of terminal states for all non-terminal nodes.

Type:

dict[str, list[str]]

check_rules()

Check set of rules itself, without any graph. Check the rules for having at least one terminal rule for every node that appears in the end graph of a nonterminal rule.

create_rule(name: str, current_nodes: list[str], new_nodes: list[str], current_in_edges: int, current_out_edges: int, new_edges: list[tuple[int, int]] = [], current_links: list[tuple[int, int]] = [])

Create a rule and add it to the dictionary. The method checks the created rule. There is no method to add already created rule to the vocabulary.

Parameters:
  • name (str) – name of the new rule.

  • current_nodes (list[str]) – list of the nodes to be replaced.

  • new_nodes (list[str]) – list of the new nodes that to be inserted in the graph.

  • current_in_edges (int) – the node to link the in edges of the replaced node.

  • current_out_edges (int) – the node to link the out edges of the replaced node.

  • new_edges (list[(int, int)]) – the edges of the inserting subgraph.

Raises:
  • Exception – this name is already in the rule vocabulary!

  • Exception – prohibited length of the current_nodes list, should be 1.

  • Exception – the node vocabulary dose not include the replacing node or the new nodes.

  • Exception – the edge contains node idx out of graph length or the edge is a loop.

  • Exception – attempt to link in or out edges of the replaced node to the node idx out of new subgraph length.

get_list_of_applicable_nonterminal_rules(grammar: GraphGrammar)

Return the list of non-terminal applicable rules for the current graph.

Parameters:

grammar (GraphGrammar) – a GraphGrammar object analyze.

Returns:

list of rule names for non-terminal rules that can be applied for the graph.

get_list_of_applicable_rules(grammar: GraphGrammar)

Return the total list of applicable rules for the current graph. :param grammar: a GraphGrammar object analyze. :type grammar: GraphGrammar

Returns:

list of rule names for rules that can be applied for the graph.

get_list_of_applicable_terminal_rules(grammar: GraphGrammar)

Return the list of terminal applicable rules for the current graph.

Parameters:

grammar (GraphGrammar) – a GraphGrammar object analyze.

Returns:

list of rule names for terminal rules that can be applied for the graph.

get_rule(name: str) Rule

Return a rule with the corresponding name.

Parameters:

name (str) – the name of the rule to be returned.

make_graph_terminal(grammar: GraphGrammar)

Converts a graph into a graph with only terminal nodes.

For each non-terminal node the function apply a random rule that make it terminal.

Parameters:

grammar (GraphGrammar) – GraphGrammar object that should become terminal.

terminal_rules_for_node(node_name: str)

Return a list of the terminal rules for the node

Parameters:

node_name (str) – a node label for which function returns the list of the terminal rules.

Returns:

The list of rule names for rules that can be applied to make a node terminal.