gridsimgui.java
来自「一个非常著名的网格模拟器,能够运行网格调度算法!」· Java 代码 · 共 672 行 · 第 1/2 页
JAVA
672 行
//*************************************************************** // Method that initializes and starts the simulation //*************************************************************** private void begin(int totalResources) { GridResource[] resource = new GridResource[totalResources]; textAreaOutput.append ("Starting Test program ..... \n"); try { //-------------------------------------------------- // First step: Initialize the GridSim package //-------------------------------------------------- int num_user = 1; // number of grid users Calendar calendar = Calendar.getInstance(); boolean trace_flag = true; // mean trace GridSim events // list of files or processing names to be excluded from any statistical measures String[] exclude_from_file = { "" }; String[] exclude_from_processing = { "" }; // the name of a report file to be written. We don't want to write anything here. See other examples of using the ReportWriter class String report_name = null; // initialize the GridSim package textAreaOutput.append ("Initializing GridSim package \n"); GridSim.init(num_user, calendar, trace_flag, exclude_from_file, exclude_from_processing, report_name); //-------------------------------------------------- // Second step: Creates three GridResource objects //-------------------------------------------------- int index = 0; for(int i = 0; i < resourceFile.numberOfResources(); i++) { if(checkBoxResource[i].isSelected()) { resource[index] = createGridResource(i); index++; } } //-------------------------------------------------- // Third step: Creates the Test object //-------------------------------------------------- Test obj = new Test("Test", 560.00, totalResources, numOfGridlets, averageMI, deviatePercentage, granTime, overhead, grouping); //-------------------------------------------------- // Fourth step: Starts the simulation //-------------------------------------------------- GridSim.startGridSimulation(); textAreaOutput.append (obj.getOutput()); textAreaOutput.append ("\nFinish Test program \n"); } catch (Exception e) { e.printStackTrace(); textAreaOutput.append ("Unwanted errors happen \n"); } } //*************************************************************** // Actually create the resource with the characteristics // specified in the line number 'index' of the resource // file. //*************************************************************** private GridResource createGridResource(int index) { // get resource details from resource file String name = resourceFile.getNameOfResource(index); // resource name String architecture = resourceFile.getArchitectureOfResource(index); // system architecture String OS = resourceFile.getOSOfResource(index); // operating system int machines = resourceFile.getMachinesOfResource(index); // number of machines in the resource int PE = resourceFile.getPEOfResource(index); // number of PEs in the resource int MIPSofPE = resourceFile.getMIPSofPEOfResource(index); // MIPS of each PE long seed = resourceFile.getSeedOfResource(index); // seed double timezone = resourceFile.getTimezoneOfResource(index); // time zone this resource located double cost = resourceFile.getCostOfResource(index); // the cost of using this resource double baudRate = resourceFile.getBaudRateOfResource(index); // communication speed double peakLoad = resourceFile.getPeakLoadOfResource(index); // the resource load during peak hour double offPeakLoad = resourceFile.getOffPeakLoadOfResource(index); // the resource load during off-peak hour double holidayLoad = resourceFile.getHolidayLoadOfResource(index); // the resource load during holiday MachineList mList = new MachineList(); // create machine list to store the PE lists PEList peList; for(int machineNum = 0; machineNum < machines; machineNum++) { peList = new PEList(); // create first PE list to store the PEs for(int PEnum = 0; PEnum < PE; PEnum++) { peList.add( new PE(PEnum, MIPSofPE) ); // add create PEs } mList.add( new Machine(machineNum, peList) ); // add PE list into machine list } // create a ResourceCharacteristics object to store the properties of a Grid resource: architecture, OS, list of // Machines, allocation policy: time- or space-shared, time zone and its price (G$/PE time unit). ResourceCharacteristics resConfig = new ResourceCharacteristics(architecture, OS, mList,ResourceCharacteristics.TIME_SHARED,timezone, cost); // incorporates weekends so the grid resource is on 7 days a week LinkedList Weekends = new LinkedList(); Weekends.add(new Integer(Calendar.SATURDAY)); Weekends.add(new Integer(Calendar.SUNDAY)); // incorporates holidays, but no holidays are set in this Test program LinkedList Holidays = new LinkedList(); GridResource gridRes = null; try { gridRes = new GridResource(name, baudRate, seed, resConfig, peakLoad, offPeakLoad, holidayLoad, Weekends,Holidays); } catch (Exception e) { e.printStackTrace(); } return gridRes; } //*************************************************************** // Method to be triggered when 'Calculate' button is pressed //*************************************************************** public void calculate() { //-------------------------------- // Read from GUI //-------------------------------- String strGridlets = textFieldGridlets.getText(); // get the number of gridlets String strAverageMI = textFieldAverageMI.getText(); // get the average MIPS rating of the gridlets String strDP = (String)comboDeviatePercentage.getSelectedItem(); String strGranTime = textFieldGranTime.getText(); String strOverhead = textFieldOverhead.getText(); int totalResources = 0; //------------------------------- // count the number of resources // selected by the user //------------------------------- for(int i = 0; i < resourceFile.numberOfResources(); i++) { if(checkBoxResource[i].isSelected()) totalResources++; } //------------------------------- // Check if any field is empty //------------------------------- if(!strGridlets.equals("") && !strAverageMI.equals("") && !strDP.equals("") && !strGranTime.equals("") && !strOverhead.equals("")) { //---------------------------------------------------------- // Check if the input is in correct format //---------------------------------------------------------- if(strGridlets.matches(VALID_INTEGER_INPUT) && strAverageMI.matches(VALID_DOUBLE_INPUT) && strGranTime.matches(VALID_INTEGER_INPUT) && strOverhead.matches("(" + VALID_INTEGER_INPUT + "|0)")) { //---------------------------------------------------------- // Check if there is at least one resource selected //---------------------------------------------------------- if(totalResources != 0) { //---------------------------------------------------------- // disable the command controls because gridsim cannot run // again without restarting the program //---------------------------------------------------------- buttonCalculate.setEnabled(false); for(int i = 0; i < checkBoxResource.length; i++) checkBoxResource[i].setEnabled(false); for(int i = 0; i < radButGrouping.length; i++) radButGrouping[i].setEnabled(false); menuCalculate.setEnabled(false); menuChangeFile.setEnabled(false); textFieldGridlets.setEnabled(false); textFieldAverageMI.setEnabled(false); textFieldOverhead.setEnabled(false); textFieldGranTime.setEnabled(false); comboDeviatePercentage.setEnabled(false); //---------------------------------------------------------- // Actually converting to the numerical values to start gridsim //---------------------------------------------------------- numOfGridlets = Integer.parseInt(strGridlets); averageMI = Double.parseDouble(strAverageMI); deviatePercentage = Integer.parseInt(strDP); granTime = Integer.parseInt(strGranTime); overhead = Integer.parseInt(strOverhead); grouping = radButGrouping[0].isSelected(); textAreaOutput.setText(""); begin(totalResources); } else JOptionPane.showMessageDialog(contentPane, "At least one resource must be selected." , "Select Resource", JOptionPane.ERROR_MESSAGE, null); } else JOptionPane.showMessageDialog(contentPane , "All text fields must be integer values except Average MI.\n" + "All text fields must be non-zero except Overhead time" , "Wrong Input Format" , JOptionPane.ERROR_MESSAGE , null); } else JOptionPane.showMessageDialog(contentPane, "All text fields must be non-empty." , "Empty Input", JOptionPane.ERROR_MESSAGE, null); } //*************************************************************** // Method to be called when changing resource file happens //*************************************************************** private void setResourceFile() { String FORMAT = "[a-zA-Z]+[.][a-zA-Z]+"; String input; boolean valid = false; boolean success = false; do { do { input = JOptionPane.showInputDialog(contentPane , "Current Resource File : " + fileName + "\nEnter the new file name." , "Resource File Name", JOptionPane.INFORMATION_MESSAGE); if(input == null || input.matches(FORMAT)) valid = true; else JOptionPane.showMessageDialog(contentPane, "Please enter valid file name \"<name>.<extension>\""); } while(!valid); valid = false; if(input != null) { try { resourceFile.readFile(input); fileName = input; labelResource.setText("Resource List (\"" + fileName + "\")"); createResourceList(); success = true; } catch (FileNotFoundException exception) //if file doesn't exit, display message { JOptionPane.showMessageDialog(contentPane , "The file " + input + " was not found." , "File Not Found" , JOptionPane.ERROR_MESSAGE , null); } catch (IOException exception) //if any error in IO, display message { JOptionPane.showMessageDialog(contentPane, exception, "File Reading Problem" , JOptionPane.ERROR_MESSAGE, null); } catch(NoSuchElementException exception) { JOptionPane.showMessageDialog(contentPane , "Some information seems to be missing in " + "the resource file \""+ input + "\".\nChange of resource file unsuccessful." , "Missing Information" , JOptionPane.ERROR_MESSAGE , null); } catch(NumberFormatException exception) { JOptionPane.showMessageDialog(contentPane , "Some information seems not to be in correct format in " + "the resource file \""+ input + "\".\nChange of resource file unsuccessful." , "Wrong Format" , JOptionPane.ERROR_MESSAGE , null); } } else success = true; } while(!success); } //*************************************************************** // Method to be triggered when buttons are clicked //*************************************************************** public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if(source == buttonExit) System.exit(0); else if(source == buttonCalculate) calculate(); else { String menuName = ae.getActionCommand(); if(menuName.equals("Calculate")) calculate(); else if(menuName.equals("Set Resource File")) setResourceFile(); else System.exit(0); } } //*************************************************************** // Method to change the color of a resource to red when selected //*************************************************************** public void itemStateChanged(ItemEvent ie) { JCheckBox resource = (JCheckBox)ie.getSource(); if(resource.isSelected()) resource.setForeground(Color.red); else resource.setForeground(Color.black); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?