📄 resourceproperty.java
字号:
/* * Title: Visual Modeler for GridSim Toolkit * Description: This Visual Modeler enables the user to quickly create * experiments on different Grid testbeds and generate the * default Grid Broker source codes (in Java). * * $Id: ResourceProperty.java,v 1.11 2005/04/01 06:38:24 anthony Exp $ */package visualmodeler;// to handle loading XML. These packages exist since java 1.4import org.w3c.dom.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;import java.util.*;/** * ResourceProperty stores the values needed for each grid resource * * @author Anthony Sulistio and Chee Shin Yeo * @version 1.1 * @invariant $none */public class ResourceProperty extends JDialog implements ActionListener, ChangeListener, WindowListener, ListSelectionListener{ private JSlider peakSlider_, offSlider_, holidaySlider_; private int peakLoad_, offLoad_, holidayLoad_; private double baudRate_; private int id_; // resource id - this cannot be changed & unique private String name_; // resource name ////////// GUI part // labels to display the values private JLabel peakLabel_, offLabel_, holidayLabel_; private JTextField resText_; // display the resource name private JTextField baudText_; // display baud rate value private JButton ok_, cancel_; // a flag to determine whether the GUI components has created or not private boolean guiHasCreated_; ///////// resource characteristics private int comboPolicy_; private final String[] comboValue_ = { "Time-shared", "Space-shared" }; private String arch_; // architecture name private String os_; // operating system private double timeZone_; // time zone based on GMT private double price_; // cost per sec // to display machines - this should be in a separate class? private Vector machine_; private Vector machineName_; ////////// GUI for resource characteristics private JComboBox combo_; // for allocation policy private JPanel charPanel_; // Another panel for resource characteristics private JTextField archText_, osText_, timeZoneText_, priceText_; private JList machineList_; private JPanel machinePanel_; // to display the CPUs of a machine /** * Allocates a new ResourceProperty object * @param id resource id * @param name resource name * @param random <tt>true</tt> if assigning grid resource properties * randomly, <tt>false</tt> otherwise * @param r a Random object * @pre id >= 0 * @pre name != null * @pre r != null * @post $none */ public ResourceProperty(int id, String name, boolean random, Random r) { super.setModal(true); // user can't open or use another window super.setSize(500, 500); super.setLocation(150, 100); super.setTitle("Resource Property"); this.addWindowListener(this); machine_ = new Vector(); machineName_ = new Vector(); machinePanel_ = new JPanel(); if (random == true) { randomValue(r); } else { defaultValue(); } id_ = id; name_ = name; guiHasCreated_ = false; // initializes all GUI components archText_ = null; baudText_ = null; cancel_ = null; charPanel_ = null; combo_ = null; holidayLabel_ = null; holidaySlider_ = null; machineList_ = null; offLabel_ = null; offSlider_ = null; ok_ = null; osText_ = null; peakLabel_ = null; peakSlider_ = null; priceText_ = null; resText_ = null; timeZoneText_ = null; } /** * Gets the resource name * @return resource name * @pre $none * @post $result != null */ public String getResourceName() { return name_; } /** * Gets resource id * @return resource id * @pre $none * @post $result >= 0 */ public int getId() { return id_; } /** * A method that listens to user's call * @param evt an ActionEvent object * @pre evt != null * @post $none */ public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equalsIgnoreCase("ok") == true) { if (saveValue() == false) { return; } } // for cancel button else { resetValue(); } removeListeners(); dispose(); } /** * A method that refreshes the slider bars when they received a change * @param evt a ChangeEvent object * @pre evt != null * @post $none */ public void stateChanged(ChangeEvent evt) { // have to divide by 10 since the slider value [1, 10] in integer JSlider slider = (JSlider) evt.getSource(); int num = slider.getValue(); double value = num / 10.0; if (slider == peakSlider_) { peakLabel_.setText("Peak Load: " + value); } else if (slider == offSlider_) { offLabel_.setText("Off-peak Load: " + value); } else { holidayLabel_.setText("Holiday Load: " + value); } } /** * A method that shows a dialog for a particular list selection * @param evt a ListSelectionEvent object * @pre evt != null * @post $none */ public void valueChanged(ListSelectionEvent evt) { if ( evt.getValueIsAdjusting() ) { return; } int i = machineList_.getSelectedIndex(); ((ResourceMachine) machine_.elementAt(i)).showDialog(machinePanel_); } /** * Shows the grid resource dialog * @pre $none * @post $none */ public void showDialog() { if (guiHasCreated_ == false) { guiHasCreated_ = true; initComponents(); } ok_.addActionListener(this); cancel_.addActionListener(this); machineList_.addListSelectionListener(this); peakSlider_.addChangeListener(this); offSlider_.addChangeListener(this); holidaySlider_.addChangeListener(this); this.show(); } /** * Generates a Java code for grid resource * @param indent indentation * @return a Java code in String object * @pre indent != null * @post $result != null */ public String generateCode(String indent) { String archName = "\"" + arch_ + "\""; String osName = "\"" + os_ + "\""; String resName = "\"" + name_ + "\""; StringBuffer code = new StringBuffer(1000); code.append("\n"); code.append(indent); code.append("///////// Create "); code.append(name_); code.append("\n"); code.append(indent); code.append("mList = new MachineList();\t// A list of Machines\n"); int size = machine_.size(); for (int i = 0; i < size; i++) { code.append( ( (ResourceMachine) machine_.elementAt(i) ).generateCode(indent) ); } code.append("\n"); code.append(indent); code.append("// Creates a new ResourceCharacteristics entity\n"); code.append(indent); code.append("resConfig = new ResourceCharacteristics("); code.append(archName); code.append(", "); code.append(osName); code.append(",\n"); code.append(indent); code.append(" mList, "); code.append(comboPolicy_); code.append(", "); code.append(timeZone_); code.append(", "); code.append(price_); code.append(");\n\n"); final double DIV = 10.0; code.append(indent); code.append("// Creates a new GridResource entity\n"); code.append(indent); code.append("gridRes = new GridResource("); code.append(resName); code.append(", "); code.append(baudRate_); code.append(", seed*(" + id_ + "+1)+1,\n"); code.append(indent); code.append(" resConfig, "); code.append(peakLoad_ / DIV); code.append(", "); code.append(offLoad_ / DIV); code.append(", "); code.append(holidayLoad_ / DIV); code.append(", Weekends, Holidays);\n"); return code.toString(); } /** * Saves grid resource properties into XML format * @param spaces indentation * @return XML code * @pre spaces != null * @post $result != null */ public String saveFile(String spaces) { final double DIV = 10.0; String indent = spaces + " "; String tab = indent + " "; String doubleTab = tab + " "; // write the resource header StringBuffer xml = new StringBuffer(1000); xml.append("\n\n"); xml.append(spaces); xml.append("<resource>\n"); // write the resource id xml.append(indent); xml.append("<id> "); xml.append(id_); xml.append(" </id>\n"); // write the resource name xml.append(indent); xml.append("<name> "); xml.append(name_); xml.append(" </name>\n"); // write the baud rate xml.append(indent); xml.append("<baudRate> "); xml.append(baudRate_); xml.append(" </baudRate>\n"); // write the load values xml.append(indent); xml.append("<peakLoad> "); xml.append(peakLoad_ / DIV); xml.append(" </peakLoad>\n"); xml.append(indent); xml.append("<offPeakLoad> "); xml.append(offLoad_ / DIV); xml.append(" </offPeakLoad>\n"); xml.append(indent); xml.append("<holidayLoad> "); xml.append(holidayLoad_ / DIV); xml.append(" </holidayLoad>\n"); // write the resource characteristics xml.append(indent); xml.append("<resourceCharacteristics>\n"); // write the architecture name xml.append(tab); xml.append("<architecture> "); xml.append(arch_); xml.append(" </architecture>\n"); // write the os name xml.append(tab); xml.append("<operatingSystem> "); xml.append(os_); xml.append(" </operatingSystem>\n"); // write the time zone xml.append(tab); xml.append("<timeZone> "); xml.append(timeZone_); xml.append(" </timeZone>\n"); // write the price xml.append(tab); xml.append("<gridDollarPerApplication> "); xml.append(price_); xml.append(" </gridDollarPerApplication>\n"); // write the allocation policy xml.append(tab); xml.append("<allocationPolicy> "); xml.append(comboValue_[comboPolicy_]); xml.append(" </allocationPolicy>\n\n"); // write the machines xml.append(tab); xml.append("<machineList>\n"); // write the total machine final int size = machine_.size(); xml.append(doubleTab); xml.append("<totalMachine> "); xml.append(size); xml.append(" </totalMachine>\n"); // loop to write the machine names for (int i = 0; i < size; i++) { xml.append( ( (ResourceMachine)machine_.elementAt(i) ).saveFile(doubleTab) ); } // don't forget the closing tag xml.append("\n"); xml.append(tab); xml.append("</machineList>\n"); xml.append(indent); xml.append("</resourceCharacteristics>\n"); xml.append(spaces); xml.append("</resource>\n"); return xml.toString(); } /** * Loads a section of grid resource from XML file * @param nodeList a NodeList object * @throws Exception if error occurs in parsing and retrieving XML file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -