ntMolecules.add(ntMolecule);
}
keys.addAll(nodes2Keys.remove(nodeInGroup));
}
FunctionallyGroundedNodeImpl newNode = new FunctionallyGroundedNodeImpl(
ntMolecules);
newNodes.add(newNode);
nodes2Keys.put(newNode, keys);
for (FunctionallyGroundedNode nodeInGroup : group) {
old2NewMap.put(nodeInGroup, newNode);
}
}
// replace member of the group occuring in nt-molecules of any fg-node
// with the new node,
// this involves replacing original-fg-nodes which unfinalised ones,
// where this
// occurs node2Keys and recursively the other nodes are updated
// for all nodes
Collection<NonTerminalMoleculeImpl> newNtMolecules = new ArrayList<NonTerminalMoleculeImpl>();
Map<FunctionallyGroundedNode, Set<T>> resultNodes2Keys = nodes2Keys;
while (true) {
Map<FunctionallyGroundedNode, FunctionallyGroundedNode> newOld2NewMap = new HashMap<FunctionallyGroundedNode, FunctionallyGroundedNode>();
Map<FunctionallyGroundedNode, Set<T>> newNodes2Keys = new HashMap<FunctionallyGroundedNode, Set<T>>();
for (FunctionallyGroundedNode node : resultNodes2Keys.keySet()) {
// we add the replacement and the untouched nodes to a
// newNodes2keySet
// if newNodes contains a key we modify it
// if we create a new key it is added to newOld2NewMap
boolean moleculeReplaced = false;
Set<NonTerminalMolecule> moleculesOfNewNode = new HashSet<NonTerminalMolecule>();
for (NonTerminalMolecule molecule : node
.getGroundingMolecules()) {
NonTerminalMoleculeImpl replacementMolecule = null;
Triple triple = molecule.iterator().next(); // nt-molecules
// have exactly
// one element
Node subject = triple.getSubject();
if (subject != NonTerminalMolecule.GROUNDED_NODE) {
Node replacementSubject = old2NewMap.get(subject);
if (replacementSubject != null) {
replacementMolecule = new NonTerminalMoleculeImpl(
new TripleImpl(replacementSubject, triple
.getPredicate(),
NonTerminalMolecule.GROUNDED_NODE));
}
} else {
Node replacementObject = old2NewMap.get(triple
.getObject());
if (replacementObject != null) {
replacementMolecule = new NonTerminalMoleculeImpl(
new TripleImpl(
NonTerminalMolecule.GROUNDED_NODE,
triple.getPredicate(),
replacementObject));
}
}
if (replacementMolecule != null) {
moleculeReplaced = true;
moleculesOfNewNode.add(replacementMolecule);
newNtMolecules.add(replacementMolecule);
} else {
moleculesOfNewNode.add(molecule);
}
}
Set<T> keys = resultNodes2Keys.get(node);
if (newNodes.contains(node)) {
((FunctionallyGroundedNodeImpl) node)
.setMolecules(moleculesOfNewNode);
newNodes2Keys.put(node, keys);
} else {
if (moleculeReplaced) {
FunctionallyGroundedNodeImpl newNode = new FunctionallyGroundedNodeImpl(
moleculesOfNewNode);
newNodes.add(newNode);
newOld2NewMap.put(node, newNode);
// Set<T> keys = nodes2Keys.remove(node);
newNodes2Keys.put(newNode, keys);
} else {
newNodes2Keys.put(node, keys);
}
}
}
old2NewMap = newOld2NewMap;
resultNodes2Keys = newNodes2Keys;
if (old2NewMap.size() == 0) {
break;
}
}
// at the end we finalize our nodes and molecules
for (FunctionallyGroundedNodeImpl newNode : newNodes)
newNode.markFinalized();
for (NonTerminalMoleculeImpl newMol : newNtMolecules)
newMol.markFinalized();