📄 gridstatistics.java
字号:
private class OrderByIndex implements Comparator { /** * Allocates a new OrderByIndex value * @pre $none * @post $none */ public OrderByIndex() { super(); } /** * Compares two objects based on their indices * @param a the first Object to be compared * @param b the second Object to be compared * @return the value 0 if both Objects are equal; * a value less than 0 if the first Object is lexicographically * less than the second Object; * and a value greater than 0 if the first Object is * lexicographically greater than the second Object. * @throws ClassCastException <tt>a</tt> and <tt>b</tt> are expected * to be of type <tt>Stat</tt> * @see gridsim.Stat * @pre a != null * @pre b != null * @post $none */ public int compare(Object a, Object b) throws ClassCastException { Integer i1 = new Integer( ((Times) a).getIndex() ); Integer i2 = new Integer( ((Times) b).getIndex() ); return i1.compareTo(i2); } } // end internal class ////////////////////////// INTERNAL CLASS ///////////////////////// /** * Accumulates objects based on a given category * @param category user-defined name for data type * @return an Accumulator object contains double values of objects * that match the given category * @see gridsim.Accumulator * @pre category != null * @post $result != null */ public Accumulator accumulate(String category) { LinkedList statList = statSortByCategoryData_; Accumulator acc = new Accumulator(); int pos = Collections.binarySearch( statList, new Stat(0.0, category, "", ""), new OrderByCategory() ); if (pos < 0) { return acc; } Stat statObj = (Stat) statList.get(pos); acc.add( new Double(statObj.getData()).doubleValue() ); int i = 0; for (i = pos+1; i < statList.size(); i++) { statObj = (Stat) statList.get(i); if ( statObj.getCategory().equals(category) ) { acc.add( new Double(statObj.getData()).doubleValue() ); } else { break; } } for (i = pos-1; i >= 0; i--) { statObj = (Stat) statList.get(i); if ( statObj.getCategory().equals(category) ) { acc.add( new Double(statObj.getData()).doubleValue() ); } else { break; } } return acc; } /** * Accumulates objects based on a given category * @param category user-defined name for data type * @param counter user-defined name for data type * @return an Accumulator object contains double values of objects * that match the given category * @see gridsim.Accumulator * @pre category != null * @pre counter != null * @post $result != null */ public Accumulator accumulate(String category, String counter) { LinkedList statList = statSortByCategoryData_; Accumulator acc = new Accumulator(); LinkedList timeList = new LinkedList(); int pos = Collections.binarySearch( statList, new Stat(0.0, counter, "", ""), new OrderByCategory() ); if (pos < 0) { return acc; } Stat statObj = (Stat) statList.get(pos); int j = Integer.parseInt( statObj.getName().substring( statObj.getName().indexOf('_')+1 ) ); timeList.add(new Times(j, Integer.parseInt(statObj.getData())) ); int i = 0; for (i = pos+1; i < statList.size(); i++) { statObj = (Stat) statList.get(i); if ( statObj.getCategory().equals(counter) ) { j = Integer.parseInt( statObj.getName().substring( statObj.getName().indexOf('_')+1 ) ); timeList.add( new Times(j, Integer.parseInt( statObj.getData()) ) ); } else { break; } } for (i = pos-1; i >= 0; i--) { statObj = (Stat) statList.get(i); if ( statObj.getCategory().equals(counter) ) { j = Integer.parseInt( statObj.getName().substring( statObj.getName().indexOf('_')+1 ) ); timeList.add( new Times(j, Integer.parseInt( statObj.getData()) ) ); } else { break; } } Collections.sort(timeList, new OrderByIndex()); pos = Collections.binarySearch( statList, new Stat(0.0, category, "", ""), new OrderByCategory() ); if (pos < 0) { return acc; } statObj = (Stat) statList.get(pos); j = Integer.parseInt( statObj.getName().substring( statObj.getName().indexOf('_')+1 ) ); int k = Collections.binarySearch( timeList, new Times(j,0), new OrderByIndex() ); acc.add( new Double(statObj.getData()).doubleValue(), ((Times) timeList.get(k)).getValue() ); for (i = pos+1; i < statList.size(); i++) { statObj = (Stat) statList.get(i); if ( statObj.getCategory().equals(category) ) { j = Integer.parseInt( statObj.getName().substring( statObj.getName().indexOf('_')+1 ) ); k = Collections.binarySearch(timeList, new Times(j,0), new OrderByIndex()); acc.add(new Double(statObj.getData()).doubleValue(), ((Times) timeList.get(k)).getValue() ); } else { break; } } for (i = pos-1; i >= 0; i--) { statObj = (Stat) statList.get(i); if (statObj.getCategory().equals(category)) { j = Integer.parseInt( statObj.getName().substring( statObj.getName().indexOf('_')+1 ) ); k = Collections.binarySearch( timeList, new Times(j,0), new OrderByIndex() ); acc.add( new Double(statObj.getData()).doubleValue(), ((Times) timeList.get(k)).getValue() ); } else { break; } } return acc; } /** * A method that gets one process event at one time until the end * of a simulation, then records its statistics. * <p> * The services available to other GridSim entities are: * <ul> * <li> GridSimTags.RECORD_STATISTICS </li> * <li> GridSimTags.RETURN_ACC_STATISTICS_BY_CATEGORY </li> * </ul> * @pre $none * @post $none */ public void body() { // Process Events until END_OF_SIMULATION is received Sim_event ev = new Sim_event(); for (sim_get_next(ev); ev.get_tag() != GridSimTags.END_OF_SIMULATION; sim_get_next(ev) ) { if ( !active_ ) { continue; // Skip processing of this event } switch ( ev.get_tag() ) { case GridSimTags.RECORD_STATISTICS: if (ev.get_data() != null) { recordStat( (Stat) ev.get_data() ); } break; case GridSimTags.RETURN_ACC_STATISTICS_BY_CATEGORY: returnAccStatByCategory(ev); break; default: System.out.println("GridStatistics.body() : " + "Unable to handle request from GridSimTags " + "with constant number " + ev.get_tag() ); break; } } if (active_) { outFile_.close(); } } /** * Records the given statistics into a file * @param stat a Stat object * @see gridsim.Stat * @pre stat != null * @post $none */ public void RecordStat(Stat stat) { this.recordStat(stat); } /** * Records the given statistics into a file * @param stat a Stat object * @see gridsim.Stat * @pre stat != null * @post $none */ public void recordStat(Stat stat) { boolean flag = true; int i = 0; int length = 0; if (excludeFromProcessing_ != null) { length = excludeFromProcessing_.length; for (i = 0; i < length; i++) { if ( (stat.getCategory()+".").startsWith(excludeFromProcessing_[i]+".") ) { flag = false; break; } } } if (flag) { statList_.add(stat); } flag = true; if (excludeFromFile_ != null) { length = excludeFromFile_.length; for (i = 0; i < length; i++) { if ( (stat.getCategory()+".").startsWith(excludeFromFile_[i]+".") ) { flag = false; break; } } } if (flag) { outFile_.println( stat.toString() ); } } /** * Sends an Accumulator object based on category into an event scheduler. * @param ev an object of Sim_event * @see eduni.simjava.Sim_event * @pre ev != null * @post $none */ public void ReturnAccStatByCategory(Sim_event ev) { this.returnAccStatByCategory(ev); } /** * Sends an Accumulator object based on category into an event scheduler. * @param ev an object of Sim_event * @see eduni.simjava.Sim_event * @pre ev != null * @post $none */ public void returnAccStatByCategory(Sim_event ev) { statSortByCategoryData_ = new LinkedList(statList_); Collections.sort( statSortByCategoryData_, new OrderByCategoryData() ); String category = (String) ev.get_data(); Accumulator acc = accumulate(category); super.sim_schedule(ev.get_src(), 0.0, ev.get_tag(), acc); }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -