public static SUnit TranslateMethodCall(Statement statement) { var expressions = statement.GetExpressions(); // Give an empty SUnit if statement has no expressions. if (expressions.Count() == 0) { return new SUnit(SUnitType.SingleMethodCall, "", "", "", new List<string>(), "void"); } // Build a minimal method context and declaration node required by SWUM. var exp = expressions.First(); string type = exp.ResolveType().ToString(); MethodContext mc = new MethodContext(type); MethodDeclarationNode mdn = new MethodDeclarationNode(exp.ToString(), mc); // Apply the SWUM to our statement var swumRule = SetupBaseVerbRule(); swumRule.InClass(mdn); swumRule.ConstructSwum(mdn); // Build and return SUnit from the SWUM SUnit sunit = new SUnit(); sunit.action = GetAction(mdn); sunit.theme = GetTheme(mdn); sunit.args = GetArgs(mdn); return sunit; }
/// <summary> /// Construct the method summarizer /// </summary> /// <param name="md"></param> public SwumSummary(MethodDefinition md) { this.Method = md; var classBelong = md.GetAncestors<TypeDefinition>().FirstOrDefault(); //class name if (classBelong != null) { ClassName = classBelong.Name; } else { ClassName = ""; } //return type string returnType = "void"; if (md.ReturnType != null) { returnType = md.ReturnType.ToString(); } //Check if md returns primitive bool IsPrimitive = IsPrimitiveType(md.ReturnType); HashSet<FormalParameterRecord> paras = new HashSet<FormalParameterRecord>(); foreach (var para in md.Parameters) { var vType = para.VariableType; var tempPara = new FormalParameterRecord(vType.ToString(), BuiltInTypeFactory.IsBuiltIn(vType), para.Name); paras.Add(tempPara); } MethodContext mc = new MethodContext(returnType, IsPrimitive, ClassName, paras, false, md.IsConstructor, md.IsDestructor); this._mDeclarationNode = new MethodDeclarationNode(md.Name, mc); this._builder = new UnigramSwumBuilder(); this.IsSummarized = false; }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { if (node.ParsedName != null && node.ParsedName.Size() > 0) { return PosData.IsPreposition(node.ParsedName[0].Text); } else { return false; } }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { if (node.ParsedName != null && node.ParsedName.Size() > 0) { return IsNonBaseVerb(node.ParsedName[0].Text); } else { return false; } }
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()); }
public void AddManyFiles() { for (int i = 0; i < 100; i++) { foreach (XElement methodElement in functions) { string funcName = SrcMLElement.GetNameForMethod(methodElement).Value; MethodContext mc = ContextBuilder.BuildMethodContext(methodElement); MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, mc); builder.ApplyRules(mdn); } } }
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()); }
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>&</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()); }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { string firstWord = node.ParsedName[0].Text; if (IsChecker(firstWord) || IsSpecialCase(firstWord) || IsEventHandler(node.ParsedName) //|| IsEventHandler(node.FormalParameters) //pretty sure the parameters haven't been set yet || StartsNounPhrase(firstWord) || IsPrepositionalPhrase(node.ParsedName) || IsNonBaseVerb(firstWord)) { return false; } else { return true; } }
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>&</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>&&</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()); }
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)); //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); Console.WriteLine("InClass = " + rule.InClass(mdn)); rule.ConstructSwum(mdn); Console.WriteLine(mdn.ToString()); }
private static SUnit TranslateAssignment(Statement statement) { // action = "Assign" // define left-hand-side (lhs) // theme = right hand side var fieldRule = SetupFieldRule(); // var equalsSign = statement.GetDescendants<OperatorUse>() // .Where(o => o.Text.Equals("=")).First(); // var lhs = equalsSign.GetSiblingsBeforeSelf<VariableUse>().First(); var assignExpression = (VariableDeclaration) statement.GetExpressions().First(); var lhs = assignExpression.Name; var lhsFieldContext = new FieldContext(assignExpression.VariableType.ToString(), false, ""); var lhsDecNode = new FieldDeclarationNode(lhs.ToString(), lhsFieldContext); fieldRule.InClass(lhsDecNode); fieldRule.ConstructSwum(lhsDecNode); var rhsString = ""; var rhsAction = ""; var rhsTheme = ""; Expression rhs = new Expression(); if (assignExpression.Initializer != null) { rhs = assignExpression.Initializer; } if (rhs is VariableUse) { var rhsFieldContext = new FieldContext(rhs.ResolveType().First().ToString(), false, ""); var rhsDecNode = new FieldDeclarationNode(rhs.ToString(), lhsFieldContext); fieldRule.InClass(rhsDecNode); fieldRule.ConstructSwum(rhsDecNode); rhsAction = "Assign"; rhsString = rhsDecNode.ToPlainString(); } else if (rhs is MethodCall) { string type = rhs.ResolveType().ToString(); MethodContext mc = new MethodContext(type); MethodDeclarationNode mdn = new MethodDeclarationNode(rhs.ToString(), mc); var swumRule = SetupBaseVerbRule(); swumRule.InClass(mdn); swumRule.ConstructSwum(mdn); rhsAction = mdn.Action.ToPlainString(); rhsTheme = mdn.Action.ToPlainString(); rhsString = mdn.ToPlainString(); } else { rhsString = rhs.ToString(); } var sunit = new SUnit(); sunit.type = SUnitType.Assignment; sunit.action = rhsString; //sunit.lhs = lhsDecNode.ToPlainString(); sunit.lhs = lhs.ToString(); sunit.theme = rhsString; return sunit; }
//generate theme from MDN static String GetTheme(MethodDeclarationNode mdn) { // If mdn sucks, then theme is empty string. if (mdn.Theme == null){ return ""; } return mdn.Theme.ToPlainString().ToLower(); }
//generate args from MDN static IEnumerable<String> GetArgs(MethodDeclarationNode mdn) { var list = new List<String>(); List<ArgumentNode> gg = mdn.SecondaryArguments; if (gg != null) { foreach (ArgumentNode i in gg) { list.Add(i.ToPlainString().ToLower()); } } return list; }
//generate action from MDN static String GetAction(MethodDeclarationNode mdn) { return mdn.Action.ToPlainString().ToLower(); }
/// <summary> /// Checks for semantic overlaps between parts of the given method's name and its UnknownArguments. /// If overlaps are found, appropriate EquivalenceNodes are created. /// </summary> /// <param name="mdn">The MethodDeclarationNode to check for overlaps.</param> /// <param name="checkDO">Indicates whether the Direct Object was taken from the method name, and therefore the Theme must be checked for overlap with UnknownArguments.</param> /// <param name="checkIO">Indicates whether the Indirect Object was taken from the method name, and therefore the SecondaryArguments must be checked for overlap with UnknownArguments.</param> private void CheckOverlap(MethodDeclarationNode mdn, bool checkDO, bool checkIO) { if (mdn.ParsedName[0].Text.ToLower() == "set") { return; //special case } PhraseNode theme = null; ArgumentNode arg = null; //get DO word from name string wordDO = ""; if (checkDO) //theme is in the method name { theme = (PhraseNode)mdn.Theme; wordDO = theme.LastWord().Text; } //get IO word from name string wordIO = ""; if (checkIO) //IO is in the method name { arg = mdn.SecondaryArguments[0]; PhraseNode argn = (PhraseNode)arg.Argument; wordIO = argn.LastWord().Text; if (wordDO == wordIO) { return; //no equivalence if multiple overlap } } //find overlap List<Node> unknownArgs = mdn.UnknownArguments; List<Node> DOOverlappingArgs = new List<Node>(); List<Node> IOOverlappingArgs = new List<Node>(); for (int i = 0; i < unknownArgs.Count; i++) { if (unknownArgs[i] is VariableDeclarationNode) { VariableDeclarationNode var = (VariableDeclarationNode)unknownArgs[i]; PhraseNode name = var.ParsedName; PhraseNode type = var.Type.ParsedName; bool DOOverlaps = false; bool IOOverlaps = false; if (checkDO) { DOOverlaps = HasOverlap(name, wordDO) || HasOverlap(type, wordDO); if (DOOverlaps) { DOOverlappingArgs.Add(unknownArgs[i]); } } if (checkIO) { IOOverlaps = HasOverlap(name, wordIO) || HasOverlap(type, wordIO); if (IOOverlaps) { IOOverlappingArgs.Add(unknownArgs[i]); } } if (DOOverlaps && IOOverlaps) { return; //no equivalence if multiple overlap } } } //Create overlap in SWUM if (DOOverlappingArgs.Count > 0) { EquivalenceNode en = mdn.CreateEquivalenceFromUnknownArguments(theme, DOOverlappingArgs); mdn.Theme = en; //reset theme in MethodDeclarationNode to new equivalence node } if (IOOverlappingArgs.Count > 0) { EquivalenceNode en = mdn.CreateEquivalenceFromUnknownArguments(arg.Argument, IOOverlappingArgs); arg.Argument = en; //reset mdn.SecondaryArguments to point to new equivalence node } }
public void GenerateEndingSUnit() { /*var dataProject = new DataProject<CompleteWorkingSet>("npp_6.2.3", Path.GetFullPath("..//..//..//projects//npp_6.2.3"), "..//..//..//SrcML");*/ 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, uses global methodName variable var testMethod = globalNamespace.GetDescendants<MethodDefinition>().Where(m => m.Name == methodName).First(); var testMethodXElement = DataHelpers.GetElement(dataProject.SourceArchive, testMethod.PrimaryLocation); //generate swum for method declaration MethodContext mc = ContextBuilder.BuildMethodContext(testMethodXElement); MethodDeclarationNode mdn = new MethodDeclarationNode(methodName, mc); //Console.WriteLine(mdn.ToString()); //returns nothing since it hasn't been written var exp = testMethod.GetDescendants(); //var verb = mdn.Action.ToString(); var expResult = exp.ElementAt(exp.Count() - 1); Console.WriteLine(expResult); MethodDeclarationNode expMDN = null; if (expResult is ReturnStatement) { Console.WriteLine("return"); } else { var mCall = expResult.FindExpressions<MethodCall>().First(); expMDN = new MethodDeclarationNode(mCall.Name, mc); } //MethodDeclarationNode mdn2 = new MethodDeclarationNode(expResult.ToString(), mc); //BaseVerbRule rule = new BaseVerbRule(posData, tagger, splitter); //Console.WriteLine("InClass = " + rule.InClass(mdn)); //REQUIRED in order for the ConstructSwum method to work //rule.ConstructSwum(mdn); //rewrites mdn.ToString to a SWUM breakdown //Console.WriteLine(mdn.Action.ToString()); BaseVerbRule rule2 = new BaseVerbRule(posData, tagger, splitter); Console.WriteLine("InClass = " + rule2.InClass(expMDN)); //REQUIRED in order for the ConstructSwum method to work rule2.ConstructSwum(expMDN); //rewrites mdn.ToString to a SWUM breakdown Console.WriteLine(expMDN.Action.ToString()); //Console.WriteLine(mdn.Action.ToString()); }
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; string methodName = arg; 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(); }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { return IsEventHandler(node.ParsedName); }
/// <summary> /// Sets the Role on the given MethodDeclarationNode based on its name. /// </summary> /// <param name="mdn">The node to set the role on.</param> private void DetermineMethodRole(MethodDeclarationNode mdn) { if (mdn.ReturnType == null) { //Constructors and destructors don't have a return type. //However, they should be processed by a different rule, and therefore shouldn't reach this point. mdn.Role = MethodRole.Unknown; } else if (mdn.ReturnType.Name.ToLower() == "void") { if (mdn.ParsedName[0].Text.ToLower() == "set") { mdn.Role = MethodRole.Setter; } else { mdn.Role = MethodRole.Action; } } else { if (mdn.ParsedName[0].Text.ToLower() == "get") { mdn.Role = MethodRole.Getter; } else { mdn.Role = MethodRole.Function; } } }
/// <summary> /// Sets the UnknownArguments list to its default state. This includes all the formal parameters, the declaring class, and the return type (if not primitive). /// </summary> /// <param name="mdn">The MethodDeclarationNode to set UnknownArguments on.</param> protected void SetDefaultUnknownArguments(MethodDeclarationNode mdn) { mdn.ClearUnknownArguments(); mdn.AddUnknownArguments(mdn.FormalParameters); if (mdn.DeclaringClass != null) { mdn.AddUnknownArgument(mdn.DeclaringClass); } if (mdn.ReturnType != null && !mdn.ReturnType.IsPrimitive) { mdn.AddUnknownArgument(mdn.ReturnType); } }
/// <summary> /// Assigns part-of-speech tags, and sets Action, Theme and Arguments for methods that have a reactive name. /// </summary> /// <param name="mdn">The MethodDeclarationNode to parse.</param> protected void ParseReactiveName(MethodDeclarationNode mdn) { //this assumes that the name has already been split and the preamble stripped mdn.Action = mdn.ParsedName[0].GetNewWord("handle", PartOfSpeechTag.Verb); this.PosTagger.TagNounPhrase(mdn.ParsedName); mdn.CreateThemeFromPhrases(mdn.Preamble, mdn.ParsedName); SetDefaultUnknownArguments(mdn); mdn.IsReactive = true; }
/// <summary> /// Assigns part-of-speech tags to the words in the given MethodDeclarationNode's name, assuming that it follows a base verb pattern. /// This assumes that the node has already had its name split and preamble stripped. /// </summary> /// <param name="node">The MethodDeclarationNode to tag.</param> protected void ParseBaseVerbName(MethodDeclarationNode node) { //this assumes that the node has already had its name split and preamble stripped PhraseNode parsedName = node.ParsedName; //TODO: from Emily, what if it starts with an adverb?? //if 1 word, assume verb if (parsedName.Size() == 1) { if (PosData.IsIgnorableVerb(parsedName[0].Text)) { parsedName[0].Tag = PartOfSpeechTag.VerbIgnorable; } else { parsedName[0].Tag = PartOfSpeechTag.Verb; } return; } int currentWord = 0; currentWord = CheckForIgnorableVerb(parsedName, currentWord); //check for a verb modifier if (currentWord < parsedName.Size()) { if (PosData.IsAdverb(parsedName[currentWord].Text) && parsedName[currentWord].Text.EndsWith("ly") && !PosData.IsDeterminer(parsedName[currentWord].Text) && !PosData.IsPronoun(parsedName[currentWord].Text) && !PosData.IsPreposition(parsedName[currentWord].Text)) { if (currentWord + 1 < parsedName.Size() && PosData.IsPotentialVerb(parsedName[currentWord + 1].Text)) { parsedName[currentWord].Tag = PartOfSpeechTag.VerbModifier; } } } if (PosData.IsIgnorableVerb(parsedName[currentWord].Text)) { parsedName[currentWord].Tag = PartOfSpeechTag.VerbIgnorable; } else { parsedName[currentWord].Tag = PartOfSpeechTag.Verb; } currentWord++; //check for verb particle if (currentWord < parsedName.Size()) { if (PosData.IsVerbParticle(parsedName[currentWord - 1].Text, parsedName[currentWord].Text)) { parsedName[currentWord].Tag = PartOfSpeechTag.VerbParticle; } } //rest of words should be objects or prepositions if (currentWord < parsedName.Size()) { int prep = FindFirstPreposition(parsedName, currentWord); if (prep == -1) { PosTagger.TagNounPhrase(parsedName, currentWord, parsedName.Size() - 1); } else { //found a preposition, could be VX?PY?(f?) bool noX = false; bool noY = false; if (currentWord == prep) { noX = true; } else { PosTagger.TagNounPhrase(parsedName, currentWord, prep - 1); } currentWord = prep + 1; if (currentWord >= parsedName.Size()) { noY = true; } else { PosTagger.TagNounPhrase(parsedName, currentWord, parsedName.Size() - 1); } } } }
private MethodDeclarationNode FromMethodCallToSWUM(MethodCall mcall, NamespaceDefinition globalNamespace, DataProject<CompleteWorkingSet> dataProject) { var mdef = globalNamespace.GetDescendants<MethodDefinition>().Where(decl => mcall.SignatureMatches(decl)); if (mdef.Count() > 0) { var mdefXml = DataHelpers.GetElement(dataProject.SourceArchive, mdef.First().PrimaryLocation); MethodContext mc = ContextBuilder.BuildMethodContext(mdefXml); MethodDeclarationNode mdn = new MethodDeclarationNode(mcall.Name, mc); BaseVerbRule rule = new BaseVerbRule(posData, tagger, splitter); if (rule.InClass(mdn)) { rule.ConstructSwum(mdn); return mdn; } else { return null; } } else { Console.WriteLine(mcall.Name + " could not be found"); return null; } }
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()); } } } }
/// <summary> /// Sets the Action and Theme properties of the given MethodDeclarationNode using the default algorithms. /// This also sets the node's SecondaryArguments and UnknownArguments properties. /// </summary> /// <param name="node">The MethodDeclarationNode to set the Action and Theme on.</param> protected void SetDefaultActionAndTheme(MethodDeclarationNode node) { node.Action = GetVerbPhrase(node.ParsedName, node.Preamble); SetPrepositionThemeAndArguments(node); }
Dictionary<SwumRule, MethodDeclarationNode> BuildValidSwums(String name, MethodContext mc) { Dictionary<SwumRule, bool> inClasses = InClassChecker(name, mc); MethodDeclarationNode mdn = null; Dictionary<SwumRule, MethodDeclarationNode> validSwums = new Dictionary<SwumRule, MethodDeclarationNode>(); foreach (KeyValuePair<SwumRule, bool> entry in inClasses) { if (entry.Value) { mdn = new MethodDeclarationNode(name, mc); entry.Key.InClass(mdn); entry.Key.ConstructSwum(mdn); validSwums.Add(entry.Key, mdn); } } return validSwums; }
/// <summary> /// Sets the Theme, SecondaryArguments and UnknownArguments properties of the given MethodDeclarationNode. /// </summary> /// <param name="node">The node to set the properties for.</param> private void SetPrepositionThemeAndArguments(MethodDeclarationNode node) { List<VariableDeclarationNode> unusedArgs = new List<VariableDeclarationNode>(); //populate UnknownArgs if (node.ParsedName.Size() > 0 && BooleanArgumentVerbs.Contains(node.ParsedName[0].Text)) { node.AddUnknownArguments(node.FormalParameters); } else { //only add non-boolean arguments foreach (VariableDeclarationNode arg in node.FormalParameters) { if (arg.Type.Name.ToLower().Contains("bool")) { unusedArgs.Add(arg); } else { node.AddUnknownArgument(arg); } } } int prepIndex = FindFirstPreposition(node.ParsedName, 0); PhraseNode nameTheme = GetNounPhrase(node.ParsedName); bool checkDO = false; //check Direct Object in name for overlap bool checkIO = false; //check Indirect Object in name for overlap //Assign args if (prepIndex > -1) { //There's a preposition in the name WordNode prep = node.ParsedName[prepIndex]; PhraseNode indirectObject = GetNounPhrase(node.ParsedName, prepIndex + 1); //set IO or P->NM if (!indirectObject.IsEmpty()) //IO in name { node.AddSecondaryArgument(indirectObject, prep); checkIO = true; } else if (node.UnknownArguments != null && node.UnknownArguments.Count() > 0) //or IO = f { node.AddSecondaryArgument(node.UnknownArguments[0], prep); } else { //The preposition doesn't seem to have an object, so change it to a NounModifier prep.Tag = PartOfSpeechTag.NounModifier; nameTheme = GetNounPhrase(node.ParsedName); //reset name theme after changing prep POS tag } //set Theme if (!nameTheme.IsEmpty()) { //theme is in the name nameTheme.SetLocation(Location.Name); node.Theme = nameTheme; checkDO = true; } else //use class as theme { node.Theme = node.DeclaringClass; } } else { //no prep in name, so set Theme only if (!nameTheme.IsEmpty()) { //theme is in the name nameTheme.SetLocation(Location.Name); node.Theme = nameTheme; checkDO = true; } else { //theme is first UnknownArg, or class name //also, potentially leaves class on list of unknown args, which is intentional if (node.DeclaringClass != null) { node.AddUnknownArgument(node.DeclaringClass); } if (node.UnknownArguments != null && node.UnknownArguments.Count > 0) { node.Theme = node.UnknownArguments[0]; node.UnknownArguments.RemoveAt(0); } } } //find equivalences if ((checkDO || checkIO) && node.UnknownArguments != null && node.UnknownArguments.Count > 0) { CheckOverlap(node, checkDO, checkIO); } //do cleanup node.AddUnknownArguments(unusedArgs); if(node.ReturnType != null && node.ReturnType.Name.ToLower() != "void") { //TODO: should this be done for primitive return types? SetDefaultUnknownArguments() excludes them node.AddUnknownArgument(node.ReturnType); } // Note: adding class as unknown arg indep of checkIO // if checkIO = true, and not checkDO, then DO will be class // if check IO = true and checkDO, then this will be executed // if no prep & DO not in name, class will already be on unused args // list for finding theme. if (checkDO && node.DeclaringClass != null) { node.AddUnknownArgument(node.DeclaringClass); } }
/// <summary> /// Checks if each SwumRule (13) returns (InClass = true) for the given method name. returns a dictionary of the SwumRule and boolean. /// Note that to build the swum you still have to run InClass for that method name. /// </summary> /// <param name="name">The name of the method</param> /// <param name="mc">The MethodContext object generated from the top-level method. This is required to generate a swum for any method</param> /// <returns>Dictionary of SwumRules and boolean thats true if InClass = true</returns> Dictionary<SwumRule, bool> InClassChecker(String name, MethodContext mc) { MethodDeclarationNode mdn = new MethodDeclarationNode(name, mc); Dictionary<SwumRule, bool> rules = new Dictionary<SwumRule, bool> { {new BaseVerbRule(posData, tagger, splitter), false}, {new CheckerRule (posData, tagger, splitter), false}, {new ConstructorRule(posData, tagger, splitter), false}, {new DefaultBaseVerbRule(posData, tagger, splitter), false}, {new DestructorRule(posData, tagger, splitter), false}, {new EmptyNameRule(posData, tagger, splitter), false}, {new EventHandlerRule(posData, tagger, splitter), false}, {new FieldRule(posData, tagger, splitter), false}, {new LeadingPrepositionRule(posData, tagger, splitter), false}, {new NonBaseVerbRule(posData, tagger, splitter), false}, {new NounPhraseRule(posData, tagger, splitter), false}, {new ReactiveRule(posData, tagger, splitter), false}, {new SpecialCaseRule(posData, tagger, splitter), false} //{new SwumRule(posData, tagger, splitter),""}, these are abstract //{new UnigramMethodRule(posData, tagger, splitter),""}, //{new UnigramRule(posData, tagger, splitter),""} }; var listing = rules.Keys.ToList(); foreach (SwumRule ent in listing) { // Console.WriteLine(ent.GetType() + " InClass = " + ent.InClass(mdn)); if (ent.InClass(mdn)) { rules[ent] = true; } } return rules; }
/// <summary> /// Determines whether the given MethodDeclarationNode meets the conditions for this rule. /// This method assumes that the name is parsed, preamble is stripped, and digits and prepositions are tagged. /// </summary> /// <param name="node">The MethodDeclarationNode to test.</param> /// <returns>True if the node meets the conditions for this rule, False otherwise.</returns> protected override bool MakeClassification(MethodDeclarationNode node) { return node.ParsedName == null || node.ParsedName.IsEmpty(); }