Search in sources :

Example 1 with RootNode

use of mrmathami.cia.cpp.ast.RootNode in project Cpp4CIA by thanhminhmr.

the class VersionBuilder method build.

@Nonnull
public static ProjectVersion build(@Nonnull String versionName, @Nonnull Path projectRoot, @Nonnull List<Path> projectFiles, @Nonnull List<Path> includePaths, @Nonnull Map<DependencyType, Double> dependencyTypeWeightMap) throws CppException {
    try {
        final List<Path> projectFileList = createPathList(projectFiles);
        final List<Path> externalIncludePaths = createPathList(includePaths);
        final List<Path> internalIncludePaths = createInternalIncludePaths(projectFileList);
        final List<Path> includePathList = combinePathList(externalIncludePaths, internalIncludePaths);
        final Path projectRootPath = projectRoot.toRealPath(LinkOption.NOFOLLOW_LINKS);
        final char[] fileContentCharArray = PreprocessorBuilder.build(projectRootPath, projectFileList, includePathList, false);
        final IASTTranslationUnit translationUnit = TranslationUnitBuilder.build(fileContentCharArray);
        final RootNode root = AstBuilder.build(translationUnit);
        final List<String> projectFilePaths = createRelativePathStrings(projectFileList, projectRootPath);
        final List<String> projectIncludePaths = createRelativePathStrings(externalIncludePaths, projectRootPath);
        final DependencyType[] types = DependencyType.values();
        final double[] typeWeights = new double[types.length];
        for (final DependencyType type : types) typeWeights[type.ordinal()] = dependencyTypeWeightMap.get(type);
        final double[] weights = calculateWeights(typeWeights, root);
        return new ProjectVersion(versionName, projectFilePaths, projectIncludePaths, root, typeWeights, weights);
    } catch (IOException e) {
        throw new CppException("Error when trying to build project!", e);
    }
}
Also used : Path(java.nio.file.Path) RootNode(mrmathami.cia.cpp.ast.RootNode) CppException(mrmathami.cia.cpp.CppException) IOException(java.io.IOException) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) DependencyType(mrmathami.cia.cpp.ast.DependencyType) Nonnull(mrmathami.annotations.Nonnull)

Example 2 with RootNode

use of mrmathami.cia.cpp.ast.RootNode in project Cpp4CIA by thanhminhmr.

the class VersionDiffer method compare.

