Skip to content

Commit d571066

Browse files
committed
Add functionality for appending unique IDs to sequence statements
1 parent a9e87aa commit d571066

9 files changed

Lines changed: 36 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ There are also certain options when extracting the snippets provided as paramete
3333
- <code>keepBranches</code>: denotes if all branch paths should be kept, or only the first path of each branch.
3434
- <code>outputTree</code>: denotes if the output should be a tree, or a sequence.
3535
- <code>flattenOutput</code>: denotes if the output should be flattened, i.e. all paths to be merged in a single sequence, or different paths should be retained.
36+
- <code>addUniqueIDs</code>: denotes if statements should have unique IDs, default is false.
3637

3738

3839
Using in Python

src/outputhelpers/FlattenedSequencePrinter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public class FlattenedSequencePrinter implements SnippetPrinter {
1818
* functions, this function would return [A, B, C, D, E].
1919
*
2020
* @param snippet the snippet to be parsed.
21+
* @param addUniqueIDs boolean denoting whether statements should have IDs ({@code true}) or not ({@code false}).
2122
* @return a flattened tree representation for the snippet.
2223
*/
2324
@Override
24-
public String snippetToString(Snippet snippet) {
25+
public String snippetToString(Snippet snippet, boolean addUniqueIDs) {
26+
int id = 0;
2527
String seq = "[";
2628
for (LevelOrderPair orderAndLevel : snippet.blocks.keySet()) {
2729
for (Block block : snippet.blocks.get(orderAndLevel)) {
@@ -39,7 +41,8 @@ public String snippetToString(Snippet snippet) {
3941
|| statement.toString().equals("END_CONDITION")
4042
|| statement.toString().equals("END_CASE") || statement.toString().equals("END_TRY")
4143
|| statement.toString().equals("END_LOOP")))
42-
seq += (seq.charAt(seq.length() - 1) == '[' ? "" : ", ") + statement.toString();
44+
seq += (seq.charAt(seq.length() - 1) == '[' ? "" : ", ") + statement.toString()
45+
+ (addUniqueIDs ? "#" + (++id) : "");
4346
}
4447
}
4548
}

src/outputhelpers/FlattenedTreePrinter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public class FlattenedTreePrinter implements SnippetPrinter {
1515
* [[A, B, D], [A, B, E], [A, C, D], [A, C, E]].
1616
*
1717
* @param snippet the snippet to be parsed.
18+
* @param addUniqueIDs boolean denoting whether statements should have IDs ({@code true}) or not ({@code false}).
1819
* @return a flattened tree representation for the snippet.
1920
*/
2021
@Override
21-
public String snippetToString(Snippet snippet) {
22+
public String snippetToString(Snippet snippet, boolean addUniqueIDs) {
2223
throw new UnsupportedOperationException("Not implemented yet");
2324
}
2425

src/outputhelpers/SequencePrinter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public class SequencePrinter implements SnippetPrinter {
1717
* An example of a sequence would be [[A, B, C], [D, E]], where [A, B, C] and [D, E] are sequences of two functions.
1818
*
1919
* @param snippet the snippet to be parsed.
20+
* @param addUniqueIDs boolean denoting whether statements should have IDs ({@code true}) or not ({@code false}).
2021
* @return a tree representation for the snippet.
2122
*/
2223
@Override
23-
public String snippetToString(Snippet snippet) {
24+
public String snippetToString(Snippet snippet, boolean addUniqueIDs) {
25+
int id = 0;
2426
String seq = "[";
2527
for (LevelOrderPair orderAndLevel : snippet.blocks.keySet()) {
2628
for (Block block : snippet.blocks.get(orderAndLevel)) {
@@ -43,7 +45,8 @@ else if (!(statement.toString().equals("START_METHOD")
4345
|| statement.toString().equals("END_CONDITION")
4446
|| statement.toString().equals("END_CASE") || statement.toString().equals("END_TRY")
4547
|| statement.toString().equals("END_LOOP")))
46-
seq += (seq.charAt(seq.length() - 1) == '[' ? "" : ", ") + statement.toString();
48+
seq += (seq.charAt(seq.length() - 1) == '[' ? "" : ", ") + statement.toString()
49+
+ (addUniqueIDs ? "#" + (++id) : "");
4750
}
4851
}
4952
}

src/outputhelpers/SnippetPrinter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public interface SnippetPrinter {
1313
* Receives a snippet and returns a string representation.
1414
*
1515
* @param snippet the snippet to be parsed.
16+
* @param addUniqueIDs boolean denoting whether statements should have IDs ({@code true}) or not ({@code false}).
1617
* @return a string representation for the snippet.
1718
*/
18-
public String snippetToString(Snippet snippet);
19+
public String snippetToString(Snippet snippet, boolean addUniqueIDs);
1920

2021
}

src/outputhelpers/TreePrinter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public class TreePrinter implements SnippetPrinter {
1717
* An example of the snippet tree would be [[A, [B, C], [D, E]]].
1818
*
1919
* @param snippet the snippet to be parsed.
20+
* @param addUniqueIDs boolean denoting whether statements should have IDs ({@code true}) or not ({@code false}).
2021
* @return a tree representation for the snippet.
2122
*/
2223
@Override
23-
public String snippetToString(Snippet snippet) {
24+
public String snippetToString(Snippet snippet, boolean addUniqueIDs) {
25+
int id = 0;
2426
String treeseq = "[";
2527
for (LevelOrderPair orderAndLevel : snippet.blocks.keySet()) {
2628
for (Block block : snippet.blocks.get(orderAndLevel)) {
@@ -49,7 +51,8 @@ else if (statement.toString().equals("END_CONDITION") || statement.toString().eq
4951
else if (statement.toString().equals("END_TRY"))
5052
treeseq += "";
5153
else
52-
treeseq += (treeseq.charAt(treeseq.length() - 1) == '[' ? "" : ", ") + statement.toString();
54+
treeseq += (treeseq.charAt(treeseq.length() - 1) == '[' ? "" : ", ") + statement.toString()
55+
+ (addUniqueIDs ? "#" + (++id) : "");
5356
}
5457
}
5558
}

src/sequenceextractor/PythonBinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void main(String[] args) {
2626
boolean keepBranches = args.length > 2 ? Boolean.parseBoolean(args[2]) : true;
2727
boolean outputTree = args.length > 3 ? Boolean.parseBoolean(args[3]) : false;
2828
boolean flattenOutput = args.length > 4 ? Boolean.parseBoolean(args[4]) : true;
29+
boolean addUniqueIDs = args.length > 5 ? Boolean.parseBoolean(args[5]) : false;
2930
Scanner scanner = new Scanner(System.in);
3031
while (scanner.hasNextLine()) {
3132
// Receive message and decode it
@@ -50,7 +51,7 @@ public static void main(String[] args) {
5051
break;
5152
} else {
5253
messageresult = SequenceExtractor.extractSequence(message, keepFunctionCallTypes, keepLiterals,
53-
keepBranches, outputTree, flattenOutput).toString();
54+
keepBranches, outputTree, flattenOutput, addUniqueIDs).toString();
5455
String b64messageresult = DatatypeConverter
5556
.printBase64Binary(messageresult.getBytes(Charset.forName("US-ASCII")));
5657
System.out.println(b64messageresult);

src/sequenceextractor/SequenceExtractor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ protected static String getASTofSnippet(String snippet) {
452452
* @return the snippet as a list of statements.
453453
*/
454454
public static String extractSequence(String snippet) {
455-
return extractSequence(snippet, false, false, true, false, true);
455+
return extractSequence(snippet, false, false, true, false, true, false);
456456
}
457457

458458
/**
@@ -467,7 +467,7 @@ public static String extractSequence(String snippet) {
467467
* @return the snippet as a list of statements.
468468
*/
469469
public static String extractSequence(String snippet, boolean keepFunctionCallTypes, boolean keepLiterals,
470-
boolean keepBranches, boolean outputTree, boolean flattenOutput) {
470+
boolean keepBranches, boolean outputTree, boolean flattenOutput, boolean addUniqueIDs) {
471471
String ast = getASTofSnippet(snippet);
472472
Snippet seq = createSequence(ast, keepFunctionCallTypes, keepLiterals, keepBranches);
473473
SnippetPrinter printer;
@@ -482,7 +482,7 @@ public static String extractSequence(String snippet, boolean keepFunctionCallTyp
482482
else
483483
printer = new FlattenedSequencePrinter();
484484
}
485-
return printer.snippetToString(seq);
485+
return printer.snippetToString(seq, addUniqueIDs);
486486
}
487487

488488
}

srcpy/sequenceextractor.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class _SequenceExtractor(object):
88
and opening pipes to the standard input and standard output so that messages can be sent and received.
99
Instead of using this class, it is highly recommended to use the abstracted SequenceExtractor class.
1010
"""
11-
def __init__(self, path_to_SequenceExtractor_jar, keep_function_call_types=False, keep_literals=False, keep_branches=True, output_tree=False, flatten_output=True):
11+
def __init__(self, path_to_SequenceExtractor_jar, keep_function_call_types=False, keep_literals=False, keep_branches=True, output_tree=False, flatten_output=True, add_unique_ids=False):
1212
"""
1313
Initializes this inner extractor.
1414
@@ -17,11 +17,13 @@ def __init__(self, path_to_SequenceExtractor_jar, keep_function_call_types=False
1717
:param keep_literals: boolean denoting whether literals (primitives) should be retained.
1818
:param keep_branches: boolean denoting whether all branches should be kept.
1919
:param output_tree: boolean denoting whether the output should be a tree or a sequence.
20-
:param flatten_output: boolean denoting whether the output should be flattened-.
20+
:param flatten_output: boolean denoting whether the output should be flattened.
21+
:param add_unique_ids: boolean denoting whether statements should have IDs.
2122
"""
22-
self.cmd = ['java', '-cp', path_to_SequenceExtractor_jar, 'sequenceextractor.PythonBinder', 'true' if keep_function_call_types else 'false',
23-
'true' if keep_literals else 'false', 'true' if keep_branches else 'false',
24-
'true' if output_tree else 'false', 'true' if flatten_output else 'false']
23+
self.cmd = ['java', '-cp', path_to_SequenceExtractor_jar, 'sequenceextractor.PythonBinder',
24+
'true' if keep_function_call_types else 'false', 'true' if keep_literals else 'false',
25+
'true' if keep_branches else 'false', 'true' if output_tree else 'false',
26+
'true' if flatten_output else 'false', 'true' if add_unique_ids else 'false']
2527
self.proc = subprocess.Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
2628
self.nummessages = 0
2729
line = self.send_message("START_OF_TRANSMISSION")
@@ -80,7 +82,7 @@ class SequenceExtractor(_SequenceExtractor):
8082
"""
8183
Class used as a python binding to the SequenceExtractor library. It contains functions for parsing java snippets to sequences.
8284
"""
83-
def __init__(self, path_to_SequenceExtractor_jar, keep_function_call_types=False, keep_literals=False, keep_branches=True, output_tree=False, flatten_output=True):
85+
def __init__(self, path_to_SequenceExtractor_jar, keep_function_call_types=False, keep_literals=False, keep_branches=True, output_tree=False, flatten_output=True, add_unique_ids=False):
8486
"""
8587
Initializes this Sequence Extractor.
8688
@@ -89,9 +91,10 @@ def __init__(self, path_to_SequenceExtractor_jar, keep_function_call_types=False
8991
:param keep_literals: boolean denoting whether literals (primitives) should be retained.
9092
:param keep_branches: boolean denoting whether all branches should be kept.
9193
:param output_tree: boolean denoting whether the output should be a tree or a sequence.
92-
:param flatten_output: boolean denoting whether the output should be flattened-.
94+
:param flatten_output: boolean denoting whether the output should be flattened.
95+
:param add_unique_ids: boolean denoting whether statements should have IDs.
9396
"""
94-
super(SequenceExtractor, self).__init__(path_to_SequenceExtractor_jar, keep_function_call_types, keep_literals, keep_branches, output_tree, flatten_output)
97+
super(SequenceExtractor, self).__init__(path_to_SequenceExtractor_jar, keep_function_call_types, keep_literals, keep_branches, output_tree, flatten_output, add_unique_ids)
9598

9699
def parse_snippet(self, snippet_contents):
97100
"""

0 commit comments

Comments
 (0)