📄 xmlreader.java
字号:
// As these elements are not mandatory, sets them to 0, then tries to parses them
String tmp = currclass.getAttribute(XML_A_CLASS_CUSTOMERS);
if (tmp != null && tmp != "")
customers = Integer.valueOf(tmp);
tmp = currclass.getAttribute(XML_A_CLASS_PRIORITY);
if (tmp != null && tmp != "")
priority = Integer.parseInt(tmp);
// Now adds user class. Note that distribution will be set lately.
key = model.addClass(name, type, priority, customers, defaultDistr);
// Stores reference station as will be set lately (when we will have stations key)
refStations.put(key, currclass.getAttribute(XML_A_CLASS_REFSOURCE));
// Creates mapping class-name -> key into stations data structure
classes.put(name, key);
}
}
// -----------------------------------------------------------------------------------------------
// --- Station section ---------------------------------------------------------------------------
/**
* Parses all station related informations and puts them into data structure
* @param root root element of XML Document
* @param model data structure where all properties have to be set
*/
protected static void parseStations(Element root, CommonModel model) {
// Initialize stations data structure
stations = new TreeMap();
NodeList nodestations = root.getElementsByTagName(XML_E_STATION);
Object key;
Element station;
String type, name;
NodeList sections;
// For every station, identifies its type and parses its parameters
for (int i=0; i<nodestations.getLength(); i++) {
station = (Element) nodestations.item(i);
sections = station.getElementsByTagName(XML_E_STATION_SECTION);
type = getStationType(station);
name = station.getAttribute(XML_A_STATION_NAME);
// Puts station into data structure
key = model.addStation(name, type);
// Creates mapping station-name -> key into stations data structure
stations.put(name, key);
// Handles source (set distribution)
if (type.equals(STATION_TYPE_SOURCE)) {
parseSource((Element)sections.item(0), model, key, name);
parseRouter((Element)sections.item(2), model, key);
}else if (type.equals(STATION_TYPE_TERMINAL) ||
type.equals(STATION_TYPE_ROUTER) ||
type.equals(STATION_TYPE_JOIN)) {
parseRouter((Element)sections.item(2), model, key);
}else if (type.equals(STATION_TYPE_DELAY)) {
parseDelay((Element)sections.item(1), model, key);
parseRouter((Element)sections.item(2), model, key);
}else if (type.equals(STATION_TYPE_SERVER)) {
parseQueue((Element)sections.item(0), model, key);
parseServer((Element)sections.item(1), model, key);
parseRouter((Element)sections.item(2), model, key);
}else if (type.equals(STATION_TYPE_FORK)) {
parseQueue((Element)sections.item(0), model, key);
parseFork((Element)sections.item(2), model, key);
}
}
}
/**
* Extract all informations regarding Source section. If this source is reference class
* for any kind of open class, uses service time informations stored here to set distribution
* for this class.
* @param section input section of source station
* @param model link to data structure
* @param key key of search for this source station into data structure
* @param stationName Name of current station. This is used to correctly set reference station
* distribution. That cannot be derived from model.getStationName(key) as JSim can change
* source name upon opening a model stored with JModel.
*/
protected static void parseSource(Element section, CommonModel model, Object key, String stationName) {
Element parameter = (Element)section.getElementsByTagName(XML_E_PARAMETER).item(0);
// Now parses Service Distribution
Map distributions = parseParameterRefclassArray(parameter);
// Assign distribution for a class only if current source is its reference station
Object[] classNames = distributions.keySet().toArray();
Object classkey;
for (int i=0; i<classNames.length;i++) {
// If current class has this station as reference source and is open...
if (refStations.get(classes.get(classNames[i])) != null &&
refStations.get(classes.get(classNames[i])).equals(stationName) &&
model.getClassType(classes.get(classNames[i])) == CLASS_TYPE_OPEN) {
classkey = classes.get(classNames[i]);
model.setClassDistribution(
parseServiceStrategy((Element)distributions.get(classNames[i])),
classkey);
model.setClassRefStation(classkey, key);
// Removes this class from refStations as it was already handled
refStations.remove(classkey);
}
}
}
/**
* Extract all informations regarding Queue section.
* @param section input section of source station
* @param model link to data structure
* @param key key of search for this source station into data structure
*/
protected static void parseQueue(Element section, CommonModel model, Object key) {
NodeList parameters = section.getElementsByTagName(XML_E_PARAMETER);
Element curr;
String name, classpath;
boolean fcfs = true;
Map putStrategy = null;
Map dropRules = null;
for (int i=0; i<parameters.getLength(); i++) {
curr = (Element) parameters.item(i);
name = curr.getAttribute(XML_A_PARAMETER_NAME);
classpath = curr.getAttribute(XML_A_PARAMETER_CLASSPATH);
if (classpath.equals(queueGetFCFS)) {
fcfs = true;
}else if(classpath.equals(queueGetLCFS)) {
fcfs = false;
}else if (classpath.equals(queuePut)) {
putStrategy = parseParameterRefclassArray(curr);
}else if (name.equals("size")) {
Integer size = Integer.valueOf(
findText(curr.getElementsByTagName(XML_E_PARAMETER_VALUE).item(0)));
model.setStationQueueCapacity(size, key);
}else if (name.equals("dropStrategies"))
dropRules = parseParameterRefclassArray(curr);
}
if (putStrategy != null) {
Object[] classNames = putStrategy.keySet().toArray();
String strategy;
for (int i=0; i<classNames.length; i++) {
strategy = ((Element)putStrategy.get(classNames[i])).getAttribute(XML_A_SUBPARAMETER_CLASSPATH);
// Takes away classpath from put strategy name
strategy = strategy.substring(strategy.lastIndexOf(".")+1, strategy.length());
// Now sets correct queue strategy, given combination of queueget and queueput policies
if (strategy.equals("HeadStrategy")) {
if (fcfs)
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_LCFS);
else
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_FCFS);
} else if (strategy.equals("HeadStrategyPriority")) {
if (fcfs)
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_LCFS_PRIORITY);
else
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_FCFS_PRIORITY);
} else if (strategy.equals("TailStrategy")) {
if (fcfs)
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_FCFS);
else
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_LCFS);
} else if (strategy.equals("TailStrategyPriority")) {
if (fcfs)
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_FCFS_PRIORITY);
else
model.setQueueStrategy(key, classes.get(classNames[i]), QUEUE_STRATEGY_LCFS_PRIORITY);
}
}
}
// Decodes drop rules
if (dropRules != null) {
Object[] classNames = dropRules.keySet().toArray();
String strategy;
for (int i=0; i<classNames.length; i++) {
strategy = findText(((Element)dropRules.get(classNames[i])).getElementsByTagName(XML_E_PARAMETER_VALUE).item(0));
model.setDropRule(key, classes.get(classNames[i]), (String)dropRulesNamesMatchings.get(strategy));
}
}
}
/**
* Extract all informations regarding Delay section.
* @param section input section of source station
* @param model link to data structure
* @param key key of search for this source station into data structure
*/
protected static void parseDelay(Element section, CommonModel model, Object key) {
Element parameter = (Element)section.getElementsByTagName(XML_E_PARAMETER).item(0);
// Retrives all distributions subParameters
Map distributions = parseParameterRefclassArray(parameter);
Object[] classNames = distributions.keySet().toArray();
// Sets service time distributions
for (int i=0; i<classNames.length;i++) {
model.setServiceTimeDistribution(key,
classes.get(classNames[i]),
parseServiceStrategy((Element)distributions.get(classNames[i])));
}
}
/**
* Extract all informations regarding Delay section.
* @param section input section of source station
* @param model link to data structure
* @param key key of search for this source station into data structure
*/
protected static void parseServer(Element section, CommonModel model, Object key) {
NodeList parameters = section.getElementsByTagName(XML_E_PARAMETER);
Element curr;
String name, classpath;
for (int i=0; i<parameters.getLength(); i++) {
curr = (Element) parameters.item(i);
name = curr.getAttribute(XML_A_PARAMETER_NAME);
classpath = curr.getAttribute(XML_A_PARAMETER_CLASSPATH);
if (classpath.equals(serviceStrategy)) {
// Retrives all distributions subParameters
Map distributions = parseParameterRefclassArray((Element)parameters.item(i));
Object[] classNames = distributions.keySet().toArray();
// Sets service time distributions
for (int j=0; j<classNames.length;j++) {
model.setServiceTimeDistribution(key,
classes.get(classNames[j]),
parseServiceStrategy((Element)distributions.get(classNames[j])));
}
}else if (name.equals("maxJobs")) {
// Sets number of servers
Integer jobs = Integer.valueOf(
findText(curr.getElementsByTagName(XML_E_PARAMETER_VALUE).item(0)));
model.setStationNumberOfServers(jobs, key);
}
}
}
/**
* Parses A Parameter Array node, returning a Map of ClassName -> subParameter
* @param parameterNode
* @return a Map of ClassName -> subParameter
*/
protected static Map parseParameterRefclassArray (Element parameterNode) {
// For some reasons getElementsByTagName returns only first service time strategy.
// So we need to look every children of parameterNode node.
TreeMap res = new TreeMap();
Node child = parameterNode.getFirstChild();
String refClass;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -