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);
}
}
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);
}
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);
}
Aggregations