1616import com .sampullara .cli .Argument ;
1717
1818import java .io .File ;
19+ import java .nio .file .Files ;
20+ import java .nio .file .Path ;
1921import java .util .ArrayList ;
2022import java .util .Arrays ;
2123import java .util .Collections ;
@@ -125,20 +127,20 @@ public String clusterAction(int cluster) throws IOException{
125127 throw new IOException ("Clustering Failed" );
126128 }
127129
128- public void implementClusterAction (String action , String className , String methodName , List <UnitTest > tests , Patch patch , int iteration , int cluster ) {
130+ public void implementClusterAction (String action , String className , String methodName , List <UnitTest > tests , Patch patch , int iteration , int cluster , String diff ) {
129131 // ACTION C - Throw away patch
130132 if (action == "C" ) {
131133 UnitTestResultSet results = new UnitTestResultSet (patch , "" , null , new ArrayList <>(), null , "" , null , new ArrayList <>());
132- super .writePatchWithPatchCatInfo (iteration , iteration , results , methodName , null , 0 , cluster , "C" );
134+ super .writePatchWithPatchCatInfo (iteration , iteration , results , methodName , null , 0 , cluster , "C" , diff );
133135 }
134136
135137 // ACTION B - Proceed as normal
136- if (action == "B" ) {
138+ else if (action == "B" ) {
137139 // Calculate fitness
138140 UnitTestResultSet results = testPatch (className , tests , patch , null );
139141 double newFitness = fitness (results );
140- super .writePatchWithPatchCatInfo (iteration , iteration , results , methodName , newFitness , compareFitness (newFitness , best ), cluster , "B" );
141-
142+ super .writePatchWithPatchCatInfo (iteration , iteration , results , methodName , newFitness , compareFitness (newFitness , best ), cluster , "B" , diff );
143+
142144 // Check if better
143145 if (compareFitness (newFitness , best ) > 0 ) {
144146 best = newFitness ;
@@ -151,7 +153,8 @@ public void implementClusterAction(String action, String className, String metho
151153 else {
152154 //Add dummy fitness entry as we don't want to test the patch
153155 UnitTestResultSet results = new UnitTestResultSet (patch , "" , null , new ArrayList <>(), null , "" , null , new ArrayList <>());
154- super .writePatchWithPatchCatInfo (iteration , iteration , results , methodName , null , 0 , cluster , "A" );
156+ super .writePatchWithPatchCatInfo (iteration , iteration , results , methodName , null , 0 , cluster , "A" , diff );
157+
155158 bestPatch = patch ;
156159 }
157160 }
@@ -175,7 +178,7 @@ protected void search(TargetMethod method, Patch origPatch) {
175178 // Calculate fitness and record result, including fitness improvement (currently 0)
176179 double orig = fitness (results );
177180 if (Boolean .TRUE .equals (patchCat )){
178- super .writePatchWithPatchCatInfo (-1 , 0 , results , methodName , orig , 0 , -1 , "Original" );
181+ super .writePatchWithPatchCatInfo (-1 , 0 , results , methodName , orig , 0 , -1 , "Original" , "" );
179182 } else { super .writePatch (-1 , 0 , results , methodName , orig , 0 ); }
180183
181184 // Set original as best for now
@@ -187,26 +190,44 @@ protected void search(TargetMethod method, Patch origPatch) {
187190 // Add a mutation
188191 Patch patch = neighbour (bestPatch );
189192 boolean toTest = false ;
193+ String lastReplacement = "" ;
190194 Logger .info ("Patch is: " + patch .toString ());
191195 Logger .info ("Original Patch is: " + origPatch .toString ());
192196
193197 try {
194198 // Support for PatchCat Integration
195199 if (Boolean .TRUE .equals (patchCat )) {
196200 Logger .info ("Running PatchCat" );
197-
201+
202+ patch .apply ();
203+ Edit lastEdit = patch .getEdits ().get (patch .getEdits ().size () - 1 );
204+ if (lastEdit instanceof LLMReplaceStatement llmEdit ) {
205+ lastReplacement = llmEdit .getLastReplacement ();
206+ } else {
207+ continue ;
208+ }
209+
210+ // Logger.info("Current patch diff is: " + diff);
211+ Logger .info ("Last replacement is: " + lastReplacement );
212+
198213 ProcessBuilder builder = new ProcessBuilder (
199214 "python3" ,
200- "../gin/PatchCat/src/PatchCat.py" ,
201- patch .toString (), origPatch .toString ()
202- );
215+ "../gin/PatchCat/src/PatchCatGin.py" ,
216+ "--diff-text" , lastReplacement .toString (),
217+ "--vectorizer-path" , "../gin/PatchCat/src/running-model/vectorizer.pkl" ,
218+ "--model-path" , "../gin/PatchCat/src/running-model/model.pkl"
219+ );
203220
204221 builder .environment ().put ("PYTHONUNBUFFERED" , "1" );
205-
222+ // builder.redirectErrorStream(true);
223+
206224 Process process = builder .start ();
225+
207226 BufferedReader reader = new BufferedReader (
208227 new InputStreamReader (process .getInputStream ())
209228 );
229+
230+ //Read the output from PatchCatGin to get the cluster number
210231 String line = reader .readLine ();
211232 int cluster = -1 ;
212233
@@ -227,7 +248,7 @@ protected void search(TargetMethod method, Patch origPatch) {
227248
228249 int exit = process .waitFor ();
229250
230- implementClusterAction (action , className , methodName , tests , patch , i , cluster );
251+ implementClusterAction (action , className , methodName , tests , patch , i , cluster , lastReplacement );
231252
232253 } else { // Regular Local Search without PatchCat
233254 // Calculate fitness
0 commit comments