Skip to content

Commit

Permalink
feat: add pattern id to each pattern. Threadsafe by relying on blocki…
Browse files Browse the repository at this point in the history
…ng i/o
  • Loading branch information
lukasrothenberger committed Oct 25, 2023
1 parent 47524d8 commit a53900b
Showing 1 changed file with 15 additions and 0 deletions.
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

0 comments on commit a53900b

Please sign in to comment.