Search in sources :

Example 1 with DependencyType

use of mrmathami.cia.cpp.ast.DependencyType 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 DependencyType

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

the class VersionBuilder method calculateWeights.

@Nonnull
private static double[] calculateWeights(@Nonnull double[] weightMap, @Nonnull RootNode rootNode) {
    final double[] weights = new double[rootNode.getNodeCount()];
    for (final CppNode node : rootNode) {
        double directWeight = 0.0;
        for (final CppNode dependencyNode : node.getAllDependencyFrom()) {
            final DependencyMap dependencyMap = node.getNodeDependencyFrom(dependencyNode);
            for (final DependencyType type : DependencyType.values) {
                directWeight += weightMap[type.ordinal()] * dependencyMap.getCount(type);
            }
        }
        weights[node.getId()] = directWeight;
    }
    return weights;
}
Also used : DependencyType(mrmathami.cia.cpp.ast.DependencyType) CppNode(mrmathami.cia.cpp.ast.CppNode) DependencyMap(mrmathami.cia.cpp.ast.DependencyMap) Nonnull(mrmathami.annotations.Nonnull)

Example 3 with DependencyType

use of mrmathami.cia.cpp.ast.DependencyType 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)

Aggregations

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