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