Search in sources :

Example 1 with Sequence

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;
}
Also used : PrintWriter(java.io.PrintWriter) java.util(java.util) org.eclipse.jdt.core.dom(org.eclipse.jdt.core.dom) Multiset(com.google.common.collect.Multiset) IOException(java.io.IOException) DASTNode(edu.rice.cs.caper.bayou.core.dsl.DASTNode) Sequence(edu.rice.cs.caper.bayou.core.dsl.Sequence) Collectors(java.util.stream.Collectors) GsonBuilder(com.google.gson.GsonBuilder) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) FileNotFoundException(java.io.FileNotFoundException) Pair(org.apache.commons.lang3.tuple.Pair) Gson(com.google.gson.Gson) DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair) Sequence(edu.rice.cs.caper.bayou.core.dsl.Sequence) DASTNode(edu.rice.cs.caper.bayou.core.dsl.DASTNode) DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Multiset(com.google.common.collect.Multiset)

Example 2 with Sequence

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);
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) ArrayList(java.util.ArrayList) Sequence(edu.rice.cs.caper.bayou.core.dsl.Sequence) HashSet(java.util.HashSet)

Example 3 with Sequence

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);
}
Also used : DSubTree(edu.rice.cs.caper.bayou.core.dsl.DSubTree) Sequence(edu.rice.cs.caper.bayou.core.dsl.Sequence)

Aggregations

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