<commit_before>/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.artifacts.ivyservice;

import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.descriptor.DependencyArtifactDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.ConfigurationResolveReport;
import org.apache.ivy.core.report.ResolveReport;
import org.apache.ivy.core.resolve.IvyNode;
import org.apache.ivy.core.resolve.IvyNodeCallers;
import org.gradle.api.artifacts.*;
import org.gradle.api.internal.artifacts.DefaultResolvedArtifact;
import org.gradle.api.internal.artifacts.DefaultResolvedDependency;
import org.gradle.api.internal.artifacts.ResolvedConfigurationIdentifier;
import org.gradle.api.internal.artifacts.ivyservice.moduleconverter.dependencies.DependencyDescriptorFactory;
import org.gradle.util.Clock;
import org.gradle.util.WrapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author <NAME>
 */
public class DefaultIvyReportConverter implements IvyReportConverter {
    private static Logger logger = LoggerFactory.getLogger(DefaultIvyReportConverter.class);

    private DependencyDescriptorFactory dependencyDescriptorFactory;

    public DefaultIvyReportConverter(DependencyDescriptorFactory dependencyDescriptorFactory) {
        this.dependencyDescriptorFactory = dependencyDescriptorFactory;
    }

    public IvyConversionResult convertReport(ResolveReport resolveReport, Configuration configuration) {
        Clock clock = new Clock();
        ReportConversionContext context = new ReportConversionContext();
        context.configurationResolveReport = resolveReport.getConfigurationReport(configuration.getName());
        context.firstLevelDependenciesModuleRevisionIds = createFirstLevelDependenciesModuleRevisionIds(configuration.getAllDependencies(ModuleDependency.class));
        context.conf = configuration.getName();
        List<IvyNode> resolvedNodes = findResolvedNodes(resolveReport, context);
        for (IvyNode node : resolvedNodes) {
            constructConfigurationsForNode(node, context);
        }
        for (IvyNode node : resolvedNodes) {
            attachToParents(node, context);
        }

        if (context.root == null) {
            context.root = new DefaultResolvedDependency(resolveReport.getModuleDescriptor().getModuleRevisionId().getOrganisation(),
                    resolveReport.getModuleDescriptor().getModuleRevisionId().getName(),
                    resolveReport.getModuleDescriptor().getModuleRevisionId().getRevision(), configuration.getName(),
                    Collections.EMPTY_SET);
        }

        logger.debug("Timing: Translating report for configuration {} took {}", configuration, clock.getTime());
        return new DefaultIvyConversionResult(context.root, context.firstLevelResolvedDependencies, context.resolvedArtifacts);
    }

    private List<IvyNode> findResolvedNodes(ResolveReport resolveReport, ReportConversionContext context) {
        List<IvyNode> nodes = resolveReport.getDependencies();
        List<IvyNode> resolvedNodes = new ArrayList<IvyNode>();
        for (IvyNode node : nodes) {
            if (!isResolvedNode(node, context.conf)) {
                continue;
            }
            resolvedNodes.add(node);
        }
        if (!resolvedNodes.isEmpty()) {
            resolvedNodes.add(resolvedNodes.get(0).getRoot());
        }
        return resolvedNodes;
    }

    private boolean isResolvedNode(IvyNode node, String configuration) {
        return node.isLoaded() && !node.isEvicted(configuration);
    }

    private void attachToParents(IvyNode ivyNode, ReportConversionContext context) {
        Map<String, ConfigurationDetails> resolvedDependencies = context.handledNodes.get(ivyNode.getId());
        for (IvyNodeCallers.Caller caller : ivyNode.getCallers(context.conf)) {
            Set<String> dependencyConfigurationsForNode = getDependencyConfigurationsByCaller(ivyNode, caller);
            IvyNode parentNode = isRootCaller(context.configurationResolveReport, caller) ? ivyNode.getRoot() : context.configurationResolveReport.getDependency(caller.getModuleRevisionId());
            Map<String, ConfigurationDetails> parentResolvedDependencies = context.handledNodes.get(parentNode.getId());
            createAssociationsBetweenChildAndParentResolvedDependencies(ivyNode, resolvedDependencies, context.resolvedArtifacts, parentNode, caller,
                    dependencyConfigurationsForNode, parentResolvedDependencies.values());
        }
    }

