📄 xmlreader.java
字号:
// This manual parsing is a bit unclean but works well and it's really fast.
// I was forced to do in this way for the problem said before.
while (child != null) {
while ( child != null &&
(child.getNodeType() != Node.ELEMENT_NODE ||
!child.getNodeName().equals(XML_E_PARAMETER_REFCLASS)))
child = child.getNextSibling();
if (child == null)
break;
refClass = findText(child);
// Now finds first subParameter element
while ( child != null &&
(child.getNodeType() != Node.ELEMENT_NODE ||
!child.getNodeName().equals(XML_E_SUBPARAMETER)))
child = child.getNextSibling();
if (child == null)
break;
// Puts className and subParameter into destination Map
res.put(refClass, child);
child = child.getNextSibling();
}
return res;
}
/**
* Parses a parameter array and returns Vector of found subParameters
* @param parameterNode
* @return Vector with found subParameters
*/
protected static Vector parseParameterArray (Element parameterNode) {
Vector ret = new Vector();
Node child = parameterNode.getFirstChild();
while (child != null) {
while ( child != null &&
(child.getNodeType() != Node.ELEMENT_NODE ||
!child.getNodeName().equals(XML_E_SUBPARAMETER)))
child = child.getNextSibling();
if (child == null)
break;
// Puts found subParameter into destination Vector
ret.add(child);
child = child.getNextSibling();
}
return ret;
}
/**
* Parse router section
* @param section router section
* @param model data structure
* @param key station's key
*/
protected static void parseRouter(Element section, CommonModel model, Object key) {
Element parameter = (Element)section.getElementsByTagName(XML_E_PARAMETER).item(0);
Map routing = parseParameterRefclassArray(parameter);
Object[] classNames = routing.keySet().toArray();
String className;
// Creates a Map of Name --> Routing Strategy if needed
if (engineToGuiRouting == null) {
engineToGuiRouting = new TreeMap();
RoutingStrategy[] allRS = RoutingStrategy.findAll();
for (int i=0; i<allRS.length; i++)
engineToGuiRouting.put(allRS[i].getClass().getName(), allRS[i]);
}
Object[] routStratKeys = engineToGuiRouting.keySet().toArray();
for (int i=0; i<classNames.length; i++) {
className = ((Element) routing.get(classNames[i])).getAttribute(XML_A_SUBPARAMETER_CLASSPATH);
// Searches all available routing strategy to find the one saved
for (int j=0; j<routStratKeys.length; j++)
if (className.equals(((RoutingStrategy)engineToGuiRouting.get(routStratKeys[j])).getClassPath()))
model.setRoutingStrategy(key,
classes.get(classNames[i]),
((RoutingStrategy)engineToGuiRouting.get(routStratKeys[j])).clone());
// Treat particular case of Empirical (Probabilities) Routing
RoutingStrategy rs = (RoutingStrategy)model.getRoutingStrategy(key, classes.get(classNames[i]));
if (rs instanceof ProbabilityRouting) {
// Creates a Vector of all empirical entris. Could not be done automaticly
// for the above problem with array (see parseParameterRefclassArray)
Vector entries = new Vector();
// Finds EntryArray node
Node entryArray = ((Node)routing.get(classNames[i])).getFirstChild();
while (entryArray.getNodeType() != Node.ELEMENT_NODE ||
!entryArray.getNodeName().equals(XML_E_SUBPARAMETER))
entryArray = entryArray.getNextSibling();
// Now finds every empirical entry
Node child = entryArray.getFirstChild();
while (child != null) {
// Find first subParameter element
while ( child != null &&
(child.getNodeType() != Node.ELEMENT_NODE ||
!child.getNodeName().equals(XML_E_SUBPARAMETER)))
child = child.getNextSibling();
if (child != null) {
entries.add(child);
child = child.getNextSibling();
}
}
// For each empirical entry get station name and probability
for (int j =0; j < entries.size(); j++) {
NodeList values = ((Element)entries.get(j)).getElementsByTagName(XML_E_SUBPARAMETER);
String stationName = findText(((Element)values.item(0))
.getElementsByTagName(XML_E_PARAMETER_VALUE).item(0));
Double probability = Double.valueOf(findText(((Element)values.item(1))
.getElementsByTagName(XML_E_PARAMETER_VALUE).item(0)));
// Now puts the tuple stationName -> probability into a Map, then adds it
// to empiricalRouting Map. This is needed as at this
// point we don't have all station's key so will be adjusted latelly
Map tmp = new TreeMap();
tmp.put(stationName, probability);
// Put into empiricalRouting a pair of station key and class key and map with
// station names instead of station key
empiricalRouting.put(new Object[] {key, classes.get(classNames[i])}, tmp);
}
}
}
}
/**
* Extract all informations regarding Fork 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 parseFork(Element section, CommonModel model, Object key) {
NodeList parameters = section.getElementsByTagName(XML_E_PARAMETER);
for (int i=0; i<parameters.getLength(); i++) {
Element parameter = (Element)parameters.item(i);
String parameterName = parameter.getAttribute(XML_A_PARAMETER_NAME);
// Fork number of server is used as number of jobs per link
if (parameterName.equals("jobsPerLink"))
model.setStationNumberOfServers(Integer.decode(
findText(parameter.getElementsByTagName(XML_E_PARAMETER_VALUE).item(0))), key);
else if (parameterName.equals("block"))
model.setForkBlock(key, Integer.valueOf(
findText(parameter.getElementsByTagName(XML_E_PARAMETER_VALUE).item(0))));
}
}
/**
* Parses service section informations contained in serviceTimeStrategy element to create a
* correct Distribution or LDStrategy object
* @param serviceTimeStrategy Element that holds all distribution informations
* @return created Distribution or LDStrategy or null if this field is set to null
*/
protected static Object parseServiceStrategy (Element serviceTimeStrategy) {
// Ccreates a map with distribution classpath --> Distribution if needed
if (engineToGuiDistr == null) {
Distribution[] allDistr = Distribution.findAll();
engineToGuiDistr = new TreeMap();
for (int i=0; i<allDistr.length; i++)
engineToGuiDistr.put(allDistr[i].getClassPath(), allDistr[i]);
}
String serviceClassPath = serviceTimeStrategy.getAttribute(XML_A_SUBPARAMETER_CLASSPATH);
if (serviceClassPath.equals(ZeroStrategy.getEngineClassPath())) {
// Zero service time strategy
return new ZeroStrategy();
}
else if (serviceClassPath.equals(LDStrategy.getEngineClassPath())) {
// Load dependent Service Strategy
Element LDParameterArray = (Element)serviceTimeStrategy.getElementsByTagName(XML_E_SUBPARAMETER).item(0);
LDStrategy strategy = new LDStrategy();
// Now parses LDStrategy ranges
Vector ranges = parseParameterArray(LDParameterArray);
for (int i=0; i<ranges.size(); i++) {
Vector parameters = parseParameterArray((Element)ranges.get(i));
int from = Integer.parseInt(findText(((Element)parameters.get(0)).getElementsByTagName(XML_E_SUBPARAMETER_VALUE).item(0)));
Distribution distr = parseDistribution((Element)parameters.get(1), (Element)parameters.get(2));
String mean = findText(((Element)parameters.get(3)).getElementsByTagName(XML_E_SUBPARAMETER_VALUE).item(0));
Object key;
if (from == 1)
// If this is first range
key = strategy.getAllRanges()[0];
else {
// next ranges
key = strategy.addRange();
strategy.setRangeFrom(key, from);
// This is needed as key will change
key = strategy.getAllRanges()[strategy.getRangeNumber() - 1];
}
strategy.setRangeDistributionNoCheck(key, distr);
strategy.setRangeDistributionMeanNoCheck(key, mean);
}
return strategy;
}
else {
// Service time strategy
NodeList distribution = serviceTimeStrategy.getElementsByTagName(XML_E_SUBPARAMETER);
// If distribution is not set, returns null
if (distribution.getLength() == 0)
return null;
return parseDistribution((Element)distribution.item(0), (Element)distribution.item(1));
}
}
/**
* Parses a distribution, given its distribution and distributionPar nodes
* @param distr distribution node
* @param distrPar distribution's parameter node
* @return parsed distribution
*/
protected static Distribution parseDistribution(Element distr, Element distrPar) {
String classname = distr.getAttribute(XML_A_SUBPARAMETER_CLASSPATH);
NodeList parameters = (distrPar).getElementsByTagName(XML_E_SUBPARAMETER);
// Gets correct instance of distribution
Distribution dist = (Distribution)((Distribution)engineToGuiDistr.get(classname)).clone();
Element currpar;
String param_name, param_value;
for (int i=0; i<parameters.getLength(); i++) {
currpar = (Element)parameters.item(i);
param_name = currpar.getAttribute(XML_A_SUBPARAMETER_NAME);
param_value = findText(currpar.getElementsByTagName(XML_E_SUBPARAMETER_VALUE).item(0));
// Sets param_name value to param_value content. Distribution object will do String parsing
// according to expected parameter class.
dist.getParameter(param_name).setValue(param_value);
dist.updateCM(); // Updates values of c and mean
}
return dist;
}
/**
* Returns the type of a station, reconstructing it from section names. This method must be
* modified if a new station type is inserted.
* @param station element containing sections
* @return station type as expected by CommonModel / JMODELModel
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -