diprism / fggs

Factor Graph Grammars in Python
MIT License
13 stars 3 forks source link

Convenience functions for node/edge creation; lighten stringification #126

Closed davidweichiang closed 2 years ago

davidweichiang commented 2 years ago

Adds the convenience functions suggested in #108. This is just a thin layer on top of existing functions; no changes to the data structures were made (although I think that could be done in the future).

Original interface:

        self.nl1   = NodeLabel("nl1")
        self.nl2   = NodeLabel("nl2")
        self.node1 = Node(self.nl1, id='node1')
        self.node2 = Node(self.nl2)

        self.el1   = EdgeLabel("el1", (self.nl1, self.nl2), is_terminal=True)
        self.el2   = EdgeLabel("el2", (self.nl2,), is_nonterminal=True)
        self.edge1 = Edge(self.el1, (self.node1, self.node2), id='edge1')
        self.edge2 = Edge(self.el2, (self.node2,))

        self.graph = Graph()
        self.graph.add_node(self.node1)
        self.graph.add_node(self.node2)
        self.graph.add_edge(self.edge1)
        self.graph.add_edge(self.edge2)
        self.graph.ext = [self.node2]

Convenience functions:

    g = Graph()
        node1 = g.new_node('nl1', id='node1')
        node2 = g.new_node('nl2')
        edge1 = g.new_edge('el1', [node1, node2], is_terminal=True, id='edge1')
        edge2 = g.new_edge('el2', [node2], is_nonterminal=True)
        g.ext = [node2]

Also, I slipped in some changes to make printing of FGGs a little easier to read. Now it looks like this:

Graph containing:
  Node node1 with NodeLabel nl1
  Node 4470135968 with NodeLabel nl2
  Edge 4470141904 with Nonterminal EdgeLabel el2 connecting to:
    Node 4470135968 with NodeLabel nl2
  Edge edge1 with Terminal EdgeLabel el1 connecting to:
    Node node1 with NodeLabel nl1
    Node 4470135968 with NodeLabel nl2

Closes #108.