 |
Exercise 8 Test Cases
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using Plato.Core; using Plato.Core.Search; using Plato.Language; using Plato.Language.Parser; using Plato.Core.Parser;
namespace Tests { [TestFixture] public class Exercise8 { Brain brain; Parser parser; TransformationParser transParser; WordMappingParser wordMappingParser;
[SetUp] public void SetUp() { brain = new Brain(); parser = new Parser(brain); transParser = new TransformationParser(brain); wordMappingParser = new WordMappingParser(brain);
brain.CreateEntity("noun");
parser.Parse( @"speaker is_a person self is_a person person has_a first_name person has_a age 'Daniel' is_a first_name 27 is_a number number is_a age 'Plato' self.first_name = 'Plato' self.age = 51 'first' person has_a eye eye has_a color blue is_a color DanielBigham is_a person DanielBigham is_a man man has_a wife wife is_a person 'Meredith' is_a first_name");
wordMappingParser.Parse( @"'name' -> first_name: noun 'nom' -> first_name: noun 'age' -> age: noun '27' -> 27: noun 'I' -> speaker: noun 'you' -> self: noun 'is' -> is_am_are: verb 'am' -> is_am_are: verb 'are' -> is_am_are: verb 'eye' -> eye: noun 'color' -> color: noun 'colour' -> color: noun 'blue' -> blue: noun 'Daniel' -> DanielBigham: noun 'person' -> person: noun 'wife' -> wife: noun");
transParser.Parse( @"my {noun} -> speaker.$1 mon {noun} -> speaker.$1 {noun} is {noun} -> $1 = $2 {noun} est {noun} -> $1 = $2 what is {noun} ? -> $1 {person} {is_am_are} {number} -> $1.age = $3 your {noun} -> self.$1 how old {is_am_are} {noun} ? -> $2.age first name -> first_name {noun} {noun} -> $1.$2 {noun} 's {noun} -> $1.$2");
}
/// <summary> /// Basic statement. /// </summary> [Test] public void BasicTest() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "My name is Daniel");
parser.Parse(search.Output[0].ToString());
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.first_name = 'Daniel'", search.Output[0].ToString()); Assert.AreEqual("'Daniel'", brain.GetValue("speaker.first_name").ToString());
search = new TransformationSearchHelper(brain, "What is my name?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.first_name", search.Output[0].ToString()); Assert.AreEqual("'Daniel'", brain.GetValue(search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 2: My age is 27, What is my age? /// </summary> [Test] public void Story2() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "My age is 27");
parser.Parse(search.Output[0].ToString());
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age = 27", search.Output[0].ToString()); Assert.AreEqual("27", brain.GetValue("speaker.age").ToString());
search = new TransformationSearchHelper(brain, "What is my age?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age", search.Output[0].ToString()); Assert.AreEqual("27", brain.GetValue(search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 3: I am 27, What is my age? /// </summary> [Test] public void Story3() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "I am 27");
parser.Parse(search.Output[0].ToString());
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age = 27", search.Output[0].ToString()); Assert.AreEqual("27", brain.GetValue("speaker.age").ToString());
search = new TransformationSearchHelper(brain, "What is my age?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age", search.Output[0].ToString()); Assert.AreEqual("27", brain.GetValue(search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 4: I am 28, What is my age? /// </summary> /// <remarks> /// Using a number that wasn't previously defined. /// </remarks> [Test] public void Story4() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "I am 28");
parser.Parse(search.Output[0].ToString());
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age = 28", search.Output[0].ToString()); Assert.AreEqual("28", brain.GetValue("speaker.age").ToString());
search = new TransformationSearchHelper(brain, "What is my age?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age", search.Output[0].ToString()); Assert.AreEqual("28", brain.GetValue(search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 5: What is your name? /// </summary> [Test] public void Story5() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "What is your name?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("self.first_name", search.Output[0].ToString()); Assert.AreEqual("'Plato'", brain.GetValue(search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 6: Mon nom est Daniel, What is my name? /// </summary> [Test] public void Story6() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "My nom est Daniel");
parser.Parse(search.Output[0].ToString());
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.first_name = 'Daniel'", search.Output[0].ToString()); Assert.AreEqual("'Daniel'", brain.GetValue("speaker.first_name").ToString());
search = new TransformationSearchHelper(brain, "What is my name?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.first_name", search.Output[0].ToString()); Assert.AreEqual("'Daniel'", brain.GetValue( search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 7: I am 30, How old am I? /// </summary> [Test] public void Story7() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "I am 30");
parser.Parse(search.Output[0].ToString());
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.age = 30", search.Output[0].ToString()); Assert.AreEqual("30", brain.GetValue("speaker.age").ToString());
search = new TransformationSearchHelper(brain, "How old am I?");
Assert.AreEqual(2, search.Output.Count); Assert.AreEqual("speaker.age", search.Output[0].ToString()); Assert.AreEqual("30", brain.GetValue( search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 8: How old are you? /// </summary> [Test] public void Story8() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "How old are you?");
Assert.AreEqual(2, search.Output.Count); Assert.AreEqual("self.age", search.Output[0].ToString()); Assert.AreEqual("51", brain.GetValue( search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 9: What is your first name? /// </summary> [Test] public void Story9() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "What is your first name?");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("self.first_name", search.Output[0].ToString()); Assert.AreEqual("'Plato'", brain.GetValue( search.Output[0].ToString()).ToString()); }
/// <summary> /// Story 10: My eye color is blue /// </summary> [Test] public void Story10() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "My eye color is blue");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("speaker.eye.color = blue", search.Output[0].ToString());
// Note: The following won't work since the entity [speaker] doesn't // have a property [color]. //parser.Parse(search.Output[0].ToString()); }
/// <summary> /// Story 11: Daniel's age is 27, What is Daniel's age? /// </summary> [Test] public void Story11() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "Daniel's age is 27");
Assert.AreEqual(2, search.Output.Count); Assert.AreEqual("DanielBigham.age = 27", search.Output[0].ToString());
parser.Parse(search.Output[0].ToString()); Assert.AreEqual("27", brain.GetValue("DanielBigham.age").ToString());
search = new TransformationSearchHelper(brain, "What is Daniel's age?"); Assert.AreEqual(2, search.Output.Count); Assert.AreEqual("DanielBigham.age", search.Output[0].ToString()); }
/// <summary> /// Story 12: Daniel is 27 /// </summary> [Test] public void Story12() { TransformationSearchHelper search = new TransformationSearchHelper(brain, "Daniel is 27");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("DanielBigham.age = 27", search.Output[0].ToString());
parser.Parse(search.Output[0].ToString()); Assert.AreEqual("27", brain.GetValue("DanielBigham.age").ToString()); }
/// <summary> /// Story 13: Daniel's wife's name is Meredith /// </summary> [Test] public void Story13() { TransformationSearchHelper search = new TransformationSearchHelper( brain, "Daniel's wife's name is Meredith");
Assert.AreEqual(1, search.Output.Count); Assert.AreEqual("DanielBigham.wife.first_name = 'Meredith'", search.Output[0].ToString()); }
/// <summary> /// Ensure FindTransformations returns null if the /// first entity type doesn't match. /// </summary> [Test] public void FindTransformations_NotFoundInIndex1() { Assert.IsNull(brain.FindTransformations("a", "b")); }
/// <summary> /// Ensure FindTransformations returns null if the /// second entity type doesn't match. /// </summary> [Test] public void FindTransformations_NotFoundInIndex2() { brain.CreateEntity("a"); brain.CreateEntity("b"); IList<Transformation> list = transParser.Parse("{a} {b} -> $1.$2"); brain.AddTransformation(list[0]); Assert.IsNull(brain.FindTransformations("a", "c")); }
/// <summary> /// Ensure FindTransformations returns null if the /// second entity type doesn't match. /// </summary> [Test] public void FindTransformations() { Assert.AreEqual(1, brain.FindTransformations("'my'", "noun").Count); }
/// <summary> /// Ensure that an exception is thrown for unrecognized words. /// </summary> [Test] [ExpectedException(typeof(TransformationSearch.UnrecognizedWordException))] public void UnrecognizedWordException() { TransformationSearch search = new TransformationSearch(brain, "abc"); }
/// <summary> /// Test the resolution of a variable entity given an IsATree. /// </summary> [Test] public void Resolve() { EntityTree tree = brain.Get("'Daniel'").GetIsATree();
EntityTreeNode node = tree.Root.Children[1].Children[2]; Assert.AreEqual("noun", node.Entity.Id); Assert.AreEqual("DanielBigham", node.Parents[2].Entity.Id);
IToken parent = new EntityTypeToken(brain.Get("person"));
IList<IEntity> resolvedEntities = TransformationHelper.ResolveHelper(brain, node, parent);
Assert.AreEqual(1, resolvedEntities.Count); Assert.AreEqual("DanielBigham", resolvedEntities[0].Id); }
/// <summary> /// Helper class for testing purposes. /// </summary> internal class TransformationSearchHelper : TransformationSearch { public TransformationSearchHelper(Brain brain, string str) : base(brain, str) { }
public IList<Fragment> GetFragments() { return fragments; } }
/// <summary> /// Helper class for testing purposes. /// </summary> internal class TransformationHelper : Transformation { public TransformationHelper(Brain brain, TransInput input, ITransOutput output) : base(brain, input, output) { }
public static IList<IEntity> ResolveHelper( Brain brain, EntityTreeNode entityTypeNode, IToken parent) { return Transformation.Resolve(brain, entityTypeNode, parent, RelationType.IsA); } } } }
|
|
|
|
 |