Ejemplo n.º 1
0
    def test_cant_delete_subgraph( self, ug ):
        (edges, moreEdges) = ug
        assume( len( edges ) < 6 )
        assume( len( moreEdges ) < 4 )

        # OK, build the graph and label the edges so the deletion can only happen
        # in one way (which is disallowed)
        l = self.undirectedGraphFromEdgeList( edges )
        for (s,t) in edges:
            l.nodes[s]['tag'] = 'kill'
            l.nodes[t]['tag'] = 'kill'
            
        r = nx.Graph()
        self._buildRename( r )
        g_orig = self.undirectedGraphFromEdgeList( edges + moreEdges )
        for (s,t) in edges:
            g_orig.nodes[s]['tag'] = 'kill'
            g_orig.nodes[t]['tag'] = 'kill'                   
        g = sg.graphIdentifiersToNumbers( g_orig )

        finder = sg.MatchFinder( g, verbose=False )
        finder.maxMatches = 2
        finder.maxMatchTime = 0.2
        finder.leftSide( l )
        finder.rightSide( r )
        
        m = finder.matches()
        self.assertEqual( len( m ), 0 )
Ejemplo n.º 2
0
def showMatches(l, r, g, outputFile):
    try:
        l = parseGraphString(l)
        r = parseGraphString(r, joinAllowed=True)
        g = graphIdentifiersToNumbers(parseGraphString(g))
    except ParseError as pe:
        pe.prettyPrint()
        exit(1)

    finder = MatchFinder(g)
    finder.leftSide(l)
    finder.rightSide(r)
    allMatches = finder.matches()

    if len(allMatches) == 0:
        h = parseGraphString("")
        drawMatch(l, r, g, h, m, outputFile)

    for (i, m) in enumerate(allMatches[:20]):
        if len(allMatches) > 0:
            fn = outputFile.split(".")
            myFile = ".".join(fn[0:-1] + [str(i)] + fn[-1:])
            print(myFile, m)
        else:
            myFile = outputFile

        h = RuleApplication(finder, m).result()
        drawMatch(l, r, g, h, m, myFile)
Ejemplo n.º 3
0
 def __init__( self, initialGraph, grammar = None, callback = None ):
     """Specify the initial graph and (optionally) a starting grammar.
     The "callback" function will be called once per iteration with the
     iteration number and the current graph.
     """
     self.grammar = grammar
     self.graph = graphIdentifiersToNumbers( initialGraph )
     self.iteration = 0
     self.callback = callback
     self.verbose = True
     self.timing = None
     self.fast_mode = False
Ejemplo n.º 4
0
    def test_graph_renumber(self):
        g = nx.Graph()
        g.add_edge( 'A', 'B', tag='x' )
        g.add_edge( 'B', 'C', tag='y' )
        g2 = sg.graphIdentifiersToNumbers( g )
        self.assertEqual( len( g2.nodes ), 3 )
        self.assertIn( 0, g2.nodes )
        self.assertIn( 1, g2.nodes )
        self.assertIn( 2, g2.nodes )
        self.assertEqual( g2.graph['nextId'], 3 )

        edgeTags = [ g2.edges[e]['tag'] for e in g2.edges ]
        self.assertIn( 'x', edgeTags )
        self.assertIn( 'y', edgeTags )
Ejemplo n.º 5
0
    def test_match_self( self, edges ):
        # FIXME: empty graphs do cause problems, they should be fixed
        # or else error out more explicitly.
        assume( len( edges ) > 0 )
        assume( len( edges ) < 6 )
                
        l = self.undirectedGraphFromEdgeList( edges )
        g = sg.graphIdentifiersToNumbers( l )

        finder = sg.MatchFinder( g )
        finder.maxMatches = 2
        finder.maxMatchTime = 0.2
        finder.leftSide( l )
        m = finder.matches()

        self.assertGreaterEqual( len( m ), 1 )
Ejemplo n.º 6
0
    def test_match_subgraph( self, edges, moreEdges ):
        assume( len( edges ) > 0 )
        assume( len( moreEdges ) > 0 )
        # FIXME: temporarily reduced maximum number of edges.
        assume( len( edges ) < 6 )
        assume( len( moreEdges ) < 4 )
        
        l = self.undirectedGraphFromEdgeList( edges )
        g = self.undirectedGraphFromEdgeList( edges + moreEdges )
        g = sg.graphIdentifiersToNumbers( g )

        finder = sg.MatchFinder( g )
        finder.maxMatches = 2
        finder.maxMatchTime = 0.2
        finder.leftSide( l )
        m = finder.matches()

        self.assertGreaterEqual( len( m ), 1 )
Ejemplo n.º 7
0
    def setup_rewrite( self, l, r, g, realMatches = True ):
        l = parseGraphString( l )
        r = parseGraphString( r, joinAllowed=True )
        g = sg.graphIdentifiersToNumbers( parseGraphString( g ) )
        (l, r, g) = self.make_all_directed( l, r, g )
        
        self.before = g

        if verbose:
            print()
            print( "*** BEFORE ***" )
            self.dump_text( g )
        
        finder = sg.MatchFinder( g, verbose=False)
        #finder = sg.MatchFinder( g, verbose=True)
        finder.leftSide( l )
        finder.rightSide( r )
        self.finder = finder

        if realMatches:
            return finder.matches()
Ejemplo n.º 8
0
    def test_cant_delete_directed_subgraph( self, dg ):
        (edges, moreEdges) = dg

        l = self.directedGraphFromEdgeList( edges )
        for (s,t) in edges:
            l.nodes[s]['tag'] = 'kill'
            l.nodes[t]['tag'] = 'kill'
            
        r = nx.DiGraph()
        self._buildRename( r )
        g_orig = self.directedGraphFromEdgeList( edges + moreEdges )
        for (s,t) in edges:
            g_orig.nodes[s]['tag'] = 'kill'
            g_orig.nodes[t]['tag'] = 'kill'                   
        g = sg.graphIdentifiersToNumbers( g_orig )

        finder = sg.MatchFinder( g, verbose=False )
        finder.maxMatches = 2
        finder.maxMatchTime = 0.2
        finder.leftSide( l )
        finder.rightSide( r )
        
        m = finder.matches()
        self.assertEqual( len( m ), 0 )
Ejemplo n.º 9
0
    def test_delete_directed_subgraph( self, sgs ):
        (edges, moreEdges ) = sgs

        nodesG = set( [ s for (s,t) in moreEdges ] + [ t for (s,t) in moreEdges ] )
        
        l = self.directedGraphFromEdgeList( edges )
        r = nx.DiGraph()
        self._buildRename( r )
        g_orig = self.directedGraphFromEdgeList( edges + moreEdges )        
        g = sg.graphIdentifiersToNumbers( g_orig )

        finder = sg.MatchFinder( g, verbose=False )
        finder.maxMatches = 2
        finder.maxMatchTime = 0.2
        finder.leftSide( l )
        finder.rightSide( r )
        
        m = finder.matches()
        self.assertGreater( len( m ), 0 )
        
        app = sg.RuleApplication( finder, m[0] )
        g2 = app.result()

        self.assertEqual( len( g2.nodes ), len( nodesG ) )