Browse Source

#22 API is now updated. I also added an example Algorithm.

Kevin Trometer 7 years ago
parent
commit
a6d3419d31

+ 5 - 37
API_Instruction

@@ -1,47 +1,15 @@
-Create a new Java Project and add the jars "CpsClassesModelController" and "CpsAlgorithm" to your Build Path.
+Create a new Java Project and add the jar "CpsAlgorithm" to your Build Path.
 Then create a new Package and create a new Class.
 That Class has to implement the Interface "cpsAlgorithm".
-Your Class then has to implement the method RunAlgorithm.
+Your Class then has to implement the method runAlgorithm.
 This Method gets a Model and the Controller.
 You can access all Data via the Model.
 The Controller has several Methods you can use as well.
 
-Your Class should look something like this:
+Your Class should look something like the example in the exampleAlgorithm Package
 
-package projectPackage;
-
-import java.awt.Color;
-import classes.HolonObject;
-import classes.SubNet;
-import cpsAlgorithm.CpsAlgorithm;
-import ui.controller.Control;
-import ui.model.Model;
-
-public class Test implements CpsAlgorithm {
-
-	@Override
-	public void RunAlgorithm(Model model, Control control) {
-		Color c = Color.RED;
-		for (SubNet s : control.getSimManager().getSubNets()) {
-			if (c == Color.RED) {
-				c = Color.CYAN;
-			} else if (c == Color.CYAN) {
-				c = Color.PINK;
-			} else {
-				c = Color.RED;
-			}
-
-			for (HolonObject cps : s.getObjects()) {
-				cps.setBorderColor(c);
-				controller.addTextToConsole(cps.getName());
-			}
-		}
-	}
-}
-
-Then in the program click on the Algorithm Button in the Simulation Menu.
+Then in the program click on the Algorithm Button.
 Go to your project and select the folder where your .java files are.
-Then you can Select the Algorithm you want to use in the ComboBox.
-Then Click on the Simulation Radio Button to switch to Simulation Mode.
+Then you can Select the Algorithm you want to use in the ComboBox, which then will be compiled.
 
 Your algorithm will now be used.

BIN
jars/CpsAlgorithm.jar


BIN
jars/CpsClassesModelController.jar


+ 0 - 44
src/api/AbstractAlgorithmSuperClass.java

@@ -1,44 +0,0 @@
-package api;
-
-/**
- * API Algorithm Super Class.
- * 
- * @author Gruppe14
- */
-public abstract class AbstractAlgorithmSuperClass {
-	private CpsAPI api;
-
-	/**
-	 * Constructor.
-	 * 
-	 * @param api
-	 *            the API
-	 */
-	public AbstractAlgorithmSuperClass(CpsAPI api) {
-		this.api = api;
-	}
-
-	/**
-	 * Returns the API.
-	 * 
-	 * @return The API
-	 */
-	public CpsAPI getAPI() {
-		return api;
-	}
-
-	/**
-	 * Sets the API.
-	 * 
-	 * @param api
-	 *            the API
-	 */
-	public void setAPI(CpsAPI api) {
-		this.api = api;
-	}
-
-	/**
-	 * This method will be called in each Iteration.
-	 */
-	abstract public void runAlgorithm();
-}

+ 0 - 166
src/api/CpsAPI.java

