00001 """
00002 Interface of Graph and Vector and Linear operator classes
00003 """
00004
00005 __copyright__ = """
00006 Copyright 2008 Sean Ross-Ross
00007 """
00008 __license__ = """
00009 This file is part of SLIMpy .
00010
00011 SLIMpy is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU Lesser General Public License as published by
00013 the Free Software Foundation, either version 3 of the License, or
00014 (at your option) any later version.
00015
00016 SLIMpy is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU Lesser General Public License for more details.
00020
00021 You should have received a copy of the GNU Lesser General Public License
00022 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025 from slimpy_base.Core.Command.Command import Command as general_command
00026 from slimpy_base.Core.Command.CommandPack import CommandPack
00027 from slimpy_base.Core.Interface.node import Source, Target
00028 from slimpy_base.Core.User.Structures.Scalar import Scalar
00029 from slimpy_base.Environment.InstanceManager import InstanceManager
00030 from traceback import extract_stack, format_list
00031 from slimpy_base.Core.Interface.PSpace import voidSpace
00032
00033
00034 class Structure( object ):
00035 """
00036 Structure class
00037 SLIMpy is set up such that there is a hierarchy of
00038 + Structure
00039 |--+ Graph
00040 |--+ Container
00041 | |--+ Data
00042
00043 This is because we need the abstraction at each level
00044 The container provides a constant interface for each type of data for the
00045 structure to use.
00046 The structure knows about the standard container interface and
00047 Graph and interfaces the two.
00048 structure is designed to be the super class for all of the SLIMpy concrete
00049 data class (e.g., Vector, Linear Operator, Array, ...)
00050 """
00051 __scalarcount = 0
00052 name = "Sructure"
00053
00054 env = InstanceManager()
00055
00056 @classmethod
00057 def generate_command( cls, cmnd, *args, **kargs ):
00058 """
00059 return a new slimpy_base.Command.command instance
00060 """
00061
00062
00063
00064
00065 command = general_command( cmnd, None, *args, **kargs )
00066
00067 if cls.env['slimvars']['keep_tb_info']:
00068 stack = extract_stack( )
00069 st_list = format_list( stack[:-3] )
00070 command.tb_info = "".join(st_list)
00071
00072 return command
00073
00074 @classmethod
00075 def apply_command(cls,structure, command ):
00076
00077 container = structure.container
00078 converter = container.get_converter( command )
00079 compack, newcontainer = converter.apply_command( command, container )
00080
00081 compack.source = container
00082 compack.target = newcontainer
00083
00084 cls.env['graphmgr'].graphAppend( compack )
00085
00086 return newcontainer
00087
00088 @classmethod
00089 def testCommand( cls, space, cmnd_tag, *args, **kargs ):
00090 """
00091 Returns the resulting space from applying a command
00092 """
00093 command = cls.generate_command( cmnd_tag, *args, **kargs )
00094
00095
00096 converter = space.plugin.get_converter( command )
00097
00098 return converter.transform( command, space )
00099
00100 @classmethod
00101 def generateNewWithSpace( cls, data, space, cmd, *args, **kargs ):
00102 """
00103 Generate a new Structure instance (note: that the new instance will be the current subclass)
00104 Also see: genData
00105 """
00106
00107 command = cls.generate_command( cmd, *args, **kargs )
00108
00109
00110 try:
00111 newdata = cls.apply_command( data, command, space )
00112 except TypeError, msg:
00113 if not cls.env['slimvars']['WARNCONFLICT']:
00114 raise
00115 else:
00116 print "Warning", msg
00117
00118
00119 return data.__class__( newdata )
00120
00121 @classmethod
00122 def generateNew( cls, data, cmd_tag, *args, **kargs ):
00123 """
00124 Generate a new Structure instance (note: that the new instance will be the current subclass)
00125 Also see: genData
00126 """
00127
00128 command = cls.generate_command( cmd_tag, *args, **kargs )
00129
00130
00131
00132 newdata = cls.apply_command( data, command )
00133
00134
00135 return data.__class__( newdata )
00136
00137 @classmethod
00138 def genData( cls, data, cmd, *args, **kargs ):
00139 """
00140 Same as generateNew. However genData returns a dataContainer instance
00141 """
00142
00143 command = cls.generate_command( cmd, *args, **kargs )
00144
00145 return cls.apply_command( data, command )
00146
00147 @classmethod
00148 def AppendToGraph( cls, container1, cmnd, container2 ):
00149 """
00150 Append to the current graph in the keystone Class
00151 """
00152 raise DeprecationWarning( "do not use this function" )
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 @classmethod
00188 def scalar_reduction(cls, container, tag, *args, **kargs ):
00189 """
00190 generate a Scalar from a command
00191 """
00192 scalar_methods = container.scalar_methods
00193
00194 scmd = scalar_methods.get_command( tag )
00195
00196 scalar = Scalar( )
00197 cont = Source( container )
00198 scal = Target( scalar )
00199
00200 if cls.env['slimvars']['use_scalar_obj']:
00201
00202 command = general_command( tag, None, cont, scal, *args, **kargs )
00203
00204 command.func = scmd
00205 compack = CommandPack( [ command ], None, None )
00206 cls.env['graphmgr'].graphAppend( compack )
00207 else:
00208 scalar = scal.data
00209
00210 return scalar
00211 @classmethod
00212 def genScalar( cls, container, tag, *args, **kargs ):
00213 """
00214 generate a Scalar from a command
00215 """
00216 return cls.scalar_reduction(container, tag, *args, **kargs)
00217
00218 @classmethod
00219 def source_or_num( cls, obj ):
00220 """
00221 struc.source_or_num( obj ) -> result
00222 returns a source if obj is a SLIMpy scalar, or returns obj if obj is
00223 a number
00224 """
00225 if isinstance( obj, Scalar ):
00226 return Source( obj )
00227 elif hasattr( obj, 'container' ):
00228 return Source( getattr( obj, 'container' ) )
00229 else:
00230 return obj
00231
00232 @classmethod
00233 def flush( cls, data ):
00234 'see GraphManager.flush'
00235 cls.env['graphmgr'].flush( data )
00236
00237 @classmethod
00238 def dependant( cls, container ):
00239 """
00240 prints a dependency table for
00241 container
00242 """
00243 v = id( container )
00244 return cls.env['graphmgr'].printInvAdj( v )
00245
00246 @classmethod
00247 def addBreakPoint( cls, container ):
00248 """
00249 add a breakpoint in the graph
00250 container will always be build, even if it
00251 breaks a pipe
00252 """
00253 cls.env['graphmgr'].addBreakPoint( container )
00254
00255
00256 def __nonzero__( self ):
00257 """
00258 always true
00259 """
00260 return 1
00261
00262 def __repr__( self ):
00263 return "<SLIMpy: " + self.__class__.__name__ + ">"
00264