use of edu.rice.cs.caper.bayou.core.dsl.Sequence in project bayou by capergroup.
the class Visitor method visit.
@Override
public boolean visit(TypeDeclaration clazz) {
if (clazz.isInterface())
return false;
List<TypeDeclaration> classes = new ArrayList<>();
classes.addAll(Arrays.asList(clazz.getTypes()));
classes.add(clazz);
for (TypeDeclaration cls : classes) allMethods.addAll(Arrays.asList(cls.getMethods()));
List<MethodDeclaration> constructors = allMethods.stream().filter(m -> m.isConstructor()).collect(Collectors.toList());
List<MethodDeclaration> publicMethods = allMethods.stream().filter(m -> !m.isConstructor() && Modifier.isPublic(m.getModifiers())).collect(Collectors.toList());
List<String> skeletons = new ArrayList<>();
List<Multiset<Integer>> cfg3_bfs = new ArrayList<>();
List<Multiset<Integer>> cfg4_bfs = new ArrayList<>();
List<Multiset<Integer>> cfg3_dfs = new ArrayList<>();
List<Multiset<Integer>> cfg4_dfs = new ArrayList<>();
for (MethodDeclaration m : allMethods) {
DecoratedSkeletonFeature skeleton = new DecoratedSkeletonFeature(m);
skeletons.add(skeleton.toString());
CFGFeature cfg = new CFGFeature(m);
cfg3_bfs.add(cfg.gen_subgraph(3, true));
cfg4_bfs.add(cfg.gen_subgraph(4, true));
cfg3_dfs.add(cfg.gen_subgraph(3, false));
cfg4_dfs.add(cfg.gen_subgraph(4, false));
}
Set<Pair<DSubTree, String>> astsWithJavadoc = new HashSet<>();
if (!constructors.isEmpty() && !publicMethods.isEmpty()) {
for (MethodDeclaration c : constructors) for (MethodDeclaration m : publicMethods) {
String javadoc = Utils.getJavadoc(m, options.JAVADOC_TYPE);
callStack.push(c);
DSubTree ast = new DOMMethodDeclaration(c, this).handle();
callStack.push(m);
ast.addNodes(new DOMMethodDeclaration(m, this).handle().getNodes());
callStack.pop();
callStack.pop();
if (ast.isValid())
astsWithJavadoc.add(new ImmutablePair<>(ast, javadoc));
}
} else if (!constructors.isEmpty()) {
// no public methods, only constructor
for (MethodDeclaration c : constructors) {
String javadoc = Utils.getJavadoc(c, options.JAVADOC_TYPE);
callStack.push(c);
DSubTree ast = new DOMMethodDeclaration(c, this).handle();
callStack.pop();
if (ast.isValid())
astsWithJavadoc.add(new ImmutablePair<>(ast, javadoc));
}
} else if (!publicMethods.isEmpty()) {
// no constructors, methods executed typically through Android callbacks
for (MethodDeclaration m : publicMethods) {
String javadoc = Utils.getJavadoc(m, options.JAVADOC_TYPE);
callStack.push(m);
DSubTree ast = new DOMMethodDeclaration(m, this).handle();
callStack.pop();
if (ast.isValid())
astsWithJavadoc.add(new ImmutablePair<>(ast, javadoc));
}
}
for (Pair<DSubTree, String> astDoc : astsWithJavadoc) {
List<Sequence> sequences = new ArrayList<>();
sequences.add(new Sequence());
try {
astDoc.getLeft().updateSequences(sequences, options.MAX_SEQS, options.MAX_SEQ_LENGTH);
List<Sequence> uniqSequences = new ArrayList<>(new HashSet<>(sequences));
if (okToPrintAST(uniqSequences))
addToJson(astDoc.getLeft(), uniqSequences, astDoc.getRight(), skeletons, cfg3_bfs, cfg4_bfs, cfg3_dfs, cfg4_dfs);
} catch (DASTNode.TooManySequencesException e) {
System.err.println("Too many sequences from AST");
} catch (DASTNode.TooLongSequenceException e) {
System.err.println("Too long sequence from AST");
}
}
return false;
}
use of edu.rice.cs.caper.bayou.core.dsl.Sequence in project bayou by capergroup.
the class JaccardSequencesMetric method compute.
/**
* Computes the minimum Jaccard distance on the set of sequences 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<Sequence> A;
try {
List<Sequence> _A = new ArrayList<>();
_A.add(new Sequence());
originalAST.updateSequences(_A, 999, 999);
A = new HashSet<>(_A);
} catch (DASTNode.TooManySequencesException | DASTNode.TooLongSequenceException e) {
return (float) 1;
}
for (DSubTree predictedAST : predictedASTs) {
Set<Sequence> B;
try {
List<Sequence> _B = new ArrayList<>();
_B.add(new Sequence());
predictedAST.updateSequences(_B, 999, 999);
B = new HashSet<>(_B);
} catch (DASTNode.TooManySequencesException | DASTNode.TooLongSequenceException e) {
jaccard.add((float) 1);
continue;
}
// A union B
Set<Sequence> AunionB = new HashSet<>();
AunionB.addAll(A);
AunionB.addAll(B);
// A intersect B
Set<Sequence> AinterB = new HashSet<>();
AinterB.addAll(A);
AinterB.retainAll(B);
jaccard.add(1 - ((float) AinterB.size()) / AunionB.size());
}
return Metric.min(jaccard);
}
use of edu.rice.cs.caper.bayou.core.dsl.Sequence in project bayou by capergroup.
the class JaccardSequencesMetric method compute.
/**
* Computes the minimum Jaccard distance on the set of sequences 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<Sequence> A;
try {
List<Sequence> _A = new ArrayList<>();
_A.add(new Sequence());
originalAST.updateSequences(_A, 999, 999);
A = new HashSet<>(_A);
} catch (DASTNode.TooManySequencesException | DASTNode.TooLongSequenceException e) {
return (float) 1;
}
for (DSubTree predictedAST : predictedASTs) {
Set<Sequence> B;
try {
List<Sequence> _B = new ArrayList<>();
_B.add(new Sequence());
predictedAST.updateSequences(_B, 999, 999);
B = new HashSet<>(_B);
} catch (DASTNode.TooManySequencesException | DASTNode.TooLongSequenceException e) {
jaccard.add((float) 1);
continue;
}
// A union B
Set<Sequence> AunionB = new HashSet<>();
AunionB.addAll(A);
AunionB.addAll(B);
// A intersect B
Set<Sequence> AinterB = new HashSet<>();
AinterB.addAll(A);
AinterB.retainAll(B);
jaccard.add(1 - ((float) AinterB.size()) / AunionB.size());
}
return Metric.aggregate(jaccard, aggregate);
}
Aggregations