Пример #1
0
        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");
        }
Пример #2
0
        public override void MergeToCoalescedTree(MathIdentifier patternId, IList <CoalescedTreeNode> parents)
        {
            if (_children.Count == 0)
            {
                base.MergeToCoalescedTree(patternId, parents);
            }
            else
            {
                // Merge Conditions & Groups -> "node"-List
                IList <CoalescedTreeNode> nodes = Condition.MergeToCoalescedTree(parents);
                MergeGroupToCoalescedTree(patternId, nodes);

                // children: find matching pattern or create one
                //AlwaysTrueCondition atc = AlwaysTrueCondition.Instance;
                foreach (CoalescedTreeNode node in nodes)
                {
                    // check all patterns the current node already has
                    IList <CoalescedChildPattern> nodePatterns = node.PatternAxis;
                    bool nodeMatch = false;
                    foreach (CoalescedChildPattern pattern in nodePatterns)
                    {
                        // check all nodes of the current pattern whether they match.
                        IList <CoalescedTreeNode> patternChildren = pattern.ChildrenAxis;
                        if (patternChildren.Count != _children.Count)
                        {
                            continue;
                        }
                        bool patternMatch = true;
                        for (int i = 0; i < _children.Count; i++)
                        {
                            if (!_children[i].Condition.CouldMergeToCoalescedTree(patternChildren[i]))
                            {
                                patternMatch = false;
                                break;
                            }
                        }
                        if (patternMatch)
                        {
                            // we found a matching pattern. merge our tree pattern to this pattern.
                            nodeMatch = true;
                            for (int i = 0; i < _children.Count; i++)
                            {
                                IList <CoalescedTreeNode> list = new List <CoalescedTreeNode>();
                                list.Add(patternChildren[i]);
                                _children[i].MergeToCoalescedTree(patternId, list);
                            }
                        }
                    }
                    if (!nodeMatch)
                    {
                        // we didn't find a matching pattern. build a new such pattern.
                        CoalescedChildPattern pattern = new CoalescedChildPattern();
                        for (int i = 0; i < _children.Count; i++)
                        {
                            CoalescedTreeNode parent = new CoalescedTreeNode(AlwaysTrueCondition.Instance);
                            pattern.AddChild(parent);
                            List <CoalescedTreeNode> list = new List <CoalescedTreeNode>();
                            list.Add(parent);
                            _children[i].MergeToCoalescedTree(patternId, list);
                        }
                        node.PatternAxis.Add(pattern);
                    }
                }
            }
        }