Search code examples
pythonnetworkxhierarchy

Is there a way to have edge attributes as placeholders in a regraph hierarchy?


I am working on a python script that is using the regraph library based on networkx. I created a hierarchy that contains different graphs that are typed to eachother. In one of the graphs, I want edges to have attributes, for example a name. When I do this, I get a message saying that the homomorphism is broken, because the graph it is typed to does not have the exact same edge attributes.

I know this might not be what regraph is designed for, but is there a way to specify an edge attribute in a graph in the hierarchy OR a rule that does not only take patterns but also node names into account?

I have already tried adding empty attributes to the other graphs in the hierarchy, this does not work.

hierarchy = rg.NetworkXHierarchy()

g_type = nx.DiGraph()
g_type.add_nodes_from(['list', 'number'])
g_type.add_edges_from([('list', 'number')])

g_obj = nx.DiGraph()
g_obj.add_nodes_from(['A', 'a'])
rg.add_edge(g_obj, 'A', 'a', attrs={'name': 'input'})

hierarchy.add_graph('type', g_type)
hierarchy.add_graph('obj', g_obj)

hierarchy.add_typing('obj', 'type', {'A': 'list', 'a': 'number'})

I want the edge of the graph to have an attribute name, but it causes a homomorphism error.


Solution

  • For people who are interested, here is how I managed to solve the problem:

    hierarchy = rg.NetworkXHierarchy()
    
    g_type = nx.DiGraph()
    g_type.add_nodes_from(['list', 'number'])
    g_type.add_edges_from([('list', 'number')])
    rg.add_edge_attr(g_type, 'list', 'number', attrs={'name': rg.UniversalSet()})
    
    g_obj = nx.DiGraph()
    g_obj.add_nodes_from(['A', 'a'])
    rg.add_edge(g_obj, 'A', 'a', attrs={'name': 'input'})
    
    hierarchy.add_graph('type', g_type)
    hierarchy.add_graph('obj', g_obj)
    
    hierarchy.add_typing('obj', 'type', {'A': 'list', 'a': 'number'})
    

    You simply have to add the same attribute with an rg.UniversalSet as value.