11package org .variantsync .diffdetective .experiments .thesis_es ;
22
33import java .io .IOException ;
4- import java .nio .file .Files ;
5- import java .nio .file .Path ;
64import org .eclipse .jgit .diff .DiffAlgorithm .SupportedAlgorithm ;
75import org .variantsync .diffdetective .analysis .Analysis ;
86import org .variantsync .diffdetective .diff .git .PatchDiff ;
1715import org .variantsync .diffdetective .variation .diff .construction .JGitDiff ;
1816import org .variantsync .diffdetective .variation .diff .parse .VariationDiffParseOptions ;
1917import org .variantsync .diffdetective .variation .tree .VariationTree ;
18+ import org .variantsync .diffdetective .variation .tree .source .VariationTreeSource ;
2019
2120public class UnparseAnalysis implements Analysis .Hooks {
2221
2322 public static final String CSV_EXTENSION = ".thesis_es.csv" ;
2423
24+ public static int count = 0 ;
25+
2526 private StringBuilder csv ;
2627
2728 @ Override
@@ -38,37 +39,50 @@ public boolean analyzeVariationDiff(Analysis analysis) throws Exception {
3839 String textDiff = patch .getDiff ();
3940 String codeBefore = "" ;
4041 String codeAfter = "" ;
42+ /*
43+ if (count < 10) {
44+ Path pathTreeBefore = Paths.get("results", "thesis_es", "treeBefore" + count + ".txt");
45+ Path pathTreeAfter = Paths.get("results", "thesis_es", "treeAfter" + count + ".txt");
46+ Path pathDiff = Paths.get("results", "thesis_es", "diff" + count + ".txt");
47+ Files.writeString(pathTreeBefore, codeBefore);
48+ Files.writeString(pathTreeAfter, codeAfter);
49+ Files.writeString(pathDiff, textDiff);
50+ count++;
51+ }
52+ */
53+
4154 codeBefore = VariationUnparser .undiff (patch .getDiff (), Time .BEFORE );
4255 codeAfter = VariationUnparser .undiff (patch .getDiff (), Time .AFTER );
43- codeBefore = codeBefore .replaceAll ("\\ r\\ n" , "\n " );
44- codeAfter = codeAfter .replaceAll ("\\ r\\ n" , "\n " );
56+ // codeBefore = codeBefore.replaceAll("\\r\\n", "\n");
57+ // codeAfter = codeAfter.replaceAll("\\r\\n", "\n");
4558 boolean [][] diffTestAll = runTestsDiff (textDiff );
46- boolean [] treeBeforeTest = runTestsTree (codeBefore , Path . of ( patch . getFileName ( Time . BEFORE )) );
47- boolean [] treeAfterTest = runTestsTree (codeAfter , Path . of ( patch . getFileName ( Time . AFTER )) );
59+ boolean [] treeBeforeTest = runTestsTree (codeBefore );
60+ boolean [] treeAfterTest = runTestsTree (codeAfter );
4861 boolean [] dataTests = runDataTest (textDiff , codeBefore , codeAfter );
4962 int error = 1 ;
50- String [] errorSave = new String [] { null , null , null };
51- if (!(boolOr (diffTestAll [0 ]) || ( boolOr (diffTestAll [1 ]) && boolOr ( diffTestAll [ 2 ]) ))) {
63+ String [] errorSave = new String [] { "" , "" , "" };
64+ if (!(boolOr (diffTestAll [0 ]) || boolOr (diffTestAll [1 ]))) {
5265 error = error * 2 ;
53- errorSave [0 ] = textDiff ;
66+ // errorSave[0] = ;
5467 }
5568 if (!boolOr (treeBeforeTest )) {
5669 error = error * 3 ;
5770 errorSave [1 ] = codeBefore ;
5871 }
5972 if (!boolOr (treeAfterTest )) {
6073 error = error * 5 ;
61- errorSave [2 ] = codeAfter ;
74+ // errorSave[2] = codeAfter;
6275 }
76+ /*
6377 if (!boolOr(dataTests)) {
6478 error = error * 7;
6579 }
80+ */
6681
6782 final UnparseEvaluation ue = new UnparseEvaluation (
6883 boolToInt (dataTests ),
6984 boolToInt (diffTestAll [0 ]),
7085 boolToInt (diffTestAll [1 ]),
71- boolToInt (diffTestAll [2 ]),
7286 boolToInt (treeBeforeTest ),
7387 boolToInt (treeAfterTest ),
7488 error ,
@@ -84,14 +98,36 @@ public void endBatch(Analysis analysis) throws IOException {
8498 csv .toString ());
8599 }
86100
101+ public static String removeWhitespace1 (String string ) {
102+ string = string .replaceAll ("\n (\\ s*\n )+" , "\n " );
103+ string = string .replaceAll ("\n \\ s+" , "\n " );
104+ if (!string .isEmpty ()) {
105+ if (string .charAt (string .length () - 1 ) == '\n' ) {
106+ return string .substring (0 , string .length () - 1 );
107+ }
108+ }
109+ return string ;
110+ }
111+
87112 public static String removeWhitespace (String string ) {
88- return string .replaceAll ("\\ s+" , "" );
113+ if (string .isEmpty ()) {
114+ return "" ;
115+ } else {
116+ StringBuilder result = new StringBuilder ();
117+ for (String line : string .split ("\n " )) {
118+ if (!line .replaceAll ("\\ s+" , "" ).isEmpty ()) {
119+ result .append (line .trim ());
120+ result .append ("\n " );
121+ }
122+ }
123+ return result .substring (0 , result .length () - 1 );
124+ }
89125 }
90126
91- public static String parseUnparseTree (Path path , VariationDiffParseOptions option ) {
127+ public static String parseUnparseTree (String text , VariationDiffParseOptions option ) {
92128 String temp = "b" ;
93129 try {
94- VariationTree <DiffLinesLabel > tree = VariationTree .fromFile ( path , option );
130+ VariationTree <DiffLinesLabel > tree = VariationTree .fromText ( text , VariationTreeSource . Unknown , option );
95131 temp = VariationUnparser .variationTreeUnparser (tree );
96132 } catch (Exception e ) {
97133 e .printStackTrace ();
@@ -131,27 +167,28 @@ public static boolean equalsText(String text1, String text2, boolean whitespace)
131167 }
132168
133169 public static boolean [][] runTestsDiff (String text ) {
134- boolean [][] array = new boolean [3 ][8 ];
170+ boolean [][] array = new boolean [2 ][8 ];
135171 for (int i = 0 ; i < 4 ; i ++) {
136172 String diff = parseUnparseDiff (text , optionsSetter (i ));
137173 array [0 ][i ] = equalsText (text , diff , true );
138174 array [0 ][i + 4 ] = equalsText (text , diff , false );
139- array [1 ][i ] = equalsText (VariationUnparser .undiff (text , Time .BEFORE ),
140- VariationUnparser .undiff (diff , Time .BEFORE ), true );
141- array [1 ][i + 4 ] = equalsText (VariationUnparser .undiff (text , Time .BEFORE ),
142- VariationUnparser .undiff (diff , Time .BEFORE ), false );
143- array [2 ][i ] = equalsText (VariationUnparser .undiff (text , Time .AFTER ),
144- VariationUnparser .undiff (diff , Time .AFTER ), true );
145- array [2 ][i + 4 ] = equalsText (VariationUnparser .undiff (text , Time .AFTER ),
146- VariationUnparser .undiff (diff , Time .AFTER ), false );
175+ array [1 ][i ] = (equalsText (VariationUnparser .undiff (text , Time .BEFORE ),
176+ VariationUnparser .undiff (diff , Time .BEFORE ), true )
177+ && equalsText (VariationUnparser .undiff (text , Time .AFTER ),
178+ VariationUnparser .undiff (diff , Time .AFTER ), true ))
179+ || (equalsText (VariationUnparser .undiff (text , Time .BEFORE ),
180+ VariationUnparser .undiff (diff , Time .BEFORE ), false )
181+ && equalsText (VariationUnparser .undiff (text , Time .AFTER ),
182+ VariationUnparser .undiff (diff , Time .AFTER ), false ));
183+ array [1 ][i + 4 ] = false ;
147184 }
148185 return array ;
149186 }
150187
151- public static boolean [] runTestsTree (String text , Path path ) {
188+ public static boolean [] runTestsTree (String text ) {
152189 boolean [] array = new boolean [8 ];
153190 for (int i = 0 ; i < 4 ; i ++) {
154- String temp = parseUnparseTree (path , optionsSetter (i ));
191+ String temp = parseUnparseTree (text , optionsSetter (i ));
155192 array [i ] = equalsText (text , temp , true );
156193 array [i + 4 ] = equalsText (text , temp , false );
157194 }
0 commit comments