    private void constructConfigurationsForNode(IvyNode ivyNode, ReportConversionContext context) {
        Map<String, ConfigurationDetails> resolvedDependencies = new LinkedHashMap<String, ConfigurationDetails>();
        for (IvyNodeCallers.Caller caller : ivyNode.getCallers(context.conf)) {
            Set<String> dependencyConfigurationsForNode = getDependencyConfigurationsByCaller(ivyNode, caller);
            for (String dependencyConfiguration : dependencyConfigurationsForNode) {
                if (!resolvedDependencies.containsKey(dependencyConfiguration)) {
                    ConfigurationDetails configurationDetails = context.addConfiguration(ivyNode, dependencyConfiguration);
                    context.resolvedArtifacts.addAll(configurationDetails.dependency.getModuleArtifacts());
                    resolvedDependencies.put(dependencyConfiguration, configurationDetails);
                }
            }
        }
        if (ivyNode == ivyNode.getRoot()) {
            ConfigurationDetails rootConfiguration = resolvedDependencies.get(context.conf);
            if (rootConfiguration == null) {
                rootConfiguration = context.addConfiguration(ivyNode, context.conf);
                resolvedDependencies.put(context.conf, rootConfiguration);<commit_msg>- Some trace for GRADLE-923<commit_after>/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.artifacts.ivyservice;

import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.descriptor.DependencyArtifactDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.ConfigurationResolveReport;
import org.apache.ivy.core.report.ResolveReport;
import org.apache.ivy.core.resolve.IvyNode;
import org.apache.ivy.core.resolve.IvyNodeCallers;
import org.gradle.api.artifacts.*;
import org.gradle.api.internal.artifacts.DefaultResolvedArtifact;
import org.gradle.api.internal.artifacts.DefaultResolvedDependency;
import org.gradle.api.internal.artifacts.ResolvedConfigurationIdentifier;
import org.gradle.api.internal.artifacts.ivyservice.moduleconverter.dependencies.DependencyDescriptorFactory;
import org.gradle.util.Clock;
import org.gradle.util.WrapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author <NAME>
 */
public class DefaultIvyReportConverter implements IvyReportConverter {
    private static Logger logger = LoggerFactory.getLogger(DefaultIvyReportConverter.class);

    private DependencyDescriptorFactory dependencyDescriptorFactory;

    public DefaultIvyReportConverter(DependencyDescriptorFactory dependencyDescriptorFactory) {
        this.dependencyDescriptorFactory = dependencyDescriptorFactory;
    }

    public IvyConversionResult convertReport(ResolveReport resolveReport, Configuration configuration) {
        Clock clock = new Clock();
        ReportConversionContext context = new ReportConversionContext();
        context.configurationResolveReport = resolveReport.getConfigurationReport(configuration.getName());
        context.firstLevelDependenciesModuleRevisionIds = createFirstLevelDependenciesModuleRevisionIds(configuration.getAllDependencies(ModuleDependency.class));
        context.conf = configuration.getName();
        List<IvyNode> resolvedNodes = findResolvedNodes(resolveReport, context);
        for (IvyNode node : resolvedNodes) {
            constructConfigurationsForNode(node, context);
        }
        for (IvyNode node : resolvedNodes) {
            attachToParents(node, context);
        }

        if (context.root == null) {
            context.root = new DefaultResolvedDependency(resolveReport.getModuleDescriptor().getModuleRevisionId().getOrganisation(),
                    resolveReport.getModuleDescriptor().getModuleRevisionId().getName(),
                    resolveReport.getModuleDescriptor().getModuleRevisionId().getRevision(), configuration.getName(),
                    Collections.EMPTY_SET);
        }

        logger.debug("Timing: Translating report for configuration {} took {}", configuration, clock.getTime());
        return new DefaultIvyConversionResult(context.root, context.firstLevelResolvedDependencies, context.resolvedArtifacts);
    }

    private List<IvyNode> findResolvedNodes(ResolveReport resolveReport, ReportConversionContext context) {
        List<IvyNode> nodes = resolveReport.getDependencies();
        List<IvyNode> resolvedNodes = new ArrayList<IvyNode>();
        for (IvyNode node : nodes) {
            if (!isResolvedNode(node, context.conf)) {
                continue;
            }
            resolvedNodes.add(node);
        }
        if (!resolvedNodes.isEmpty()) {
            resolvedNodes.add(resolvedNodes.get(0).getRoot());
        }
        return resolvedNodes;
    }

    private boolean isResolvedNode(IvyNode node, String configuration) {
        return node.isLoaded() && !node.isEvicted(configuration);
    }

    private void attachToParents(IvyNode ivyNode, ReportConversionContext context) {
        Map<String, ConfigurationDetails> resolvedDependencies = context.handledNodes.get(ivyNode.getId());
        for (IvyNodeCallers.Caller caller : ivyNode.getCallers(context.conf)) {
            Set<String> dependencyConfigurationsForNode = getDependencyConfigurationsByCaller(ivyNode, caller);
            IvyNode parentNode = isRootCaller(context.configurationResolveReport, caller) ? ivyNode.getRoot() : context.configurationResolveReport.getDependency(caller.getModuleRevisionId());
            Map<String, ConfigurationDetails> parentResolvedDependencies = context.handledNodes.get(parentNode.getId());
            if (parentResolvedDependencies == null) {
                throw new IllegalStateException(String.format("Could not find caller node %s for node %s. Available nodes: %s",
                        parentNode.getId(), ivyNode.getId(), context.handledNodes.keySet()));
            }
            createAssociationsBetweenChildAndParentResolvedDependencies(ivyNode, resolvedDependencies, context.resolvedArtifacts, parentNode, caller,
                    dependencyConfigurationsForNode, parentResolvedDependencies.values());
        }
    }

    private void constructConfigurationsForNode(IvyNode ivyNode, ReportConversionContext context) {
        Map<String, ConfigurationDetails> resolvedDependencies = new LinkedHashMap<String, ConfigurationDetails>();
        for (IvyNodeCallers.Caller caller : ivyNode.getCallers(context.conf)) {
            Set<String> dependencyConfigurationsForNode = getDependencyConfigurationsByCaller(ivyNode, caller);
            for (String dependencyConfiguration : dependencyConfigurationsForNode) {
                if (!resolvedDependencies.containsKey(dependencyConfiguration)) {
                    ConfigurationDetails configurationDetails = context.addConfiguration(ivyNode, dependencyConfiguration);
                    context.resolvedArtifacts.addAll(configurationDetails.dependency.getModuleArtifacts());
                    resolvedDependencies.put(dependencyConfiguration, configurationDetails);
                }
            }
        }
        if (ivyNode == ivyNode.getRoot()) {
            ConfigurationDetails rootConfiguration = resolvedDependencies.get(context.conf);
            if (rootConfiguration == null) {
                rootConfiguration = context.addConfiguration(ivyNode, context.conf);
                resolvedDependencies.put(context.conf, rootConfiguration);
{'fraction_non_alphanumeric': 0.08583496756675214, 'fraction_numerical': 0.0018856539447880524, 'mean_word_length': 3.8710506980161647, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4160285', 'n_tokens_mistral': 3267, 'n_tokens_neox': 3151, 'n_words': 661}