Преглед изворни кода

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons

jess пре 7 година
родитељ
комит
79197094bd

+ 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.

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
Examples/Example_Big.json


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);
 		}
 	}
 

+ 4 - 1
src/ui/controller/SimulationManager.java

@@ -595,7 +595,10 @@ public class SimulationManager {
 
 	public void setFlexiblePane(FlexiblePane fp) {
 		flexPane = fp;
-		
+	}
+	
+	public FlexiblePane getFlexiblePane(){
+		return flexPane;
 	}
 
 }

+ 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;
 

+ 9 - 7
src/ui/view/FlexiblePane.java

@@ -23,6 +23,12 @@ public class FlexiblePane extends JScrollPane {
 		flexPanel = new JPanel();
 		flexPanel.setLayout(new BoxLayout(flexPanel, BoxLayout.Y_AXIS));
 		setViewportView(flexPanel);
+		class ResizeListener extends ComponentAdapter{
+			public void componentResized(ComponentEvent e){
+				flexPanel.requestFocusInWindow();
+			}
+		}
+		this.addComponentListener(new ResizeListener());
 	}
 	
 	public void recalculate(){
@@ -66,13 +72,9 @@ public class FlexiblePane extends JScrollPane {
 			((FlexibleData)flexPanel.getComponent(0)).setCons(gridCons);
 			((FlexibleData)flexPanel.getComponent(0)).setProdConsVal(gridProd, gridCons);
 		}
-		
-		class ResizeListener extends ComponentAdapter{
-			public void componentResized(ComponentEvent e){
-				flexPanel.requestFocusInWindow();
-			}
-		}
-		this.addComponentListener(new ResizeListener());
+		flexPanel.revalidate();
+		flexPanel.repaint();
+		flexPanel.updateUI();
 	}
 	
 	public JPanel getPanel(){

+ 7 - 7
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?
@@ -655,7 +655,7 @@ public class GUI<E> implements CategoryListener {
 		mntmEditShowedInformation.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				try {
-					ShowedInformationPopUp dialog = new ShowedInformationPopUp(canvas, contentPane);
+					ShowedInformationPopUp dialog = new ShowedInformationPopUp(canvas, contentPane, controller);
 					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 					dialog.setVisible(true);
 				} catch (Exception ex) {
@@ -832,7 +832,7 @@ public class GUI<E> implements CategoryListener {
 
 		splitPane_1.setLeftComponent(lblHolonBodySize);
 
-		mnAlgorithm.add(simMenu);
+		menuBar.add(algorithmMenu);
 
 		menuBar.add(mnAlgorithm);
 
@@ -1919,7 +1919,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);
@@ -2151,7 +2151,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]);

+ 7 - 2
src/ui/view/ShowedInformationPopUp.java

@@ -10,6 +10,8 @@ import javax.swing.JDialog;
 import javax.swing.JPanel;
 import javax.swing.border.EmptyBorder;
 
+import ui.controller.Control;
+
 /**
  * This Class represents a Popup to edit the shown Information.
  * 
@@ -21,6 +23,7 @@ public class ShowedInformationPopUp extends JDialog {
 	private final JPanel contentPanel = new JPanel();
 	private final JButton btnOk = new JButton("OK");
 	private MyCanvas canvas;
+	private Control controller;
 	private JCheckBox objectEnergyCheckbox;
 	private JCheckBox connectionCheckbox;
 	private JCheckBox colorizedBorderCheckbox;
@@ -33,17 +36,18 @@ public class ShowedInformationPopUp extends JDialog {
 	 * @param canvas
 	 *            the Canvas
 	 */
-	public ShowedInformationPopUp(MyCanvas canvas, JPanel update) {
+	public ShowedInformationPopUp(MyCanvas canvas, JPanel update, Control cont) {
 		super((java.awt.Frame) null, true);
 		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
 		this.setTitle(Languages.getLanguage()[31]);
-		setBounds(100, 100, 400, 254);
+		setBounds(100, 100, 400, 276);
 		getContentPane().setLayout(new BorderLayout());
 		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
 		getContentPane().add(contentPanel, BorderLayout.CENTER);
 		contentPanel.setLayout(null);
 		this.canvas = canvas;
 		this.toUpdate = update;
+		controller = cont;
 
 		objectEnergyCheckbox = new JCheckBox(Languages.getLanguage()[32]);
 		objectEnergyCheckbox.setBounds(19, 19, 300, 23);
@@ -59,6 +63,7 @@ public class ShowedInformationPopUp extends JDialog {
 			public void actionPerformed(ActionEvent arg0) {
 				setInformation(connectionCheckbox.isSelected(), objectEnergyCheckbox.isSelected(),
 						colorizedBorderCheckbox.isSelected(), nodeOfnodeConnectionCheckbox.isSelected());
+				controller.getSimManager().getFlexiblePane().recalculate();
 				dispose();
 			}
 		});

+ 26 - 0
src/ui/view/StatPanel2.java

@@ -61,6 +61,10 @@ public class StatPanel2 extends JSplitPane implements GraphListener {
 	public static final String AVG_WASTED_ENERGY_HOLONS = "Average wasted Energy";
 	public static final String NR_BROKEN_EDGES = "Amount of broken Edges";
 	public static final String PROD_CONS_RATIO = "Producer/Consumer Ratio";
+	public static final String AVG_CLOSED_SW_HOLON = "Average of closed Switches per Holon";
+	public static final String AVG_ACTIVE_ELEMENTS_HOLON = "Average of active Elements per Holon";
+	public static final String AVG_INACTIVE_ELEMENTS_HOLON = "Average of inactive Elements per Holon";
+	public static final String AVG_PRODUCTION_HOLON = "Average Production per Holon";
 	
 	public static final String TOT_PROD_OBJ = "total Production";
 	public static final String TOT_CONS_OBJ = "total Consumption";
@@ -127,6 +131,11 @@ public class StatPanel2 extends JSplitPane implements GraphListener {
 		holonHashtable.put(AVG_WASTED_ENERGY_HOLONS, new PropertyDataSet());
 		holonHashtable.put(NR_BROKEN_EDGES, new PropertyDataSet());
 		holonHashtable.put(PROD_CONS_RATIO, new PropertyDataSet());
+		holonHashtable.put(AVG_CLOSED_SW_HOLON, new PropertyDataSet());
+		holonHashtable.put(AVG_ACTIVE_ELEMENTS_HOLON, new PropertyDataSet());
+		holonHashtable.put(AVG_INACTIVE_ELEMENTS_HOLON, new PropertyDataSet());
+		holonHashtable.put(AVG_PRODUCTION_HOLON, new PropertyDataSet());
+
 		
 		//propValTable associates the Strings to the numbers
 		propValTable = new Hashtable<String, Integer>();
@@ -150,6 +159,15 @@ public class StatPanel2 extends JSplitPane implements GraphListener {
 		propValTable.put(AVG_WASTED_ENERGY_HOLONS, TrackedDataSet.AVG_WASTED_ENERGY_IN_HOLONS);
 		propValTable.put(NR_BROKEN_EDGES, TrackedDataSet.AMOUNT_BROKEN_EDGES);
 		propValTable.put(PROD_CONS_RATIO, TrackedDataSet.RATIO_PRODUCERS_CONSUMERS);
+		propValTable.put(AVG_CLOSED_SW_HOLON, TrackedDataSet.AVG_AMOUNT_CLOSED_SWITCHES_IN_HOLONS);
+		propValTable.put(AVG_ACTIVE_ELEMENTS_HOLON, TrackedDataSet.AVG_AMOUNT_ACTIVE_ELEMENTS_IN_HOLONS);
+		propValTable.put(AVG_INACTIVE_ELEMENTS_HOLON, TrackedDataSet.AVG_AMOUNT_INACTIVE_ELEMENTS_IN_HOLONS);
+		propValTable.put(AVG_PRODUCTION_HOLON, TrackedDataSet.AVG_PRODUCED_ENERGY_IN_HOLONS);
+
+
+		
+
+
 
 		JScrollPane graphScrollPane = new JScrollPane();
 		graphScrollPane.setBorder(null);
@@ -193,6 +211,14 @@ public class StatPanel2 extends JSplitPane implements GraphListener {
 		holonNode.add(new DefaultMutableTreeNode(AVG_WASTED_ENERGY_HOLONS));
 		holonNode.add(new DefaultMutableTreeNode(AVG_CONS_ENERGY_IN_HOLONS));
 		holonNode.add(new DefaultMutableTreeNode(AVG_PRODS_IN_HOLONS));
+		holonNode.add(new DefaultMutableTreeNode(AVG_CLOSED_SW_HOLON));
+		holonNode.add(new DefaultMutableTreeNode(AVG_ACTIVE_ELEMENTS_HOLON));
+		holonNode.add(new DefaultMutableTreeNode(AVG_INACTIVE_ELEMENTS_HOLON));
+		holonNode.add(new DefaultMutableTreeNode(AVG_PRODUCTION_HOLON));
+
+
+
+
 
 		objectsNode = new DefaultMutableTreeNode("Objects");
 		objectsNode.add(new DefaultMutableTreeNode("empty"));

Неке датотеке нису приказане због велике количине промена