Пример #1
0
        public void GenerateSimpleSwum()
        {
            var dataProject = new DataProject <CompleteWorkingSet>("npp_6.2.3",
                                                                   Path.GetFullPath("..//..//..//projects//npp_6.2.3"),
                                                                   "..//..//..//SrcML");

            dataProject.UpdateAsync().Wait();

            //get srcml stuff in order
            NamespaceDefinition globalNamespace;

            Assert.That(dataProject.WorkingSet.TryObtainReadLock(5000, out globalNamespace));

            //initialize swum stuff
            splitter = new ConservativeIdSplitter();
            tagger   = new UnigramTagger();
            posData  = new PCKimmoPartOfSpeechData();

            //find an example method
            var guiMethod         = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == "saveGUIParams").First();
            var guiMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, guiMethod.PrimaryLocation);

            //generate swum for method declaration
            MethodContext         mc   = ContextBuilder.BuildMethodContext(guiMethodXElement);
            MethodDeclarationNode mdn  = new MethodDeclarationNode("saveGUIParams", mc);
            BaseVerbRule          rule = new BaseVerbRule(posData, tagger, splitter);

            rule.ConstructSwum(mdn);
            Console.WriteLine(mdn.ToString());
        }
        public void TestInClass()
        {
            string        testSrcML = "<function><type><name>int</name></type> <name><name>CBidMarkup</name><op:operator>::</op:operator><name>modifyBid</name></name><parameter_list>(<param><decl><type><name>bool</name></type> <name>Recalc</name></decl></param>)</parameter_list><block>{<return>return <expr><lit:literal type=\"number\">0</lit:literal></expr>;</return>}</block></function>";
            XElement      xml       = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);
            MethodContext mc        = ContextBuilder.BuildMethodContext(xml.Descendants(SRC.Function).First());

            MethodDeclarationNode mdn  = new MethodDeclarationNode("modifyBid", mc);
            BaseVerbRule          rule = new BaseVerbRule(posData, tagger, splitter);

            Console.WriteLine("InClass(): {0}", rule.InClass(mdn));
            rule.ConstructSwum(mdn);
            Console.WriteLine(mdn.ToString());
        }
Пример #3
0
        public void TestNounPhraseRule_KeyFileExists()
        {
            var xml  = @"<function><type><name>bool</name></type> <name><name>COptionsPageConnectionSFTP</name><op:operator>::</op:operator><name>KeyFileExists</name></name><parameter_list>(<param><decl><type><name>const</name> <name>wxString</name><type:modifier>&amp;</type:modifier></type> <name>keyFile</name></decl></param>)</parameter_list> <block>{
	<return>return <expr><lit:literal type=""boolean"">true</lit:literal></expr>;</return>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);

            Assert.AreEqual(typeof(NounPhraseRule), mdn.SwumRuleUsed.GetType());
            var expected = @"get(Verb) | Key(NounModifier) File(NounModifier) Exists(Noun)
	 ++ [wx(NounModifier) String(NounIgnorable) - key(Unknown) File(Unknown)] ++ C(NounModifier) Options(NounModifier) Page(NounModifier) Connection(NounModifier) SFTP(Noun)"    ;

            Assert.AreEqual(expected, mdn.ToString());
        }
Пример #4
0
        public void TestModalVerb()
        {
            var xml  = @"<function><type><name>int</name></type> <name><name>ToolBarXmlHandlerEx</name><op:operator>::</op:operator><name>CanHandle</name></name><parameter_list>(<param><decl><type><name>wxXmlNode</name> <type:modifier>*</type:modifier></type><name>node</name></decl></param>)</parameter_list> <block>{
	<return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);

            Assert.AreEqual(typeof(CheckerRule), mdn.SwumRuleUsed.GetType());
            var expected = @"Can(VerbIgnorable) Handle(VerbIgnorable) | [wx(NounModifier) Xml(NounModifier) Node(NounIgnorable) - node(Unknown)]
	 ++ Tool(NounModifier) Bar(NounModifier) Xml(NounModifier) Handler(NounModifier) Ex(Noun) ++ int(Noun)"    ;

            Assert.AreEqual(expected, mdn.ToString());
        }
