|
@@ -0,0 +1,234 @@
|
|
|
+package exampleAlgorithms;
|
|
|
+
|
|
|
+import java.awt.BorderLayout;
|
|
|
+import java.awt.Dimension;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.text.NumberFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Hashtable;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import javax.swing.BorderFactory;
|
|
|
+import javax.swing.BoxLayout;
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JFileChooser;
|
|
|
+import javax.swing.JFormattedTextField;
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JPanel;
|
|
|
+import javax.swing.JSlider;
|
|
|
+import javax.swing.filechooser.FileNameExtensionFilter;
|
|
|
+import javax.swing.text.NumberFormatter;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.JsonArray;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonIOException;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
+import com.google.gson.JsonSyntaxException;
|
|
|
+
|
|
|
+import api.Algorithm;
|
|
|
+import classes.AbstractCpsObject;
|
|
|
+import classes.HolonElement;
|
|
|
+import classes.HolonObject;
|
|
|
+import ui.controller.Control;
|
|
|
+import ui.model.Model;
|
|
|
+
|
|
|
+public class Randomizer implements Algorithm {
|
|
|
+ private Control control;
|
|
|
+ private int minAmountOfElements = 3;
|
|
|
+ private int maxAmountOfElements = 7;
|
|
|
+ private JPanel content = new JPanel();
|
|
|
+ //To Test the Layout Faster
|
|
|
+ public static void main(String[] args)
|
|
|
+ {
|
|
|
+ JFrame newFrame = new JFrame("exampleWindow");
|
|
|
+ Randomizer instance = new Randomizer();
|
|
|
+ newFrame.setContentPane(instance.getAlgorithmPanel());
|
|
|
+ newFrame.pack();
|
|
|
+ newFrame.setVisible(true);
|
|
|
+ newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
+ }
|
|
|
+ private File file;
|
|
|
+
|
|
|
+ public Randomizer(){
|
|
|
+ content.setLayout(new BorderLayout());
|
|
|
+ content.add(createParameterPanel(), BorderLayout.CENTER);
|
|
|
+ JButton buttonRun = new JButton("Run");
|
|
|
+ buttonRun.addActionListener(actionEvent -> run());
|
|
|
+ content.add(buttonRun, BorderLayout.PAGE_END);
|
|
|
+ content.setPreferredSize(new Dimension(300,300));
|
|
|
+ }
|
|
|
+ private JPanel createParameterPanel() {
|
|
|
+ JPanel parameterPanel = new JPanel(null);
|
|
|
+ parameterPanel.setPreferredSize(new Dimension(300,300));
|
|
|
+ JLabel minAmount = new JLabel("minAmountOfElements:");
|
|
|
+ minAmount.setBounds(20, 60 , 200, 20);
|
|
|
+ parameterPanel.add(minAmount);
|
|
|
+ JLabel maxAmount = new JLabel("maxAmountOfElements:");
|
|
|
+ maxAmount.setBounds(20, 85 , 200, 20);
|
|
|
+ parameterPanel.add(maxAmount);
|
|
|
+
|
|
|
+ //Integer formatter
|
|
|
+ NumberFormat format = NumberFormat.getIntegerInstance();
|
|
|
+ format.setGroupingUsed(false);
|
|
|
+ format.setParseIntegerOnly(true);
|
|
|
+ NumberFormatter integerFormatter = new NumberFormatter(format);
|
|
|
+ integerFormatter.setMinimum(0);
|
|
|
+ integerFormatter.setCommitsOnValidEdit(true);
|
|
|
+
|
|
|
+
|
|
|
+ JFormattedTextField minAmountTextField = new JFormattedTextField(integerFormatter);
|
|
|
+ minAmountTextField.setValue(this.minAmountOfElements);
|
|
|
+ minAmountTextField.setToolTipText("Only positive Integer.");
|
|
|
+ minAmountTextField.addPropertyChangeListener(actionEvent -> this.minAmountOfElements = Integer.parseInt(minAmountTextField.getValue().toString()));
|
|
|
+ minAmountTextField.setBounds(220, 60, 50, 20);
|
|
|
+ parameterPanel.add(minAmountTextField);
|
|
|
+
|
|
|
+ JFormattedTextField maxAmountTextField = new JFormattedTextField(integerFormatter);
|
|
|
+ maxAmountTextField.setValue(this.maxAmountOfElements);
|
|
|
+ maxAmountTextField.setToolTipText("Only positive Integer.");
|
|
|
+ maxAmountTextField.addPropertyChangeListener(actionEvent -> this.maxAmountOfElements = Integer.parseInt(maxAmountTextField.getValue().toString()));
|
|
|
+ maxAmountTextField.setBounds(220, 85, 50, 20);
|
|
|
+ parameterPanel.add(maxAmountTextField);
|
|
|
+
|
|
|
+
|
|
|
+ JButton chooseFileButton = new JButton("Choose File");
|
|
|
+ chooseFileButton.setBounds(20, 10, 200, 30);
|
|
|
+ chooseFileButton.addActionListener(actionEvent -> file = openCatalogFile());
|
|
|
+ parameterPanel.add(chooseFileButton);
|
|
|
+ return parameterPanel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void run() {
|
|
|
+ List<HolonObjectSketch> holonObjectCatalog = new ArrayList<HolonObjectSketch>();
|
|
|
+ if(file == null)file = openCatalogFile();
|
|
|
+ if(file == null) return;
|
|
|
+ readMsgFromJSONwithGSON(file, holonObjectCatalog);
|
|
|
+ List<HolonObject> consumerList = new ArrayList<HolonObject>();
|
|
|
+ for (AbstractCpsObject aCps : control.getModel().getObjectsOnCanvas()) {
|
|
|
+ if (aCps instanceof HolonObject) {
|
|
|
+ HolonObject hO = (HolonObject) aCps;
|
|
|
+ if(hO.getEnergyAtTimeStep(control.getModel().getCurIteration()) < 0)consumerList.add(hO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ consumerList.forEach(con -> {
|
|
|
+ con.getElements().clear();
|
|
|
+ int randomAmountOfElementsToAdd = Random.nextIntegerInRange(minAmountOfElements, maxAmountOfElements + 1);
|
|
|
+ for(int i = 0; i < randomAmountOfElementsToAdd; i++) {
|
|
|
+ con.getElements().add(holonObjectCatalog.get(Random.nextIntegerInRange(0, holonObjectCatalog.size())).createHolonElement(control.getModel()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ control.calculateStateAndVisualForCurrentTimeStep();
|
|
|
+ control.updateCanvas();
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public JPanel getAlgorithmPanel() {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void setController(Control control) {
|
|
|
+ this.control = control;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private class HolonObjectSketch {
|
|
|
+ public String name;
|
|
|
+ public int minAmount;
|
|
|
+ public int maxAmount;
|
|
|
+ public float energy;
|
|
|
+ public HolonObjectSketch(String name, int minAmount, int maxAmount, float energy) {
|
|
|
+ this.name = name;
|
|
|
+ this.minAmount = minAmount;
|
|
|
+ this.maxAmount = maxAmount;
|
|
|
+ this.energy = energy;
|
|
|
+ }
|
|
|
+ public HolonElement createHolonElement(Model model) {
|
|
|
+ return new HolonElement(name, Random.nextIntegerInRange(minAmount, maxAmount + 1), energy , model);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkMinMax() {
|
|
|
+ minAmount = Math.abs(minAmount);
|
|
|
+ maxAmount = Math.abs(maxAmount);
|
|
|
+ if(maxAmount < minAmount) {
|
|
|
+ //Swap
|
|
|
+ int intermediate = minAmount;
|
|
|
+ minAmount = maxAmount;
|
|
|
+ maxAmount = intermediate;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static class Random{
|
|
|
+
|
|
|
+
|
|
|
+ private static java.util.Random random = new java.util.Random();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * True or false
|
|
|
+ * @return the random boolean.
|
|
|
+ */
|
|
|
+ public static boolean nextBoolean(){
|
|
|
+ return random.nextBoolean();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Between 0.0(inclusive) and 1.0 (exclusive)
|
|
|
+ * @return the random double.
|
|
|
+ */
|
|
|
+ public static double nextDouble() {
|
|
|
+ return random.nextDouble();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Random Int in Range [min;max[ with UniformDistirbution
|
|
|
+ * @param min
|
|
|
+ * @param max
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int nextIntegerInRange(int min, int max) {
|
|
|
+ return min + random.nextInt(max - min);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void readMsgFromJSONwithGSON(File file, List<HolonObjectSketch> catalog) {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ JsonParser parser = new JsonParser();
|
|
|
+ try {
|
|
|
+ JsonElement jsonTreeRoot = parser.parse(new FileReader(file));
|
|
|
+ if(jsonTreeRoot.isJsonArray()) {
|
|
|
+ JsonArray jsonArray = jsonTreeRoot.getAsJsonArray();
|
|
|
+ jsonArray.forEach(jsonObject -> {
|
|
|
+ HolonObjectSketch newObject= gson.fromJson( jsonObject, HolonObjectSketch.class);
|
|
|
+ catalog.add(newObject);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (JsonSyntaxException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (JsonIOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public File openCatalogFile() {
|
|
|
+ JFileChooser fileChooser = new JFileChooser();
|
|
|
+ fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")+"/src/"));
|
|
|
+ fileChooser.setFileFilter(new FileNameExtensionFilter("JSON Files", "json"));
|
|
|
+ fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
|
|
+ fileChooser.setAcceptAllFileFilterUsed(false);
|
|
|
+ int result = fileChooser.showOpenDialog(content);
|
|
|
+ if(result == JFileChooser.APPROVE_OPTION) {
|
|
|
+ //Found a file
|
|
|
+ return fileChooser.getSelectedFile();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|