Search in sources :

Example 1 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class NumControlStructuresMetric method compute.

/**
 * Computes the minimum ratio of the difference between the number of
 * control structures in the original vs predicted ASTs.
 */
@Override
public float compute(DSubTree originalAST, List<DSubTree> predictedASTs) {
    int original = 1 + /* the body itself */
    originalAST.numBranches() + originalAST.numLoops() + originalAST.numExcepts();
    List<Integer> diffs = new ArrayList<>();
    diffs.add(original);
    for (DSubTree predictedAST : predictedASTs) {
        int predicted = 1 + predictedAST.numBranches() + predictedAST.numLoops() + predictedAST.numExcepts();
        int diff_predicted = Math.abs(predicted - original);
        diffs.add(diff_predicted);
    }
    float aggr_diff = Metric.min(diffs);
    return aggr_diff / original;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ArrayList(java.util.ArrayList)

Example 2 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class NumStatementsMetric method compute.

/**
 * Computes the minimum ratio of the difference between the number of
 * statements in the original vs predicted ASTs.
 */
@Override
public float compute(DSubTree originalAST, List<DSubTree> predictedASTs) {
    int original = originalAST.numStatements();
    List<Integer> diffs = new ArrayList<>();
    diffs.add(original);
    for (DSubTree predictedAST : predictedASTs) {
        int predicted = predictedAST.numStatements();
        int diff_predicted = Math.abs(predicted - original);
        diffs.add(diff_predicted);
    }
    float aggr_diff = Metric.min(diffs);
    return aggr_diff / original;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ArrayList(java.util.ArrayList)

Example 3 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class JaccardAPICallsMetric method compute.

/**
 * Computes the minimum Jaccard distance on the set of API calls
 * between the original and the predicted ASTs.
 */
@Override
public float compute(DSubTree originalAST, List<DSubTree> predictedASTs, String aggregate) {
    List<Float> jaccard = new ArrayList<>();
    jaccard.add((float) 1);
    Set<DAPICall> A = originalAST.bagOfAPICalls();
    for (DSubTree predictedAST : predictedASTs) {
        Set<DAPICall> B = predictedAST.bagOfAPICalls();
        // A union B
        Set<DAPICall> AunionB = new HashSet<>();
        AunionB.addAll(A);
        AunionB.addAll(B);
        // A intersect B
        Set<DAPICall> AinterB = new HashSet<>();
        AinterB.addAll(A);
        AinterB.retainAll(B);
        jaccard.add(1 - ((float) AinterB.size()) / AunionB.size());
    }
    return Metric.aggregate(jaccard, aggregate);
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) DAPICall(edu.rice.cs.caper.bayou.core.dsl.DAPICall)

Example 4 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class NumControlStructuresMetric method compute.

/**
 * Computes the minimum ratio of the difference between the number of
 * control structures in the original vs predicted ASTs.
 */
@Override
public float compute(DSubTree originalAST, List<DSubTree> predictedASTs, String aggregate) {
    int original = 1 + /* the body itself */
    originalAST.numBranches() + originalAST.numLoops() + originalAST.numExcepts();
    List<Integer> diffs = new ArrayList<>();
    diffs.add(original);
    for (DSubTree predictedAST : predictedASTs) {
        int predicted = 1 + predictedAST.numBranches() + predictedAST.numLoops() + predictedAST.numExcepts();
        int diff_predicted = Math.abs(predicted - original);
        diffs.add(diff_predicted);
    }
    float aggr_diff = Metric.aggregate(diffs, aggregate);
    return aggr_diff / original;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ArrayList(java.util.ArrayList)

Example 5 with DSubTree

use of edu.rice.cs.caper.bayou.core.dsl.DSubTree in project bayou by capergroup.

the class DOMClassInstanceCreation method handle.

@Override
public DSubTree handle() {
    DSubTree tree = new DSubTree();
    // add the expression's subtree (e.g: foo(..).bar() should handle foo(..) first)
    DSubTree Texp = new DOMExpression(creation.getExpression(), visitor).handle();
    tree.addNodes(Texp.getNodes());
    // evaluate arguments first
    for (Object o : creation.arguments()) {
        DSubTree Targ = new DOMExpression((Expression) o, visitor).handle();
        tree.addNodes(Targ.getNodes());
    }
    IMethodBinding binding = creation.resolveConstructorBinding();
    // check if the binding is of a generic type that involves user-defined types
    if (binding != null) {
        ITypeBinding cls = binding.getDeclaringClass();
        boolean userType = false;
        if (cls != null && cls.isParameterizedType())
            for (int i = 0; i < cls.getTypeArguments().length; i++) userType |= !cls.getTypeArguments()[i].getQualifiedName().startsWith("java.") && !cls.getTypeArguments()[i].getQualifiedName().startsWith("javax.");
        if (// get to the generic declaration
        userType || cls == null)
            while (binding != null && binding.getMethodDeclaration() != binding) binding = binding.getMethodDeclaration();
    }
    MethodDeclaration localMethod = Utils.checkAndGetLocalMethod(binding, visitor);
    if (localMethod != null) {
        DSubTree Tmethod = new DOMMethodDeclaration(localMethod, visitor).handle();
        tree.addNodes(Tmethod.getNodes());
    } else if (Utils.isRelevantCall(binding, visitor)) {
        try {
            tree.addNode(new DAPICall(binding, visitor.getLineNumber(creation)));
        } catch (DAPICall.InvalidAPICallException e) {
        // continue without adding the node
        }
    }
    return tree;
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) DAPICall(edu.rice.cs.caper.bayou.core.dsl.DAPICall)

Aggregations

DSubTree (edu.rice.cs.caper.bayou.core.dsl.DSubTree)26 ArrayList (java.util.ArrayList)7 DAPICall (edu.rice.cs.caper.bayou.core.dsl.DAPICall)5 DBranch (edu.rice.cs.caper.bayou.core.dsl.DBranch)3 DLoop (edu.rice.cs.caper.bayou.core.dsl.DLoop)3 Sequence (edu.rice.cs.caper.bayou.core.dsl.Sequence)3 DASTNode (edu.rice.cs.caper.bayou.core.dsl.DASTNode)2 HashSet (java.util.HashSet)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 Multiset (com.google.common.collect.Multiset)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 DExcept (edu.rice.cs.caper.bayou.core.dsl.DExcept)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1