Пример #5
0
        public override void Execute()
        {
            Console.WriteLine("Using srcML file {0}", this.File);

            UnigramSwumBuilder builder = new UnigramSwumBuilder();

            if (CountFile != null)
            {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            if (this.SamplePercent <= 0)
            {
                this.SamplePercent = 100;
            }
            Random rand = new Random();

            SrcMLFile testFile      = new SrcMLFile(this.File);
            int       methodCount   = 0;
            var       functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };

            foreach (XElement file in testFile.FileUnits)
            {
                Console.WriteLine("File {0}:", file.Attribute("filename").Value);

                //print all the function names
                foreach (var func in (from func in file.Descendants()
                                      where functionTypes.Any(c => c == func.Name) && !func.Ancestors(SRC.Declaration).Any() && (rand.Next(100) < this.SamplePercent)
                                      select func))
                {
                    string funcName = SrcMLElement.GetNameForMethod(func).Value;
                    Console.WriteLine("<{0}> {1}", func.Name.LocalName, GetMethodSignature(func));

                    MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                    builder.ApplyRules(mdn);
                    Console.WriteLine(mdn.ToString() + Environment.NewLine);
                    methodCount++;
                }
            }
            Console.WriteLine("{0} functions analyzed", methodCount);
        }
Пример #6
0
        public void TestLeadingPrepositionRule_OnGetAccObject()
        {
            var xml  = @"<function><type><name>LRESULT</name></type> <name><name>CMenuContainer</name><op:operator>::</op:operator><name>OnGetAccObject</name></name><parameter_list>( <param><decl><type><name>UINT</name></type> <name>uMsg</name></decl></param>, <param><decl><type><name>WPARAM</name></type> <name>wParam</name></decl></param>, <param><decl><type><name>LPARAM</name></type> <name>lParam</name></decl></param>, <param><decl><type><name>BOOL</name><type:modifier>&amp;</type:modifier></type> <name>bHandled</name></decl></param> )</parameter_list> <block>{
    <if>if <condition>(<expr><op:operator>(</op:operator><name>DWORD</name><op:operator>)</op:operator><name>lParam</name><op:operator>==</op:operator><op:operator>(</op:operator><name>DWORD</name><op:operator>)</op:operator><name>OBJID_CLIENT</name> <op:operator>&amp;&amp;</op:operator> <name>m_pAccessible</name></expr>)</condition><then> <block>{
        <return>return <expr><call><name>LresultFromObject</name><argument_list>(<argument><expr><name>IID_IAccessible</name></expr></argument>,<argument><expr><name>wParam</name></expr></argument>,<argument><expr><name>m_pAccessible</name></expr></argument>)</argument_list></call></expr>;</return>
    }</block></then> <else>else <block>{
        <expr_stmt><expr><name>bHandled</name><op:operator>=</op:operator><name>FALSE</name></expr>;</expr_stmt>
        <return>return <expr><lit:literal type=""number"">0</lit:literal></expr>;</return>
    }</block></else></if>
}</block></function>";
            var unit = fileUnitSetup.GetFileUnitForXmlSnippet(xml, "test.cpp");

            var func = unit.Descendants(SRC.Function).First();
            var mdn  = new MethodDeclarationNode(SrcMLElement.GetNameForMethod(func).Value, ContextBuilder.BuildMethodContext(func));

            builder.ApplyRules(mdn);

            Assert.AreEqual(typeof(LeadingPrepositionRule), mdn.SwumRuleUsed.GetType());
            var expected = @"handle(Verb) | On(NounModifier) Get(NounModifier) Acc(NounModifier) Object(NounIgnorable)
	 ++ [UINT(Noun) - u(Unknown) Msg(Unknown)] ++ [WPARAM(Noun) - w(Unknown) Param(Unknown)] ++ [LPARAM(Noun) - l(Unknown) Param(Unknown)] ++ [BOOL(Noun) - b(Unknown) Handled(Unknown)] ++ C(NounModifier) Menu(NounModifier) Container(NounIgnorable) ++ LRESULT(Noun)"    ;

            Assert.AreEqual(expected, mdn.ToString());
        }
