active inductive logic programming for code search
play

Active Inductive Logic Programming for Code Search Aishwarya - PowerPoint PPT Presentation

Active Inductive Logic Programming for Code Search Aishwarya Sivaraman, Tianyi Zhang, Guy Van den Broeck, Miryung Kim University of California, Los Angeles Tool and dataset: https://github.com/AishwaryaSivaraman/ALICE-ILP-for-Code-Search


  1. Active Inductive Logic Programming for Code Search Aishwarya Sivaraman, Tianyi Zhang, Guy Van den Broeck, Miryung Kim University of California, Los Angeles Tool and dataset: https://github.com/AishwaryaSivaraman/ALICE-ILP-for-Code-Search

  2. Developers Often Search For Similar Code • Bug fix [Kim et al. , 2006] • API-related refactoring [Dig and Johnson, 2006] • Optimization [Ahmad and Cheung, 2018]

  3. Existing Code Search • Internet code search engines [Krugle, S6, CodeGenie] • Lacks expressiveness and query refinement is tedious • Clone detection techniques [CCFinder, Deckard] • Threshold metric insufficient to capture the abstract search intent • Interactive template based code search [Critics] • Interaction is tedious

  4. ALICE: Interactive Code Search via Active Inductive Logic Programming Query Input: One code example ALICE: Generates a query (a search pattern) Output: Set of method locations that match the Code Search Results query (Iteration 0)

  5. ALICE: Interactive Code Search via Active Inductive Logic Programming Query Input: More labels Code Search Results (Iteration 1)

  6. ALICE: Interactive Code Search via Active Inductive Logic Programming Query ∧ a Input: More labels ALICE: Refines the initial query (search pattern) Output: A smaller set of method locations that Code Search Results (Iteration 2) match the new query

  7. ALICE: Interactive Code Search via Active Inductive Logic Programming Query ∧ a Input: More labels Code Search Results (Iteration 2)

  8. ALICE: Interactive Code Search via Active Inductive Logic Programming Query ∧ a ∧ b Q ∧ a Input: More labels ALICE: Keep refining the query Output: A smaller set of method locations that Code Search Results match the new query (Iteration 3)

  9. Active Learning • Obtaining labels is time consuming and expensive Inductive Logic Programming • Data as feature vectors cannot easily express the structure of code • ILP: Positive examples + negative examples + background knowledge as rules

  10. Represent Code as Logic Facts Fact Predicate if (ID, CONDITION) loop (ID, CONDITION) parent (ID, ID) next (ID, ID) methodCall (ID, NAME) type (ID, NAME) exception (ID, NAME) methodDec (ID, NAME)

  11. Represent Code as Logic Facts Extracted Logic Facts public void queryDB() { methodDec (0, queryDB) Fact Predicate try { if (ID, CONDITION) Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","root","root"); loop (ID, CONDITION) Statement stmt = con.createStatement(); parent (ID, ID) ResultSet rs = stmt.executeQuery("select * from emp"); while (rs.next()) { next (ID, ID) System.out.println(rs.getInt(1)); methodCall (ID, NAME) } con.close(); type (ID, NAME) } catch (SQLException e) { System.out.println(e); exception (ID, NAME) } methodDec (ID, NAME) }

  12. Represent Code as Logic Facts Extracted Logic Facts public void queryDB() { methodDec (0, queryDB), Fact Predicate try { type (1, Connection), if (ID, CONDITION) Connection con = DriverManager.getConnection( parent (0, 1) "jdbc:mysql://localhost:3306/db","root","root"); loop (ID, CONDITION) Statement stmt = con.createStatement(); parent (ID, ID) ResultSet rs = stmt.executeQuery("select * from emp"); while (rs.next()) { next (ID, ID) System.out.println(rs.getInt(1)); methodCall (ID, NAME) } con.close(); type (ID, NAME) } catch (SQLException e) { System.out.println(e); exception (ID, NAME) } methodDec (ID, NAME) }

  13. Represent Code as Logic Facts Extracted Logic Facts public void queryDB() { methodDec (0, queryDB), Fact Predicate try { type (1, Connection), if (ID, CONDITION) Connection con = DriverManager.getConnection( parent (0, 1), "jdbc:mysql://localhost:3306/db","root","root"); methodCall(2, getConnection), loop (ID, CONDITION) Statement stmt = con.createStatement(); parent (0, 2), parent (ID, ID) ResultSet rs = stmt.executeQuery("select * from emp"); next (2, 1) while (rs.next()) { next (ID, ID) System.out.println(rs.getInt(1)); methodCall (ID, NAME) } con.close(); type (ID, NAME) } catch (SQLException e) { System.out.println(e); exception (ID, NAME) } methodDec (ID, NAME) }

  14. Represent Code as Logic Facts Extracted Logic Facts public void queryDB() { methodDec (0, queryDB), Fact Predicate try { type (1, Connection), if (ID, CONDITION) Connection con = DriverManager.getConnection( parent (0, 1), "jdbc:mysql://localhost:3306/db","root","root"); methodCall(2, getConnection), loop (ID, CONDITION) Statement stmt = con.createStatement(); parent (0, 2), parent (ID, ID) ResultSet rs = stmt.executeQuery("select * from emp"); next (2, 1), while (rs.next()) { … next (ID, ID) System.out.println(rs.getInt(1)); loop (7, "rs.next()"), methodCall (ID, NAME) } methodCall (8, getInt), con.close(); parent (7, 8), type (ID, NAME) } catch (SQLException e) { … System.out.println(e); exception (10, SQLException), exception (ID, NAME) } parent (0, 10), methodDec (ID, NAME) } …

  15. Formulate a Search Query • A user selects a code example and annotate important features. public void queryDB() { try { Connection con = DriverManager.getConnection( methodDec (i 0 , m) ∧ "jdbc:mysql://localhost:3306/db","root","root"); type (i 1 , ResultSet) ∧ Statement stmt = con.createStatement(); contains (i 0 , i 1 ) ∧ ResultSet rs = stmt.executeQuery("select * from emp"); methodCall(i 2 , executeQuery) ∧ while (rs.next()) { contains (i 0 , i 2 ) ∧ System.out.println(rs.getInt(1)); looplike (i 3 , "*.next()") ∧ } contains (i 0 , i 3 ) con.close(); } catch (SQLException e){ System.out.println(e); } } A code example with user annotations search query

  16. Logic-based Code Search Search Query Fact Base Rules methodDec (i 0 , m) ∧ iflike (ID, regex) :- if (ID, cond), match (cond, regex) type (i 1 , ResultSet) ∧ looplike (ID, regex) :- loop (ID, cond), match (cond, contains (i 0 , i 1 ) ∧ regex) methodCall(i 2 , executeQuery) ∧ contains (ID 1 , ID 2 ) :- parent (ID 1 , ID 2 ) contains (i 0 , i 2 ) ∧ contains (ID 1 , ID 3 ) :- parent (ID 1 , ID 2 ), contains (ID 2 , ID 3 ) looplike (i 3 , "*.next()") ∧ contains (i 0 , i 3 ) before(ID 1 , ID 2 ) :- next(ID 2 , ID 1 ) before(ID 1 , ID 3 ) : - next(ID 2 , ID 1 ), before(ID 2 , ID 3 ). Costa et al., “The yap prolog system,” Theory and Practice of Logic Programming, 2012

  17. Logic-based Code Search Matched Code public void getUserName(String id) { try { ResultSet set = db.executeQuery( "select name from users where id=” + id); while (set.next()) { … } } catch (SQLException e) { …} Search Query Fact Base } methodDec (i 0 , m) ∧ public void queryDatabase() { type (i 1 , ResultSet) ∧ try { contains (i 0 , i 1 ) ∧ ResultSet result = s.executeQuery("select * from customers”); while (result.next()) { … } methodCall(i 2 , executeQuery) ∧ } catch (SQLException e) { …} contains (i 0 , i 2 ) ∧ } looplike (i 3 , "*.next()") ∧ contains (i 0 , i 3 ) public List get() { ResultSet set = stmt.executeQuery("select * from t”); Fact Rules List l = new List(); while (set.next()) { … } return l; } and 32 other matched locations

  18. Partial Feedback public void getUserName(String id) { try { ResultSet set = db.executeQuery( Search Query "select name from users where id=” + id); while (set.next()) { … } methodDec (i 0 , m) ∧ } catch (SQLException e) { …} type (i 1 , ResultSet) ∧ } contains (i 0 , i 1 ) ∧ methodCall(i 2 , executeQuery) ∧ public void queryDatabase() { contains (i 0 , i 2 ) ∧ try { looplike (i 3 , "*.next()") ∧ ResultSet result = s.executeQuery("select * from customers”); while (result.next()) { … } contains (i 0 , i 3 ) } catch (SQLException e) { …} } public List get() { ResultSet set = stmt.executeQuery("select * from t”); List l = new List(); while (set.next()) { … } return l; }

  19. Query Refinement via Active Learning public void getUserName(String id) { Refined Query try { ResultSet set = db.executeQuery( "select name from users where id=” + id); methodDec (i 0 , m) ∧ while (set.next()) { … } type (i 1 , ResultSet) ∧ } catch (SQLException e) { …} contains (i 0 , i 1 ) ∧ } methodCall(i 2 , executeQuery) ∧ contains (i 0 , i 2 ) ∧ public void queryDatabase() { looplike (i 3 , "*.next()") ∧ try { contains (i 0 , i 3 ) ResultSet result = s.executeQuery("select * from customers”); while (result.next()) { … } } catch (SQLException e) { …} } Query Refinement Optimization public List get() { ResultSet set = stmt.executeQuery("select * from t”); List l = new List(); while (set.next()) { … } return l; }

Recommend


More recommend