Ejemplo n.º 1
0
    def __clean__(self):
        """
        for each item in the graph try to remove its data
        """
        #        table = self.env['table']
        ##        for node in self.graph:
        ##            node = table[node]
        ##            # if the node is a data container try to remove it
        ##            if isinstance( node , DataContainer ):
        ##                # note: that the remove call only removes
        ##                # data that are there and not temporary
        ##                node.remove()
        ##
        #        #empty the hash of all objects
        #        table.clear()
        #
        #        from slimpy_base.utils.DotTest import DotTester
        #        dt = DotTester()
        #        dt.clear()

        #        self.log = Log()

        self.graph = DiGraph()

        self.__breakpoints = set()
        self.__sources = set()
        self.__targets = set()
Ejemplo n.º 2
0
    def makeAns(self):

        g = DiGraph()

        g.appendEdge(('zero', 'put', 'data', 'fdct'), 'sizes')

        return g
Ejemplo n.º 3
0
    def makeGraph(self):

        g = DiGraph()
        g.appendEdge('d1', 'mig', False, 'red')
        g.appendEdge('mig', 'd2', False, 'green')
        g.appendEdge('d2', 'plus1', True, 'red')
        g.appendEdge('plus1', 'd3', True, 'green')
        #        g.addSource( 'd1' )
        #        g.setBuildTargets( 'd3' )

        targets = set(['d3'])
        sources = set(['d1'])
        breakponts = set()

        return g, targets, sources, breakponts
Ejemplo n.º 4
0
    def makeGraph(self):

        g = DiGraph()
        g.appendEdge('data', 'fdct', True, 'red')
        g.appendEdge('fdct', 'sizes', False, 'green')
        g.appendEdge('sizes', 'fdctInv', False, 'red')
        g.appendEdge('fdct', 'newdata', True, 'green')
        g.appendEdge('newdata', 'fdctInv', True, 'red')
        g.appendEdge('fdctInv', 'final', True, 'green')

        #        g.addSource( 'data' )
        #        g.setBuildTargets( 'final' )

        targets = set(['final'])
        sources = set(['data'])
        breakponts = set()

        return g, targets, sources, breakponts
Ejemplo n.º 5
0
    def testGetSourceNodes(self):
        self.assertEqual(list(self.g.getSourceNodes()), self.srcnodes)

        g = DiGraph()
        g.appendEdge('d1', 'c1', True, 'red')
        g.appendEdge('d2', 'c2', True, 'green')

        self.assertEqual(set(g.getSourceNodes()), set(['d2', 'd1']))
Ejemplo n.º 6
0
    def __new_instance__(self, name):
        """
        simalar to init but for singleton
        initializes the class when  a new instance is created
        """

        Singleton.__new_instance__(self, name)

        #        self.log = Log()
        self.env = InstanceManager()

        self.graph = DiGraph()

        self.__breakpoints = set()
        self.__sources = set()
        self.__targets = set()

        self.runner = defaultRunner()
        self.builder = PipeBuilder()
Ejemplo n.º 7
0
    def makeAns(self):

        g = DiGraph()
        g.appendEdge(('d1', 'c1', 'd2', 'c3', 'd4'), 'd4', False, 'black')

        g.appendEdge('d3', ('d1', 'c1', 'd2', 'c3', 'd4'), False, 'black')
        g.appendEdge('d1', ('d1', 'c1', 'd2', 'c3', 'd4'), False, 'black')

        g.appendEdge(('d1', 'c2', 'd3'), 'd3', False, 'black')
        g.appendEdge('d1', ('d1', 'c2', 'd3'), False, 'black')

        return g
Ejemplo n.º 8
0
    def makeAns(self):

        g = DiGraph()

        g.appendEdge(('c1', 'd1'), 'd1')
        g.appendEdge(('c2', 'd2'), 'd2')

        g.appendEdge('d1', ('c3', 'd3'))
        g.appendEdge('d2', ('c3', 'd3'))

        g.appendEdge(('c3', 'd3'), 'd3')
        return g