Пример #7
0
        public void GenerateSameActionSUnit()
        {
            //   var dataProject = new DataProject<CompleteWorkingSet>("npp_6.2.3",
            //     Path.GetFullPath("..//..//..//projects//npp_6.2.3"),
            //   "..//..//..//SrcML");


            //var dataProject = new DataProject<CompleteWorkingSet>("CodeAnalysisToolkit",
            //    Path.GetFullPath("..//..//..//samples"),
            //    "..//..//..//SrcML");

            var dataProject = new DataProject <CompleteWorkingSet>(folderName,
                                                                   Path.GetFullPath(fullFilePath),
                                                                   "..//..//..//SrcML");


            List <String> success = new List <String>();
            Dictionary <SwumRule, bool> inClasses = null;

            bool debug = false; ///////////////// DEBUGGING

            // Get SrcML stuff in order
            dataProject.UpdateAsync().Wait();
            NamespaceDefinition globalNamespace;

            Assert.That(dataProject.WorkingSet.TryObtainReadLock(1000, out globalNamespace));


            // Initialize Swum
            splitter = new ConservativeIdSplitter();
            tagger   = new UnigramTagger();
            posData  = new PCKimmoPartOfSpeechData();



            var methodList             = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName);
            MethodDefinition topMethod = null;

            // Check if the method was found
            try
            {
                topMethod = methodList.First();
            }
            catch (System.InvalidOperationException)
            {
                Console.WriteLine("--ERROR: Method '" + methodName + "' Not Found--");
                Assert.Fail("Method '" + methodName + "' Not Found");
            }

            var guiMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, topMethod.PrimaryLocation);

            //generate swum for method declaration
            MethodContext         mc   = ContextBuilder.BuildMethodContext(guiMethodXElement);
            MethodDeclarationNode mdn  = new MethodDeclarationNode(methodName, mc);
            BaseVerbRule          rule = new BaseVerbRule(posData, tagger, splitter);

            Console.WriteLine("Method = \t" + methodName);
            Console.WriteLine("InClass = \t" + rule.InClass(mdn));


            // Get the action verb from the SWUM
            String methodVerb = GetMethodVerb(methodName, mc);

            Console.WriteLine("Verb = \t\t" + methodVerb);
            Console.WriteLine("============================");

            // Get all of the lines of code that contains the verb in any form
            var expr = topMethod.GetDescendants().Where(t => t.ToString().Contains(methodVerb)).ToArray();

            // Iterate each line
            foreach (Statement t in expr)
            {
                if (debug)
                {
                    Console.WriteLine("Line: " + t.ToString());
                }
                // This finds any method calls in this line and finds the verb in that method - from GetCallsTo()
                var methods = t.FindExpressions <MethodCall>(true).Where(c => c.ToString().ToLower().Contains(methodVerb));//c.Name.Contains(methodVerb));

                // Iterate through a list of the method calls in a line and find ones that contain the verb
                foreach (MethodCall i in methods)
                {
                    if (debug)
                    {
                        Console.WriteLine("===\n" + i.Name);
                    }

                    MethodDeclarationNode mdnNEW = new MethodDeclarationNode(i.Name, mc);

                    inClasses = InClassChecker(i.Name, mc);


                    String foundVerb = GetMethodVerb(i.Name, mc);
                    if (foundVerb.Equals("!NONE!"))
                    {
                        if (debug)
                        {
                            Console.WriteLine("   Method does not contain verb");
                        }
                    }
                    else
                    {
                        if (debug)
                        {
                            Console.WriteLine("GetMethodVerb= " + GetMethodVerb(i.Name, mc));
                        }

                        success.Add(i.Name);
                    }


                    if (debug)
                    {
                        Console.WriteLine("CompareSwums= " + CompareSwums(i.Name, mc));
                    }

                    // Debugging
                    if (debug)
                    {
                        int numbtrue = 0;
                        foreach (KeyValuePair <SwumRule, bool> entry in inClasses)
                        {
                            if (entry.Value)
                            {
                                numbtrue++;
                                mdnNEW = new MethodDeclarationNode(i.Name, mc);
                                entry.Key.InClass(mdnNEW);
                                entry.Key.ConstructSwum(mdnNEW);

                                Console.WriteLine("\t" + entry.Key.GetType().ToString() + new String(' ', 30 - entry.Key.GetType().ToString().Length) + " = " + mdnNEW.ToString());

                                if (mdnNEW.Action.ToPlainString().Equals(methodVerb, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    Console.WriteLine("\tMethod contains the verb");
                                }
                                else
                                {
                                    Console.WriteLine("\tMethod does not contain the verb" + "\n");
                                }
                            }
                        }
                        Console.WriteLine(" Inclasses  = " + numbtrue);
                    } //end if debug
                }     // End Method Iteration
            }         // End Line Iteration

            if (success.Count == 0)
            {
                Console.WriteLine("===== No Same-Action Methods Found =====");
                //Assert.Fail("No Same-Action Methods found");
            }
            else
            {
                Console.WriteLine("\n============= SUCCESSES ===============");
                foreach (String i in success)
                {
                    Console.WriteLine(i);
                }
                //Assert.Pass("Same-Action methods found, check Output");
            }



            dataProject.WorkingSet.ReleaseReadLock();
        } // End Same-Action main method