@@ -1,166 +0,0 @@
-package api;
-
-import java.awt.Color;
-import java.util.ArrayList;
-
-import classes.CpsEdge;
-import classes.AbstractCpsObject;
-import classes.HolonObject;
-import classes.HolonSwitch;
-import classes.SubNet;
-import ui.controller.Control;
-import ui.controller.SimulationManager;
-
-/**
- * Api Class for the Cps Controller.
- * 
- * @author Gruppe14
- */
-public class CpsAPI {
-	private Control controller;
-	private SimulationManager simManager;
-
-	/**
-	 * Constructor.
-	 * 
-	 * @param cont
-	 *            Controller
-	 */
-	public CpsAPI(Control cont) {
-		this.controller = cont;
-		this.simManager = controller.getSimManager();
-	}
-
-	/**
-	 * a SubNet contains all Components.
-	 * 
-	 * @return all SubNets on Canvas
-	 */
-	public ArrayList<SubNet> getSubNets() {
-		simManager.searchForSubNets();
-		return simManager.getSubNets();
-	}
-
-	/**
-	 * Return all Objects on the Canvas in no Specific order.
-	 * 
-	 * @return all Objects on Canvas in no specific order
-	 */
-	public ArrayList<AbstractCpsObject> getAllObjOnCanvas() {
-
-		return controller.getModel().getObjectsOnCanvas();
-	}
-
-	/**
-	 * Reutn all Edges on the Canvas in no specific order.
-	 * 
-	 * @return all Edges on Canvas
-	 */
-	public ArrayList<CpsEdge> getAllEdges() {
-		ArrayList<CpsEdge> result = new ArrayList<CpsEdge>();
-		for (SubNet sN : getSubNets()) {
-			result.addAll(sN.getEdges());
-		}
-		return result;
-	}
-
-	/**
-	 * Return all Swtiches on the Canvas in no specific order.
-	 * 
-	 * @return all Switches on Canvas.
-	 */
-	public ArrayList<HolonSwitch> getAllSwitches() {
-		ArrayList<HolonSwitch> result = new ArrayList<HolonSwitch>();
-		for (SubNet sN : getSubNets()) {
-			result.addAll(sN.getSwitches());
-		}
-		return result;
-	}
-
-	/**
-	 * Return all Holon Objects on the Canvas in no specific order.
-	 * 
-	 * @return all Holon Objects
-	 */
-	public ArrayList<HolonObject> getAllHolonObjects() {
-		ArrayList<HolonObject> result = new ArrayList<HolonObject>();
-		for (SubNet sN : getSubNets()) {
-			result.addAll(sN.getObjects());
-		}
-		return result;
-	}
-
-	/**
-	 * prints a given text on the console.
-	 * 
-	 * @param text
-	 *            Text
-	 */
-	public void consolePrint(String text) {
-		controller.addTextToConsole(text);
-	}
-
-	/**
-	 * prints a given text on the console with more details.
-	 * 
-	 * @param text
-	 *            the text that will be printed
-	 * @param color
-	 *            the color the text will have
-	 * @param p
-	 *            font size
-	 * @param bold
-	 *            true or false
-	 * @param italic
-	 *            true or false
-	 * @param nl
-	 *            new line after text
-	 */
-	public void consolePrint(String text, Color color, int p, boolean bold, boolean italic, boolean nl) {
-		controller.addTextToConsole(text, color, p, bold, italic, nl);
-	}
-
-	/**
-	 * changes the border color of given object to given color.
-	 * 
-	 * @param toChange
-	 *            CpsObject
-	 * @param color
-	 *            Color
-	 */
-	public void setBorderColorForObj(AbstractCpsObject toChange, Color color) {
-		toChange.setBorderColor(color);
-	}
-
-	/**
-	 * changes the borderColor for all objects in List to given color.
-	 * 
-	 * @param objects
-	 *            AttayList of CpsObject
-	 * @param color
-	 *            Color
-	 */
-	public void setBorderColorForMultObj(ArrayList<AbstractCpsObject> objects, Color color) {
-		for (AbstractCpsObject cps : objects) {
-			setBorderColorForObj(cps, color);
-		}
-	}
-
-	/**
-	 * resets the bordercolor of given object to default (white).
-	 * 
-	 * @param toReset
-	 *            CpsObject
-	 */
-	public void resetBorderColor(AbstractCpsObject toReset) {
-		toReset.setBorderColor(Color.WHITE);
-	}
-
-	/**
-	 * resets the bordercolor for all objects on canvas.
-	 */
-	public void resetBorderColorForAll() {
-		setBorderColorForMultObj(getAllObjOnCanvas(), Color.WHITE);
-	}
-
-}

+ 5 - 1
src/api/CpsAlgorithm.java

@@ -1,4 +1,8 @@
 package api;
+
+import ui.controller.Control;
+import ui.model.Model;
+
 /**
  * API Class for the CpsAlgorithm.
  * 
@@ -8,5 +12,5 @@ public interface CpsAlgorithm {
 	/**
 	 * This Method will be called in each Iteration.
 	 */
-	public void runAlgorithm();
+	public void runAlgorithm(Model model, Control controller);
 }

BIN
src/exampleAlgorithms/randomSwitchesAlgorithm.class


+ 34 - 0
src/exampleAlgorithms/randomSwitchesAlgorithm.java

@@ -0,0 +1,34 @@
+package exampleAlgorithms;
+
+import api.CpsAlgorithm;
+import ui.controller.Control;
+import ui.model.Model;
+import classes.*;
+
+public class randomSwitchesAlgorithm implements CpsAlgorithm {
+
+	@Override
+	public void runAlgorithm(Model model, Control controller) {
+		randomSwitches(model);
+	}
+
+	/**
+	 * Sets every Switch to a random state.
+	 * 
+	 * @param model
+	 *            the Model
+	 */
+	private void randomSwitches(Model model) {
+		for (HolonSwitch s : model.getSwitches()) {
+			// Set to Manual Mode
+			s.setManualMode(true);
+			// Generate a random number between 0 and 1
+			double randomDouble = Math.random();
+			if (randomDouble < 0.5) {
+				s.setManualState(true);
+			} else {
+				s.setManualState(false);
+			}
+		}
+	}
+}