@Nonnull
public static VersionDifference compare(@Nonnull ProjectVersion versionA, @Nonnull ProjectVersion versionB, @Nonnull Map<DependencyType, Double> dependencyTypeImpactWeightMap, int maxDepth) throws CppException {
    final RootNode rootA = versionA.getRootNode();
    final RootNode rootB = versionB.getRootNode();
    final CppNode.Matcher matcher = new CppNode.Matcher();
    final Map<CppNode.Wrapper, CppNode> nodeMapA = new HashMap<>();
    final Map<CppNode.Wrapper, CppNode> nodeMapB = new HashMap<>();
    nodeMapA.put(new CppNode.Wrapper(rootA, CppNode.MatchLevel.SIMILAR, matcher), rootA);
    nodeMapB.put(new CppNode.Wrapper(rootB, CppNode.MatchLevel.SIMILAR, matcher), rootB);
    for (final CppNode nodeA : rootA) {
        if (!(nodeA instanceof IntegralNode)) {
            final CppNode.Wrapper wrapper = new CppNode.Wrapper(nodeA, CppNode.MatchLevel.SIMILAR, matcher);
            final CppNode node = nodeMapA.put(wrapper, nodeA);
            if (node != null) {
                throw new AssertionError();
            }
        }
    }
    for (final CppNode nodeB : rootB) {
        if (!(nodeB instanceof IntegralNode)) {
            final CppNode.Wrapper wrapper = new CppNode.Wrapper(nodeB, CppNode.MatchLevel.SIMILAR, matcher);
            final CppNode node = nodeMapB.put(wrapper, nodeB);
            if (node != null) {
                throw new AssertionError();
            }
        }
    }
    final Set<CppNode> addedNodes = new HashSet<>();
    final Set<Pair<CppNode, CppNode>> changedNodes = new HashSet<>();
    final Set<Pair<CppNode, CppNode>> unchangedNodes = new HashSet<>();
    final Set<CppNode> removedNodes = new HashSet<>();
    final List<CppNode> changedListB = new LinkedList<>();
    for (final CppNode.Wrapper wrapperA : nodeMapA.keySet()) {
        final CppNode nodeA = wrapperA.getNode();
        final CppNode nodeB = nodeMapB.get(wrapperA);
        if (nodeB != null) {
            if (matcher.isNodeMatch(nodeA, nodeB, CppNode.MatchLevel.IDENTICAL)) {
                unchangedNodes.add(Pair.immutableOf(nodeA, nodeB));
            } else {
                changedNodes.add(Pair.immutableOf(nodeA, nodeB));
                changedListB.add(nodeB);
            }
        } else {
            removedNodes.add(nodeA);
        }
    }
    for (final CppNode.Wrapper wrapperB : nodeMapB.keySet()) {
        final CppNode nodeA = nodeMapA.get(wrapperB);
        final CppNode nodeB = wrapperB.getNode();
        if (nodeA == null) {
            addedNodes.add(nodeB);
            changedListB.add(nodeB);
        }
    }
    final DependencyType[] types = DependencyType.values();
    final double[] typeImpactWeights = new double[types.length];
    for (final DependencyType type : types) {
        typeImpactWeights[type.ordinal()] = dependencyTypeImpactWeightMap.get(type);
    }
    final double[] impactWeights = ImpactWeightBuilder.calculate(typeImpactWeights, rootB, changedListB, maxDepth);
    return new VersionDifference(versionA, versionB, addedNodes, changedNodes, unchangedNodes, removedNodes, typeImpactWeights, impactWeights, maxDepth);
}
Also used : RootNode(mrmathami.cia.cpp.ast.RootNode) HashMap(java.util.HashMap) CppNode(mrmathami.cia.cpp.ast.CppNode) LinkedList(java.util.LinkedList) DependencyType(mrmathami.cia.cpp.ast.DependencyType) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode) HashSet(java.util.HashSet) Pair(mrmathami.utils.Pair) Nonnull(mrmathami.annotations.Nonnull)

Example 3 with RootNode

use of mrmathami.cia.cpp.ast.RootNode in project Cpp4CIA by thanhminhmr.

the class VersionDifference method getImpactWeightMap.

@Nonnull
public Map<CppNode, Double> getImpactWeightMap() {
    if (impactWeightMap != null)
        return impactWeightMap;
    final Map<CppNode, Double> map = new IdentityHashMap<>();
    final RootNode rootNode = versionB.getRootNode();
    // root id == 0
    map.put(rootNode, impactWeights[0]);
    for (final CppNode node : rootNode) map.put(node, impactWeights[node.getId()]);
    return this.impactWeightMap = Map.copyOf(map);
}
Also used : RootNode(mrmathami.cia.cpp.ast.RootNode) IdentityHashMap(java.util.IdentityHashMap) CppNode(mrmathami.cia.cpp.ast.CppNode) Nonnull(mrmathami.annotations.Nonnull)

Aggregations

Nonnull (mrmathami.annotations.Nonnull)3 RootNode (mrmathami.cia.cpp.ast.RootNode)3 CppNode (mrmathami.cia.cpp.ast.CppNode)2 DependencyType (mrmathami.cia.cpp.ast.DependencyType)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedList (java.util.LinkedList)1 CppException (mrmathami.cia.cpp.CppException)1 IntegralNode (mrmathami.cia.cpp.ast.IntegralNode)1 Pair (mrmathami.utils.Pair)1 IASTTranslationUnit (org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)1