use of edu.rice.cs.caper.bayou.core.dsl.DAPICall 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.DAPICall 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;
}
use of edu.rice.cs.caper.bayou.core.dsl.DAPICall in project bayou by capergroup.
the class DOMMethodInvocation 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(invocation.getExpression(), visitor).handle();
tree.addNodes(Texp.getNodes());
// evaluate arguments first
for (Object o : invocation.arguments()) {
DSubTree Targ = new DOMExpression((Expression) o, visitor).handle();
tree.addNodes(Targ.getNodes());
}
IMethodBinding binding = invocation.resolveMethodBinding();
// 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) {
Stack<MethodDeclaration> callStack = visitor.callStack;
if (!callStack.contains(localMethod)) {
callStack.push(localMethod);
DSubTree Tmethod = new DOMMethodDeclaration(localMethod, visitor).handle();
callStack.pop();
tree.addNodes(Tmethod.getNodes());
}
} else if (Utils.isRelevantCall(binding, visitor)) {
try {
tree.addNode(new DAPICall(binding, visitor.getLineNumber(invocation)));
} catch (DAPICall.InvalidAPICallException e) {
// continue without adding the node
}
}
return tree;
}
use of edu.rice.cs.caper.bayou.core.dsl.DAPICall in project bayou by capergroup.
the class DOMPrefixExpression method handle.
@Override
public DSubTree handle() {
DSubTree sub = new DOMExpression(expression.getOperand(), visitor).handle();
if (expression.getOperator() == PrefixExpression.Operator.NOT && sub.getNodes().size() == 1) {
// encode the ! predicate into the API call name
DAPICall call = (DAPICall) sub.getNodes().get(0);
call.setNotPredicate();
}
return sub;
}
use of edu.rice.cs.caper.bayou.core.dsl.DAPICall 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) {
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.min(jaccard);
}