public void TestAddUnknownArguments() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Node[] args = new Node[] { new TypeNode("int", true), new VariableDeclarationNode("bar") }; mdn.AddUnknownArguments(args); Assert.IsNotNull(mdn.UnknownArguments); Assert.AreEqual(args.Length, mdn.UnknownArguments.Count); for (int i = 0; i < args.Length; i++) { Assert.AreEqual(args[i], mdn.UnknownArguments[i]); } List <Node> args2 = new List <Node>() { new VariableDeclarationNode("xyzzy"), new VariableDeclarationNode("myParam") }; mdn.AddUnknownArguments(args2); Assert.AreEqual(args.Length + args2.Count, mdn.UnknownArguments.Count); for (int i = 0; i < mdn.UnknownArguments.Count; i++) { if (i < args.Length) { Assert.AreEqual(args[i], mdn.UnknownArguments[i]); } else { Assert.AreEqual(args2[i - args.Length], mdn.UnknownArguments[i]); } } }
public void TestCreateEquivalenceFromUnknownArguments_NullNode() { var arg1 = new VariableDeclarationNode("arg1"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1 }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(null, new bool[] { true }); }
public void TestCreateEquivalenceFromUnknownArguments_MissizedArray() { var arg1 = new VariableDeclarationNode("arg1"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1 }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(new PhraseNode(), new bool[] { true, false }); }
public void TestClearUnknownArguments_ExistingArgs() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Node[] args = new Node[] { new TypeNode("int", true), new VariableDeclarationNode("bar") }; mdn.AddUnknownArguments(args); Assert.AreEqual(2, mdn.UnknownArguments.Count); mdn.ClearUnknownArguments(); Assert.AreEqual(0, mdn.UnknownArguments.Count); }
/// <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> /// Constructs the Software Word Use Model for the given node. /// This method assumes that the node has already been tested to satisfy this Rule, using the InClass method. /// </summary> /// <param name="node">The node to construct SWUM for.</param> public override void ConstructSwum(ProgramElementNode node) { if (node is MethodDeclarationNode) { MethodDeclarationNode mdn = (MethodDeclarationNode)node; mdn.Parse(this.Splitter); this.PosTagger.TagNounPhrase(mdn.ParsedName); mdn.AssignStructuralInformation(this.Splitter, this.PosTagger); mdn.Theme = mdn.ParsedName; mdn.AddUnknownArguments(mdn.FormalParameters); //TODO: from Emily, how to fill in Action? mdn.IsDestructor = true; mdn.SwumRuleUsed = this; } else { //TODO: return error? } }
public void TestAddUnknownArguments() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Node[] args = new Node[] { new TypeNode("int", true), new VariableDeclarationNode("bar") }; mdn.AddUnknownArguments(args); Assert.IsNotNull(mdn.UnknownArguments); Assert.AreEqual(args.Length, mdn.UnknownArguments.Count); for(int i = 0; i < args.Length; i++) { Assert.AreEqual(args[i], mdn.UnknownArguments[i]); } List<Node> args2 = new List<Node>() { new VariableDeclarationNode("xyzzy"), new VariableDeclarationNode("myParam") }; mdn.AddUnknownArguments(args2); Assert.AreEqual(args.Length + args2.Count, mdn.UnknownArguments.Count); for(int i = 0; i < mdn.UnknownArguments.Count; i++) { if(i < args.Length) { Assert.AreEqual(args[i], mdn.UnknownArguments[i]); } else { Assert.AreEqual(args2[i - args.Length], mdn.UnknownArguments[i]); } } }
public void TestCreateEquivalenceFromUnknownArguments() { var arg1 = new VariableDeclarationNode("arg1"); var arg2 = new VariableDeclarationNode("arg2"); var arg3 = new VariableDeclarationNode("arg3"); var arg4 = new VariableDeclarationNode("arg4"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1, arg2, arg3, arg4 }); Assert.AreEqual(4, mdn.UnknownArguments.Count); var themeNode = new PhraseNode(new string[] { "Relevent", "Args" }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(themeNode, new bool[] { true, false, true, false }); Assert.AreEqual(3, equiv.EquivalentNodes.Count); Assert.IsTrue(equiv.EquivalentNodes.Contains(themeNode)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg1)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg3)); Assert.AreEqual(2, mdn.UnknownArguments.Count); Assert.IsTrue(mdn.UnknownArguments.Contains(arg2)); Assert.IsTrue(mdn.UnknownArguments.Contains(arg4)); }
/// <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); } }
public void TestCreateEquivalenceFromUnknownArguments_MissizedArray() { var arg1 = new VariableDeclarationNode("arg1"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1 }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(new PhraseNode(), new bool[] { true, false }); }
public void TestCreateEquivalenceFromUnknownArguments_NullNode() { var arg1 = new VariableDeclarationNode("arg1"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1 }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(null, new bool[] { true }); }
public void TestCreateEquivalenceFromUnknownArguments() { var arg1 = new VariableDeclarationNode("arg1"); var arg2 = new VariableDeclarationNode("arg2"); var arg3 = new VariableDeclarationNode("arg3"); var arg4 = new VariableDeclarationNode("arg4"); var mdn = new MethodDeclarationNode("MyMethod"); mdn.AddUnknownArguments(new VariableDeclarationNode[] { arg1, arg2, arg3, arg4 }); Assert.AreEqual(4, mdn.UnknownArguments.Count); var themeNode = new PhraseNode(new string[] { "Relevent", "Args" }); EquivalenceNode equiv = mdn.CreateEquivalenceFromUnknownArguments(themeNode, new bool[] { true, false, true, false }); Assert.AreEqual(3, equiv.EquivalentNodes.Count); Assert.IsTrue(equiv.EquivalentNodes.Contains(themeNode)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg1)); Assert.IsTrue(equiv.EquivalentNodes.Contains(arg3)); Assert.AreEqual(2, mdn.UnknownArguments.Count); Assert.IsTrue(mdn.UnknownArguments.Contains(arg2)); Assert.IsTrue(mdn.UnknownArguments.Contains(arg4)); }
public void TestClearUnknownArguments_ExistingArgs() { MethodDeclarationNode mdn = new MethodDeclarationNode("MyMethod"); Node[] args = new Node[] { new TypeNode("int", true), new VariableDeclarationNode("bar") }; mdn.AddUnknownArguments(args); Assert.AreEqual(2, mdn.UnknownArguments.Count); mdn.ClearUnknownArguments(); Assert.AreEqual(0, mdn.UnknownArguments.Count); }