* @param replacementMap
* @param fgNodesGraph the graph to which the triples resulting from fg-node naturalization are added
*/
private void addGraphTriples(Graph graph, boolean addReferencedFgNodes, Naturalizer naturalizer, Map<Set<NonTerminalMolecule>, Node> replacementMap, Graph fgNodesGraph) {
for (Iterator iter = graph.iterator(); iter.hasNext();) {
Triple triple = (Triple) iter.next();
Node subject = triple.getSubject();
if (subject instanceof FunctionallyGroundedNode) {
if (addReferencedFgNodes) {
Node replacement = replacementMap.get(((FunctionallyGroundedNode) subject).getGroundingMolecules()); // (Node)fg2NormalNodeMap.get(object);
if (replacement == null) {
Set<NonTerminalMolecule> groundingMolecules = ((FunctionallyGroundedNode) subject).getGroundingMolecules();
subject = addFgNodeMolecules((FunctionallyGroundedNode) subject, naturalizer, fgNodesGraph);
replacementMap.put(groundingMolecules, subject);
} else {
subject = replacement;
}
} else {
Node replacement = replacementMap.get(((FunctionallyGroundedNode) subject).getGroundingMolecules());
if (replacement == null) {
log.error("no replacement found for the fg-node: "+ subject);
throw new RuntimeException("no replacement found for the fg-node: "+ subject);
}
subject = replacement;
}
} else {
if (subject instanceof GroundedNode) {
if (!(subject instanceof NaturallyGroundedNode)) {
throw new RuntimeException("Cannot naturalize subject node of type "+subject.getClass());
}
}
}
Node object = triple.getObject();
if (object instanceof FunctionallyGroundedNode) {
if (addReferencedFgNodes) {
Node replacement = replacementMap.get(((FunctionallyGroundedNode) object).getGroundingMolecules()); // (Node)fg2NormalNodeMap.get(object);
if (replacement == null) {
Set<NonTerminalMolecule> groundingMolecules = ((FunctionallyGroundedNode) object).getGroundingMolecules();
object = addFgNodeMolecules((FunctionallyGroundedNode) object, naturalizer, fgNodesGraph);
replacementMap.put(groundingMolecules, object);
} else {
object = replacement;
}
} else {
Node replacement = (replacementMap).get(new HashSet<NonTerminalMolecule>(((FunctionallyGroundedNode) object).getGroundingMolecules())); // (Node)fg2NormalNodeMap.get(object);
if (replacement == null) {
throw new NoReplacementFoundException("no replacement found for the fg-node: "+ object, (FunctionallyGroundedNode) object);
}
object = replacement;
}
} else {
if (object instanceof GroundedNode) {
if (!(object instanceof NaturallyGroundedNode)) {
throw new RuntimeException("Cannot naturalize object node of type "+object.getClass());
}
}
}
doAdd(new TripleImpl(subject, triple.getPredicate(), object));
}
finalized = true;
}