+ 2 - 2
src/ui/controller/Control.java

@@ -13,6 +13,7 @@ import org.apache.commons.compress.archivers.ArchiveException;
 
 import com.google.gson.JsonParseException;
 
+import api.CpsAlgorithm;
 import classes.AbstractCpsObject;
 import classes.Category;
 import classes.CpsEdge;
@@ -20,7 +21,6 @@ import classes.CpsNode;
 import classes.CpsUpperNode;
 import classes.HolonElement;
 import classes.HolonObject;
-import cpsAlgorithm.CpsAlgorithm;
 import interfaces.CategoryListener;
 import ui.model.Model;
 import ui.view.FlexiblePane;
@@ -716,7 +716,7 @@ public class Control {
 	 */
 	public void runAlgorithm(Model model, Control controller) {
 		if (model.getAlgorithm() != null) {
-			((CpsAlgorithm) model.getAlgorithm()).RunAlgorithm(model, controller);
+			((CpsAlgorithm) model.getAlgorithm()).runAlgorithm(model, controller);
 		}
 	}
 

+ 6 - 2
src/ui/view/AlgorithmMenu.java

@@ -13,6 +13,9 @@ import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.tools.JavaCompiler;
 import javax.tools.ToolProvider;
@@ -26,14 +29,15 @@ import ui.model.Model;
  * 
  * @author Gruppe14
  */
-public class AlgorithmMenu extends JPanel {
+public class AlgorithmMenu extends JPanel{
 
 	private static final long serialVersionUID = 1L;
 
 	private JComboBox<Object> algoCombo = new JComboBox<>();
 	JButton algoFolderButton = new JButton(Languages.getLanguage()[85]);
 	private HashMap<String, File> algosHash = new HashMap<>();
-
+	private JPanel menuPanel = new JPanel();
+	
 	// root Directory
 	File root = null;
 

+ 45 - 47
src/ui/view/GUI.java

@@ -98,7 +98,7 @@ public class GUI<E> implements CategoryListener {
 
 	private JFrame frmCyberPhysical;
 
-	private final AlgorithmMenu simMenu;
+	private final AlgorithmMenu algorithmMenu;
 
 	private JTabbedPane tabTemp; // tabbedPane or tabbedPane2
 	private final JMenuBar menuBar = new JMenuBar();
@@ -299,7 +299,7 @@ public class GUI<E> implements CategoryListener {
 		control.initListener(this);
 		controller.setCanvas(canvas);
 		model.setConsole(console);
-		simMenu = new AlgorithmMenu(model, control);
+		algorithmMenu = new AlgorithmMenu(model, control);
 		initialize();
 		updateCategories(model.getCategories());
 		updCon = new UpdateController(model, controller);
@@ -416,7 +416,7 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				if (tabbedPane.getMousePosition() != null) {
 					tabTemp = tabbedPane;
-				} else {
+				} else if (tabbedPane2.getMousePosition() != null) {
 					tabTemp = tabbedPane2;
 				}
 				// Uppernode Canvas?
@@ -744,50 +744,50 @@ public class GUI<E> implements CategoryListener {
 
 		mnNewMenuView.add(mntmCanvasSize);
 		mnNewMenuView.add(mntmSplitView);
-		
+
 		// Split View
-				mntmSplitView.addActionListener(new ActionListener() {
-					@Override
-					public void actionPerformed(ActionEvent e) {
-						if (splitPaneCanvasConsole.getLeftComponent() instanceof JSplitPane) {
-							initSplit = true;
-							Component tempC = tabbedPane2.getSelectedComponent();
-							tabbedPane.setComponentAt(tabbedPane2.getSelectedIndex(), tempC);
-							tabbedPane2.removeAll();
-							splitPaneCanvasConsole.setLeftComponent(tabbedPane);
-						} else {
-							for (int i = 0; i < tabbedPane.getTabCount(); i++) {
-								tabbedPane2.addTab(tabbedPane.getTitleAt(i), null);
-								if (i >= 4) {
-									// tabbedPane2.setTabComponentAt(tabbedPane.getTabCount()
-									// - 1,
-									// new ButtonTabComponent(tabbedPane, tabbedPane2));
-								}
-							}
-							if (tabbedPane.getSelectedComponent() == statScrollPane) {
-								tabbedPane.setComponentAt(0, null);
-								tabbedPane2.setComponentAt(0, canvasSP);
-								tabbedPane2.setSelectedIndex(0);
-							} else {
-								tabbedPane.setComponentAt(1, null);
-								tabbedPane2.setComponentAt(1, statScrollPane);
-								tabbedPane2.setSelectedIndex(1);
-							}
-							tempSplit = new JSplitPane();
-							tempSplit.setBorder(null);
-							tempSplit.setRightComponent(tabbedPane2);
-							tempSplit.setLeftComponent(tabbedPane);
-							tempSplit.setDividerLocation(tabbedPane.getWidth() / 2);
-							tempSplit.setResizeWeight(0.9);
-							splitPaneCanvasConsole.setLeftComponent(tempSplit);
-							initSplit = false;
+		mntmSplitView.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				if (splitPaneCanvasConsole.getLeftComponent() instanceof JSplitPane) {
+					initSplit = true;
+					Component tempC = tabbedPane2.getSelectedComponent();
+					tabbedPane.setComponentAt(tabbedPane2.getSelectedIndex(), tempC);
+					tabbedPane2.removeAll();
+					splitPaneCanvasConsole.setLeftComponent(tabbedPane);
+				} else {
+					for (int i = 0; i < tabbedPane.getTabCount(); i++) {
+						tabbedPane2.addTab(tabbedPane.getTitleAt(i), null);
+						if (i >= 4) {
+							// tabbedPane2.setTabComponentAt(tabbedPane.getTabCount()
+							// - 1,
+							// new ButtonTabComponent(tabbedPane, tabbedPane2));
 						}
-						contentPane.updateUI();
 					}
+					if (tabbedPane.getSelectedComponent() == statScrollPane) {
+						tabbedPane.setComponentAt(0, null);
+						tabbedPane2.setComponentAt(0, canvasSP);
+						tabbedPane2.setSelectedIndex(0);
+					} else {
+						tabbedPane.setComponentAt(1, null);
+						tabbedPane2.setComponentAt(1, statScrollPane);
+						tabbedPane2.setSelectedIndex(1);
+					}
+					tempSplit = new JSplitPane();
+					tempSplit.setBorder(null);
+					tempSplit.setRightComponent(tabbedPane2);
+					tempSplit.setLeftComponent(tabbedPane);
+					tempSplit.setDividerLocation(tabbedPane.getWidth() / 2);
+					tempSplit.setResizeWeight(0.9);
+					splitPaneCanvasConsole.setLeftComponent(tempSplit);
+					initSplit = false;
+				}
+				contentPane.updateUI();
+			}
 
-				});
+		});
 		mnNewMenuView.add(mntmBackground);
-		
+
 		mntmBackground.addActionListener(new ActionListener() {
 
 			@Override
@@ -832,7 +832,7 @@ public class GUI<E> implements CategoryListener {
 
 		splitPane_1.setLeftComponent(lblHolonBodySize);
 
-		mnAlgorithm.add(simMenu);
+		menuBar.add(algorithmMenu);
 
 		menuBar.add(mnAlgorithm);
 
@@ -840,8 +840,6 @@ public class GUI<E> implements CategoryListener {
 
 		mnHelp.add(mntmAboutUs);
 
-		
-
 		tabbedPane.addChangeListener(new ChangeListener() {
 
 			@Override
@@ -1918,7 +1916,7 @@ public class GUI<E> implements CategoryListener {
 		splitGraphHolonEl.setBottomComponent(scrollElements);
 		canvasSP.setViewportView(canvas);
 		// holonSP.setViewportView(holonCanvas);
-		simMenu.setBackground(new Color(240, 240, 240));
+		algorithmMenu.setBackground(new Color(240, 240, 240));
 
 		tabbedPane.setBorder(null);
 		scrollProperties.setBorder(null);
@@ -2150,7 +2148,7 @@ public class GUI<E> implements CategoryListener {
 		eraseCategory = tempArray[29];
 		selectObjBeforeErase = tempArray[30];
 		// SimMenu
-		simMenu.algoFolderButton.setText(Languages.getLanguage()[85]);
+		algorithmMenu.algoFolderButton.setText(Languages.getLanguage()[85]);
 		// TimePanel
 		timePanel.playBtn.setToolTipText(Languages.getLanguage()[89]);
 		timePanel.timeResetBtn.setToolTipText(Languages.getLanguage()[90]);