00001 """
00002 GraphBuilder CLASS
00003 manages the graph,the pipe builder and the runner
00004 """
00005
00006 __copyright__ = """
00007 Copyright 2008 Sean Ross-Ross
00008 """
00009 __license__ = """
00010 This file is part of SLIMpy .
00011
00012 SLIMpy is free software: you can redistribute it and/or modify
00013 it under the terms of the GNU Lesser General Public License as published by
00014 the Free Software Foundation, either version 3 of the License, or
00015 (at your option) any later version.
00016
00017 SLIMpy is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 GNU Lesser General Public License for more details.
00021
00022 You should have received a copy of the GNU Lesser General Public License
00023 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00024 """
00025
00026 from slimpy_base.Core.Graph.Graph.DiGraph import DiGraph
00027 from slimpy_base.Core.Interface.ContainerBase import DataContainer
00028 from slimpy_base.Environment.InstanceManager import InstanceManager
00029 from slimpy_base.Environment.Singleton import Singleton
00030
00031 from slimpy_base.Core.Builders.PipeBuilder import PipeBuilder
00032 from slimpy_base.Core.Runners.defaultRunner import defaultRunner
00033 from slimpy_base.Core.Graph.GraphPrinter import GraphPrinter as printer
00034 from slimpy_base.utils.Profileable import note
00035
00036
00037 class GraphManager( Singleton ):
00038 '''
00039 provides an interface to the graph class
00040 '''
00041
00042 def __new_instance__(self, name):
00043 """
00044 simalar to init but for singleton
00045 initializes the class when a new instance is created
00046 """
00047
00048 Singleton.__new_instance__(self, name)
00049
00050
00051 self.env = InstanceManager()
00052
00053 self.graph = DiGraph()
00054
00055 self.__breakpoints = set()
00056 self.__sources = set()
00057 self.__targets = set()
00058
00059 self.runner = defaultRunner()
00060 self.builder = PipeBuilder()
00061
00062 def __str__(self):
00063
00064 i_name = self._instance_name
00065 srcs = len(self.sources)
00066 tgts = len(self.targets)
00067 bps = len(self.breakpoints)
00068
00069 return "<graphmrg{%(i_name)s} %(srcs)s sources, %(tgts)s targets, %(bps)s bp>" %vars()
00070
00071 def __repr__(self):
00072 return self.__str__()
00073
00074 def getBreakpoints(self):
00075 'returns breakpoints'
00076 return self.__breakpoints
00077
00078 def setBreakpoints(self, value):
00079 'set breakpoints'
00080 self.__breakpoints = value
00081
00082 def getSources(self):
00083 'get sources'
00084 return self.__sources
00085
00086 def setSources(self, value):
00087 'set sources'
00088 self.__sources = value
00089
00090 def getTargets(self):
00091 'get target'
00092 return self.__targets
00093
00094 def setTargets(self, value):
00095 'set target'
00096 self.__targets = value
00097
00098 breakpoints = property(getBreakpoints, setBreakpoints, "Breakpoints should be a set of breakpoints")
00099 sources = property(getSources, setSources, "Sources's should be a set")
00100 targets = property(getTargets, setTargets, "Targets's should be a set")
00101
00102 def add_breakpoint(self, bp):
00103 'adds a breakpoint to the set from a pyhton object'
00104 bp_id = id(bp)
00105 self.add_breakpoint_id(bp_id)
00106
00107 def add_breakpoint_id(self, bp_id ):
00108 'adds a breakpoint to the set from a pyhton objects id'
00109 self.breakpoints.add( bp_id )
00110
00111 def add_source(self, src):
00112 'adds a source to the set from a pyhton object'
00113 src_id = id(src)
00114 self.add_source_id(src_id)
00115
00116 def add_source_id(self, src_id ):
00117 'adds a source to the set from a pyhton objects id'
00118 self.sources.add( src_id )
00119
00120 def add_target(self, tgt):
00121 'adds a target to the set from a pyhton object'
00122 tgt_id = id(tgt)
00123 self.add_target_id(tgt_id)
00124
00125 def add_target_id(self, tgt_id ):
00126 'adds a target to the set from a pyhton objects id'
00127 assert tgt_id in self.env['table']
00128 self.targets.add( tgt_id )
00129
00130 def graphAppend( self, commpack ):
00131 """
00132 append to the graph all sources and targets found in 'command'
00133 plus Source and Target
00134 """
00135
00136 nodelist = commpack.getNodeList()
00137
00138 for i in range( len( nodelist )-1 ):
00139 self.graph.appendEdge( nodelist[i].getID(), nodelist[i+1].getID(), Etype=True, colour='black' )
00140
00141 commandbeg = nodelist[0]
00142 commandend = nodelist[-1]
00143
00144 [self.graph.set_node_info( node.id , type='command' ) for node in nodelist]
00145
00146 if commpack.source:
00147 src_id = commpack.source_node.id
00148 self.graph.appendEdge( src_id , commandbeg.getID(), Etype=True, colour='red' )
00149 self.graph.set_node_info( src_id , type='data' )
00150 if commpack.target:
00151 tgt_id = commpack.target_node.id
00152 self.graph.appendEdge( commandend.getID() , tgt_id, Etype=True, colour='green' )
00153 self.graph.set_node_info( tgt_id, type='data' )
00154
00155 source_set = set( commpack.getSources() )
00156
00157 for source in source_set:
00158 self.graph.appendEdge( source.getID(), commandbeg.getID() , colour='red' )
00159 self.graph.set_node_info( source.id, type='data' )
00160
00161 target_set = set( commpack.getTargets() )
00162 for target in target_set:
00163 self.graph.appendEdge( commandend.getID() , target.getID() , colour='green' )
00164 self.graph.set_node_info( target.id, type='data' )
00165
00166
00167 def get_graph( self ):
00168 """
00169 returns the current graph instance that contains the current data and command nodes
00170 """
00171 return self._graph
00172
00173 def set_graph( self, graph):
00174 """
00175 Set the graph type,
00176 @pre: graph must be an instance of the SLIMDataStructure type or None
00177 """
00178 self._graph = graph
00179
00180 graph = property( get_graph, set_graph)
00181
00182 def set_builder( self, builder ):
00183 """
00184 replace the current builders with new ones
00185 """
00186 from slimpy_base.Core.Builders.BuilderBase import BuilderBase
00187 assert isinstance(builder, BuilderBase), "builerd must be an instance of BuilderBase"
00188 self._builder = builder
00189
00190 def get_builder( self ):
00191 'return builder instance'
00192 return self._builder
00193
00194
00195 builder = property( get_builder, set_builder )
00196
00197 def setRunner( self, runner ):
00198 """
00199 replace the current runner with a new one
00200 if runner is None the current runner becomes
00201 the default runner of SLIMpy
00202 """
00203 from slimpy_base.Core.Runners.RunnerBase import Runner
00204 assert isinstance(runner, Runner), "runner must be a subclass of RunnerBase.Runner"
00205 self._runner = runner
00206
00207 def get_runner(self):
00208 'returns the current runner instance'
00209 return self._runner
00210
00211 runner = property( get_runner, setRunner)
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 @note("flush is not run in any profile. It is never called with dryrun")
00232 def flush( self, target ):
00233 """
00234 Complement to run , flush uses python objects instead of
00235 object IDs to create a command chain
00236 also flush performs a clean after a graph run has been performed
00237 """
00238
00239 log = self.env['record'](1, 'stat' )
00240 print >> log, "SLIMpy: Hold AST Build - Intermediate execution command 'flush' called"
00241 print >> log, "SLIMpy: Building intermediate target", target
00242 print >> log, "SLIMpy: Executing commands ..."
00243
00244
00245
00246 target_id = set([ id(target) ])
00247
00248
00249 graph = self.builder.build( self.graph, target_id, self.sources, self.breakpoints )
00250 runner = self.runner
00251 runner.set_graph( graph )
00252 runner.run()
00253
00254 new_sources = runner.get_new_sources()
00255 self.sources.update( new_sources )
00256
00257 print >> log, "SLIMpy: Done executing commands"
00258 print >> log, "SLIMpy: Resuming Building AST"
00259
00260 def addBreakPoint( self, container ):
00261 """
00262 @type container: DataContainer
00263 add container to set of breakpoints
00264 @precondition: container is in the graph
00265 @postcondition: the container will always
00266 be built even if it breaks a pipe
00267
00268 """
00269 self.__breakpoints.add( id( container ) )
00270
00271 def __clean__( self ):
00272 """
00273 for each item in the graph try to remove its data
00274 """
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 self.graph = DiGraph()
00295
00296 self.__breakpoints = set()
00297 self.__sources = set()
00298 self.__targets = set()
00299
00300 def Execute(self):
00301 return self.End( )
00302
00303 def End( self ):
00304 """
00305 end all current slimpy ativity
00306 runs the graph and cleans all nodes in the
00307 graph and hash table
00308 """
00309 record = self.env['record']
00310 log = record(1, 'stat' )
00311 print >> log, "SLIMpy: Done building AST"
00312 print >> log, "SLIMpy: Executing commands ..."
00313
00314 import time
00315
00316 graph = self.builder.build( self.graph , self.targets, self.sources, self.breakpoints )
00317
00318 record.graph = graph
00319
00320 runner = self.runner
00321 runner.set_graph( graph )
00322
00323 record.stat( )
00324
00325 numberran = runner.run()
00326
00327 record.stat_done( )
00328
00329 new_sources = runner.get_new_sources()
00330 self.sources.update( new_sources )
00331
00332 end = time.time( )
00333
00334 disp_log = self.env['record']( 10, 'display' )
00335
00336
00337 print >> disp_log , "Display:"
00338 print >> disp_log , "\tCode ran in : %5.5s seconds" % time.clock()
00339 print >> disp_log , "\tComplexity : %5.5s nodes" % len( self.graph )
00340 print >> disp_log , "\tRan : %5.5s commands" % numberran
00341 print >> disp_log , "GraphBuilder.cleanAll called"
00342
00343
00344
00345
00346
00347
00348 print >> log, "SLIMpy: Done executing commands"
00349 return numberran
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 def printAdj( self, v ):
00392 """
00393 prints graph instance
00394 """
00395 from slimpy_base.Core.Interface.AbstractDataInterface import ADI
00396
00397 if isinstance( v, ADI ):
00398 v = v.getContainer()
00399 if isinstance( v, DataContainer ):
00400 v = id( v )
00401
00402 return printer.printAdj( self.graph, v )
00403
00404 def printInvAdj( self, v ):
00405 """
00406 prints graph instance
00407 """
00408 from slimpy_base.Core.Interface.AbstractDataInterface import ADI
00409
00410 if isinstance( v, ADI ):
00411 node = v.container()
00412 if isinstance( v, DataContainer ):
00413 node = id( v )
00414 else:
00415 node = v
00416 return printer.printInvAdj( self.graph, node )
00417
00418 def printDep( self ):
00419 """
00420 prints graph instance
00421 """
00422 return printer.printDep( self.graph )
00423
00424 def printInvDep( self ):
00425 """
00426 prints graph instance
00427 """
00428 return printer.printInvDep( self.graph )
00429
00430 def toDot( self ):
00431 """
00432 prints a dot file
00433 """
00434 printer.toDot( self.graph )
00435