From 66c87a1104cdd86f8704019f01407ede4d3c8c35 Mon Sep 17 00:00:00 2001 From: Ayushi Amin Date: Tue, 14 Jan 2025 21:58:07 -0500 Subject: [PATCH 1/5] Started long element chain mis --- docs/Design/SoftDetailedDes/MIS.tex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index b09df2c1..9bd8d4df 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -227,8 +227,10 @@ \subsubsection{Local Functions} explicitly. Even if they are implemented, they are not exported; they only have local scope.} + \newpage + \bibliographystyle {plainnat} \bibliography {../../../refs/References} From 9c89976943a8b9a94be55279c33bb2884250d007 Mon Sep 17 00:00:00 2001 From: Ayushi Amin Date: Wed, 15 Jan 2025 00:19:31 -0500 Subject: [PATCH 2/5] Added #307: MIS of long element chain --- docs/Design/SoftDetailedDes/MIS.tex | 92 +++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index 9bd8d4df..005d942b 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -227,6 +227,98 @@ \subsubsection{Local Functions} explicitly. Even if they are implemented, they are not exported; they only have local scope.} + +\section{MIS of LongElementChainRefactorer} + +\subsection{Module} + +LongElementChainRefactorer is a module that refactors long element chains, specifically focusing on flattening nested dictionaries to improve readability, maintainability, and energy efficiency. The module uses a recursive flattening strategy while caching previously seen patterns for optimization. + +\subsection{Uses} + +Refactor nested dictionaries to flatten their structure and reduce code complexity, improving energy efficiency and maintainability. + +\subsection{Syntax} + +\subsubsection{Exported Constants} +None + +\subsubsection{Exported Access Programs} + +\begin{center} +\begin{tabular}{|p{3cm}|p{5cm}|p{2cm}|p{3cm}|} +\hline +\textbf{Name} & \textbf{In} & \textbf{Out} & \textbf{Exceptions} \\ +\hline +\texttt{\_\_init\_\_} & \texttt{output\_dir: Path} & None & None \\ +\hline +\texttt{refactor} & \texttt{file\_path: Path, pylint\_smell: Smell, initial\_emissions: float} & None & Logging exceptions \\ +\hline +\end{tabular} +\end{center} + +\subsection{Semantics} + +\subsubsection{State Variables} + +\begin{itemize} + \item \textbf{\_reference\_map}: Maps element chain references to their line numbers and corresponding values. +\end{itemize} + +\subsubsection{Environment Variables} + +\begin{itemize} + \item \textbf{File system}: Used to read, write, and store temporary and refactored files. + \item \textbf{Logger}: Logs information during the refactoring process. +\end{itemize} + +\subsubsection{Assumptions} + +\begin{itemize} + \item Input files are valid Python scripts. + \item Smells identified by \textbf{pylint\_smell} include valid line numbers. + \item Refactored code must pass the provided test suite. +\end{itemize} + +\subsubsection{Access Routine Semantics} + +\paragraph{\texttt{\_\_init\_\_(output\_dir: Path)}} +\begin{itemize} +\item \textbf{Transition}: Initializes the refactorer with the specified output directory and sets up internal caching structures. +\item \textbf{Output}: None. +\item \textbf{Exception}: None. +\end{itemize} + +\paragraph{\texttt{refactor(file\_path: Path, pylint\_smell: Smell, initial\_emissions: float)}} +\begin{itemize} + \item \textbf{Transition}: + \begin{itemize} + \item Reads the file at \texttt{file\_path}. + \item Identifies nested dictionary chains for flattening. + \item Refactors the identified chain by flattening the dictionary and replacing its occurrences. + \item Writes the refactored code to a temporary file. + \end{itemize} + \item \textbf{Output}: None. Refactored file is saved if improvements are validated. + \item \textbf{Exception}: Logs exceptions during file operations or refactoring. +\end{itemize} + +\subsubsection{Local Functions} +\begin{itemize} + \item \textbf{\_flatten\_dict(d: dict[str, Any], parent\_key: str = "")} \\ + Recursively flattens a nested dictionary by combining keys with underscores. + + \item \textbf{\_extract\_dict\_literal(node: ast.AST)} \\ + Converts an Abstract Syntax Tree (AST) dictionary literal into a Python dictionary. + + \item \textbf{\_find\_dict\_assignments(tree: ast.AST, name: str)} \\ + Extracts dictionary assignments given the name of the dictionary from the AST and returns them as a dictionary. + + \item \textbf{\_collect\_dict\_references(tree: ast.AST)} \\ + Identifies and stores all dictionary access patterns in the `\_reference\_map`. + + \item \textbf{\_generate\_flattened\_access(base\_var: str, access\_chain: list[str])} \\ + Generates a flattened dictionary key string by combining elements of an access chain with underscores. +\end{itemize} \newpage From 86203f8a92c1089df4d7b0762385d8a2cd9e3083 Mon Sep 17 00:00:00 2001 From: Ayushi Amin <66652121+Ayushi1972@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:14:22 -0500 Subject: [PATCH 3/5] Update docs/Design/SoftDetailedDes/MIS.tex Co-authored-by: Sevhena Walker <83547364+Sevhena@users.noreply.github.com> --- docs/Design/SoftDetailedDes/MIS.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index 005d942b..75028496 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -297,7 +297,7 @@ \subsubsection{Access Routine Semantics} \item Identifies nested dictionary chains for flattening. \item Refactors the identified chain by flattening the dictionary and replacing its occurrences. \item Writes the refactored code to a temporary file. - \end{itemize} +\end{itemize} \item \textbf{Output}: None. Refactored file is saved if improvements are validated. \item \textbf{Exception}: Logs exceptions during file operations or refactoring. \end{itemize} From 4bfbf9ebe0fe64ce610c44d218cecd5c942ffff8 Mon Sep 17 00:00:00 2001 From: Ayushi Amin Date: Thu, 16 Jan 2025 16:43:44 -0500 Subject: [PATCH 4/5] Added LECR module to MG --- docs/Design/SoftArchitecture/MG.tex | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/Design/SoftArchitecture/MG.tex b/docs/Design/SoftArchitecture/MG.tex index 86f8d76f..5a61c01e 100644 --- a/docs/Design/SoftArchitecture/MG.tex +++ b/docs/Design/SoftArchitecture/MG.tex @@ -192,7 +192,7 @@ \section{Module Hierarchy} \label{SecMH} \begin{description} \item [\refstepcounter{mnum} \mthemnum \label{mHH}:] Hardware-Hiding Module -\item ... +\item [\refstepcounter{mnum} \mthemnum \label{mLEC}:] LongElementChainRefactorer Module \end{description} @@ -209,7 +209,7 @@ \section{Module Hierarchy} \label{SecMH} \multirow{7}{0.3\textwidth}{Behaviour-Hiding Module} & ?\\ & ?\\ & ?\\ -& ?\\ +& LongElementChainRefactorer Module\\ & ?\\ & ?\\ & ?\\ @@ -278,6 +278,21 @@ \subsection{Behaviour-Hiding Module} \item[Implemented By:] -- \end{description} +\subsubsection{Long Element Chain Module (\mref{mLEC})} + +% [Record, Library, Abstract Object, or Abstract Data Type] + +\begin{description} + \item[Secrets:] How to parse a given code file to its AST representation, traverse the + AST tree to identify dictionary assignments, analyze the structure of nested dictionaries, + and flatten them. Additionally, it identifies all access calls associated with these dictionaries + in the source code and determines how to update them to reflect the new flattened structure. + \item[Services:] Detects nested dictionaries in the source code using AST parsing, simplifies their + structure by flattening them, and updates all associated access calls throughout the file. This improves + code readability, reduces complexity, and ensures correctness while maintaining the program's intended behavior. + \item[Implemented By:] EcoOptimizer +\end{description} + \subsubsection{Input Format Module (\mref{mInput})} \begin{description} From 8d34bd38717d35a767cfdf382ad21e1617e1940d Mon Sep 17 00:00:00 2001 From: Ayushi Amin <66652121+Ayushi1972@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:57:28 -0500 Subject: [PATCH 5/5] Implemented feedback Co-authored-by: Sevhena Walker <83547364+Sevhena@users.noreply.github.com> --- docs/Design/SoftDetailedDes/MIS.tex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/Design/SoftDetailedDes/MIS.tex b/docs/Design/SoftDetailedDes/MIS.tex index 75028496..665e8524 100644 --- a/docs/Design/SoftDetailedDes/MIS.tex +++ b/docs/Design/SoftDetailedDes/MIS.tex @@ -236,7 +236,10 @@ \subsection{Module} \subsection{Uses} -Refactor nested dictionaries to flatten their structure and reduce code complexity, improving energy efficiency and maintainability. +\begin{itemize} + \item Uses \texttt{Smell} interface for data access + \item Inherits from \texttt{BaseRefactorer} +\end{itemize} \subsection{Syntax}