} else if (curNode.getChildNodes().item(j).getNodeName().equals("phaseno")) {
phaseno = Integer.parseInt(curNode.getChildNodes().item(j).getTextContent());
}
}
// TrafficLightModel existingNode = (TrafficLightModel) this.nodes.get(curID);
TrafficLightModel existingNode = trafficLights.get(curID);
int offset = 0;
int[] durations = new int[phaseno];
// iterate phases
NodeList tllogicchilds = curNode.getChildNodes();
for (int j = 0; j < tllogicchilds.getLength(); j++) {
if (tllogicchilds.item(j).getNodeName().equals("phase")) {
String dAttr = tllogicchilds.item(j).getAttributes().getNamedItem("duration")
.getTextContent();
int dur = Integer.parseInt(dAttr);
durations[offset] = dur;
offset++;
}
}
existingNode.setDurations(durations);
// build logic
HashMap<EdgeModel, TrafficLightStateList> dl = new HashMap<EdgeModel, TrafficLightStateList>();
existingNode.setNoOfPhases(phaseno);
for (int j = 0; j < tllogicchilds.getLength(); j++) {
if (tllogicchilds.item(j).getNodeName().equals("phase")) {
String phase = tllogicchilds.item(j).getAttributes().getNamedItem("phase")
.getTextContent();
String brake = tllogicchilds.item(j).getAttributes().getNamedItem("brake")
.getTextContent();
String yellow = tllogicchilds.item(j).getAttributes().getNamedItem("yellow")
.getTextContent();
int k = phase.length();
int noOutgoing = existingNode.getUsedBy().size()
- existingNode.getNoOfIncomingEdges();
int countIncomingEdge = 0;
for (EdgeModel e : existingNode.getUsedBy()) {
// incoming edge...
if (e.getToNode().equals(existingNode)) {
if (dl.get(e) == null) {
dl.put(e, new TrafficLightStateList());
}
int c = k - countIncomingEdge * noOutgoing - 1;
String phaseCode = phase.substring(c, c + 1) + brake.substring(c, c + 1)
+ yellow.substring(c, c + 1);
if (phaseCode.equals("010")) { // red
dl.get(e).add(new TrafficLightStateListEntry(TrafficLightState.Red));
} else if (phaseCode.equals("011")) { // yellow
dl.get(e).add(new TrafficLightStateListEntry(TrafficLightState.Yellow));
} else if (phaseCode.equals("110") || phaseCode.equals("100")) { // green
dl.get(e).add(new TrafficLightStateListEntry(TrafficLightState.Green));
}
countIncomingEdge++;
}
}
// System.out.println("phase-length: " + k +
// " outgoing:" + noOutgoing + " incoming:"
// + existingNode.getNoOfIncomingEdges());
}
}
existingNode.setDefaultLogic(dl);
current++;
this.setChanged();
this.notifyObservers(new ObserverNotification(NotificationType.progress,
(current/100)*all, "Build traffic light logic"));
this.clearChanged();