00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 from slimpy_base.Core.Graph.Graph.DiGraph import DiGraph
00022
00023
00024 class PipeBuilder(object):
00025
00026 def __init__(self,g,sources,*targets):
00027
00028 self.Colour = {}
00029 self.G = g
00030 self.lst = []
00031 self.done = {}
00032 self.colourSources(sources)
00033 self.bld(*targets)
00034
00035
00036 def colour(self,node):
00037 return self.Colour.get(node,'white')
00038
00039
00040
00041 def bld(self,*targets):
00042 print self.G.buildTargets.union(targets)
00043 for target in self.G.buildTargets.union(targets):
00044
00045 if self.colour(target) == 'grey':
00046 continue
00047
00048 ff = True
00049
00050 List = {'Target':[target],'Command':[target],'Source':[]}
00051 self.lst.append(List)
00052 dep = self.G.invAdj(target)
00053
00054
00055 for node in dep:
00056
00057 sig = ff
00058 stdinFlag = self.G.invAdjacencyList[target][node]
00059 depFlag = len(self.G.adjacencyList[node]) <=1
00060
00061 self.c2(ff, stdinFlag, depFlag, List, node)
00062
00063 return
00064
00065 def createCom(self,node,List):
00066 ff = True
00067 dep = self.G.invAdj(node)
00068
00069 for prev in dep:
00070
00071 stdinFlag = self.G.invAdjacencyList[node][prev]
00072 depFlag = len( self.G.adjacencyList[prev] ) <= 1
00073
00074
00075
00076 ff == self.c2(ff, stdinFlag, depFlag, List, prev)
00077
00078 def c2(self, ff, stdinFlag, depFlag, List, prev):
00079 sig = ff
00080 if ff and stdinFlag and depFlag:
00081 ff = False
00082 List['Command'].insert(0,prev)
00083 if not self.colour(prev) == 'grey':
00084 self.Colour[prev] = 'grey'
00085 self.createCom(prev, List)
00086 else:
00087 List['Source'].append(prev)
00088
00089 if not self.colour(prev) == 'grey':
00090
00091 newList = {'Target':[prev],'Command':[prev],'Source':[]}
00092 self.lst.insert(0,newList)
00093 self.Colour[prev] = 'grey'
00094 self.createCom(prev, newList)
00095
00096 if sig and stdinFlag and not depFlag:
00097
00098 List['Command'].insert(0,prev)
00099
00100 return ff
00101
00102 def toGraph(self):
00103 g = DiGraph()
00104
00105 for l in self.lst:
00106
00107 for source in l['Source']:
00108 g.appendEdge(source, tuple(l['Command']))
00109 for target in l['Target']:
00110 g.appendEdge( tuple(l['Command']),target)
00111
00112 g.setBuildTargets(*self.G.buildTargets)
00113
00114 return g
00115
00116
00117
00118
00119
00120
00121