Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: assign unique ids to identified patterns #426

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions discopop_explorer/discopop_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def run(arguments: ExplorerArguments):
# create explorer directory if not already present
if not os.path.exists(os.path.join(arguments.project_path, "explorer")):
os.mkdir(os.path.join(arguments.project_path, "explorer"))
# create file to store next free pattern ids if not already present
if not os.path.exists("next_free_pattern_id.txt"):
with open("next_free_pattern_id.txt", "w") as f:
f.write(str(0))

if arguments.enable_profiling_dump_file is not None:
profile = cProfile.Profile()
Expand Down
15 changes: 15 additions & 0 deletions discopop_explorer/pattern_detectors/PatternInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# the 3-Clause BSD License. See the LICENSE file in the package base
# directory for details.
import json
import os
from typing import Optional

from ..PETGraphX import LoopNode, Node, NodeID, LineID, PETGraphX
Expand All @@ -15,6 +16,7 @@
class PatternInfo(object):
"""Base class for pattern detection info"""

pattern_id: int
_node: Node
node_id: NodeID
start_line: LineID
Expand All @@ -31,6 +33,19 @@ def __init__(self, node: Node):
"""
:param node: node, where pipeline was detected
"""
# use blocking file i/o to synchronize threads
with open(os.path.join(os.getcwd(), "next_free_pattern_id.txt"), "r+") as f:
lines = f.readlines()
f.truncate(0)
f.seek(0)
if len(lines) == 0:
self.pattern_id = 0
f.write(str(0))
else:
for line in lines:
line = line.replace("\n", "").replace("\x00", "")
self.pattern_id = int(line)
f.write(str(self.pattern_id + 1))
self._node = node
self.node_id = node.id
self.start_line = node.start_position()
Expand Down