public static IEnumerable<Tuple<string, string, MethodDefinition>> ExtractAllMethodsFromFile(string filePath)
        {
            // open the file and convert to srcml
            //var srcml = new SrcMLFile( filePath );
            //var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //var srcmlPath = Path.Combine(baseDir,"..", "..","..","..","External");
            var srcmlPath = @"C:\Users\kh\Documents\GitHubVisualStudio\Swummary\External";
            Console.WriteLine(srcmlPath);

            var srcml = new SrcML(srcmlPath);
            //var srcml = new SrcMLGenerator();
            srcml.GenerateSrcMLFromFile(filePath, Path.Combine(filePath, ".xml"));
            //var srcmlFile =
            var srcmlFile = srcml.GenerateSrcMLFromFile(filePath, Path.Combine(filePath, ".xml"));

            //var ns = new NamespaceDefinition();

            // get a grip on the parts we care about
            var fileUnits = srcmlFile.FileUnits;
            var parser = new CPlusPlusCodeParser();    //TODO: allow language to be chosen or detected

            // iterate through the mess to build a list of what we want
            var methodList = new List<Tuple<string, string, MethodDefinition>>();

            foreach (var file in fileUnits)
            {
                var fileScope = parser.ParseFileUnit(file);
                var methods = fileScope.GetDescendants<MethodDefinition>();

                string filename = file.Name.ToString();
                foreach (var method in methods)
                {
                    string methodName = method.Name.ToString();
                    var definition = method.GetDescendants<MethodDefinition>().Single();

                    var element = new Tuple<string, string, MethodDefinition>(filename, methodName, definition);
                    methodList.Add( element);
                }
            }

            // done!
            return methodList.AsEnumerable();
        }
    public void PrepareMethodDefinition()
    {
        // SrcML objects which help generate the SrcML objects we need to test
        var fileSetup = new SrcMLFileUnitSetup(Language.CPlusPlus);
        var parser = new CPlusPlusCodeParser();

        var fileUnit = fileSetup.GetFileUnitForXmlSnippet(srcmlOutput, "sampletestmethods.cpp");
        var scope = parser.ParseFileUnit(fileUnit);

        // Create the method srcml object in which to search for s-units.
        methodDef = scope.GetDescendants<MethodDefinition>().First();
    }
Exemple #3
0
        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);
        }