Ejemplo n.º 9
0
    def toGraph(self):
        g = DiGraph()

        for l in self.lst:

            for source in l['Source']:
                g.appendEdge(source, tuple(l['Command']))
            for target in l['Target']:
                g.appendEdge(tuple(l['Command']), target)

        g.setBuildTargets(*self.G.buildTargets)

        return g
Ejemplo n.º 10
0
    def makeGraph(self):

        g = DiGraph()
        g.appendEdge('zero', 'put', True, 'black')
        g.appendEdge('put', 'data', True, 'green')
        g.appendEdge('data', 'fdct', True, 'red')
        g.appendEdge('fdct', 'sizes', False, 'green')
        #        g.setBuildTargets( 'sizes' )
        targets = set(['sizes'])
        sources = set()
        breakponts = set()

        return g, targets, sources, breakponts
Ejemplo n.º 11
0
    def testAppendEdge(self):
        g = DiGraph()
        g.appendEdge('d1', 'c1', True, 'red')

        self.assert_('d1' in g)
        self.assert_('c1' in g)

        self.assertEqual(g.adj('d1'), ['c1'], "test that d1 -> c1")
        self.assertRaises(KeyError, g.adj, ('c1', ))  # "test that c1 -/> d1"

        self.assertEqual(g.invAdj('c1'), ['d1'], "test that c1 <- d1 in inv")
        self.assertRaises(KeyError, g.invAdj,
                          ('d1', ))  # "test that c1 -/> d1 in inv"

        self.assert_(g.getEdge('d1', 'c1'))
        self.assert_(g.getEdge('c1', 'd1'))

        self.assertEqual(g.getEdgeColour('d1', 'c1'), 'red')
        self.assertEqual(g.getEdgeType('d1', 'c1'), True)
Ejemplo n.º 12
0
    def toGraph(self):
        """
        return graph representation of built object
        """
        g = DiGraph()

        for l in self.lst:
            if l['Source'] == l['Command'] == l['Target']:
                continue
            s_diff = list(set(l['Source']).intersection(set(l['Target'])))
            if s_diff:

                l['Source'].remove(s_diff[0])
                l['Target'].remove(s_diff[0])
            for source in l['Source']:
                g.appendEdge(source, tuple(l['Command']))
            for target in l['Target']:
                g.appendEdge(tuple(l['Command']), target)

        g.setBuildTargets(*self.G.getBuildTargets())

        return g
Ejemplo n.º 13
0
    def makeAns(self):

        g = DiGraph()

        g.appendEdge('d1', ('mig', ), False, 'black')
        g.appendEdge(('mig', ), 'd2', False, 'black')
        g.appendEdge('d2', ('d2', 'plus1', 'd3'), False, 'black')
        g.appendEdge(('d2', 'plus1', 'd3'), 'd3', False, 'black')

        #        g.appendEdge(('d1', 'c1', 'd2') , 'd2' , False, 'black')
        #        g.appendEdge('d2', ('c2', 'd3') , False, 'black')
        #        g.appendEdge('d3', ('c3', 'd4') , False, 'black')
        #        g.appendEdge('d4', ('c4', 'd5') , False, 'black')
        #        g.appendEdge('d5', ('c5', 'd6') , False, 'black')
        return g
Ejemplo n.º 14
0
 def makeAns(self):
     """
     returns an empty graph
     """
     return DiGraph()
Ejemplo n.º 15
0
 def makeGraph(self):
     """
     returns an empty graph
     """
     return DiGraph(), set(), set(), set()
