Skip to content

Commit

Permalink
Merge pull request #368 from NeuroML/experimental
Browse files Browse the repository at this point in the history
Better handling of nested if statements in xpp parser
  • Loading branch information
pgleeson authored May 21, 2024
2 parents 4a3c164 + 771d66c commit cea0f37
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions pyneuroml/xppaut/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@

verbose = False

temp_var_count = 0

def get_new_temp_var():
global temp_var_count
tv = 'TMP__%i'%temp_var_count
temp_var_count+=1
return tv

def _closing_bracket_index(expr, open_bracket_index):
depth = 0
logger.debug('Looking for closing bracket of %s from %i, i.e. %s'%(expr, open_bracket_index, expr[open_bracket_index:]))
Expand Down Expand Up @@ -123,8 +131,35 @@ def parse_script(file_path):
else:
# "Normal" variable...
expr = value.strip()

#### This should be refactored to make it 'infinitely' recursive...
if 'if' in expr and 'else' in expr:
data["conditional_derived_variables"][key.strip()] = _split_if_then_else(expr)
var_0 = key.strip()
ite_0 = _split_if_then_else(expr)

if 'if' in ite_0['value_true']:
ite_1 = _split_if_then_else(ite_0['value_true'])
new_var_name1 = get_new_temp_var()

if 'if' in ite_1['value_false']:
ite_2 = _split_if_then_else(ite_1['value_false'])
new_var_name2 = get_new_temp_var()

if 'if' in ite_2['value_true']:
ite_3 = _split_if_then_else(ite_2['value_true'])
new_var_name3 = get_new_temp_var()

data["conditional_derived_variables"][new_var_name3] = ite_3
ite_2['value_true'] = new_var_name3

data["conditional_derived_variables"][new_var_name2] = ite_2
ite_1['value_false'] = new_var_name2

data["conditional_derived_variables"][var_0] = ite_0
data["conditional_derived_variables"][new_var_name1] = ite_1
ite_0['value_true'] = new_var_name1

data["conditional_derived_variables"][var_0] = ite_0
else:
k = key.strip()
if ' ' in k:
Expand Down Expand Up @@ -601,7 +636,12 @@ def run_xpp_file(filename, plot, show_plot_already=True, plot_separately={}):
try:
ret_string = sp.check_output(
cmds, cwd=cwd, shell=False, stderr=sp.STDOUT
)
).decode("utf-8")

xpp_error_phrases = ['Error allocating', 'illegal expression']
for err in xpp_error_phrases:
if err in ret_string:
raise Exception("Command: %s failed! Full output from XPP: \n==========================\n%s\n==========================" % (cmds, ret_string))
logger.info(
"Commands: %s completed successfully" % (cmds)
)
Expand Down

0 comments on commit cea0f37

Please sign in to comment.