|
@@ -7,19 +7,26 @@ import java.io.FileNotFoundException;
|
|
|
import java.io.FileReader;
|
|
|
import java.text.NumberFormat;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Hashtable;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import javax.swing.BorderFactory;
|
|
|
+import javax.swing.Box;
|
|
|
import javax.swing.BoxLayout;
|
|
|
+import javax.swing.ImageIcon;
|
|
|
import javax.swing.JButton;
|
|
|
+import javax.swing.JCheckBox;
|
|
|
import javax.swing.JFileChooser;
|
|
|
import javax.swing.JFormattedTextField;
|
|
|
import javax.swing.JFrame;
|
|
|
import javax.swing.JLabel;
|
|
|
import javax.swing.JPanel;
|
|
|
+import javax.swing.JScrollPane;
|
|
|
import javax.swing.JSlider;
|
|
|
+import javax.swing.JSplitPane;
|
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
|
import javax.swing.text.NumberFormatter;
|
|
|
|
|
@@ -32,6 +39,7 @@ import com.google.gson.JsonSyntaxException;
|
|
|
import com.google.gson.annotations.Expose;
|
|
|
import com.google.gson.annotations.SerializedName;
|
|
|
|
|
|
+import addOns.JSON.HolonElementSketch;
|
|
|
import api.AddOn;
|
|
|
import classes.AbstractCanvasObject;
|
|
|
import classes.Constrain;
|
|
@@ -43,12 +51,20 @@ import classes.HolonObject;
|
|
|
import classes.HolonSwitch;
|
|
|
import ui.controller.Control;
|
|
|
import ui.model.Model;
|
|
|
+import ui.view.RandomPriotity;
|
|
|
+import utility.Random;
|
|
|
+import utility.Util;
|
|
|
|
|
|
public class Randomizer implements AddOn {
|
|
|
private Control control;
|
|
|
private int minAmountOfElements = 3;
|
|
|
private int maxAmountOfElements = 7;
|
|
|
private JPanel content = new JPanel();
|
|
|
+ private JPanel tablePanel = new JPanel();
|
|
|
+ private RandomPriotity prioPanel = new RandomPriotity();
|
|
|
+ private JCheckBox priorityCheckbox = new JCheckBox("Random");
|
|
|
+ private boolean useRandomPriority = true;
|
|
|
+
|
|
|
|
|
|
public static void main(String[] args)
|
|
|
{
|
|
@@ -62,23 +78,92 @@ public class Randomizer implements AddOn {
|
|
|
private File file;
|
|
|
public double randomChance = 1.0;
|
|
|
|
|
|
+
|
|
|
+ private HashMap<HolonObject, Boolean> objectMap = new HashMap<HolonObject, Boolean>();
|
|
|
+ private List<JCheckBox> checkboxList = new ArrayList<JCheckBox>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public Randomizer(){
|
|
|
+ for(int i = 0; i < 30; i++) {
|
|
|
+ objectMap.put(new HolonObject("dude"), true);
|
|
|
+ }
|
|
|
content.setLayout(new BorderLayout());
|
|
|
- content.add(createParameterPanel(), BorderLayout.CENTER);
|
|
|
+ JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createParameterPanel(), createFilterPanel());
|
|
|
+ splitPane.setDividerLocation(0.5);
|
|
|
+ content.add(splitPane, 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));
|
|
|
+ private JScrollPane createFilterPanel() {
|
|
|
+
|
|
|
+
|
|
|
+ tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS));
|
|
|
+ JScrollPane scrollPanel = new JScrollPane(tablePanel);
|
|
|
+ scrollPanel.setPreferredSize(new Dimension(300,300));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ fillTablePanel();
|
|
|
+ return scrollPanel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillTablePanel() {
|
|
|
+
|
|
|
+ tablePanel.removeAll();
|
|
|
+ checkboxList.clear();
|
|
|
+
|
|
|
+
|
|
|
+ JPanel headPanel = new JPanel();
|
|
|
+ headPanel.setLayout(new BoxLayout(headPanel, BoxLayout.LINE_AXIS));
|
|
|
+ headPanel.add(new JLabel("FILTER"));
|
|
|
+ headPanel.add(Box.createHorizontalGlue());
|
|
|
+ headPanel.add(new JLabel("SelectAll"));
|
|
|
+ JCheckBox selectAllCheckbox = new JCheckBox();
|
|
|
+ selectAllCheckbox.setSelected(true);
|
|
|
+ selectAllCheckbox.addItemListener(input -> checkboxList.forEach(box -> box.setSelected(selectAllCheckbox.isSelected())));
|
|
|
+ headPanel.add(selectAllCheckbox);
|
|
|
+ tablePanel.add(headPanel);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ int lineSpace = 20;
|
|
|
+ for(HolonObject hObject: objectMap.keySet().stream().sorted(Comparator.comparing(ob -> ob.getName())).collect(Collectors.toList())) {
|
|
|
+
|
|
|
+ JPanel entryPanel = new JPanel();
|
|
|
+ entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.LINE_AXIS));
|
|
|
+ JLabel label = new JLabel(hObject.getName() + "[" + hObject.getId() + "]", new ImageIcon(Util.loadImage(hObject.getImage(), lineSpace, lineSpace)), JLabel.LEFT);
|
|
|
+ entryPanel.add(label);
|
|
|
+ entryPanel.add(Box.createHorizontalGlue());
|
|
|
+ JCheckBox checkbox = new JCheckBox();
|
|
|
+ checkbox.setSelected(true);
|
|
|
+ checkbox.addItemListener(change -> {
|
|
|
+ objectMap.put(hObject, checkbox.isSelected());
|
|
|
+ System.out.println("hObject:" + hObject + " changed to " + checkbox.isSelected());
|
|
|
+ });
|
|
|
+ checkboxList.add(checkbox);
|
|
|
+ entryPanel.add(checkbox);
|
|
|
+ tablePanel.add(entryPanel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private JSplitPane createParameterPanel() {
|
|
|
+
|
|
|
+ JPanel choosePanel = new JPanel(null);
|
|
|
+ choosePanel.setPreferredSize(new Dimension(300,200));
|
|
|
+ choosePanel.setMinimumSize(new Dimension(300,200));
|
|
|
JLabel minAmount = new JLabel("minAmountOfElements:");
|
|
|
minAmount.setBounds(20, 60 , 200, 20);
|
|
|
- parameterPanel.add(minAmount);
|
|
|
+ choosePanel.add(minAmount);
|
|
|
JLabel maxAmount = new JLabel("maxAmountOfElements:");
|
|
|
maxAmount.setBounds(20, 85 , 200, 20);
|
|
|
- parameterPanel.add(maxAmount);
|
|
|
+ choosePanel.add(maxAmount);
|
|
|
|
|
|
|
|
|
NumberFormat format = NumberFormat.getIntegerInstance();
|
|
@@ -94,27 +179,41 @@ public class Randomizer implements AddOn {
|
|
|
minAmountTextField.setToolTipText("Only positive Integer.");
|
|
|
minAmountTextField.addPropertyChangeListener(actionEvent -> this.minAmountOfElements = Integer.parseInt(minAmountTextField.getValue().toString()));
|
|
|
minAmountTextField.setBounds(220, 60, 50, 20);
|
|
|
- parameterPanel.add(minAmountTextField);
|
|
|
+ choosePanel.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);
|
|
|
-
|
|
|
+ choosePanel.add(maxAmountTextField);
|
|
|
|
|
|
JButton chooseFileButton = new JButton("Choose File");
|
|
|
chooseFileButton.setBounds(20, 10, 200, 30);
|
|
|
chooseFileButton.addActionListener(actionEvent -> file = openCatalogFile());
|
|
|
- parameterPanel.add(chooseFileButton);
|
|
|
+ choosePanel.add(chooseFileButton);
|
|
|
|
|
|
|
|
|
JSlider bitSlider = createFlipChanceSlider();
|
|
|
bitSlider.setBounds(10, 110, 280, 80);
|
|
|
- parameterPanel.add(bitSlider);
|
|
|
+ choosePanel.add(bitSlider);
|
|
|
+
|
|
|
+ JPanel prioritySelectionPanel = new JPanel();
|
|
|
+ prioritySelectionPanel.setLayout(new BoxLayout(prioritySelectionPanel, BoxLayout.PAGE_AXIS));
|
|
|
+
|
|
|
+
|
|
|
+ prioritySelectionPanel.add(this.priorityCheckbox);
|
|
|
+ priorityCheckbox.addItemListener(change -> {
|
|
|
+ prioPanel.setEnabled(priorityCheckbox.isSelected());
|
|
|
+ useRandomPriority = priorityCheckbox.isSelected();
|
|
|
+ });
|
|
|
+ priorityCheckbox.setSelected(useRandomPriority);
|
|
|
|
|
|
|
|
|
+ prioritySelectionPanel.add(prioPanel);
|
|
|
+ JSplitPane parameterPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, choosePanel, prioritySelectionPanel);
|
|
|
+ parameterPanel.setPreferredSize(new Dimension(600,200));
|
|
|
+
|
|
|
return parameterPanel;
|
|
|
}
|
|
|
|
|
@@ -124,16 +223,21 @@ public class Randomizer implements AddOn {
|
|
|
if(file == null) return;
|
|
|
readCatalogFromJson(file, holonElementCatalog);
|
|
|
holonElementCatalog.forEach(con -> con.checkValues());
|
|
|
-
|
|
|
- List<HolonObject> holonObjectList = new ArrayList<HolonObject>();
|
|
|
- rollOutNodes(control.getModel().getObjectsOnCanvas(), holonObjectList, control.getModel().getCurIteration());
|
|
|
- holonObjectList.forEach(con -> {
|
|
|
- con.getElements().clear();
|
|
|
+ objectMap.forEach((hObject, state) ->{
|
|
|
+
|
|
|
+ System.out.println("hObject:" + hObject + " state:" + state);
|
|
|
+ if(!state) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ hObject.getElements().clear();
|
|
|
int randomAmountOfElementsToAdd = Random.nextIntegerInRange(minAmountOfElements, maxAmountOfElements + 1);
|
|
|
for(int i = 0; i < randomAmountOfElementsToAdd; i++) {
|
|
|
- HolonElement ele = holonElementCatalog.get(Random.nextIntegerInRange(0, holonElementCatalog.size())).createHolonElement(control.getModel(), Random.nextDouble() < randomChance , con.getName());
|
|
|
- con.getElements().add(ele);
|
|
|
+ HolonElement ele = holonElementCatalog.get(Random.nextIntegerInRange(0, holonElementCatalog.size())).createHolonElement(control.getModel(), Random.nextDouble() < randomChance , hObject.getName());
|
|
|
+ if(this.useRandomPriority) ele.setPriority(prioPanel.getPriority());
|
|
|
+ hObject.getElements().add(ele);
|
|
|
}
|
|
|
+
|
|
|
});
|
|
|
control.calculateStateAndVisualForCurrentTimeStep();
|
|
|
control.updateCanvas();
|
|
@@ -141,21 +245,26 @@ public class Randomizer implements AddOn {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void rollOutNodes(List<AbstractCanvasObject> nodes, List<HolonObject> consumerList, int timeStep) {
|
|
|
+ private void fillObjectMap(List<AbstractCanvasObject> nodes) {
|
|
|
for (AbstractCanvasObject aCps : nodes) {
|
|
|
if (aCps instanceof HolonObject) {
|
|
|
HolonObject hO = (HolonObject) aCps;
|
|
|
-
|
|
|
- consumerList.add(hO);
|
|
|
+ objectMap.put(hO, true);
|
|
|
+
|
|
|
}
|
|
|
else if(aCps instanceof GroupNode) {
|
|
|
- rollOutNodes(((GroupNode)aCps).getNodes(), consumerList ,timeStep );
|
|
|
+ fillObjectMap(((GroupNode)aCps).getNodes());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ public void updateFilterList() {
|
|
|
+ objectMap.clear();
|
|
|
+ fillObjectMap(control.getModel().getObjectsOnCanvas());
|
|
|
+
|
|
|
+ this.fillTablePanel();
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public JPanel getPanel() {
|
|
@@ -165,103 +274,11 @@ public class Randomizer implements AddOn {
|
|
|
public void setController(Control control) {
|
|
|
this.control = control;
|
|
|
|
|
|
+
|
|
|
+ updateFilterList();
|
|
|
}
|
|
|
+
|
|
|
|
|
|
- private class HolonElementSketch {
|
|
|
-
|
|
|
- public String name;
|
|
|
- public int minAmount;
|
|
|
- public int maxAmount;
|
|
|
- public float energy;
|
|
|
-
|
|
|
- public String priority;
|
|
|
-
|
|
|
-
|
|
|
- public double flexChance;
|
|
|
- @Expose
|
|
|
- public float minCost;
|
|
|
- public float maxCost;
|
|
|
-
|
|
|
-
|
|
|
- private int minDuration;
|
|
|
- private int maxDuration;
|
|
|
-
|
|
|
- private int minCooldown;
|
|
|
- private int maxCooldown;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public HolonElementSketch(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, boolean active, String nameOfHolonObject) {
|
|
|
- HolonElement ele = new HolonElement(name, Random.nextIntegerInRange(minAmount, maxAmount + 1), energy , model);
|
|
|
- ele.setActive(active);
|
|
|
- if(Random.nextDouble() < flexChance)addFlex(ele, nameOfHolonObject);
|
|
|
- ele.setPriority(Priority.valueOf(priority));
|
|
|
- return ele;
|
|
|
- }
|
|
|
-
|
|
|
- public void checkValues() {
|
|
|
- minAmount = Math.abs(minAmount);
|
|
|
- maxAmount = Math.abs(maxAmount);
|
|
|
- if(maxAmount < minAmount) {
|
|
|
-
|
|
|
- int intermediate = minAmount;
|
|
|
- minAmount = maxAmount;
|
|
|
- maxAmount = intermediate;
|
|
|
- }
|
|
|
-
|
|
|
- minDuration = Math.abs(minDuration);
|
|
|
- maxDuration = Math.abs(maxDuration);
|
|
|
- if(maxDuration < minDuration) {
|
|
|
-
|
|
|
- int intermediate = minDuration;
|
|
|
- minDuration = maxDuration;
|
|
|
- maxDuration = intermediate;
|
|
|
- }
|
|
|
- minCooldown = Math.abs(minCooldown);
|
|
|
- maxCooldown = Math.abs(maxCooldown);
|
|
|
- if(maxCooldown < minCooldown) {
|
|
|
-
|
|
|
- int intermediate = minCooldown;
|
|
|
- minCooldown = maxCooldown;
|
|
|
- maxCooldown = intermediate;
|
|
|
- }
|
|
|
- minCost = Math.abs(minCost);
|
|
|
- maxCost = Math.abs(maxCost);
|
|
|
- if(maxCost < minCost) {
|
|
|
-
|
|
|
- float intermediate = minCost;
|
|
|
- minCost = maxCost;
|
|
|
- maxCost = intermediate;
|
|
|
- }
|
|
|
- flexChance = Math.max(0, Math.min(1, flexChance));
|
|
|
- }
|
|
|
-
|
|
|
- public void addFlex(HolonElement ele, String nameOfHolonObject) {
|
|
|
- Flexibility toCreateFlex = new Flexibility(ele);
|
|
|
- boolean flexType = ele.isActive();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- toCreateFlex.name = nameOfHolonObject + "_" + ele.getEleName() + (flexType?"_OnFlex":"_OffFlex");
|
|
|
- toCreateFlex.speed = 0;
|
|
|
- toCreateFlex.setDuration(Random.nextIntegerInRange(minDuration, maxDuration+1));
|
|
|
- toCreateFlex.cost = Random.nextFloatInRange(minCost, maxCost);
|
|
|
- toCreateFlex.setCooldown(Random.nextIntegerInRange(minCooldown, maxCooldown+1));
|
|
|
- toCreateFlex.offered=true;
|
|
|
- if(flexType) {
|
|
|
- toCreateFlex.constrainList.add(Constrain.createOnConstrain());
|
|
|
- }else {
|
|
|
- toCreateFlex.constrainList.add(Constrain.createOffConstrain());
|
|
|
- }
|
|
|
- ele.flexList.add(toCreateFlex);
|
|
|
- }
|
|
|
- }
|
|
|
private JSlider createFlipChanceSlider() {
|
|
|
JSlider flipChance =new JSlider(JSlider.HORIZONTAL,0, 100, 100);
|
|
|
flipChance.setBorder(BorderFactory.createTitledBorder("ActiveProbability"));
|
|
@@ -272,7 +289,11 @@ public class Randomizer implements AddOn {
|
|
|
labelTable.put( Integer.valueOf(0), new JLabel("0.0") );
|
|
|
labelTable.put( Integer.valueOf(50), new JLabel("0.5") );
|
|
|
labelTable.put( Integer.valueOf(100), new JLabel("1.0") );
|
|
|
- flipChance.addChangeListener(actionEvent ->randomChance =(double)flipChance.getValue()/100.0);
|
|
|
+ flipChance.setToolTipText("" +randomChance);
|
|
|
+ flipChance.addChangeListener(actionEvent ->{
|
|
|
+ randomChance =(double)flipChance.getValue()/100.0;
|
|
|
+ flipChance.setToolTipText("" +randomChance);
|
|
|
+ });
|
|
|
flipChance.setLabelTable( labelTable );
|
|
|
flipChance.setPaintLabels(true);
|
|
|
return flipChance;
|
|
@@ -280,45 +301,7 @@ public class Randomizer implements AddOn {
|
|
|
|
|
|
|
|
|
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
- public static float nextFloatInRange(float min, float max) {
|
|
|
- return min + random.nextFloat() * Math.abs(max - min);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static double nextDoubleInRange(double min, double max) {
|
|
|
- return min + random.nextDouble() * Math.abs(max - min);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 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 readCatalogFromJson(File file, List<HolonElementSketch> catalog) {
|
|
|
Gson gson = new Gson();
|
|
|
JsonParser parser = new JsonParser();
|
|
@@ -328,6 +311,8 @@ public class Randomizer implements AddOn {
|
|
|
JsonArray jsonArray = jsonTreeRoot.getAsJsonArray();
|
|
|
jsonArray.forEach(jsonObject -> {
|
|
|
HolonElementSketch newObject= gson.fromJson( jsonObject, HolonElementSketch.class);
|
|
|
+ System.out.println(gson.toJson(newObject));
|
|
|
+
|
|
|
catalog.add(newObject);
|
|
|
});
|
|
|
}
|