Ejemplo n.º 16
0
class GraphManager(Singleton):
    '''
    provides an interface to the graph class 
    '''
    def __new_instance__(self, name):
        """
        simalar to init but for singleton
        initializes the class when  a new instance is created
        """

        Singleton.__new_instance__(self, name)

        #        self.log = Log()
        self.env = InstanceManager()

        self.graph = DiGraph()

        self.__breakpoints = set()
        self.__sources = set()
        self.__targets = set()

        self.runner = defaultRunner()
        self.builder = PipeBuilder()

    def __str__(self):

        i_name = self._instance_name
        srcs = len(self.sources)
        tgts = len(self.targets)
        bps = len(self.breakpoints)

        return "<graphmrg{%(i_name)s} %(srcs)s sources, %(tgts)s targets, %(bps)s bp>" % vars(
        )

    def __repr__(self):
        return self.__str__()

    def getBreakpoints(self):
        'returns breakpoints'
        return self.__breakpoints

    def setBreakpoints(self, value):
        'set breakpoints'
        self.__breakpoints = value

    def getSources(self):
        'get sources'
        return self.__sources

    def setSources(self, value):
        'set sources'
        self.__sources = value

    def getTargets(self):
        'get target'
        return self.__targets

    def setTargets(self, value):
        'set target'
        self.__targets = value

    breakpoints = property(getBreakpoints, setBreakpoints,
                           "Breakpoints should be a set of breakpoints")
    sources = property(getSources, setSources, "Sources's should be a set")
    targets = property(getTargets, setTargets, "Targets's should be a set")

    def add_breakpoint(self, bp):
        'adds a breakpoint to the set from a pyhton object'
        bp_id = id(bp)
        self.add_breakpoint_id(bp_id)

    def add_breakpoint_id(self, bp_id):
        'adds a breakpoint to the set from a pyhton objects id'
        self.breakpoints.add(bp_id)

    def add_source(self, src):
        'adds a source to the set from a pyhton object'
        src_id = id(src)
        self.add_source_id(src_id)

    def add_source_id(self, src_id):
        'adds a source to the set from a pyhton objects id'
        self.sources.add(src_id)

    def add_target(self, tgt):
        'adds a target to the set from a pyhton object'
        tgt_id = id(tgt)
        self.add_target_id(tgt_id)

    def add_target_id(self, tgt_id):
        'adds a target to the set from a pyhton objects id'
        assert tgt_id in self.env['table']
        self.targets.add(tgt_id)

    def graphAppend(self, commpack):
        """
        append to the graph all sources and targets found in 'command'
        plus Source and Target
        """

        nodelist = commpack.getNodeList()

        for i in range(len(nodelist) - 1):
            self.graph.appendEdge(nodelist[i].getID(),
                                  nodelist[i + 1].getID(),
                                  Etype=True,
                                  colour='black')

        commandbeg = nodelist[0]
        commandend = nodelist[-1]

        [
            self.graph.set_node_info(node.id, type='command')
            for node in nodelist
        ]

        if commpack.source:
            src_id = commpack.source_node.id
            self.graph.appendEdge(src_id,
                                  commandbeg.getID(),
                                  Etype=True,
                                  colour='red')
            self.graph.set_node_info(src_id, type='data')
        if commpack.target:
            tgt_id = commpack.target_node.id
            self.graph.appendEdge(commandend.getID(),
                                  tgt_id,
                                  Etype=True,
                                  colour='green')
            self.graph.set_node_info(tgt_id, type='data')

        source_set = set(commpack.getSources())

        for source in source_set:
            self.graph.appendEdge(source.getID(),
                                  commandbeg.getID(),
                                  colour='red')
            self.graph.set_node_info(source.id, type='data')

        target_set = set(commpack.getTargets())
        for target in target_set:
            self.graph.appendEdge(commandend.getID(),
                                  target.getID(),
                                  colour='green')
            self.graph.set_node_info(target.id, type='data')

    def get_graph(self):
        """
        returns the current graph instance that contains the current data and command nodes
        """
        return self._graph

    def set_graph(self, graph):
        """
        Set the graph type,
        @pre: graph must be an instance of the SLIMDataStructure type or None
        """
        self._graph = graph

    graph = property(get_graph, set_graph)

    def set_builder(self, builder):
        """
        replace the current builders with new ones
        """
        from slimpy_base.Core.Builders.BuilderBase import BuilderBase
        assert isinstance(
            builder, BuilderBase), "builerd must be an instance of BuilderBase"
        self._builder = builder

    def get_builder(self):
        'return builder instance'
        return self._builder

    builder = property(get_builder, set_builder)

    def setRunner(self, runner):
        """
        replace the current runner with a new one
        if runner is None the current runner becomes 
        the default runner of SLIMpy
        """
        from slimpy_base.Core.Runners.RunnerBase import Runner
        assert isinstance(
            runner, Runner), "runner must be a subclass of RunnerBase.Runner"
        self._runner = runner

    def get_runner(self):
        'returns the current runner instance'
        return self._runner

    runner = property(get_runner, setRunner)

    #    def setBuildTargets( self, *targets ):
    #        """
    #        set a build target
    #        """
    #        self.graph.setBuildTargets( *targets )
    #
    #    def buildGraph( self, *targets ):
    #        """
    #        returns a new graph instance that is make from applying
    #        all builders in order to the current graph
    #        """
    #        graph = self.graph
    #
    #        for builder in self.builders:
    #            b = builder( graph , breakpoints=self.getBreakpoints() , *targets )
    #            graph = b.toGraph()
    #
    #        return graph
    @note("flush is not run in any profile. It is never called with dryrun")
    def flush(self, target):
        """
        Complement to run , flush uses python objects instead of 
        object IDs to create a command chain
        also flush performs a clean after a graph run has been performed
        """

        log = self.env['record'](1, 'stat')
        print >> log, "SLIMpy: Hold AST Build - Intermediate execution command 'flush' called"
        print >> log, "SLIMpy: Building intermediate target", target
        print >> log, "SLIMpy: Executing commands ..."

        #        import pdb
        #        pdb.set_trace()
        target_id = set([id(target)])

        graph = self.builder.build(self.graph, target_id, self.sources,
                                   self.breakpoints)
        runner = self.runner
        runner.set_graph(graph)
        runner.run()

        new_sources = runner.get_new_sources()
        self.sources.update(new_sources)

        print >> log, "SLIMpy: Done executing commands"
        print >> log, "SLIMpy: Resuming Building AST"

    def addBreakPoint(self, container):
        """
        @type container: DataContainer
        add container to set of breakpoints
        @precondition: container is in the graph
        @postcondition: the container will always
        be built even if it breaks a pipe
         
        """
        self.__breakpoints.add(id(container))

    def __clean__(self):
        """
        for each item in the graph try to remove its data
        """
        #        table = self.env['table']
        ##        for node in self.graph:
        ##            node = table[node]
        ##            # if the node is a data container try to remove it
        ##            if isinstance( node , DataContainer ):
        ##                # note: that the remove call only removes
        ##                # data that are there and not temporary
        ##                node.remove()
        ##
        #        #empty the hash of all objects
        #        table.clear()
        #
        #        from slimpy_base.utils.DotTest import DotTester
        #        dt = DotTester()
        #        dt.clear()

        #        self.log = Log()

        self.graph = DiGraph()

        self.__breakpoints = set()
        self.__sources = set()
        self.__targets = set()

    def Execute(self):
        return self.End()

    def End(self):
        """
        end  all current slimpy ativity
        runs the graph and cleans all nodes in the
        graph and hash table
        """
        record = self.env['record']
        log = record(1, 'stat')
        print >> log, "SLIMpy: Done building AST"
        print >> log, "SLIMpy: Executing commands ..."

        import time

        graph = self.builder.build(self.graph, self.targets, self.sources,
                                   self.breakpoints)

        record.graph = graph

        runner = self.runner
        runner.set_graph(graph)

        record.stat()

        numberran = runner.run()

        record.stat_done()

        new_sources = runner.get_new_sources()
        self.sources.update(new_sources)

        end = time.time()

        disp_log = self.env['record'](10, 'display')

        # display the command
        print >> disp_log, "Display:"
        print >> disp_log, "\tCode ran in : %5.5s seconds" % time.clock()
        print >> disp_log, "\tComplexity  : %5.5s nodes" % len(self.graph)
        print >> disp_log, "\tRan         : %5.5s commands" % numberran
        print >> disp_log, "GraphBuilder.cleanAll called"

        #        if hasattr(runner, 'center'):
        #            runner.center.display_stats()

        #        self.cleanAll()
        #        self.env.del_instance( self.env.current_env )
        print >> log, "SLIMpy: Done executing commands"
        return numberran

