Skip to content

Commit

Permalink
feat: add inner processes & multiple file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pheetah committed Jan 16, 2024
1 parent 3a8f671 commit c5ef510
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 21 deletions.
47 changes: 44 additions & 3 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from dataclasses import dataclass
from parser.doctree_parser import Parser

from code_tokenize.tokens import TokenSequence
from pygraphviz import AGraph

from clusterer import Cluster
from compose import Composer
from logs import log


@dataclass
class FileFormat:
input_path: str
output_path: str


# Façade
Expand All @@ -11,6 +23,35 @@ def __init__(
self._parser = parser
self._composer = composer

def create_epc(self, input_file_path: str, output_path: str):
parsed = self._parser.parse(input_file_path)
self._composer.compose(export_to=output_path, parsed=parsed)
def _compose_and_draw(self, pygraph: AGraph, flow: TokenSequence):
# act: compose diagram
diagram = self._composer.compose(parsed=flow)

# act: draw diagram
current_node = diagram.head
while current_node:
current_node.draw_line(pygraph)
current_node = current_node.next

return diagram

def draw_epc(self, file_format: list[FileFormat]):
cluster = Cluster()
cluster.extract_flows(file_name_list=[file.input_path for file in file_format])

for index, process in enumerate(cluster._main_flows):
G = AGraph(directed=True)
current_main_process = self._compose_and_draw(pygraph=G, flow=process)
inner_flows = [
flow
for flow in cluster._inner_flows
if flow.name in current_main_process.inner_flow_names
]
for inner_flow in inner_flows:
self._compose_and_draw(pygraph=G, flow=inner_flow.tokens)

file = file_format[index]
G.layout()
G.draw(file.output_path, prog="dot")

log.info("Done.")
65 changes: 65 additions & 0 deletions clusterer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import re
from copy import deepcopy
from parser.doctree_parser import Parser

from settings.language import ClusterKeywords


class Flow:
tokens: list
name: str

def __init__(self) -> None:
self.tokens = []


class Flows:
processed_indexes = {}
latest_flow: Flow = None


class Cluster:
_inner_flows: list[Flow] = []
_main_flows: list[str] = []

def __init__(self) -> None:
pass

def _find_inner_diagrams(self, token: str, index: int, flows: Flows):
if ClusterKeywords.CLUSTER in token:
flow = deepcopy(Flow())
flow.name = (
re.search(rf"(?<={ClusterKeywords.CLUSTER})(.*?)$", token)[0]
.lstrip()
.rstrip()
)
flows.latest_flow = deepcopy(flow)
flows.processed_indexes.update({"start_index": index})

elif ClusterKeywords.END_CLUSTER in token:
self._inner_flows.append(deepcopy(flows.latest_flow))
flows.processed_indexes.update({"end_index": index})
flows.latest_flow = None
return True

if flows.latest_flow is not None:
flows.latest_flow.tokens.append(token)

def extract_flows(self, file_name_list: list[str]):
for file in file_name_list:
parser = Parser()
parsed = parser.parse(file)
flows = Flows()

for index, token_raw in enumerate(parsed):
indexes_processed = self._find_inner_diagrams(
str(token_raw), index, flows
)

if indexes_processed:
start_index = flows.processed_indexes["start_index"]
end_index = flows.processed_indexes["end_index"] + 1

del parsed[start_index:end_index]

self._main_flows.append(parsed)
35 changes: 18 additions & 17 deletions compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from uuid import uuid4

from code_tokenize.tokens import TokenSequence
from pygraphviz import AGraph

from doctree import ActivityNode, EpcDiagram, EpcNode, EventNode, IfNode
from logs import log
from settings.language import NODE_KEYWORDS, SYMBOLS, ContextKeywords, Keywords

G = AGraph(directed=True)
from doctree import ActivityNode, EpcDiagram, EpcNode, EventNode, IfNode, ProcessNode
from settings.language import (
NODE_KEYWORDS,
SYMBOLS,
ClusterKeywords,
ContextKeywords,
Keywords,
)


class DiagramNodeAdder:
Expand Down Expand Up @@ -74,7 +76,15 @@ def _handle_event(self, token: str, diagram: EpcDiagram) -> EpcNode:
raw_action = self._get_after(token, Keywords.EVENT)
diagram.push(EventNode(description=raw_action))

def _handle_inner_flow(self, token: str, diagram: EpcDiagram) -> EpcNode:
if ClusterKeywords.INNER_FLOW in token:
raw_name = self._get_after(token, ClusterKeywords.INNER_FLOW)
diagram.inner_flow_names.append(str(raw_name))
diagram.push(ProcessNode(description=raw_name))

def _handle_flow(self, token: str, diagram: EpcDiagram):
self._handle_inner_flow(token=token, diagram=diagram)

flows = self._split_flow(token=token)
for flow in flows:
self._handle_activity(token=flow, diagram=diagram)
Expand Down Expand Up @@ -165,15 +175,6 @@ def _struct(self, parsed: TokenSequence) -> EpcDiagram:

return diagram

def compose(self, export_to: str, parsed: TokenSequence):
def compose(self, parsed: TokenSequence):
diagram = self._struct(parsed=parsed)
current_node = diagram.head

while current_node:
current_node.draw_line(G)
current_node = current_node.next

G.layout()
G.draw(export_to, prog="dot")

log.info("Done.")
return diagram
19 changes: 18 additions & 1 deletion doctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ def add_node(self, pygraph: pgv.AGraph, description: str):
)


class ProcessNode(EpcNode):
def add_node(self, pygraph: pgv.AGraph, description: str):
pygraph.add_node(
description,
color="darkblue",
shape="polygon",
fontcolor="white",
style="filled",
fontsize=16,
width=4,
group="1",
)


class IfNode(EpcNode):
branches: list[EpcNode]

Expand Down Expand Up @@ -214,12 +228,15 @@ def draw_line(self, pygraph: pgv.AGraph, end_id: str = None):
class EpcDiagram:
head: EpcNode
tail: EpcNode
inner_flow_names: list[str]
length: int

def __init__(self) -> None:
def __init__(self, name: str = "EPC") -> None:
self.name = name
self.head = None
self.tail = None
self.length = 0
self.inner_flow_names = []

def add(self, node: EpcNode):
self.head = node
Expand Down
6 changes: 6 additions & 0 deletions settings/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class ContextKeywords(StringEnum):
API_CALL_IN = "<-"


class ClusterKeywords(StringEnum):
CLUSTER = "inner-diagram:"
END_CLUSTER = "end-diagram:"
INNER_FLOW = "inner-flow:"


NODE_KEYWORDS = [
Keywords.ACTIVITY,
Keywords.EVENT,
Expand Down

0 comments on commit c5ef510

Please sign in to comment.