@@ -349,3 +349,50 @@ def _is_solve_step(self, name: str):
349349
350350 def report (self ):
351351 return {"solver_logs" : self ._logs }
352+
353+
354+ class ModelVariables (Action ):
355+ """Extract and format model variables."""
356+
357+ VAR_TYPE = "var"
358+ PARAM_TYPE = "param"
359+
360+ def __init__ (self , runner , ** kwargs ):
361+ assert isinstance (runner , FlowsheetRunner ) # makes no sense otherwise
362+ super ().__init__ (runner , ** kwargs )
363+
364+ def after_run (self ):
365+ self ._blocks = self ._extract_vars (self ._runner .model )
366+
367+ def _extract_vars (self , m ):
368+ model_vars = []
369+ for c in m .component_objects ():
370+ id_c = id (c )
371+ if c .is_variable_type ():
372+ subtype = self .VAR_TYPE
373+ elif c .is_parameter_type ():
374+ subtype = self .PARAM_TYPE
375+ else :
376+ continue # we just don't care!
377+ # XXX block: subtype, id, name, parent_id, [is_indexed, num]
378+ # XXX b = [subtype, id_c, c.name, id(c.parent_block())]
379+ b = [subtype , c .name ]
380+ items = []
381+ for index in c :
382+ v = c [index ]
383+ if index is None :
384+ b .append (False ) # not indexed
385+ else :
386+ b .append (True ) # indexed
387+ # item: index, value, [fixed, stale, lb, ub]
388+ if subtype == self .VAR_TYPE :
389+ item = (index , v .value , v .fixed , v .stale , v .lb , v .ub )
390+ else :
391+ item = (index , v .value )
392+ items .append (item )
393+ b .append (items )
394+ model_vars .append (b )
395+ return model_vars
396+
397+ def report (self ) -> dict :
398+ return {"blocks" : self ._blocks }
0 commit comments