Skip to content

Commit

Permalink
Merge pull request #23 from cjarmstrong97/main
Browse files Browse the repository at this point in the history
Some issue fixing
  • Loading branch information
ASinanSaglam authored Sep 17, 2021
2 parents a9e6e15 + 74e3f43 commit ddd3066
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 22 deletions.
61 changes: 43 additions & 18 deletions bionetgen/modelapi/bngparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,51 +73,76 @@ def _parse_model_bngpl(self, model_obj) -> None:
self.parse_xml(xml_str, model_obj)
model_obj.reset_compilation_tags()
else:
print("The extension of {} is not supported".format(model_file))
raise NotImplementedError
raise NotImplementedError(
"The extension of {} is not supported".format(model_file)
)

def parse_actions(self, model_obj):
if len(self.bngfile.parsed_actions) > 0:
ablock = ActionBlock()
# we have actions in file, let's get them
for action in self.bngfile.parsed_actions:
action = re.sub("\#.*", "", action)
action = re.sub("\s", "", action)
action = re.sub(
r"\#.*", "", action
) # should this be (r"\#.*) or just ("\#.*")
action = re.sub(
r"\s", "", action
) # should this be (r"\s) or just ("\s")
if len(action) == 0:
continue
m = re.match(r"^\s*(\S+)\(\s*(\S*)\s*\)(\;)?\s*(\#\s*\S*)?\s*", action)
if m is not None:
# here we have an action
atype = m.group(1)
in_parans = m.group(2)
if len(in_parans) > 0:
# in paranthesis group can have curly or square braces
m = re.match(r"\{(\S*)\}", in_parans)
in_parens = m.group(2)
if len(in_parens) > 0:
# in parenthesis group can have curly or square braces
m = re.match(r"\{(\S*)\}$", in_parens)
arg_tuples = []
if m is not None:
# this is a normal action
arg_list_str = m.group(1)
# First let's check for lists
# Please note that this will only replace a single list that doesn't reoccur
L = re.match(r"\S+\[(\S*)\]\S*", m.group(1))
if L is not None:
arg_list_str = re.sub(
f"[{L.group(1)}]",
f"[{L.group(1).replace(',','_')}]",
arg_list_str,
)
# Now check for curly braces
# Please note that this will only replace a single dictionary that doesn't reoccur
L = re.match(r"\S+\{(\S*)\}\S*", m.group(1))
if L is not None:
arg_list_str = re.sub(
"{%s}" % L.group(1),
"{%s}" % L.group(1).replace(",", "_"),
arg_list_str,
)
arg_list = arg_list_str.split(",")
for arg_str in arg_list:
m = re.match(r"(\S*)\=\>(\S*)", arg_str)
if m is not None:
# add to arg_tuples
arg = m.group(1)
val = m.group(2)
splt = arg_str.split("=>")
if len(splt) > 1:
arg = splt[0]
val = "=>".join(splt[1:])
# now we need to check if we have a list/dictionary
if "_" in val:
val = val.replace("_", ",")
# add the tuple to the list
arg_tuples.append((arg, val))
else:
m = re.match(r"\[(\S*)\]", in_parans)
m = re.match(r"\[(\S*)\]", in_parens)
if m is not None:
# this is a list of items
arg_list_str = m.group(1)
else:
# what we have in parantheses has to
# what we have in parentheses has to
# be a simple list of arguments
arg_list_str = in_parans
arg_list_str = in_parens
arg_list = arg_list_str.split(",")
for arg_str in arg_list:
# add to arg_tuples
arg_tuples.append((arg_str, None))
arg_tuples.append((arg_str, None)) # Why is this None?
ablock.add_action(atype, arg_tuples)
else:
ablock.add_action(atype, [])
Expand Down
27 changes: 24 additions & 3 deletions bionetgen/modelapi/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class Action(ModelObj):
(argument,value) pair list for the action
"""

def __init__(self, action_type=None, action_args=[]) -> None:
def __init__(self, action_type=None, action_args={}) -> None:
super().__init__()
AList = ActionList()
self.normal_types = AList.normal_types
Expand All @@ -327,8 +327,29 @@ def __init__(self, action_type=None, action_args=[]) -> None:
self.args = action_args
# check type
if self.type not in self.possible_types:
print("Action type not recognized!")
raise RuntimeError
raise RuntimeError(f"Action type {self.type} not recognized!")
seen_args = []
for arg in action_args:
arg_name, arg_value = arg
valid_arg_list = AList.arg_dict[self.type]
# TODO: actions that don't take argument names should be parsed separately to check validity of arg-val tuples
# TODO: currently not type checking arguments
if valid_arg_list is None:
raise RuntimeError(
f"Argument {arg_name} is given, but action {self.type} does not take arguments"
)
if len(valid_arg_list) > 0:
if arg_name not in AList.arg_dict[self.type]:
raise RuntimeError(
f"Action argument {arg_name} not recognized!\nCheck to make sure action is correctly formatted"
)
# TODO: If arg_value is the correct type
if arg_name in seen_args:
print(
f"Warning: argument {arg_name} already given, using latter value {arg_value}"
)
else:
seen_args.append(arg_name)

def gen_string(self) -> str:
# TODO: figure out every argument that has special
Expand Down
Loading

0 comments on commit ddd3066

Please sign in to comment.