|
@@ -0,0 +1,209 @@
|
|
|
+package exampleAlgorithms;
|
|
|
+
|
|
|
+import java.awt.BorderLayout;
|
|
|
+import java.awt.Dimension;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Hashtable;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import javax.swing.BorderFactory;
|
|
|
+import javax.swing.BoxLayout;
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JPanel;
|
|
|
+import javax.swing.JSlider;
|
|
|
+
|
|
|
+import api.Algorithm;
|
|
|
+import classes.AbstractCpsObject;
|
|
|
+import classes.CpsUpperNode;
|
|
|
+import classes.HolonElement;
|
|
|
+import classes.HolonElement.Priority;
|
|
|
+import classes.HolonObject;
|
|
|
+import classes.HolonSwitch;
|
|
|
+import ui.controller.Control;
|
|
|
+
|
|
|
+/**
|
|
|
+ * A short algorithm to distribute the Priorities for the whole Canvas.
|
|
|
+ * @author tom
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+public class RandomPriority implements Algorithm {
|
|
|
+ /**
|
|
|
+ * Its like a Gradient the Values are on one Axis from Start to End:
|
|
|
+ * |--start--lm--mh--he--end--|
|
|
|
+ * lm: lowMedium
|
|
|
+ * mh: mediumHigh
|
|
|
+ * he: highEssential
|
|
|
+ */
|
|
|
+ private int lm = 25 , mh = 50, he = 75;
|
|
|
+ private final int start = 0, end = 100;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Control control;
|
|
|
+
|
|
|
+ private JPanel content = new JPanel();
|
|
|
+ private JSlider lowChanceSlider = new JSlider(JSlider.HORIZONTAL,0, 100, lm - start);
|
|
|
+ private JSlider mediumChanceSlider = new JSlider(JSlider.HORIZONTAL,0, 100, mh - lm);
|
|
|
+ private JSlider highChanceSlider = new JSlider(JSlider.HORIZONTAL,0, 100, he - mh);
|
|
|
+ private JSlider essentialChanceSlider = new JSlider(JSlider.HORIZONTAL,0, 100, end - he);
|
|
|
+ public static void main(String[] args)
|
|
|
+ {
|
|
|
+ JFrame newFrame = new JFrame("exampleWindow");
|
|
|
+ RandomPriority instance = new RandomPriority();
|
|
|
+ newFrame.setContentPane(instance.getAlgorithmPanel());
|
|
|
+ newFrame.pack();
|
|
|
+ newFrame.setVisible(true);
|
|
|
+ newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public RandomPriority(){
|
|
|
+ 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();
|
|
|
+ parameterPanel.setLayout(new BoxLayout(parameterPanel, BoxLayout.PAGE_AXIS));
|
|
|
+ createParamaterSlider(parameterPanel);
|
|
|
+ return parameterPanel;
|
|
|
+ }
|
|
|
+ private void createParamaterSlider(JPanel parameterPanel) {
|
|
|
+
|
|
|
+ parameterPanel.add(initVisualizeSlider(this.lowChanceSlider, "Low"));
|
|
|
+ parameterPanel.add(initVisualizeSlider(this.mediumChanceSlider, "Medium"));
|
|
|
+ parameterPanel.add(initVisualizeSlider(this.highChanceSlider, "High"));
|
|
|
+ parameterPanel.add(initVisualizeSlider(this.essentialChanceSlider, "Essential"));
|
|
|
+
|
|
|
+
|
|
|
+ lowChanceSlider.addChangeListener(actionEvent ->{
|
|
|
+ setLM(lowChanceSlider.getValue());
|
|
|
+ updateSliders();
|
|
|
+ System.out.println("lowChance = " +lowChanceSlider.getValue());
|
|
|
+ });
|
|
|
+ mediumChanceSlider.addChangeListener(actionEvent ->{
|
|
|
+ if(lm + mediumChanceSlider.getValue() <= 100) setMH(lm + mediumChanceSlider.getValue());
|
|
|
+ else setLM(end - mediumChanceSlider.getValue());
|
|
|
+ updateSliders();
|
|
|
+ System.out.println("mediumChance = " + mediumChanceSlider.getValue());
|
|
|
+ });
|
|
|
+ highChanceSlider.addChangeListener(actionEvent ->{
|
|
|
+ if(mh + highChanceSlider.getValue() <= 100) setHE(mh + highChanceSlider.getValue());
|
|
|
+ else setMH(end - highChanceSlider.getValue());
|
|
|
+ updateSliders();
|
|
|
+ System.out.println("highChance = " +highChanceSlider.getValue());
|
|
|
+ });
|
|
|
+ essentialChanceSlider.addChangeListener(actionEvent ->{
|
|
|
+ setHE(end - essentialChanceSlider.getValue());
|
|
|
+ updateSliders();
|
|
|
+ System.out.println("essentialChance = " +essentialChanceSlider.getValue());
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setLM(int value) {
|
|
|
+ lm = clamp(value, 0, 100);
|
|
|
+ if(lm > mh) mh = lm;
|
|
|
+ if(lm > he) he = lm;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setMH(int value) {
|
|
|
+ mh = clamp(value, 0, 100);
|
|
|
+ if(mh < lm) lm = mh;
|
|
|
+ if(mh > he) he = mh;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setHE(int value) {
|
|
|
+ he = clamp(value, 0, 100);
|
|
|
+ if(he < lm) lm = he;
|
|
|
+ if(he < mh) mh = he;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private int clamp(int input, int min , int max) {
|
|
|
+ return Math.max(min, Math.min(max, input));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void updateSliders(){
|
|
|
+ lowChanceSlider.setValue(lm - start);
|
|
|
+ mediumChanceSlider.setValue(mh - lm);
|
|
|
+ highChanceSlider.setValue(he - mh);
|
|
|
+ essentialChanceSlider.setValue(end - he);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private JSlider initVisualizeSlider(JSlider jslider, String name) {
|
|
|
+ jslider.setBorder(BorderFactory.createTitledBorder(name));
|
|
|
+ jslider.setMajorTickSpacing(50);
|
|
|
+ jslider.setMinorTickSpacing(5);
|
|
|
+ jslider.setPaintTicks(true);
|
|
|
+ Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
|
|
|
+ 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") );
|
|
|
+ jslider.setLabelTable( labelTable );
|
|
|
+ jslider.setPaintLabels(true);
|
|
|
+ return jslider;
|
|
|
+ }
|
|
|
+ private void run() {
|
|
|
+ //control.getModel().getObjectsOnCanvas().stream().filter(aCps -> aCps instanceof HolonObject)
|
|
|
+ List<HolonElement> elementList = createListOfHolonObjects(control.getModel().getObjectsOnCanvas()).stream().flatMap(hObject -> hObject.getElements().stream()).collect(Collectors.toList());
|
|
|
+ for(HolonElement element : elementList) {
|
|
|
+ // Generate a random number between 0 and 100
|
|
|
+ double randomDoubleInRange = Math.random() * 100.0;
|
|
|
+ if(randomDoubleInRange <= lm) {
|
|
|
+ element.setPriority(Priority.Low);
|
|
|
+ } else if(randomDoubleInRange <= mh) {
|
|
|
+ element.setPriority(Priority.Medium);
|
|
|
+ } else if(randomDoubleInRange <= he) {
|
|
|
+ element.setPriority(Priority.High);
|
|
|
+ } else {
|
|
|
+ element.setPriority(Priority.Essential);
|
|
|
+ }
|
|
|
+ //System.out.println(randomDoubleInRange + ":" + element.getPriority());
|
|
|
+
|
|
|
+ }
|
|
|
+ control.calculateStateAndVisualForCurrentTimeStep();
|
|
|
+ control.updateCanvas();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private List<HolonObject> createListOfHolonObjects(List<AbstractCpsObject> objectsOnCanvas) {
|
|
|
+ List<HolonObject> list = new ArrayList<HolonObject>();
|
|
|
+ for(AbstractCpsObject aCps : objectsOnCanvas) {
|
|
|
+ if(aCps instanceof HolonObject) list.add((HolonObject) aCps);
|
|
|
+ else if(aCps instanceof CpsUpperNode)list.addAll(createListOfHolonObjects(((CpsUpperNode)aCps).getNodes()));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JPanel getAlgorithmPanel() {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void setController(Control control) {
|
|
|
+ this.control = control;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|