Skip to content

Commit

Permalink
fix insert parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketa Opichalova committed Jan 24, 2024
1 parent 32cbbcd commit 490c4a2
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions eBCSgen/Parsing/ParseBCSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from eBCSgen.TS.Edge import edge_from_dict
from eBCSgen.Core.Side import Side
from eBCSgen.Core.Model import Model
from eBCSgen.Errors.ComplexParsingError import ComplexParsingError


def load_TS_from_json(json_file: str) -> TransitionSystem:
Expand Down Expand Up @@ -335,9 +336,14 @@ def insert_atomic_to_struct(self, atomic, struct):
Adds or replaces atomic subtree in struct tree.
"""
if len(struct.children) == 2:
for i in range(len(struct.children[1].children)):
if self.get_name(atomic) == self.get_name(
struct.children[1].children[i]
):
raise ComplexParsingError("Matching agent was not found", struct)
struct.children[1].children.append(atomic)
else:
struct.children.append(Tree('composition', [atomic]))
struct.children.append(Tree("composition", [atomic]))
return struct

def insert_struct_to_complex(self, struct, complex):
Expand All @@ -346,19 +352,42 @@ def insert_struct_to_complex(self, struct, complex):
"""
for i in range(len(complex.children)):
if self.get_name(struct) == self.get_name(complex.children[i].children[0]):
complex.children[i] = Tree('agent', [struct])
break
return complex
struct_found = True
for j in range(len(struct.children[1].children)):
for k in range(
len(complex.children[i].children[0].children[1].children)
):
if self.get_name(
struct.children[1].children[j]
) == self.get_name(
complex.children[i].children[0].children[1].children[k]
):
struct_found = False
break
if not struct_found:
break

if struct_found:
if self.is_empty(complex.children[i]):
complex.children[i] = Tree("agent", [struct])
else:
complex.children[i].children[0].children[
1
].children += struct.children[1].children
return complex

raise ComplexParsingError("Matching agent was not found", complex)

def insert_atomic_to_complex(self, atomic, complex):
"""
Adds or replaces atomic subtree in complex tree.
"""
for i in range(len(complex.children)):
if self.get_name(atomic) == self.get_name(complex.children[i].children[0]):
complex.children[i] = Tree('agent', [atomic])
break
return complex
if self.is_empty(complex.children[i].children[0]):
complex.children[i] = Tree("agent", [atomic])
return complex
raise ComplexParsingError("Matching agent was not found", complex)

def get_name(self, agent):
return str(agent.children[0].children[0])
Expand All @@ -370,7 +399,17 @@ def complex(self, matches):
result += match.children
else:
result.append(match)
return Tree('complex', [Tree('value', result)] + matches[1:])
return Tree("complex", [Tree("value", result)] + matches[1:])

def is_empty(self, agent):
"""
Checks if the agent is empty.
"""
if agent.data == "atomic":
return agent.children[1].children[0] == "_"
elif agent.data == "agent":
return len(agent.children[0].children[1].children) == 0
return False


class TreeToComplex(Transformer):
Expand Down

0 comments on commit 490c4a2

Please sign in to comment.