Skip to content

Commit f6291e4

Browse files
committed
Backup continue an application files
1 parent 10a68f2 commit f6291e4

8 files changed

Lines changed: 189 additions & 45 deletions

File tree

AppWish/AppWish/dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>pn.dev</groupId>
55
<artifactId>code-generator-gui-ollama</artifactId>
66
<name>CodeGenerator-GUI</name>
7-
<version>1.8.1</version>
7+
<version>2.0.1</version>
88
<description>The GUI interface for the pn.dev.code-generator-ollama library is included in this package</description>
99
<inceptionYear>2024</inceptionYear>
1010
<developers>

cg/CodeGenerator/CodeGenerator/dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>pn.dev</groupId>
55
<artifactId>code-generator-ollama</artifactId>
6-
<version>1.8.1</version>
6+
<version>2.0.1</version>
77
<description>The Java Application-Generator (pn.dev.code-generator-ollama) creates applications by giving a string of your desired features for a Java application, like this: AppSystem.StartCodeGenerator(String appWish)</description>
88
<inceptionYear>2024</inceptionYear>
99
<developers>

cg/CodeGenerator/CodeGenerator/src/main/java/pn/cg/app_system/AppSystem.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pn.cg.datastorage.DataStorage;
88
import pn.cg.ollama_ai_remote.OllamaRemoteSystem;
99
import pn.cg.util.CodeGeneratorUtil;
10+
import pn.cg.util.StringUtil;
1011

1112

1213
import java.io.File;
@@ -54,6 +55,13 @@ private static void StartCodeGenerator(String appWish, boolean isFirstRun, boole
5455
if (isCreateAppGeneration) {
5556
appWishCompileResult = ollamaRemoteSystem.CreateApp(appWish, true, "", null);
5657
} else {
58+
DataStorage.getInstance().setContinueAnAppDirectoryName(CodeGeneratorUtil.getIncrementedContinueOnAppDirectoryName(StringUtil.extractClassNameFromTextWithJavaClasses(javaFileContentInLines)));
59+
try {
60+
Files.createDirectory(new File(COMPILE_CLASS_STORAGE + File.separator + DataStorage.getInstance().getContinueAnAppDirectoryName()).toPath());
61+
} catch (IOException e) {
62+
throw new RuntimeException(e);
63+
}
64+
5765
appWishCompileResult = ollamaRemoteSystem.CreateApp(appWish, true, pathOfJavaFileIfModifyRequest, javaFileContentInLines);
5866
}
5967
}

