def createarray(in_equations, listin=False): ''' expands all to_array(list) in a model returns a new model''' nymodel = [] equations = in_equations[:].upper() # we want do change the e liste_dict = listin if listin else list_extract( equations) # Search the whold model for lists for comment, command, value in find_statements(equations): # print('>>',comment,'<',command,'>',value) if comment: nymodel.append(comment) else: while 'TO_ARRAY(' in value.upper(): forcreate, createudtryk, eftercreate = find_arg( 'TO_ARRAY', value.upper()) temp = createudtryk.split(',', 2) mat_name = temp[-1] create_row = temp[0] row_dict = liste_dict[create_row] if len(temp) == 2: ibsud = '[' + sub_frml(row_dict, mat_name, ',', '', sep='') + ']' else: create_column = temp[1] column_dict = liste_dict[create_column] ibsud0 = '[' + sub_frml( column_dict, mat_name, ',', '', sep='') + ']' ibsud = '[' + sub_frml(row_dict, ibsud0, ',\n', '', sep='') + ']' value = forcreate + 'array(\n' + ibsud + ')' + eftercreate nymodel.append(command + ' ' + value) equations = '\n'.join(nymodel) return equations
def explode(model,norm=True,sym=False,funks=[],sep='\n'): '''prepares a model from a model template. Returns a expanded model which is ready to solve Eksempel: model = udrul_model(MinModel.txt)''' udrullet=tofrml(model,sep=sep) modellist = list_extract(udrullet) if norm : udrullet = normalize(udrullet,sym,funks=funks ) # to save time if we know that normalization is not needed udrullet = exounroll(udrullet) # finaly the exogeneous and adjustment terms - if present - are handled udrullet = dounloop(udrullet,listin=modellist) # we unroll the do loops udrullet = sumunroll(udrullet,listin=modellist) # then we unroll the sum udrullet = creatematrix(udrullet,listin=modellist) udrullet = createarray(udrullet,listin=modellist) udrullet = argunroll(udrullet,listin=modellist) return udrullet
def argunroll(in_equations,listin=False): ''' expands all ARGEXPAND(list,'expression') in a model returns a new model''' nymodel = [] equations = in_equations[:].upper() # we want do change the e liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists for comment, command, value in find_statements(equations): # print('>>',comment,'<',command,'>',value) if comment: nymodel.append(comment) else: while 'ARGEXPAND(' in value.upper(): forsum, sumudtryk, eftersum = find_arg('ARGEXPAND', value.upper()) sumover, sumled = sumudtryk.split(',', 1) current_dict = liste_dict[sumover] ibsud = sub_frml(current_dict, sumled, ',', '', sep='') value = forsum + '' + ibsud + '' + eftersum nymodel.append(command + ' ' + value) equations = '\n'.join(nymodel) return equations
def dounloop(in_equations, listin=False): ''' Expands (unrolls do loops in a model template goes trough a model template until there is no more nested do loops ''' equations = in_equations[:].upper() # we want do change the equations liste_dict = listin if listin else list_extract( equations) # Search the whold model for lists rest = 1 while rest: # We want to eat all doo loops nymodel = [] domodel = [] dolevel = 0 rest = 0 liste_name, liste_key, liste_select = '', '', '' for comment, command, value in find_statements(equations): # print('>>',comment,'<',command,'>',value) if command.upper() == 'DO': dolevel = dolevel + 1 if dolevel >= 2: rest = 1 # do we have more doolops domodel.append(command + ' ' + value) #========================================================================= # else: # find listname key=select #========================================================================= elif dolevel == 1: liste_name = [ t.strip().upper() for t in re.split(r'[\s,]\s*', value[:-1]) if t != '' ] current_dict = liste_dict[liste_name[0]] #print('listenavn ',liste_name, current_dict) #ibdic, text, plus='', xvar='', lig='', sep='\n',selectlist=None,match='' if len(liste_name) == 1: liste_key, liste_select = '', '' elif len(liste_name) == 4: liste_key, liste_select = liste_name[1], liste_name[3] # current_dict=liste_dict[value[0:-2].strip()] # find the name of the list else: assert 1 == 2, print( ' *** error in DO statement either 1 or 4 arguments:', comment, command, value) domodel = [] elif (command.upper() == 'ENDDO'): dolevel = dolevel - 1 if dolevel == 0: if len(liste_name) == 1 or len(liste_name) == 4 or len( liste_name) == 5: ibsud = sub_frml(current_dict, '\n'.join(domodel), plus='', sep='\n', xvar=liste_key, lig=liste_select) else: print('Fejl i liste', liste_name) print('Ibsud>', ibsud) nymodel.append('\n' + ibsud + '\n') elif dolevel >= 1: domodel.append(command + ' ' + value + '\n') elif dolevel >= 1: # a command to store for do looping if comment: domodel.append(comment + '') else: domodel.append(command + ' ' + value) else: # a command outside a dooloop if comment: nymodel.append(comment) else: nymodel.append(command + ' ' + value) equations = '\n'.join(nymodel) return equations
sub_frml({ 'weed': ['scrubbery', 'herring'], 'where': ['land', 'sea'] }, 'we have brought you your {weed} from {where} ') sub_frml({ 'weed': ['scrubbery', 'herring'], 'where': ['land', 'sea'] }, 'we have brought you your {weed} from {where} ') a = {'bankdic': {'bank': ['Danske', 'Nordea'], 'danske': ['yes', 'no']}} sub_frml(a['bankdic'], 'Dette er {bank}') sub_frml(a['bankdic'], 'Dette er {bank}', sep=' and ') sub_frml(a['bankdic'], 'Dette er {bank}', plus='+', sep='') sub_frml(a['bankdic'], 'Dette er {bank}', xvar='danske', lig='yes') sub_frml(a['bankdic'], 'Dette er {bank}', xvar='danske', lig='no') sub_frml(a['bankdic'], 'Dette er {bank}') list_extract( 'list bankdic = bank : Danske , Nordea / danske : yes , no $') kw_frml_name('<res=abe>', 'res') kw_frml_name('<res=abe,animal>', 'animal') find_res('FRML <res=x> ib =x+y+v $') find_res('FRML <res=J> ib =x+y+v + ib_j $') find_res('FRML <res=JR> ib =(x+y+v + ib_j)*(1+ ib_JR) $)') find_hist_model('FRML <res=x> ib =x+y+v $ frml <ident> y=c+i+x-m $') (exounroll('frml <j> ib=x+y $ frml <jr> ib=x+y $ frml <j,jr')) find_arg('log', 'x=log(a+b)+77') find_arg('log', 'x=log(a+log(b))+77') #%% x = (dounloop('''list bankdic = bank : Danske , Nordea / ko : yes , no $ do bankdic ko = no $ frml x {bank}_income = {bank}_a +{bank}_b $ enddo $