Kevin Trometer 7 years ago
parent
commit
14426ec1d4

+ 1 - 1
.classpath

@@ -6,6 +6,6 @@
 	<classpathentry kind="lib" path="jars/json-simple-1.1.1.jar"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
-	<classpathentry kind="lib" path="C:/Users/krabs/Desktop/API_Testing/GlobalBukkit.jar"/>
+	<classpathentry kind="lib" path="jars/Cps Library.jar"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>

BIN
jars/Cps Library.jar


BIN
jars/CpsClassesModelController.jar


+ 22 - 0
src/ui/controller/Control.java

@@ -6,6 +6,8 @@ import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 
+import com.scarekroow.exampleapi.interfaceTest;
+
 import classes.Category;
 import classes.CpsEdge;
 import classes.CpsNode;
@@ -466,6 +468,7 @@ public class Control {
 	public void calculateStateForCurrentTimeStep() {
 		simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(model.getCurIteration());
+		runAlgorithm(model, this);
 	}
 
 	/**
@@ -477,6 +480,7 @@ public class Control {
 	public void calculateStateForTimeStep(int x) {
 		simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(x);
+		runAlgorithm(model, this);
 	}
 
 	/**
@@ -660,4 +664,22 @@ public class Control {
 		globalController.setCanvasY(canvasY);
 	}
 	
+	/**
+	 * Set the Algorithm.
+	 * 
+	 * @param obj the Algorithm
+	 */
+	public void setAlgorithm(Object obj) {
+		multiPurposeController.setAlgorithm(obj);
+	}
+	
+	/**
+	 * Run the Algorithm.
+	 */
+	public void runAlgorithm(Model model, Control controller) {
+		if (model.getAlgorithm() != null) {
+			((interfaceTest)model.getAlgorithm()).RunAlgorithm(model, controller);
+		}
+	}
+	
 }

+ 10 - 0
src/ui/controller/MultiPurposeController.java

@@ -177,4 +177,14 @@ public class MultiPurposeController {
 		return newMap;
 	}
 
+	/**
+	 * Set the Algorithm.
+	 * 
+	 * @param obj
+	 *            the Algorithm
+	 */
+	public void setAlgorithm(Object obj) {
+		model.setAlgorithm(obj);
+	}
+
 }

+ 23 - 0
src/ui/model/Model.java

@@ -75,6 +75,11 @@ public class Model {
 	private List<CategoryListener> categoryListeners;
 	private List<ObjectListener> objectListeners;
 
+	/*
+	 * Object that runs the Algorithm
+	 */
+	private Object algorithm = null;
+	
 	/**
 	 * Constructor for the model. It initializes the categories and
 	 * objectsOnCanvas by default values. Listeners are also initialized by
@@ -555,4 +560,22 @@ public class Model {
 	public void setCanvasY(int canvasY) {
 		this.canvasY = canvasY;
 	}
+	
+	/**
+	 * get the Algorithm.
+	 * 
+	 * @return the Algorithm
+	 */
+	public Object getAlgorithm() {
+		return algorithm;
+	}
+
+	/**
+	 * Set the Algorithm.
+	 * 
+	 * @param obj the Algorithm
+	 */
+	public void setAlgorithm(Object obj) {
+		this.algorithm = obj;
+	}
 }

+ 1 - 0
src/ui/view/GUI.java

@@ -1528,6 +1528,7 @@ public class GUI<E> implements CategoryListener {
 			public void stateChanged(ChangeEvent e) {
 				int i = model.getCurIteration();
 				controller.calculateStateForTimeStep(i);
+				
 				unitGraph.repaint();
 			}
 		});

+ 38 - 26
src/ui/view/SimulationMenu.java

@@ -27,6 +27,7 @@ import classes.AbstractCpsObject;
 import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.HolonTransformer;
+import jdk.nashorn.api.scripting.URLReader;
 import ui.controller.Control;
 import ui.model.Model;
 import java.util.ArrayList;
@@ -46,6 +47,7 @@ import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -71,6 +73,9 @@ public class SimulationMenu extends JMenuBar {
 	private JButton algoFolderButton = new JButton("Algorithm:");
 	private HashMap<String, File> algosHash = new HashMap<>();
 
+	// root Directory
+	File root = null;
+
 	Model model;
 	Control controller;
 
@@ -106,37 +111,18 @@ public class SimulationMenu extends JMenuBar {
 					algoCombo.removeAllItems();
 					File[] files = fileChooser.getSelectedFile().listFiles();
 					for (int i = 0; i < files.length; i++) {
-						if ( files[i].toString().endsWith(".java") /*||  files[i].toString().endsWith(".class")*/) {
+						if (files[i].toString()
+								.endsWith(".java") /*
+													 * || files[i].toString().
+													 * endsWith(".class")
+													 */) {
 							String name = files[i].getName();
 							int tmpB = name.lastIndexOf('.');
 							name = name.substring(0, tmpB);
 							algosHash.put(name, files[i]);
 							algoCombo.addItem(name);
-
-							try {
-
-								// Save source in .java file.
-								File root = new File(fileChooser.getCurrentDirectory().getPath());
-								// Compile source file.
-
-								JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
-
-								compiler.run(null, null, null, files[i].getPath());
-
-								// Load and instantiate compiled class.
-								URLClassLoader classLoader = URLClassLoader
-										.newInstance(new URL[] { root.toURI().toURL() });
-								Class<?> cls = Class.forName("projectPackage.APITest", true, classLoader);
-
-								Object t = cls.newInstance();
-
-								controller.addTextToConsole(((interfaceTest)t).outPrintTest());
-
-								controller.addTextToConsole(classLoader.getClass().getPackage()+"");
-							} catch (Exception e) {
-								// TODO Auto-generated catch block
-								e.printStackTrace();
-							}
+							root = new File(fileChooser.getCurrentDirectory().getPath());
+							setAlgorithm(files[i]);
 						}
 					}
 				}
@@ -211,4 +197,30 @@ public class SimulationMenu extends JMenuBar {
 		// Add Panel to SimulationMenu
 		this.add(menuPanel);
 	}
+
+	public void setAlgorithm(File file) {
+		try {
+
+			// Compile source file.
+
+			JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+
+			compiler.run(null, null, null, file.getPath());
+
+			// Load and instantiate compiled class.
+			controller.addTextToConsole(root.toString());
+			URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
+			Class<?> cls = Class.forName("projectPackage.APITest", true, classLoader);
+
+			Object t = cls.newInstance();
+
+			controller.addTextToConsole("success");
+
+			controller.setAlgorithm(t);
+
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
 }