public void Pattern_TreePattern() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Pattern psimp = new Pattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Assert.AreEqual(true, psimp.Match(sinx2, sinx2.DrivenByPort), "B01"); Assert.AreEqual(false, psimp.Match(x2, x2.DrivenByPort), "B02"); TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinsqr.Add(new Pattern(new EntityCondition(new MathIdentifier("Square", "Std")))); Assert.AreEqual(true, psinsqr.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinsqr.Match(x2, x2.DrivenByPort), "B04"); TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinadd.Add(new Pattern(new EntityCondition(new MathIdentifier("Add", "Std")))); Assert.AreEqual(false, psinadd.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinadd.Match(x2, x2.DrivenByPort), "B04"); }
public void Pattern_CoalescedTreeDeduction() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinadd_add = new Pattern(new EntityCondition(new MathIdentifier("Add", "Std"))); psinadd.Add(psinadd_add); psinadd_add.Group = "add"; TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinsqr_sqr = new Pattern(new EntityCondition(new MathIdentifier("Square", "Std"))); psinsqr.Add(psinsqr_sqr); psinsqr.Group = "sin"; psinsqr_sqr.Group = "sqr"; // generate coalesced tree CoalescedTreeNode root; List<CoalescedTreeNode> list = CoalescedTreeNode.CreateRootTree(out root); psinadd.MergeToCoalescedTree(new MathIdentifier("SinAdd", "Test"), list); psinsqr.MergeToCoalescedTree(new MathIdentifier("SinSqr", "Test"), list); // test whether the tree was generated correctly Assert.AreEqual(1, root.ConditionAxis.Count, "D01"); Assert.AreEqual(0, root.PatternAxis.Count, "D02"); CoalescedTreeNode csin = root.ConditionAxis[0]; Assert.AreEqual(true, csin.Condition is EntityCondition, "D03"); Assert.AreEqual(new MathIdentifier("Sine", "Std"), ((EntityCondition)csin.Condition).EntityId, "D04"); Assert.AreEqual(1, csin.GroupAxis.Count, "D05"); Assert.AreEqual(0, csin.SubscriptionAxis.Count, "D06"); Assert.AreEqual("sin", csin.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D07"); Assert.AreEqual(1, csin.PatternAxis.Count, "D08"); Assert.AreEqual(1, csin.PatternAxis[0].ChildrenAxis.Count, "D09"); Assert.AreEqual(true, csin.PatternAxis[0].ChildrenAxis[0].Condition is AlwaysTrueCondition, "D10"); CoalescedTreeNode cadd = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[0]; Assert.AreEqual(true, cadd.Condition is EntityCondition, "D11"); Assert.AreEqual(new MathIdentifier("Add", "Std"), ((EntityCondition)cadd.Condition).EntityId, "D12"); Assert.AreEqual(1, cadd.GroupAxis.Count, "D13"); Assert.AreEqual(1, cadd.SubscriptionAxis.Count, "D14"); Assert.AreEqual("add", cadd.GroupAxis[new MathIdentifier("SinAdd", "Test")], "D15"); Assert.AreEqual(new MathIdentifier("SinAdd", "Test"), cadd.SubscriptionAxis[0], "D16"); Assert.AreEqual(0, cadd.PatternAxis.Count, "D18"); CoalescedTreeNode csqr = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[1]; Assert.AreEqual(true, csqr.Condition is EntityCondition, "D19"); Assert.AreEqual(new MathIdentifier("Square", "Std"), ((EntityCondition)csqr.Condition).EntityId, "D20"); Assert.AreEqual(1, csqr.GroupAxis.Count, "D21"); Assert.AreEqual(1, csqr.SubscriptionAxis.Count, "D22"); Assert.AreEqual("sqr", csqr.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D23"); Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), csqr.SubscriptionAxis[0], "D24"); Assert.AreEqual(0, csqr.PatternAxis.Count, "D26"); // test whether the tree works as expected MatchCollection res = Match.MatchAll(sinx2, sinx2.DrivenByPort, root); Assert.AreEqual(true, res.Contains(new MathIdentifier("SinSqr", "Test")), "D27"); Assert.AreEqual(false, res.Contains(new MathIdentifier("SinAdd", "Test")), "D28"); Match match = res[new MathIdentifier("SinSqr", "Test")]; Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), match.PatternId, "D29"); Assert.AreEqual(2, match.GroupCount, "D30"); Assert.AreEqual(1, match["sin"].Count, "D31"); Assert.AreEqual(sinx2.InstanceId, match["sin"][0].First.InstanceId, "D32"); Assert.AreEqual(1, match["sqr"].Count, "D33"); Assert.AreEqual(x2.InstanceId, match["sqr"][0].First.InstanceId, "D34"); }
public void Pattern_CoalescedTreeDeduction() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinadd_add = new Pattern(new EntityCondition(new MathIdentifier("Add", "Std"))); psinadd.Add(psinadd_add); psinadd_add.Group = "add"; TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinsqr_sqr = new Pattern(new EntityCondition(new MathIdentifier("Square", "Std"))); psinsqr.Add(psinsqr_sqr); psinsqr.Group = "sin"; psinsqr_sqr.Group = "sqr"; // generate coalesced tree CoalescedTreeNode root; List <CoalescedTreeNode> list = CoalescedTreeNode.CreateRootTree(out root); psinadd.MergeToCoalescedTree(new MathIdentifier("SinAdd", "Test"), list); psinsqr.MergeToCoalescedTree(new MathIdentifier("SinSqr", "Test"), list); // test whether the tree was generated correctly Assert.AreEqual(1, root.ConditionAxis.Count, "D01"); Assert.AreEqual(0, root.PatternAxis.Count, "D02"); CoalescedTreeNode csin = root.ConditionAxis[0]; Assert.AreEqual(true, csin.Condition is EntityCondition, "D03"); Assert.AreEqual(new MathIdentifier("Sine", "Std"), ((EntityCondition)csin.Condition).EntityId, "D04"); Assert.AreEqual(1, csin.GroupAxis.Count, "D05"); Assert.AreEqual(0, csin.SubscriptionAxis.Count, "D06"); Assert.AreEqual("sin", csin.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D07"); Assert.AreEqual(1, csin.PatternAxis.Count, "D08"); Assert.AreEqual(1, csin.PatternAxis[0].ChildrenAxis.Count, "D09"); Assert.AreEqual(true, csin.PatternAxis[0].ChildrenAxis[0].Condition is AlwaysTrueCondition, "D10"); CoalescedTreeNode cadd = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[0]; Assert.AreEqual(true, cadd.Condition is EntityCondition, "D11"); Assert.AreEqual(new MathIdentifier("Add", "Std"), ((EntityCondition)cadd.Condition).EntityId, "D12"); Assert.AreEqual(1, cadd.GroupAxis.Count, "D13"); Assert.AreEqual(1, cadd.SubscriptionAxis.Count, "D14"); Assert.AreEqual("add", cadd.GroupAxis[new MathIdentifier("SinAdd", "Test")], "D15"); Assert.AreEqual(new MathIdentifier("SinAdd", "Test"), cadd.SubscriptionAxis[0], "D16"); Assert.AreEqual(0, cadd.PatternAxis.Count, "D18"); CoalescedTreeNode csqr = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[1]; Assert.AreEqual(true, csqr.Condition is EntityCondition, "D19"); Assert.AreEqual(new MathIdentifier("Square", "Std"), ((EntityCondition)csqr.Condition).EntityId, "D20"); Assert.AreEqual(1, csqr.GroupAxis.Count, "D21"); Assert.AreEqual(1, csqr.SubscriptionAxis.Count, "D22"); Assert.AreEqual("sqr", csqr.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D23"); Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), csqr.SubscriptionAxis[0], "D24"); Assert.AreEqual(0, csqr.PatternAxis.Count, "D26"); // test whether the tree works as expected MatchCollection res = Match.MatchAll(sinx2, sinx2.DrivenByPort, root); Assert.AreEqual(true, res.Contains(new MathIdentifier("SinSqr", "Test")), "D27"); Assert.AreEqual(false, res.Contains(new MathIdentifier("SinAdd", "Test")), "D28"); Match match = res[new MathIdentifier("SinSqr", "Test")]; Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), match.PatternId, "D29"); Assert.AreEqual(2, match.GroupCount, "D30"); Assert.AreEqual(1, match["sin"].Count, "D31"); Assert.AreEqual(sinx2.InstanceId, match["sin"][0].First.InstanceId, "D32"); Assert.AreEqual(1, match["sqr"].Count, "D33"); Assert.AreEqual(x2.InstanceId, match["sqr"][0].First.InstanceId, "D34"); }