/// <returns>A SwumDataRecord containing <paramref name="swumNode"/> and various data extracted from it.</returns>
        protected SwumDataRecord ProcessSwumNode(FieldDeclarationNode swumNode)
        {
            var record = new SwumDataRecord();

            record.SwumNode = swumNode;
            return(record);
        }
        /// <summary>
        /// Initializes the cache of SWUM data from a file. Any existing SWUM data will be cleared before reading the file.
        /// </summary>
        /// <param name="path">The path to the SWUM cache file.</param>
        public void ReadSwumCache(string path)
        {
            using (var cacheFile = new StreamReader(path)) {
                lock (signaturesToSwum) {
                    //clear any existing SWUMs
                    signaturesToSwum.Clear();

                    //read each SWUM entry from the cache file
                    string entry;
                    while ((entry = cacheFile.ReadLine()) != null)
                    {
                        //the expected format is <signature>|<SwumDataRecord.ToString()>
                        string[] fields = entry.Split(new[] { '|' }, 2);
                        if (fields.Length != 2)
                        {
                            Debug.WriteLine(string.Format("Too few fields in SWUM cache entry: {0}", entry));
                            continue;
                        }
                        try {
                            string sig  = fields[0].Trim();
                            string data = fields[1].Trim();
                            signaturesToSwum[sig] = SwumDataRecord.Parse(data);
                        } catch (FormatException fe) {
                            Debug.WriteLine(string.Format("Improperly formatted SwumDataRecord in Swum cache entry: {0}", entry));
                            Debug.WriteLine(fe.Message);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Performs additional processing on a MethodDeclarationNode to put the data in the right format for the Comment Generator.
        /// </summary>
        /// <param name="swumNode">The MethodDeclarationNode from SWUM to process.</param>
        /// <returns>A SwumDataRecord containing <paramref name="swumNode"/> and various data extracted from it.</returns>
        protected SwumDataRecord ProcessSwumNode(MethodDeclarationNode swumNode)
        {
            var record = new SwumDataRecord();

            record.SwumNode = swumNode;
            //set Action
            if (swumNode.Action != null)
            {
                record.Action       = swumNode.Action.ToPlainString();
                record.ParsedAction = swumNode.Action.GetParse();
            }
            //TODO: action is not lowercased. Should it be?

            //set Theme
            if (swumNode.Theme != null)
            {
                if (swumNode.Theme is EquivalenceNode && ((EquivalenceNode)swumNode.Theme).EquivalentNodes.Any())
                {
                    var firstNode = ((EquivalenceNode)swumNode.Theme).EquivalentNodes[0];
                    record.Theme       = firstNode.ToPlainString().ToLower();
                    record.ParsedTheme = firstNode.GetParse();
                }
                else
                {
                    record.Theme       = swumNode.Theme.ToPlainString().ToLower();
                    record.ParsedTheme = swumNode.Theme.GetParse();
                }
            }

            //set Indirect Object
            if (string.Compare(record.Action, "set", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                //special handling for setter methods?
                //TODO: should this set the IO to the declaring class? will that work correctly for sando?
            }
            else
            {
                if (swumNode.SecondaryArguments != null && swumNode.SecondaryArguments.Any())
                {
                    var IONode = swumNode.SecondaryArguments.First();
                    if (IONode.Argument is EquivalenceNode && ((EquivalenceNode)IONode.Argument).EquivalentNodes.Any())
                    {
                        var firstNode = ((EquivalenceNode)IONode.Argument).EquivalentNodes[0];
                        record.IndirectObject       = firstNode.ToPlainString().ToLower();
                        record.ParsedIndirectObject = firstNode.GetParse();
                    }
                    else
                    {
                        record.IndirectObject       = IONode.Argument.ToPlainString().ToLower();
                        record.ParsedIndirectObject = IONode.Argument.GetParse();
                    }
                }
            }

            return(record);
        }
        /// <summary>
        /// Returns the SWUM data for the given method signature.
        /// </summary>
        /// <param name="methodSignature">The method signature to get SWUM data about.</param>
        /// <returns>A SwumDataRecord containing the SWUM data about the given method, or null if no data is found.</returns>
        public SwumDataRecord GetSwumForSignature(string methodSignature)
        {
            if (methodSignature == null)
            {
                throw new ArgumentNullException("methodSignature");
            }

            SwumDataRecord result = null;

            lock (signaturesToSwum) {
                if (signaturesToSwum.ContainsKey(methodSignature))
                {
                    result = signaturesToSwum[methodSignature];
                }
            }
            return(result);
        }
        public void TestRoundTrip()
        {
            var a1 = new WordNode("DB", PartOfSpeechTag.Preamble);
            var a2 = new WordNode("Get", PartOfSpeechTag.Verb);
            var t1 = new WordNode("Hydro", PartOfSpeechTag.NounModifier);
            var t2 = new WordNode("Fixed", PartOfSpeechTag.NounModifier);
            var t3 = new WordNode("Schedule", PartOfSpeechTag.Noun);

            var sdr = new SwumDataRecord();
            sdr.ParsedAction = new PhraseNode(new[] {a1, a2}, Location.None, false);
            sdr.Action = sdr.ParsedAction.ToPlainString();
            sdr.ParsedTheme = new PhraseNode(new[] {t1, t2, t3}, Location.None, false);
            sdr.Theme = sdr.ParsedTheme.ToPlainString();

            var actual = SwumDataRecord.Parse(sdr.ToString());
            Assert.IsTrue(SwumDataRecordsAreEqual(sdr, actual));
        }
Exemple #6
0
        /// <summary>
        /// Creates a SwumDataRecord from a string representation
        /// </summary>
        /// <param name="source">The string to parse the SwumDataRecord from.</param>
        /// <returns>A new SwumDataRecord.</returns>
        public static SwumDataRecord Parse(string source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            string[] fields = source.Split('|');
            if (fields.Length != 5)
            {
                throw new FormatException(string.Format("Wrong number of |-separated fields. Expected 4, saw {0}", fields.Length));
            }
            var sdr = new SwumDataRecord();

            if (!string.IsNullOrWhiteSpace(fields[0]))
            {
                sdr.ParsedAction = PhraseNode.Parse(fields[0].Trim());
                sdr.Action       = sdr.ParsedAction.ToPlainString();
            }
            if (!string.IsNullOrWhiteSpace(fields[1]))
            {
                sdr.ParsedTheme = PhraseNode.Parse(fields[1].Trim());
                sdr.Theme       = sdr.ParsedTheme.ToPlainString();
            }
            if (!string.IsNullOrWhiteSpace(fields[2]))
            {
                sdr.ParsedIndirectObject = PhraseNode.Parse(fields[2].Trim());
                sdr.IndirectObject       = sdr.ParsedIndirectObject.ToPlainString();
            }
            if (!string.IsNullOrWhiteSpace(fields[3]))
            {
                foreach (var file in fields[3].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    sdr.FileNames.Add(file);
                }
            }
            if (!string.IsNullOrWhiteSpace(fields[4]))
            {
                sdr.SwumNode = new MethodDeclarationNode(fields[4]);
            }
            return(sdr);
        }
        public bool SwumDataRecordsAreEqual(SwumDataRecord sdr1, SwumDataRecord sdr2)
        {
            if(sdr1 == sdr2) {
                return true;
            }
            if(sdr1 == null ^ sdr2 == null) {
                return false;
            }

            bool areEqual = PhraseNodesAreEqual(sdr1.ParsedAction, sdr2.ParsedAction)
                            && PhraseNodesAreEqual(sdr1.ParsedTheme, sdr2.ParsedTheme)
                            && PhraseNodesAreEqual(sdr1.ParsedIndirectObject, sdr2.ParsedIndirectObject)
                            && sdr1.Action == sdr2.Action
                            && sdr1.Theme == sdr2.Theme
                            && sdr1.IndirectObject == sdr2.IndirectObject
                            && sdr1.FileNameHashes.Count == sdr2.FileNameHashes.Count;
            if(areEqual) {
                //both FileNames are the same size
                foreach(var file in sdr1.FileNameHashes) {
                    areEqual = areEqual && sdr2.FileNameHashes.Contains(file);
                }
            }
            return areEqual;
        }
        public void AddRecord(int signature, SwumDataRecord record)
        {
            record.Signature = signature;

            lock (hashOfSignaturesToSwumRecord)
            {
                hashOfSignaturesToSwumRecord[signature] = record;
            }

            lock (trie)
            {
                foreach (var actionWord in record.Action.ToLowerInvariant().Split(' '))
                {
                    if (!String.IsNullOrEmpty(actionWord))
                    {
                        trie.Add(actionWord.Trim(), record);
                    }
                }

                foreach (var indirectObjectWord in record.IndirectObject.ToLowerInvariant().Split(' ') )
                {
                    if (!String.IsNullOrEmpty(indirectObjectWord))
                    {
                        trie.Add(indirectObjectWord.Trim(), record);
                    }
                }

                foreach (var themeWord in record.Theme.Split(' '))
                {
                    if (!String.IsNullOrEmpty(themeWord))
                    {
                        trie.Add(themeWord.Trim().ToLowerInvariant(), record);
                    }
                }
            }
        }
Exemple #9
0
 private void AddFullMethodName(string query, Dictionary <string, int> recommendations, int NormalWeight, string[] terms, SwumDataRecord swumRecord)
 {
     try
     {
         if (swumRecord.SwumNode.Name.ToLower().Contains(query.ToLower()))
         {
             AddRecommendation(swumRecord.SwumNode.Name, NormalWeight + (int)(NormalWeight * 10 / Distance(swumRecord.SwumNode.Name, query)), recommendations);
             //Debug.WriteLine(swumRecord.SwumNode.Name + " " + (NormalWeight + (int)(NormalWeight * 10 / Distance(swumRecord.SwumNode.Name, query))));
         }
         else
         {
             bool shouldAdd = true;
             foreach (var term in terms)
             {
                 if (!swumRecord.SwumNode.Name.ToLower().Contains(term))
                 {
                     shouldAdd = false;
                 }
             }
             if (shouldAdd)
             {
                 AddRecommendation(swumRecord.SwumNode.Name, NormalWeight + (int)(NormalWeight * 10 / Distance(swumRecord.SwumNode.Name, query)), recommendations);
                 //Debug.WriteLine(swumRecord.SwumNode.Name+" "+(NormalWeight + (int)(NormalWeight * 10 / Distance(swumRecord.SwumNode.Name, query))));
             }
         }
     }
     catch (NullReferenceException nre)
     {
         Debug.WriteLine("he");
         //ignore
     }
 }
        /// <summary>
        /// Creates a SwumDataRecord from a string representation
        /// </summary>
        /// <param name="source">The string to parse the SwumDataRecord from.</param>
        /// <returns>A new SwumDataRecord.</returns>
        public static SwumDataRecord Parse(string source)
        {
            if(source == null) {
                throw new ArgumentNullException("source");
            }

            string[] fields = source.Split('|');
            if(fields.Length != 5) {
                throw new FormatException(string.Format("Wrong number of |-separated fields. Expected 4, saw {0}", fields.Length));
            }
            var sdr = new SwumDataRecord();
            if(!string.IsNullOrWhiteSpace(fields[0])) {
                sdr.ParsedAction = PhraseNode.Parse(fields[0].Trim());
                sdr.Action = sdr.ParsedAction.ToPlainString();
            }
            if(!string.IsNullOrWhiteSpace(fields[1])) {
                sdr.ParsedTheme = PhraseNode.Parse(fields[1].Trim());
                sdr.Theme = sdr.ParsedTheme.ToPlainString();
            }
            if(!string.IsNullOrWhiteSpace(fields[2])) {
                sdr.ParsedIndirectObject = PhraseNode.Parse(fields[2].Trim());
                sdr.IndirectObject = sdr.ParsedIndirectObject.ToPlainString();
            }
            if(!string.IsNullOrWhiteSpace(fields[3])) {
                foreach(var file in fields[3].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) {
                        sdr.FileNameHashes.Add(int.Parse(file));
                }
            }
            if (!string.IsNullOrWhiteSpace(fields[4]))
            {
                sdr.SwumNodeName = fields[4].Trim();
            }
            return sdr;
        }
        public void TestRoundTrip_FileNames()
        {
            var a1 = new WordNode("DB", PartOfSpeechTag.Preamble);
            var a2 = new WordNode("Get", PartOfSpeechTag.Verb);
            var t1 = new WordNode("Hydro", PartOfSpeechTag.NounModifier);
            var t2 = new WordNode("Fixed", PartOfSpeechTag.NounModifier);
            var t3 = new WordNode("Schedule", PartOfSpeechTag.Noun);
            var f1 = @"C:\foo\bar.cpp";
            var f2 = @"C:\foo\baz\xyzzy.h";
            var f3 = "test.cpp";

            var sdr = new SwumDataRecord();
            sdr.ParsedAction = new PhraseNode(new[] { a1, a2 }, Location.None, false);
            sdr.Action = sdr.ParsedAction.ToPlainString();
            sdr.ParsedTheme = new PhraseNode(new[] { t1, t2, t3 }, Location.None, false);
            sdr.Theme = sdr.ParsedTheme.ToPlainString();
            sdr.FileNameHashes.Add(f1.GetHashCode());
            sdr.FileNameHashes.Add(f2.GetHashCode());
            sdr.FileNameHashes.Add(f3.GetHashCode());

            var actual = SwumDataRecord.Parse(sdr.ToString());
            Assert.IsTrue(SwumDataRecordsAreEqual(sdr, actual));
        }