示例#1
0
        protected void AddSwumForFieldDefinitions(XElement file, string fileName)
        {
            //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;

                    FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                    builder.ApplyRules(fdn);
                    //var signature = string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos);
                    var signature = nameElement.GetXPath(false);
                    var swumData  = ProcessSwumNode(fdn);
                    swumData.FileNames.Add(fileName);
                    lock (signaturesToSwum)
                    {
                        signaturesToSwum[signature] = swumData;
                    }
                    declPos++;
                }
            }
        }
示例#2
0
        public void TestBuildFieldContext_Global()
        {
            string   testSrcML = "<decl_stmt><decl><type><name>int</name></type> <name>a</name> =<init> <expr><lit:literal type=\"number\">42</lit:literal></expr></init></decl>;</decl_stmt>";
            XElement xml       = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);

            FieldContext fc1 = new FieldContext("int", true, "");
            FieldContext fc2 = ContextBuilder.BuildFieldContext(xml.Descendants(SRC.Declaration).First());

            Assert.IsTrue(FieldContextsAreEqual(fc1, fc2));
        }
示例#3
0
        public void TestBuildFieldContext_Struct()
        {
            string   testSrcML = "<struct>struct <name>foo</name> <block>{<public type=\"default\"><decl_stmt><decl><type><name>int</name></type> <name>a</name></decl>;</decl_stmt></public>}</block>;</struct>";
            XElement xml       = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);

            FieldContext fc1 = new FieldContext("int", true, "foo");
            FieldContext fc2 = ContextBuilder.BuildFieldContext(xml.Descendants(SRC.Declaration).First());

            Assert.IsTrue(FieldContextsAreEqual(fc1, fc2));
        }
示例#4
0
        public void TestConstructSwum()
        {
            string testSrcML = "<class>class <name>foo</name> <block>{<private type=\"default\"><decl_stmt><decl><type><name>int</name></type> <name>a</name></decl>;</decl_stmt></private>}</block>;</class>";
            XElement xml = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);
            FieldContext fc = ContextBuilder.BuildFieldContext(xml.Descendants(SRC.Declaration).First());

            FieldDeclarationNode fdn = new FieldDeclarationNode("a", fc);
            FieldRule rule = new FieldRule(posData, tagger, splitter);
            rule.ConstructSwum(fdn);
            Console.WriteLine(fdn.ToString());
        }
示例#5
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();
            }
        }