public static Match MatchBest(Signal output, Port port, CoalescedTreeNode tree) { if (tree == null) { throw new ArgumentNullException("tree"); } MatchCollection res = tree.MatchAll(output, port, 1); Match bestMatch = null; int bestScore = -1; foreach (Match m in res) { if (m.Score > bestScore) { bestMatch = m; bestScore = m.Score; } } if (bestScore == -1) { throw new MathNet.Symbolics.Exceptions.NotFoundException(); } return(bestMatch); }
public static MatchCollection MatchAll(Signal output, Port port, CoalescedTreeNode tree) { if (tree == null) { throw new ArgumentNullException("tree"); } return(tree.MatchAll(output, port, 1)); }
public static bool TryMatchBest(Signal output, Port port, CoalescedTreeNode tree, out Match match) { if (tree == null) { throw new ArgumentNullException("tree"); } MatchCollection res = tree.MatchAll(output, port, 1); Match bestMatch = null; int bestScore = -1; foreach (Match m in res) { if (m.Score > bestScore) { bestMatch = m; bestScore = m.Score; } } match = bestMatch; return(bestScore != -1); }
public void Pattern_CoalescedTreeMatching() { 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"; CoalescedTreeNode root = new CoalescedTreeNode(AlwaysTrueCondition.Instance); root.Subscribe(new MathIdentifier("A", "Test")); CoalescedTreeNode sin = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Sine", "Std"))); sin.AddGroup(new MathIdentifier("B", "Test"), "sin"); root.ConditionAxis.Add(sin); CoalescedTreeNode sqr = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Square", "Std"))); sqr.AddGroup(new MathIdentifier("B", "Test"), "sqr"); sqr.Subscribe(new MathIdentifier("B", "Test")); CoalescedChildPattern sqrPattern = new CoalescedChildPattern(); sqrPattern.AddChild(sqr); sin.PatternAxis.Add(sqrPattern); CoalescedTreeNode tan = new CoalescedTreeNode(new EntityCondition(new MathIdentifier("Tangent", "Std"))); tan.AddGroup(new MathIdentifier("B", "Test"), "tan"); tan.Subscribe(new MathIdentifier("C", "Test")); root.ConditionAxis.Add(tan); MatchCollection res = root.MatchAll(sinx2, sinx2.DrivenByPort, 1); Assert.AreEqual(true, res.Contains(new MathIdentifier("A", "Test")), "C01"); Assert.AreEqual(true, res.Contains(new MathIdentifier("B", "Test")), "C02"); Assert.AreEqual(false, res.Contains(new MathIdentifier("C", "Test")), "C03"); Match mA = res[new MathIdentifier("A", "Test")]; Assert.AreEqual(new MathIdentifier("A", "Test"), mA.PatternId, "C04"); Assert.AreEqual(0, mA.GroupCount, "C05"); Match mB = res[new MathIdentifier("B", "Test")]; Assert.AreEqual(new MathIdentifier("B", "Test"), mB.PatternId, "C06"); Assert.AreEqual(2, mB.GroupCount, "C07"); Group mBsqr = mB["sqr"]; Assert.AreEqual(1, mBsqr.Count, "C08"); Assert.AreEqual(x2.InstanceId, mBsqr[0].First.InstanceId, "C09"); Assert.AreEqual(x2.DrivenByPort.InstanceId, mBsqr[0].Second.InstanceId, "C10"); Group mBsin = mB["sin"]; Assert.AreEqual(1, mBsin.Count, "C11"); Assert.AreEqual(sinx2.InstanceId, mBsin[0].First.InstanceId, "C12"); Assert.AreEqual(sinx2.DrivenByPort.InstanceId, mBsin[0].Second.InstanceId, "C13"); }