#    def addSource( self, container ):
#        """
#        add a source to the graph
#        can be a hash value or a container
#        """
#
#
#        # in the case that the con
#        if isinstance( container, int ):
#            self.graph.addSource( container )
#        elif isinstance( container, DataContainer ):
#            self.env['table'].addSource( id(container) )
#            # if the container is not full
#            # it can not be a source
#            if not container.isfull():
#                pass #raise TypeError, "%(container)s in not a source" %vars()
#            self.graph.addSource( id( container ) )
#
#        else:
#            raise TypeError, "%(container)s must be either a container or an integer" %vars()

#    def isSource( self, val ):
#        """
#        test if a value is a source uses the
#        adc.isfull method
#        @raise KeyError: if val is not in the HashTable
#        """
#        # try and get the value from the hash
#        table = self.env['table']
#        try:
#            val =  table[val]
#        except KeyError:
#            return False
#
#        if isinstance( val, DataContainer ):
#
#            return val.isfull()
#
#        return False

    def printAdj(self, v):
        """
        prints graph instance
        """
        from slimpy_base.Core.Interface.AbstractDataInterface import ADI

        if isinstance(v, ADI):
            v = v.getContainer()
        if isinstance(v, DataContainer):
            v = id(v)

        return printer.printAdj(self.graph, v)

    def printInvAdj(self, v):
        """
        prints graph instance
        """
        from slimpy_base.Core.Interface.AbstractDataInterface import ADI

        if isinstance(v, ADI):
            node = v.container()
        if isinstance(v, DataContainer):
            node = id(v)
        else:
            node = v
        return printer.printInvAdj(self.graph, node)

    def printDep(self):
        """
        prints graph instance
        """
        return printer.printDep(self.graph)

    def printInvDep(self):
        """
        prints graph instance
        """
        return printer.printInvDep(self.graph)

    def toDot(self):
        """
        prints a dot file
        """
        printer.toDot(self.graph)
Ejemplo n.º 17
0
    def makeAns(self):

        g = DiGraph()

        g.appendEdge('data', ('data', 'fdct', 'newdata'))

        g.appendEdge(('data', 'fdct', 'newdata'), 'newdata')
        g.appendEdge(('data', 'fdct', 'newdata'), 'sizes')

        g.appendEdge('newdata', ('newdata', 'fdctInv', 'final'))
        g.appendEdge('sizes', ('newdata', 'fdctInv', 'final'))

        g.appendEdge(('newdata', 'fdctInv', 'final'), 'final')

        return g
Ejemplo n.º 18
0
 def makeG(self):
     g = DiGraph()
     g.appendEdge('d1', 'c1', True, 'red')
     g.appendEdge('c1', 'd2', True, 'green')
     g.appendEdge('d1', 'c2', True, 'red')
     g.appendEdge('c2', 'd3', True, 'green')
     g.appendEdge('d2', 'c3', True, 'red')
     g.appendEdge('d3', 'c3', False, 'red')
     g.appendEdge('c3', 'd4', True, 'green')
     return g