@Override
protected void compile(final DependencyGraphBuilder builder) {
final ViewCalculationConfiguration calcConfig = getContext().getViewDefinition().getCalculationConfiguration(builder.getCalculationConfigurationName());
final Pair<DependencyGraph, Set<ValueRequirement>> graphPair = _previousGraphs.remove(builder.getCalculationConfigurationName());
if (graphPair != null) {
final DependencyGraph graph = graphPair.getFirst();
if (builder.getCompilationContext().getPortfolio() != null) {
// Remove any invalid terminal outputs from the graph
final PortfolioIdentifierGatherer gatherer = new PortfolioIdentifierGatherer();
PortfolioNodeTraverser.parallel(gatherer, getContext().getServices().getExecutorService()).traverse(builder.getCompilationContext().getPortfolio().getRootNode());
final Set<UniqueId> identifiers = gatherer.getIdentifiers();
final Set<ValueRequirement> specifics = calcConfig.getSpecificRequirements();
final Map<ValueSpecification, Set<ValueRequirement>> terminalOutputs = graph.getTerminalOutputs();
ValueSpecification[] removeValueSpec = null;
Set<ValueRequirement>[] removeValueReq = null;
int i = 0;
for (Map.Entry<ValueSpecification, Set<ValueRequirement>> terminal : terminalOutputs.entrySet()) {
if (!identifiers.contains(terminal.getKey().getTargetSpecification().getUniqueId())) {
// Can't be a portfolio requirement
Set<ValueRequirement> toRemove = null;
for (ValueRequirement requirement : terminal.getValue()) {
if (!specifics.contains(requirement)) {
// Not a specific requirement
if (toRemove == null) {
toRemove = Sets.newHashSetWithExpectedSize(terminal.getValue().size());
}
toRemove.add(requirement);
}
}
if (toRemove != null) {
if (i == 0) {
removeValueSpec = new ValueSpecification[terminalOutputs.size()];
removeValueReq = new Set[terminalOutputs.size()];
}
removeValueSpec[i] = terminal.getKey();
removeValueReq[i++] = toRemove;
}
}
}
if (i > 0) {
s_logger.info("Removing {} unmatched terminal outputs from {}", i, graph);
do {
i--;
graph.removeTerminalOutputs(removeValueReq[i], removeValueSpec[i]);
} while (i > 0);
}
}
// Populate the builder with the graph
builder.setDependencyGraph(graph);