public static IEnumerable <Tuple <string, string> > Swummarize(string directoryPath) { var swummaries = new List <Tuple <string, string> >(); var srcmlMethods = MethodExtractor.ExtractAllMethodsFromDirectory(directoryPath); foreach (var methodDef in srcmlMethods) { // Extract SUnit Statements from MethodDefinition var statements = SUnitExtractor.ExtractAll(methodDef).ToList(); // Translate Statements into SUnits List <SUnit> sunits = statements.ConvertAll( new Converter <Statement, SUnit>(SUnitTranslator.Translate)); // Generate text from SUnits List <string> sentences = sunits.ConvertAll( new Converter <SUnit, string>(TextGenerator.GenerateText)); // Collect text and summarize var methodDocument = String.Join <string>("\n", sentences); // TEMP: Just use full set of sentences for now. Don't use Summarizer. // Maybe set a flag for this. //var summary = Summarizer.Summarize(methodDocument); var summary = methodDocument; // Add swummary to collection with its full method name var methodName = methodDef.GetFullName(); swummaries.Add(new Tuple <string, string>(methodName, summary)); } return(swummaries.AsEnumerable()); }
public void GetEndingSUnits() { // Test if same action s-unit is returned by the SUnitExtractor var sameAction = methodDef.GetDescendants <Statement>().Last <Statement>(); var sameActionsFound = (System.Collections.IList)SUnitExtractor.ExtractEnding(methodDef); Assert.Contains(sameAction, sameActionsFound); }
/// <summary> /// Extracts all selected s_units from a method: ending, void-return, and same-action. /// TODO: add data-facilitating s_unit selection. /// </summary> /// <param name="methodDef">The SrcML MethodDefinition from which to extract same action s_units.</param> /// <returns>An IEnumerable collection containing the same action s_units found in methodDef</returns> public static IEnumerable <Statement> ExtractAll(MethodDefinition methodDef) { var summarySet = new List <Statement>(); summarySet.AddRange(SUnitExtractor.ExtractSameAction(methodDef)); summarySet.AddRange(SUnitExtractor.ExtractVoidReturn(methodDef)); summarySet.AddRange(SUnitExtractor.ExtractEnding(methodDef)); return(summarySet.Distinct <Statement>().OrderBy(s => s.PrimaryLocation.StartingLineNumber)); }
public void GetSameActionSUnits() { // Test if same action s-unit is returned by the SUnitExtractor var sameAction = methodDef.GetDescendants <Statement>() .First(s => Regex.IsMatch(s.ToString(), "setFindInFilesDirFilter")); var sameActionsFound = (System.Collections.IList)SUnitExtractor.ExtractSameAction(methodDef); Assert.Contains(sameAction, sameActionsFound); }
public void TestPipelineWithSourceFile() { // TODO: rewrite unit test to handle all the methods in a file // For now, just pull the first method from the file and proceed as TestPipelineXMLSnipper(). var srcmlMethod = MethodExtractor.ExtractAllMethodsFromFile("../Sample Methods/sample methods.cpp").First().Item3; // Verify the method definition Assert.IsInstanceOf <MethodDefinition>(srcmlMethod, "MethodDefinition found."); Console.WriteLine(srcmlMethod.ToString()); // Extract SUnit Statements from MethodDefinition var statements = SUnitExtractor.ExtractAll(srcmlMethod).ToList(); // verify the statements selected Assert.IsNotEmpty(statements, "statements selected from method definition"); Console.WriteLine(statements.ToString()); // Translate Statements into SUnits List <SUnit> sunits = statements.ConvertAll( new Converter <Statement, SUnit>(SUnitTranslator.Translate)); // verify sunits have been translated Assert.That(sunits.TrueForAll(s => s.action != null), "All SUnits initialized."); Console.WriteLine(sunits.ToString()); // Generate text from SUnits List <string> sentences = sunits.ConvertAll( new Converter <SUnit, string>(TextGenerator.GenerateText)); // verify string generated Assert.That(sentences.TrueForAll(s => s.Length > 0)); Console.WriteLine(sentences); // Collect text and summarize var methodDocument = String.Join <string>(" ", sentences); var summary = Summarizer.Summarize(methodDocument); // verify summary Assert.That(!summary.Equals("")); Console.WriteLine(summary); }
public void TestPipelineXMLSnippet() { // SrcML sample method string srcmlOutput = @"<function><type><name> bool </name></type> <name> findInFiles </name><parameter_list> () </parameter_list> <block>{ <decl_stmt><decl><type><specifier>const</specifier> <name>TCHAR</name> <modifier>*</modifier></type><name>dir2Search</name> <init>= <expr><call><name><name>_findReplaceDlg</name><operator>.</operator><name>getDir2Search</name></name><argument_list>()</argument_list></call></expr></init></decl>;</decl_stmt> <expr_stmt><expr><call><name>findFilesInOut</name><argument_list>()</argument_list></call></expr>;</expr_stmt> <if>if <condition>(<expr><operator>!</operator><name><name>dir2Search</name><index>[<expr><literal type=""number"">0</literal></expr>]</index></name> <operator>||</operator> <operator>!</operator><call><name><operator>::</operator><name>PathFileExists</name></name><argument_list>(<argument><expr><name>dir2Search</name></expr></argument>)</argument_list></call></expr>)</condition><then> <block>{ <return>return <expr><literal type = ""boolean"" > false </literal></expr>;</return> }</block></then></if> <decl_stmt><decl><type><name>string</name></type> <name>findString</name> <init>= <expr><literal type = ""string"" > """" </literal ></expr ></init ></decl >;</decl_stmt> <expr_stmt><expr><call><name>gethurry</name><argument_list>()</argument_list></call></expr>;</expr_stmt> <macro><name>findInOne</name><argument_list>(<argument>int a</argument>, <argument>findString</argument>)</argument_list></macro><empty_stmt>;</empty_stmt> <decl_stmt><decl><type><name>bool</name></type> <name>isRecursive</name> <init>= <expr><call><name><name>_findReplaceDlg</name><operator >.</operator><name>isRecursive</name></name><argument_list>()</argument_list></call></expr></init></decl>;</decl_stmt> <decl_stmt><decl><type><name>bool</name></type> <name>isInHiddenDir</name> <init>= <expr><call><name><name>_findReplaceDlg</name><operator >.</operator><name>isInHiddenDir</name></name><argument_list>()</argument_list></call></expr></init></decl>;</decl_stmt> <if>if <condition>(<expr><call><name><name>a</name><operator >.</operator><name>size</name></name><argument_list>()</argument_list></call> <operator >==</operator> <literal type = ""number"" > 0 </literal></expr>)</condition><then> <block>{ <expr_stmt><expr><call><name><name>a</name><operator >.</operator><name>setFindInFilesDirFilter</name></name><argument_list>(<argument><expr><literal type = ""string""> ""dddd"" </literal ></expr ></argument>, <argument><expr><call><name>TEXT</name><argument_list>(<argument><expr><literal type = ""string"" > ""*.*"" </literal ></expr></argument>)</argument_list></call></expr></argument>)</argument_list></call></expr>;</expr_stmt> <expr_stmt><expr><call><name><name>a</name><operator >.</operator><name>getPatterns</name></name><argument_list>(<argument><expr><name>findString</name></expr></argument>)</argument_list></call></expr>;</expr_stmt> }</block></then></if> <return>return <expr><literal type = ""boolean"" > true </literal ></expr>;</return> }</block></function>"; // Convert raw string to MethodDefinition var fileSetup = new SrcMLFileUnitSetup(Language.CPlusPlus); var parser = new CPlusPlusCodeParser(); var fileUnit = fileSetup.GetFileUnitForXmlSnippet(srcmlOutput, "sampletestmethods.cpp"); var scope = parser.ParseFileUnit(fileUnit); var srcmlMethod = scope.GetDescendants <MethodDefinition>().First(); // Verify the method definition Assert.IsInstanceOf <MethodDefinition>(srcmlMethod, "MethodDefinition found."); Console.WriteLine(srcmlMethod.ToString()); // Extract SUnit Statements from MethodDefinition var statements = new List <Statement>(); statements.AddRange(SUnitExtractor.ExtractEnding(srcmlMethod)); statements.AddRange(SUnitExtractor.ExtractSameAction(srcmlMethod)); statements.AddRange(SUnitExtractor.ExtractVoidReturn(srcmlMethod)); // verify the statements selected Assert.IsNotEmpty(statements, "statements selected from method definition"); Console.WriteLine(statements.ToString()); // Translate Statements into SUnits List <SUnit> sunits = statements.ConvertAll( new Converter <Statement, SUnit> (SUnitTranslator.Translate)); // verify sunits have been translated Assert.That(sunits.TrueForAll(s => s.action != null), "All SUnits initialized."); Console.WriteLine(sunits.ToString()); // Generate text from SUnits List <string> sentences = sunits.ConvertAll( new Converter <SUnit, string> (TextGenerator.GenerateText)); // verify string generated Assert.That(sentences.TrueForAll(s => s.Length > 0)); Console.WriteLine(sentences); // Collect text and summarize var methodDocument = String.Join <string>(" ", sentences); var summary = Summarizer.Summarize(methodDocument); // verify summary Assert.That(!summary.Equals("")); Console.WriteLine(summary); }
public void TestPipelineWithSourceDirectory(string directoryName) { var currentDirectory = Path.GetFullPath(Assembly.GetExecutingAssembly().Location); var sourceDirectory = Path.GetFullPath(Path.Combine(currentDirectory, @"..\..\..\..\testData\", directoryName)); var srcmlMethods = MethodExtractor.ExtractAllMethodsFromDirectory(sourceDirectory); foreach (var methodDef in srcmlMethods) { // Print Class and Method Name Console.WriteLine("*************************\n\n{0}\n*************************", methodDef.GetFullName()); // Extract SUnit Statements from MethodDefinition var statements = SUnitExtractor.ExtractAll(methodDef).ToList(); // verify the statements selected //Assert.IsNotEmpty(statements, "statements selected from method definition"); /* * foreach (var s in statements) * { * Console.WriteLine(statements.ToString()); * } */ // Translate Statements into SUnits List <SUnit> sunits = statements.ConvertAll( new Converter <Statement, SUnit>(SUnitTranslator.Translate)); // verify sunits have been translated Assert.That(sunits.TrueForAll(s => s.action != null), "All SUnits initialized."); /* * foreach (var s in sunits) * { * Console.WriteLine(s); * } */ // Generate text from SUnits List <string> sentences = sunits.ConvertAll( new Converter <SUnit, string>(TextGenerator.GenerateText)); // verify string generated /* * Assert.That(sentences.TrueForAll(s => s.Length > 0)); * foreach (var s in sentences) * { * Console.WriteLine(s); * } */ // Collect text and summarize var methodDocument = String.Join <string>("\n", sentences); // Actually, lets skip the summary for now Console.WriteLine(methodDocument); /* * var summary = Summarizer.Summarize(methodDocument); * * // verify summary * Assert.That(!summary.Equals("")); * Console.WriteLine(summary); */ } }