IConceptMap<IConceptSet> allAffected = new SparseConceptMap<IConceptSet>(newConceptSubs.size());
for (IntIterator itr = newConceptSubs.keyIterator(); itr.hasNext();) {
final int x = itr.next();
if (!factory.isVirtualConcept(x)) {
IConceptSet set = new SparseConceptHashSet();
allNew.put(x, set);
for (IntIterator it = newConceptSubs.get(x).iterator(); it.hasNext();) {
int next = it.next();
if (!factory.isVirtualConcept(next)) {
set.add(next);
}
}
}
}
for (IntIterator itr = affectedConceptSubs.keyIterator(); itr.hasNext();) {
final int x = itr.next();
if (!factory.isVirtualConcept(x)) {
IConceptSet set = new SparseConceptHashSet();
allAffected.put(x, set);
for (IntIterator it = affectedConceptSubs.get(x).iterator(); it.hasNext();) {
int next = it.next();
if (!factory.isVirtualConcept(next)) {
set.add(next);
}
}
}
}
// 2. Create nodes for new concepts and connect to node hierarchy
// a. First create the nodes and add to index
for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
final String key = factory.lookupConceptId(itr.next()).toString();
Node cn = new Node();
cn.getEquivalentConcepts().add(key);
conceptNodeIndex.put(key, cn);
}
// b. Now connect the nodes disregarding redundant connections
Node bottomNode = conceptNodeIndex.get(au.csiro.ontology.model.NamedConcept.BOTTOM);
for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
int id = itr.next();
final String key = factory.lookupConceptId(id).toString();
Node cn = conceptNodeIndex.get(key);
IConceptSet parents = allNew.get(id);
for (IntIterator itr2 = parents.iterator(); itr2.hasNext();) {
// Create a connection to each parent
int parentId = itr2.next();
if (parentId == id)
continue;
Node parent = conceptNodeIndex.get(factory.lookupConceptId(parentId));
cn.getParents().add(parent);
parent.getChildren().add(cn);
// All nodes that get new children and are connected to BOTTOM
// must be disconnected
if (parent.getChildren().contains(bottomNode)) {
parent.getChildren().remove(bottomNode);
bottomNode.getParents().remove(parent);
}
}
}
Set<Integer> toRemoveFromAffected = new HashSet<Integer>();
for (IntIterator itr = allAffected.keyIterator(); itr.hasNext();) {
final int id = itr.next();
final String key = factory.lookupConceptId(id).toString();
Node cn = conceptNodeIndex.get(key);
IConceptSet parents = allAffected.get(id);
if(parents.contains(IFactory.BOTTOM_CONCEPT)) {
// Special case - bottom is parent
// a. add equivalents to bottom node
bottomNode.getEquivalentConcepts().addAll(cn.getEquivalentConcepts());
Set<Node> tempParents = cn.getParents();
Set<Node> tempChildren = cn.getChildren();
// b. reconnect parents to children
for(Node parent : tempParents) {
parent.getChildren().remove(cn);
parent.getChildren().addAll(tempChildren);
}
for(Node child : tempChildren) {
child.getParents().remove(cn);
child.getParents().addAll(tempParents);
}
for(String k : cn.getEquivalentConcepts()) {
conceptNodeIndex.remove(k);
conceptNodeIndex.put(key, bottomNode);
}
toRemoveFromAffected.add(id);
} else {
for (IntIterator itr2 = parents.iterator(); itr2.hasNext();) {
// Create a connection to each parent
int parentId = itr2.next();
if (parentId == id)
continue;
Node parent = conceptNodeIndex.get(factory.lookupConceptId(parentId));