cg/CodeGenerator/CodeGenerator/src/main/java/pn/cg/datastorage/DataStorage.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ public class DataStorage {
3434
/**
3535
* Data holder for the current Super App Directory name
3636
*/
37-
private String SuperAppDirectoryName;
37+
private String superAppDirectoryName;
3838

39+
/**
40+
* Data holder for the current Continue an App Directory
41+
*/
42+
private String continueAnAppDirectoryName;
3943
/**
4044
* The class that is selected for implementation tries in a super app creation process
4145
*/
@@ -52,15 +56,15 @@ public class DataStorage {
5256
* The path for the project root directory
5357
*/
5458
private final Path PROJECT_ROOT_WORKING_DIR;
55-
private DataStorage() {
59+
private DataStorage() {
5660

5761
this.compilationJob = getCompilationJob();
5862
this.isSuperAppCreated = false;
5963
this.PROJECT_ROOT_WORKING_DIR = Path.of(System.getProperty("user.dir"));
6064
}
6165

6266

63-
synchronized public static DataStorage getInstance() {
67+
synchronized public static DataStorage getInstance() {
6468
if (instance == null) {
6569

6670
instance = new DataStorage();
@@ -69,70 +73,78 @@ synchronized public static DataStorage getInstance() {
6973
}
7074

7175

72-
public CompilationJob getCompilationJob() {
76+
public final CompilationJob getCompilationJob() {
7377
return compilationJob;
7478
}
7579

76-
public void setCompilationJob(CompilationJob compilationJob) {
80+
public final void setCompilationJob(CompilationJob compilationJob) {
7781
this.compilationJob = compilationJob;
7882
}
7983

80-
public String getJavaExecutionPath() {
84+
public final String getJavaExecutionPath() {
8185
return javaExecutionPath;
8286
}
8387

84-
public void setJavaExecutionPath(String javaExecutionPath) {
88+
public final void setJavaExecutionPath(String javaExecutionPath) {
8589
this.javaExecutionPath = javaExecutionPath;
8690
}
8791

88-
public SuperApp getCurrentSuperClass() {
92+
public final SuperApp getCurrentSuperClass() {
8993
return currentSuperClass;
9094
}
9195

92-
public void setCurrentSuperClass(SuperApp currentSuperClass) {
96+
public final void setCurrentSuperClass(SuperApp currentSuperClass) {
9397
this.currentSuperClass = currentSuperClass;
9498
}
9599

96-
public boolean isSuperAppCreated() {
100+
public final boolean isSuperAppCreated() {
97101
return isSuperAppCreated;
98102
}
99103

100-
public void setSuperAppCreated(boolean superAppCreated) {
104+
public final void setSuperAppCreated(boolean superAppCreated) {
101105
isSuperAppCreated = superAppCreated;
102106
}
103107

104-
public String getSuperAppDirectoryName() {
105-
return SuperAppDirectoryName;
108+
public final String getSuperAppDirectoryName() {
109+
return superAppDirectoryName;
106110
}
107111

108-
public void setSuperAppDirectoryName(String superAppDirectoryName) {
109-
SuperAppDirectoryName = superAppDirectoryName;
112+
public final void setSuperAppDirectoryName(String superAppDirectoryName) {
113+
this.superAppDirectoryName = superAppDirectoryName;
110114
}
111115

112-
public List<SuperApp> getListOfCurrentSuperAppClasses() {
116+
public final List<SuperApp> getListOfCurrentSuperAppClasses() {
113117
return listOfCurrentSuperAppClasses;
114118
}
115119

116-
public void setListOfCurrentSuperAppClasses(List<SuperApp> listOfCurrentSuperAppClasses) {
120+
public final void setListOfCurrentSuperAppClasses(List<SuperApp> listOfCurrentSuperAppClasses) {
117121
this.listOfCurrentSuperAppClasses = listOfCurrentSuperAppClasses;
118122
}
119123

120-
public List<Path> getListOfPathsToTmpFiles() {
124+
public final List<Path> getListOfPathsToTmpFiles() {
121125
return listOfPathsToTmpFiles;
122126
}
123127

124-
public void setListOfPathsToTmpFiles(List<Path> listOfPathsToTmpFiles) {
128+
public final void setListOfPathsToTmpFiles(List<Path> listOfPathsToTmpFiles) {
125129
this.listOfPathsToTmpFiles = listOfPathsToTmpFiles;
126130
}
127131

128-
public void addPathToTmpFileList(Path path){
132+
public final void addPathToTmpFileList(Path path){
129133

130134
this.listOfPathsToTmpFiles.add(path);
131135
}
132136

133-
public Path getPROJECT_ROOT_WORKING_DIR() {
137+
public final Path getPROJECT_ROOT_WORKING_DIR() {
134138
return PROJECT_ROOT_WORKING_DIR;
135139
}
140+
141+
public final String getContinueAnAppDirectoryName() {
142+
return continueAnAppDirectoryName;
143+
}
144+
145+
public final void setContinueAnAppDirectoryName(String continueAnAppDirectoryName) {
146+
this.continueAnAppDirectoryName = continueAnAppDirectoryName;
147+
}
136148
}
137149

138150

cg/CodeGenerator/CodeGenerator/src/main/java/pn/cg/ollama_ai_remote/OllamaRemoteSystem.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import pn.cg.app_wish.QuestionBuilder;
99
import pn.cg.datastorage.DataStorage;
1010
import pn.cg.datastorage.constant.CommonStringConstants;
11-
import pn.cg.datastorage.constant.PathConstants;
1211
import pn.cg.datastorage.constant.QuestionConstants;
13-
import pn.cg.datastorage.constant.ScriptConstants;
1412
import pn.cg.ollama_ai_remote.request.*;
1513
import pn.cg.util.CodeGeneratorUtil;
1614
import pn.cg.util.FileUtil;
@@ -112,8 +110,8 @@ public synchronized boolean CreateApp(String appWish, boolean firstRun, String i
112110

113111

114112
log.info("Java source code after modification = {}", javaSourceCode);
115-
// Create file instance with class name and file extension
116-
File file = new File(TaskUtil.addFilePathToClassName(className + JAVA_FILE_EXTENSION));
113+
114+
File file = getFileForJavaFileOutput(isCreateNewApp, className);
117115

118116
// Save the path to shared storage (if user wants to execute the java app after the build
119117
DataStorage.getInstance().setJavaExecutionPath(file.getAbsolutePath());
@@ -134,6 +132,35 @@ public synchronized boolean CreateApp(String appWish, boolean firstRun, String i
134132
return DataStorage.getInstance().getCompilationJob().isResult();
135133
}
136134

135+
/**
136+
* Gets a File that is initialized with the correct path
137+
*
138+
* @param isCreateNewApp A new app will be stationed in the default resource folder while a continue an app will
139+
* be stationed in its designated folder
140+
* @param className The name of the Class
141+
* @return File
142+
*/
143+
private static File getFileForJavaFileOutput(boolean isCreateNewApp, String className) {
144+
145+
// Placeholder for the output .java file
146+
File file = null;
147+
148+
if (isCreateNewApp) {
149+
// Create file instance with class name and file extension
150+
file = new File(TaskUtil.addFilePathToClassName(className + JAVA_FILE_EXTENSION));
151+
152+
}
153+
154+
// Continue on existing application
155+
else {
156+
157+
// Create file instance with class name and file extension
158+
file = new File(TaskUtil.AddFilePathToContinueOnApplication(className + JAVA_FILE_EXTENSION, DataStorage.getInstance().getContinueAnAppDirectoryName()));
159+
160+
}
161+
return file;
162+
}
163+
137164
/**
138165
* Get a list of classes needed for the creation of the super app and sets the output list as reference in the shared singleton
139166
*
@@ -193,9 +220,10 @@ public synchronized boolean CreateSuperApp(SuperApp classInSuperAppDesign, boole
193220
// Create file instance with class name and file extension
194221
File file = new File(TaskUtil.addFilePathOfSuperAppToClassName(className + CommonStringConstants.JAVA_FILE_EXTENSION, DataStorage.getInstance().getSuperAppDirectoryName()));
195222

196-
if(className.equals("Main")){
197-
// Save the path to shared storage (if user wants to execute the java app after the build
198-
DataStorage.getInstance().setJavaExecutionPath(file.getAbsolutePath());}
223+
if (className.equals("Main")) {
224+
// Save the path to shared storage (if user wants to execute the java app after the build
225+
DataStorage.getInstance().setJavaExecutionPath(file.getAbsolutePath());
226+
}
199227

200228
// Write the Java code provided from OLLAMA to file
201229
try {
@@ -224,17 +252,17 @@ public synchronized boolean CreateSuperApp(SuperApp classInSuperAppDesign, boole
224252
try {
225253
CodeGeneratorUtil.SetMethodAndConstructorListForImplementedClass(className, Path.of(TaskUtil.addFilePathOfSuperAppToClassName(className + CommonStringConstants.CLASS_FILE_EXTENSION, DataStorage.getInstance().getSuperAppDirectoryName())));
226254
} catch (MalformedURLException | ClassNotFoundException e) {
227-
log.error("Could not set Methods or/ and constructors");
228-
classInSuperAppDesign.setConstructors(new LinkedList<String>());
229-
classInSuperAppDesign.setMethods(new LinkedList<String>());
255+
log.error("Could not set Methods or/ and constructors");
256+
classInSuperAppDesign.setConstructors(new LinkedList<String>());
257+
classInSuperAppDesign.setMethods(new LinkedList<String>());
230258
}
231259

232260
// Copy class file to the script directory for the javac script, so it will be included in the class path
233261
try {
234-
Path destinationPathForTempFile= Path.of(DataStorage.getInstance().getPROJECT_ROOT_WORKING_DIR().toString()+File.separator+className+CLASS_FILE_EXTENSION);
262+
Path destinationPathForTempFile = Path.of(DataStorage.getInstance().getPROJECT_ROOT_WORKING_DIR().toString() + File.separator + className + CLASS_FILE_EXTENSION);
235263
Files.deleteIfExists(destinationPathForTempFile);
236264
DataStorage.getInstance().addPathToTmpFileList(destinationPathForTempFile);
237-
Files.copy(Path.of(TaskUtil.addFilePathOfSuperAppToClassName(className + CommonStringConstants.CLASS_FILE_EXTENSION, DataStorage.getInstance().getSuperAppDirectoryName())),destinationPathForTempFile);
265+
Files.copy(Path.of(TaskUtil.addFilePathOfSuperAppToClassName(className + CommonStringConstants.CLASS_FILE_EXTENSION, DataStorage.getInstance().getSuperAppDirectoryName())), destinationPathForTempFile);
238266
} catch (IOException e) {
239267
throw new RuntimeException(e);
240268
}

cg/CodeGenerator/CodeGenerator/src/main/java/pn/cg/util/CodeGeneratorUtil.java

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pn.cg.app_system.code_generation.model.enum_.OPTION_METHOD_CONSTRUCTOR;
77
import pn.cg.datastorage.DataStorage;
88
import pn.cg.datastorage.ThreadPoolMaster;
9+
import pn.cg.datastorage.constant.CommonStringConstants;
910

1011
import java.io.File;
1112
import java.io.IOException;
@@ -110,7 +111,60 @@ public static String getIncrementedSuperAppDirectoryName() {
110111
}
111112

112113
} catch (NumberFormatException | IOException e) {
113-
log.error("You have a folder in the that resides in the directory for the generated java applications that starts with \"APP_\" and have a suffix that is not an Integer\nPlease move or delete that folder to resolve this error");
114+
log.error("You have a folder that resides in the directory of the generated java applications that starts with \"APP_\" and have a suffix that is not an Integer\nPlease move or delete that folder to resolve this error");
115+
ThreadPoolMaster.getInstance().TerminateThreads();
116+
System.exit(1);
117+
throw new RuntimeException(e);
118+
}
119+
}
120+
121+
/**
122+
* Read the files in the COMPILE_CLASS_STORAGE folder and filter the current continueClass directories (if any) and increment the number by one
123+
*
124+
* @param className The name of class that has been continued
125+
* @return String
126+
*/
127+
public static String getIncrementedContinueOnAppDirectoryName(String className) {
128+
final String CONTINUE_ON_APP_DIRECTORY = className + "_";
129+
130+
// The number that will be appended to the APP_ directory
131+
int appNumber = 1;
132+
133+
//Placeholder list that will be filled with the appending number of existing directories (if any)
134+
List<Integer> listOfNumbersThatAreAppendedToExistingContinueOnAppDirectories = new LinkedList<>();
135+
136+
try {
137+
138+
139+
List<Path> classNameFolders = (Files.list(new File(COMPILE_CLASS_STORAGE)
140+
.toPath())
141+
.filter(file -> file.getFileName().toString()
142+
.startsWith(CONTINUE_ON_APP_DIRECTORY))
143+
.filter(file -> !(file.getFileName().toString().equalsIgnoreCase(className + CommonStringConstants.CLASS_FILE_EXTENSION)))
144+
.filter(file -> !(file.getFileName().toString().equalsIgnoreCase(className + CommonStringConstants.JAVA_FILE_EXTENSION)))
145+
.toList());
146+
147+
148+
if (classNameFolders.isEmpty()) {
149+
return CONTINUE_ON_APP_DIRECTORY + appNumber;
150+
} else {
151+
152+
classNameFolders.forEach(p -> listOfNumbersThatAreAppendedToExistingContinueOnAppDirectories
153+
.add(Integer.parseInt(p.getFileName().toString().split(CONTINUE_ON_APP_DIRECTORY)[1])));
154+
155+
// Take the highest and increment it by one
156+
int maxNumberOfExistingSuperAppDirectories = listOfNumbersThatAreAppendedToExistingContinueOnAppDirectories
157+
.stream()
158+
.mapToInt(Integer::intValue)
159+
.max()
160+
.getAsInt();
161+
162+
return CONTINUE_ON_APP_DIRECTORY + (maxNumberOfExistingSuperAppDirectories + 1);
163+
}
164+
165+
} catch (NumberFormatException | IOException e) {
166+
log.error(e.getMessage());
167+
log.error("You have a folder in the that resides in the directory for the generated java applications that starts with \"{}\" and have a suffix that is not an Integer\nPlease move or delete that folder to resolve this error", className);
114168
ThreadPoolMaster.getInstance().TerminateThreads();
115169
System.exit(1);
116170
throw new RuntimeException(e);
@@ -233,25 +287,28 @@ public static boolean WriteDocFileToSuperApp(List<SuperApp> successfulSuperAppCr
233287

234288
/**
235289
* Gets the path in text format to the current super app directory
290+
*
236291
* @return String
237292
*/
238-
public static String GetDocFilePathForSuperApp(){
293+
public static String GetDocFilePathForSuperApp() {
239294

240-
final String DOC_FILE_FOR_SUPER_APP_PATH = COMPILE_CLASS_STORAGE + File.separator + DataStorage.getInstance().getSuperAppDirectoryName()+File.separator+"DOCS.txt";
295+
final String DOC_FILE_FOR_SUPER_APP_PATH = COMPILE_CLASS_STORAGE + File.separator + DataStorage.getInstance().getSuperAppDirectoryName() + File.separator + "DOCS.txt";
241296
return DOC_FILE_FOR_SUPER_APP_PATH;
242297
}
243298

244299
/**
245300
* Validate the response on a "Get All Classes" question to an AI-Model
301+
*
246302
* @param responseFromAiModelOnGetAllClassesFromSuperAppQuestion Response from AI-Model
247303
* @return boolean
248304
*/
249-
public static boolean ValidateResponseOnSuperAppGetAllClassesQuestion(String responseFromAiModelOnGetAllClassesFromSuperAppQuestion){
305+
public static boolean ValidateResponseOnSuperAppGetAllClassesQuestion(String responseFromAiModelOnGetAllClassesFromSuperAppQuestion) {
250306

251307
return !responseFromAiModelOnGetAllClassesFromSuperAppQuestion.contains(":")
252308
&& !responseFromAiModelOnGetAllClassesFromSuperAppQuestion.contains(",");
253309

254310
}
311+
255312
/**
256313
* Get a formatted String that will differ depending on wherever the super app contains methods or not
257314
*
@@ -301,7 +358,7 @@ private static List<String> ExtractDeclared(String className, Path pathToClassFi
301358

302359
List<String> returnList = new LinkedList<>();
303360

304-
String filePath = pathToClassFileDirectory.toString() + className + ".class"; // Replace with the actual path to your class
361+
String filePath = pathToClassFileDirectory.toString() + className + ".class";
305362

306363
File file = new File(filePath);
307364
URL url = file.getParentFile().toURI().toURL();

0 commit comments

Comments
 (0)