Пример #8
0
        public void GenerateVoidReturnSUnit()
        {
            var dataProject = new DataProject <CompleteWorkingSet>(folderName,
                                                                   Path.GetFullPath(fullFilePath),
                                                                   "..//..//..//SrcML");

            dataProject.UpdateAsync().Wait();

            //get srcml stuff in order
            NamespaceDefinition globalNamespace;

            Assert.That(dataProject.WorkingSet.TryObtainReadLock(5000, out globalNamespace));

            //initialize swum stuff
            splitter = new ConservativeIdSplitter();
            tagger   = new UnigramTagger();
            posData  = new PCKimmoPartOfSpeechData();

            //find an example method
            var guiMethod         = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName).First();
            var guiMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, guiMethod.PrimaryLocation);

            // forget that, find ALL the methods
            var methods = globalNamespace.GetDescendants <MethodDefinition>().Where(m => m.Name == methodName);

            foreach (MethodDefinition method in methods)
            {
                //Console.WriteLine(method.ToString());
                var statements = method.ChildStatements;
                foreach (Statement statement in statements)
                {
                    var expressions = statement.GetExpressions();
                    foreach (Expression expression in expressions)
                    {
                        // Skip any expression that contains an assignment
                        if (expression.ToString().Contains(" =") || expression.ToString().Contains(" ->"))
                        {
                            continue;
                        }

                        // Print whatever's left. It should be a void return.
                        Console.WriteLine(expression.ToString());

                        // *** PoS tag it ***
                        // convert the string to 'PhraseNode' objects so we can feed them to SWUM
                        var pn = PhraseNode.Parse(new WordNode(expression.ToString()).ToString());

                        Console.WriteLine(pn.ToString());

                        // construct the "rule" to break up method names into sentences
                        BaseVerbRule thisrule = new BaseVerbRule(posData, tagger, splitter);

                        var methodNode = new MethodDeclarationNode(expression.ToString());

                        thisrule.ConstructSwum(methodNode);

                        Console.WriteLine(methodNode.ToString());
                    }
                }
            }
        }
Пример #9
0
        public override void Execute()
        {
            if (Pause)
            {
                Console.WriteLine("Ready to begin (press Enter)");
                Console.ReadLine();
            }

            Console.WriteLine("Using srcML file {0}", this.File);

            var builder = new UnigramSwumBuilder();

            if (!string.IsNullOrWhiteSpace(CountFile))
            {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            int methodCount = 0, fieldCount = 0;

            {
                SrcMLFile testFile      = new SrcMLFile(this.File);
                var       functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };
                foreach (XElement file in testFile.FileUnits)
                {
                    string fileName = file.Attribute("filename").Value;
                    Console.WriteLine("File {0}:", fileName);

                    //compute SWUM on each function
                    foreach (var func in (from func in file.Descendants()
                                          where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any()
                                          select func))
                    {
                        var nameElement = SrcMLElement.GetNameForMethod(func);
                        if (nameElement != null)
                        {
                            string funcName      = nameElement.Value;
                            string funcSignature = SrcMLElement.GetMethodSignature(func);
                            if (PrintSwum)
                            {
                                Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcSignature);
                            }

                            MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                            builder.ApplyRules(mdn);
                            methodSwum[string.Format("{0}:{1}", fileName, funcSignature)] = mdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(mdn.ToString() + Environment.NewLine);
                            }

                            methodCount++;
                        }
                    }

                    //compute SWUM on each field
                    foreach (var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement)
                                               where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name))
                                               select declStmt.Element(SRC.Declaration)))
                    {
                        int declPos = 1;
                        foreach (var nameElement in fieldDecl.Elements(SRC.Name))
                        {
                            string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value;
                            if (PrintSwum)
                            {
                                Console.WriteLine("Field: {0}, Name: {1}", fieldDecl.Value, fieldName);
                            }

                            FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                            builder.ApplyRules(fdn);
                            fieldSwum[string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos)] = fdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(fdn.ToString() + Environment.NewLine);
                            }

                            fieldCount++;
                            declPos++;
                        }
                    }
                }
            }

            GC.Collect();

            Console.WriteLine("{0} functions analyzed", methodCount);
            Console.WriteLine("{0} functions in dictionary", methodSwum.Count);
            Console.WriteLine("{0} fields analyzed", fieldCount);
            Console.WriteLine("{0} fields in dictionary", fieldSwum.Count);

            if (Pause)
            {
                Console.WriteLine("Finished building SWUM (press Enter)");
                Console.ReadLine();
            }
        }