From 7ee5460c62dadba194ba4eae40213f3cf7100058 Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 12 Jan 2024 14:16:45 +0800 Subject: [PATCH 1/2] Improve initialization code and add comments --- .idea/inspectionProfiles/Project_Default.xml | 8 + .idea/uiDesigner.xml | 124 +++++++++++ .idea/workspace.xml | 192 +++++++++++++++--- pom.xml | 52 ++++- src/main/java/module-info.java | 10 + .../java/org/jcnc/IDEology/kernel/Kernel.java | 42 +++- .../jcnc/IDEology/kernel/KernelModule.java | 91 +++++++-- .../java/org/jcnc/IDEology/kernel/Module.java | 22 +- .../org/jcnc/IDEology/kernel/Module1.java | 10 - .../IDEology/kernel/example/JavaFXDemo.java | 38 ++++ .../jcnc/IDEology/kernel/example/launch.java | 16 ++ .../java/org/jcnc/IDEology/kernel/launch.java | 12 -- 12 files changed, 543 insertions(+), 74 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/module-info.java delete mode 100644 src/main/java/org/jcnc/IDEology/kernel/Module1.java create mode 100644 src/main/java/org/jcnc/IDEology/kernel/example/JavaFXDemo.java create mode 100644 src/main/java/org/jcnc/IDEology/kernel/example/launch.java delete mode 100644 src/main/java/org/jcnc/IDEology/kernel/launch.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..3153295 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9006818..73393bd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,41 +4,148 @@ + + + \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index ee61142..8dc4ee9 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -6,5 +6,7 @@ module org.jcnc.IDEology.kernel { exports org.jcnc.IDEology.kernel to javafx.graphics; exports org.jcnc.IDEology.kernel.example to javafx.graphics; + exports org.jcnc.IDEology.kernel.module to javafx.graphics; + exports org.jcnc.IDEology.kernel.kernel to javafx.graphics; } \ No newline at end of file diff --git a/src/main/java/org/jcnc/IDEology/kernel/Module.java b/src/main/java/org/jcnc/IDEology/kernel/Module.java index 644b665..541405c 100644 --- a/src/main/java/org/jcnc/IDEology/kernel/Module.java +++ b/src/main/java/org/jcnc/IDEology/kernel/Module.java @@ -18,4 +18,13 @@ public interface Module { * Method to stop a module. */ void stopModule(); + + /** + * Receives a message. + * @param message The received message. + */ + default void receiveMessage(String message) { + // Default implementation, can be overridden in specific module classes + System.err.println("Received message: " + message); + } } diff --git a/src/main/java/org/jcnc/IDEology/kernel/example/JavaFXDemo.java b/src/main/java/org/jcnc/IDEology/kernel/example/JavaFXDemo.java index 1b9bb84..e2db29e 100644 --- a/src/main/java/org/jcnc/IDEology/kernel/example/JavaFXDemo.java +++ b/src/main/java/org/jcnc/IDEology/kernel/example/JavaFXDemo.java @@ -4,13 +4,17 @@ import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.stage.Stage; +import org.jcnc.IDEology.kernel.Module; public class JavaFXDemo extends Application implements org.jcnc.IDEology.kernel.Module { + // Module name String ModuleName = "JavaFXDemo"; + // Primary stage for JavaFX application private Stage primaryStage; + // Overriding start method from Application class @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; @@ -20,19 +24,27 @@ public class JavaFXDemo extends Application implements org.jcnc.IDEology.kernel. primaryStage.show(); } + // Overriding initModule method from Module interface @Override public void initModule() { System.out.println(ModuleName + " is init!"); } + // Overriding startModule method from Module interface @Override public void startModule() { new Thread(JavaFXDemo::launch).start(); } + // Overriding stopModule method from Module interface @Override public void stopModule() { System.out.println(ModuleName + " is stop!"); } + // Overriding receiveMessage method from Module interface + @Override + public void receiveMessage(String message) { + Module.super.receiveMessage(message); + } } diff --git a/src/main/java/org/jcnc/IDEology/kernel/example/launch.java b/src/main/java/org/jcnc/IDEology/kernel/example/launch.java index 74d0d00..c425c1d 100644 --- a/src/main/java/org/jcnc/IDEology/kernel/example/launch.java +++ b/src/main/java/org/jcnc/IDEology/kernel/example/launch.java @@ -1,16 +1,31 @@ package org.jcnc.IDEology.kernel.example; -import org.jcnc.IDEology.kernel.KernelModule; +import org.jcnc.IDEology.kernel.module.CommunicationModule; +import org.jcnc.IDEology.kernel.module.KernelModule; public class launch { - public static void main(String[] args) throws Exception { + public static void main(String[] args) { + // Creating a new KernelModule object KernelModule kernelModule = new KernelModule(); + + // Registering a new submodule "JavaFXDemo" and initializing it kernelModule.registerSubModule("JavaFXDemo", new JavaFXDemo(), true); + kernelModule.initSubModule("JavaFXDemo"); - kernelModule.getAllRegisteredModules(); + // Registering a new CommunicationModule "CommModule" + kernelModule.registerModule("CommModule", CommunicationModule.class, true); - kernelModule.initSubModule("JavaFXDemo"); + // Getting the CommunicationModule "CommModule" + CommunicationModule commModule = (CommunicationModule) kernelModule.getSubModule("CommModule"); + // Adding a new communication partner "JavaFXDemo" + commModule.addCommunicationPartner("JavaFXDemo", new JavaFXDemo()); + + // Sending a message to "JavaFXDemo" + commModule.sendMessage("JavaFXDemo", "Hello, JavaFXDemo!\nI am kernelModule!!!"); + + // Getting all registered modules + kernelModule.getAllRegisteredModules(); } } diff --git a/src/main/java/org/jcnc/IDEology/kernel/Kernel.java b/src/main/java/org/jcnc/IDEology/kernel/kernel/Kernel.java similarity index 94% rename from src/main/java/org/jcnc/IDEology/kernel/Kernel.java rename to src/main/java/org/jcnc/IDEology/kernel/kernel/Kernel.java index e264e5d..9c5205e 100644 --- a/src/main/java/org/jcnc/IDEology/kernel/Kernel.java +++ b/src/main/java/org/jcnc/IDEology/kernel/kernel/Kernel.java @@ -1,4 +1,6 @@ -package org.jcnc.IDEology.kernel; +package org.jcnc.IDEology.kernel.kernel; + +import org.jcnc.IDEology.kernel.Module; import java.util.HashMap; import java.util.Set; @@ -6,7 +8,7 @@ import java.util.Set; /** * The Kernel class manages modules in a HashMap. */ -class Kernel { +public class Kernel{ /** * A HashMap to store modules. The key is the module name, and the value is the Module object. */ @@ -67,6 +69,8 @@ class Kernel { } } + + /** * Method to get all registered module names. * @return A set of all registered module names @@ -74,4 +78,5 @@ class Kernel { public Set getAllRegisteredModules() { return modules.keySet(); } + } diff --git a/src/main/java/org/jcnc/IDEology/kernel/module/CommunicationModule.java b/src/main/java/org/jcnc/IDEology/kernel/module/CommunicationModule.java new file mode 100644 index 0000000..031afeb --- /dev/null +++ b/src/main/java/org/jcnc/IDEology/kernel/module/CommunicationModule.java @@ -0,0 +1,43 @@ +package org.jcnc.IDEology.kernel.module; + +import org.jcnc.IDEology.kernel.Module; +import org.jcnc.IDEology.kernel.kernel.Kernel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Communication module, responsible for managing communication between modules + */ +public class CommunicationModule extends KernelModule implements Module{ + private final Kernel kernel; + private final Map communicationPartners; + + public CommunicationModule(Kernel kernel) { + this.kernel = kernel; + this.communicationPartners = new HashMap<>(); + } + + /** + * Adds a communication partner + * @param name The name of the partner + * @param module The module of the partner + */ + public void addCommunicationPartner(String name, Module module) { + this.communicationPartners.put(name, module); + } + + /** + * Sends a message to a communication partner + * @param name The name of the partner + * @param message The message to be sent + */ + public void sendMessage(String name, String message) { + Module partner = this.communicationPartners.get(name); + if (partner != null) { + partner.receiveMessage(message); + } else { + System.out.println("Communication partner " + name + " does not exist."); + } + } +} diff --git a/src/main/java/org/jcnc/IDEology/kernel/KernelModule.java b/src/main/java/org/jcnc/IDEology/kernel/module/KernelModule.java similarity index 71% rename from src/main/java/org/jcnc/IDEology/kernel/KernelModule.java rename to src/main/java/org/jcnc/IDEology/kernel/module/KernelModule.java index ec29d4f..8f89f12 100644 --- a/src/main/java/org/jcnc/IDEology/kernel/KernelModule.java +++ b/src/main/java/org/jcnc/IDEology/kernel/module/KernelModule.java @@ -1,4 +1,7 @@ -package org.jcnc.IDEology.kernel; +package org.jcnc.IDEology.kernel.module; + +import org.jcnc.IDEology.kernel.Module; +import org.jcnc.IDEology.kernel.kernel.Kernel; import java.util.HashMap; import java.util.Map; @@ -33,8 +36,9 @@ public class KernelModule implements Module { /** * Registers a submodule. - * @param name The name of the submodule. - * @param module The module to register. + * + * @param name The name of the submodule. + * @param module The module to register. * @param startOnRegister Whether to start the module upon registration. */ public void registerSubModule(String name, Module module, boolean startOnRegister) { @@ -52,6 +56,7 @@ public class KernelModule implements Module { /** * Starts a submodule. + * * @param name The name of the submodule. */ public void startSubModule(String name) { @@ -74,6 +79,7 @@ public class KernelModule implements Module { /** * Stops a submodule. + * * @param name The name of the submodule. */ public void stopSubModule(String name) throws Exception { @@ -103,6 +109,7 @@ public class KernelModule implements Module { /** * Removes a submodule. + * * @param name The name of the submodule. */ public void removeSubModule(String name) throws Exception { @@ -120,6 +127,7 @@ public class KernelModule implements Module { /** * Initializes a submodule. + * * @param name The name of the submodule. */ public void initSubModule(String name) { @@ -132,6 +140,47 @@ public class KernelModule implements Module { } } + /** + * Registers a module with the kernel, and optionally starts it. + * + * @param name The name of the module. + * @param moduleClass The class of the module. + * @param startOnRegister If true, the module is started immediately after registration. + */ + public void registerModule(String name, Class moduleClass, boolean startOnRegister) { + try { + // Use reflection API to create a module instance + Module module = moduleClass.getDeclaredConstructor(Kernel.class).newInstance(this.kernel); + + // Register the module + this.kernel.registerModule(name, module); + System.out.println("Registered module: " + name); + + if (startOnRegister) { + module.startModule(); + this.moduleStatus.put(name, ModuleStatus.STARTED); + System.out.println("Started module: " + name); + } else { + this.moduleStatus.put(name, ModuleStatus.REGISTERED); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Module getSubModule(String name) { + return kernel.getModule(name); + } + + /** + * Gets the status of all modules. + * + * @return A map containing the name and status of each module. + */ + public Map getModuleStatus() { + return this.moduleStatus; + } + /** * Initializes the module. */ -- Gitee