Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions cddiff/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,44 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"
testImplementation "org.junit.vintage:junit-vintage-engine:$junit_version"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
testImplementation ("de.monticore:monticore-runtime:${project.properties['mc_version']}") capabilities {
requireCapability("de.monticore:monticore-runtime-tests") }
testImplementation("de.monticore:monticore-runtime:${project.properties['mc_version']}") capabilities {
requireCapability("de.monticore:monticore-runtime-tests")
}
testImplementation "de.monticore:monticore-grammar-trafo:$mc_version"

// dependencies for cddiff
implementation("de.monticore.lang:od:$mc_version") {
exclude group: 'de.monticore.lang', module:'cd4analysis'
exclude group: 'de.monticore.lang', module: 'cd4analysis'
}
implementation "edu.mit.alloy:alloy:6"
implementation "com.github.vinhkhuc:jfasttext:0.5"
}

tasks.register('downloadVectors') {
description = "Downloads the FastText vectors, including subword vectors"

def resourceDir = "src/main/resources/"

def vectors = file("$resourceDir/crawl-300d-2M-subword.bin")
if (!vectors.exists()) {

def zipFile = file("$resourceDir/vectors.zip")
if(zipFile.exists()){
zipFile.delete()
}
zipFile.createNewFile()
println("Downloading vectors into ${zipFile.absolutePath}")
new URL("https://dl.fbaipublicfiles.com/fasttext/vectors-english/crawl-300d-2M-subword.zip").withInputStream { input -> zipFile.withOutputStream { it << input } }

copy {
from zipTree(zipFile)
into resourceDir
}
zipFile.delete()
file("$resourceDir/crawl-300d-2M-subword.vec").delete()
println("Vectors downloaded and extracted to $resourceDir")
} else {
println("Vectors already exist at $resourceDir")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.cdmatcher.iterative;

import java.util.Objects;

public class MutablePair<T, V> {

private T a;
private V b;

public MutablePair(T a, V b) {
this.a = a;
this.b = b;
}

public T getA() { return a; }

public void setA(T a) { this.a = a; }

public V getB() { return b; }

public void setB(V b) { this.b = b; }

public String toString() {
return "(" + a + ", " + b + ")";
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
MutablePair<?, ?> that = (MutablePair<?, ?>) o;
return Objects.equals(a, that.a) && Objects.equals(b, that.b);
}

@Override
public int hashCode() {
return Objects.hash(a, b);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.cdmatcher.iterative.matching;

import de.monticore.cdmatcher.CDSimilarity;
import de.monticore.cdmatcher.MatchingStrategy;

public class MatchBySimilarity<T> implements MatchingStrategy<T> {

private final CDSimilarity<T> similarity;

public MatchBySimilarity(CDSimilarity<T> similarity) {
this.similarity = similarity;
}

@Override
public double getScore(T srcElem, T tgtElem) {
return similarity.computeWeight(srcElem, tgtElem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import de.monticore.cdassociation._ast.ASTCDAssociation;
import de.monticore.cdbasis._ast.ASTCDType;
import de.monticore.cdmatcher.MatchingStrategy;
import de.monticore.cdmatcher.MultipleMatchingStrategy;
import de.monticore.cdmatcher.iterative.matching.caching.CachedMatches;
import de.monticore.cdmatcher.iterative.matching.caching.StructureCache;
Expand All @@ -15,18 +16,21 @@
public class MatchCDAssocByBestSuperType extends
MultipleMatchingStrategy<ASTCDAssociation, ASTCDType> {

public CachedMatches cachedMatches;
public StructureCache structureCache;
private final CachedMatches cachedMatches;
private final StructureCache structureCache;
private final MatchingStrategy<ASTCDAssociation> nameMatchingStrategy;

public MatchCDAssocByBestSuperType(CachedMatches cachedMatches, StructureCache structureCache) {
public MatchCDAssocByBestSuperType(CachedMatches cachedMatches, StructureCache structureCache,
MatchingStrategy<ASTCDAssociation> nameMatchingStrategy) {
this.cachedMatches = cachedMatches;
this.structureCache = structureCache;
this.nameMatchingStrategy = nameMatchingStrategy;
}

@Override
public double getScore(ASTCDAssociation srcElem, ASTCDAssociation tgtElem) {

double nameScore = new MatchCDAssocByName().getScore(srcElem, tgtElem);
double nameScore = nameMatchingStrategy.getScore(srcElem, tgtElem);
double typeScore = -1;

if (structureCache.getLeftType(srcElem).isPresent() && structureCache.getLeftType(tgtElem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

public class MatchCDAssocByDirectType implements MatchingStrategy<ASTCDAssociation> {

public CachedMatches cachedMatches;
public StructureCache structureCache;
private final CachedMatches cachedMatches;
private final StructureCache structureCache;
private final MatchingStrategy<ASTCDAssociation> nameMatchingStrategy;

public MatchCDAssocByDirectType(CachedMatches cachedMatches, StructureCache structureCache) {
public MatchCDAssocByDirectType(CachedMatches cachedMatches, StructureCache structureCache,
MatchingStrategy<ASTCDAssociation> nameMatchingStrategy) {
this.cachedMatches = cachedMatches;
this.structureCache = structureCache;
this.nameMatchingStrategy = nameMatchingStrategy;
}

@Override
Expand All @@ -28,7 +31,7 @@ public double getScore(ASTCDAssociation srcElem, ASTCDAssociation tgtElem) {
Optional<ASTCDType> tgtRightType = structureCache.getRightType(tgtElem);
Optional<ASTCDType> tgtLeftType = structureCache.getLeftType(tgtElem);

double nameScore = new MatchCDAssocByName().getScore(srcElem, tgtElem);
double nameScore = nameMatchingStrategy.getScore(srcElem, tgtElem);
double typeScore = -1;
Double leftTypeScore = null;
Double rightTypeScore = null;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

public class MatchCDAttributeByNameAndType implements MatchingStrategy<ASTCDAttribute> {

public CachedMatches cachedMatches;
private final CachedMatches cachedMatches;
private final MatchingStrategy<ASTCDAttribute> nameMatchingStrategy;

public MatchCDAttributeByNameAndType(CachedMatches cachedMatches) {
public MatchCDAttributeByNameAndType(CachedMatches cachedMatches,
MatchingStrategy<ASTCDAttribute> nameMatchingStrategy) {
this.cachedMatches = cachedMatches;
this.nameMatchingStrategy = nameMatchingStrategy;
}

@Override
Expand All @@ -25,7 +28,7 @@ public double getScore(ASTCDAttribute srcElem, ASTCDAttribute tgtElem) {
Double attributeClassType = cachedMatches.getMatch(srcAttributeClassType,
tgtAttributeClassType);

double score = new MatchCDAttributeByName().getScore(srcElem, tgtElem);
double score = nameMatchingStrategy.getScore(srcElem, tgtElem);

if (attributeClassType != null) {
score = mean(score, attributeClassType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MatchCDTypeByDirectAssocs extends
MultipleMatchingStrategy<ASTCDType, ASTCDAssociation> {

private final MatchingStrategy<ASTCDAssociation> strategy;
public StructureCache structureCache;
private final StructureCache structureCache;

public MatchCDTypeByDirectAssocs(MatchingStrategy<ASTCDAssociation> associationMatchingStrategy,
StructureCache structureCache) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@

import de.monticore.cdbasis._ast.ASTCDAttribute;
import de.monticore.cdbasis._ast.ASTCDType;
import de.monticore.cdmatcher.iterative.matching.attribute.MatchCDAttributeByNameAndType;
import de.monticore.cdmatcher.MatchingStrategy;
import de.monticore.cdmatcher.MultipleMatchingStrategy;
import de.monticore.cdmatcher.iterative.matching.caching.CachedMatches;
import de.monticore.cdmatcher.iterative.matching.caching.StructureCache;

public class MatchCDTypeByDirectAttributes extends
MultipleMatchingStrategy<ASTCDType, ASTCDAttribute> {

public CachedMatches cachedMatches;
public StructureCache structureCache;
private final StructureCache structureCache;
private final MatchingStrategy<ASTCDAttribute> strategy;

public MatchCDTypeByDirectAttributes(CachedMatches cachedMatches, StructureCache structureCache) {
this.cachedMatches = cachedMatches;
public MatchCDTypeByDirectAttributes(StructureCache structureCache,
MatchingStrategy<ASTCDAttribute> strategy) {
this.structureCache = structureCache;
this.strategy = strategy;
}

@Override
public double getScore(ASTCDType srcElem, ASTCDType tgtElem) {
return getBestMatchingScore(srcElem, tgtElem, structureCache::getAttributes,
new MatchCDAttributeByNameAndType(cachedMatches));
return getBestMatchingScore(srcElem, tgtElem, structureCache::getAttributes, strategy);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public class MatchCDTypeByDirectSubClasses extends MultipleMatchingStrategy<ASTCDType, ASTCDType> {

public CachedMatches cachedMatches;
public StructureCache structureCache;
private final CachedMatches cachedMatches;
private final StructureCache structureCache;

public MatchCDTypeByDirectSubClasses(CachedMatches cachedMatches, StructureCache structureCache) {
this.cachedMatches = cachedMatches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
public class MatchCDTypeByDirectSuperClasses extends
MultipleMatchingStrategy<ASTCDType, ASTCDType> {

public CachedMatches cachedMatches;
public StructureCache structureCache;
private final CachedMatches cachedMatches;
private final StructureCache structureCache;

public MatchCDTypeByDirectSuperClasses(CachedMatches cachedMatches,
StructureCache structureCache) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public class MatchCDTypeComposite implements MatchingStrategy<ASTCDType> {

Set<MatchingStrategy<ASTCDType>> strategies;
public CachedMatches cachedMatches;
private final Set<MatchingStrategy<ASTCDType>> strategies;
private final CachedMatches cachedMatches;

public MatchCDTypeComposite(Set<MatchingStrategy<ASTCDType>> strategies,
CachedMatches cachedMatches) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@
package de.monticore.cdmatcher.iterative.matching.cdtype;

import de.monticore.cdbasis._ast.ASTCDType;
import de.monticore.cdmatcher.CDTypeSimilarity;
import de.monticore.cdmatcher.MatchingStrategy;
import de.monticore.cdmatcher.iterative.matching.MatchBySimilarity;
import de.monticore.cdmatcher.iterative.matching.caching.CachedMatches;

public class MatchCDTypeFromCache implements MatchingStrategy<ASTCDType> {

public CachedMatches cachedMatches;
private static MatchingStrategy<ASTCDType> defaultFallbackStrategy = new MatchBySimilarity<>(
new CDTypeSimilarity());
private final CachedMatches cachedMatches;
private final MatchingStrategy<ASTCDType> fallbackStrategy;

public MatchCDTypeFromCache(CachedMatches cachedMatches) {
public MatchCDTypeFromCache(CachedMatches cachedMatches,
MatchingStrategy<ASTCDType> fallbackStrategy) {
this.cachedMatches = cachedMatches;
this.fallbackStrategy = fallbackStrategy;
}

public MatchCDTypeFromCache(CachedMatches cachedMatches) {
this(cachedMatches, defaultFallbackStrategy);
}

public static MatchingStrategy<ASTCDType> getDefaultFallbackStrategy() {
return defaultFallbackStrategy;
}

public static void setDefaultFallbackStrategy(
MatchingStrategy<ASTCDType> defaultFallbackStrategy) {
MatchCDTypeFromCache.defaultFallbackStrategy = defaultFallbackStrategy;
}

@Override
public double getScore(ASTCDType srcElem, ASTCDType tgtElem) {
Double cachedScore = cachedMatches.getMatch(srcElem, tgtElem);

return cachedScore != null ? cachedScore : new MatchCDTypeByName().getScore(srcElem, tgtElem);
return cachedScore != null ? cachedScore : fallbackStrategy.getScore(srcElem, tgtElem);
}

}
Loading
Loading