Example #1
0
        protected override void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List <CoalescedTreeNode> children)
        {
            List <Condition>  conditions  = new List <Condition>(_conditions);
            CoalescedTreeNode localParent = parent;

            while (conditions.Count > 0)
            {
                bool found = false;
                foreach (CoalescedTreeNode child in localParent.ConditionAxis)
                {
                    if (conditions.Contains(child.Condition))
                    {
                        conditions.Remove(child.Condition);
                        localParent = child;
                        found       = true;
                    }
                }
                if (!found)
                {
                    CoalescedTreeNode node = new CoalescedTreeNode(conditions[conditions.Count - 1]);
                    conditions.RemoveAt(conditions.Count - 1);
                    localParent.ConditionAxis.Add(node);
                    localParent = node;
                }
            }
            children.Add(localParent);
        }
Example #2
0
        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);
        }
 protected override void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List<CoalescedTreeNode> children)
 {
     List<Condition> conditions = new List<Condition>(_conditions);
     CoalescedTreeNode localParent = parent;
     while(conditions.Count > 0)
     {
         bool found = false;
         foreach(CoalescedTreeNode child in localParent.ConditionAxis)
         {
             if(conditions.Contains(child.Condition))
             {
                 conditions.Remove(child.Condition);
                 localParent = child;
                 found = true;
             }
         }
         if(!found)
         {
             CoalescedTreeNode node = new CoalescedTreeNode(conditions[conditions.Count - 1]);
             conditions.RemoveAt(conditions.Count - 1);
             localParent.ConditionAxis.Add(node);
             localParent = node;
         }
     }
     children.Add(localParent);
 }
Example #4
0
        public IList <CoalescedTreeNode> MergeToCoalescedTree()
        {
            List <CoalescedTreeNode> res      = new List <CoalescedTreeNode>();
            CoalescedTreeNode        sentinel = new CoalescedTreeNode(AlwaysTrueCondition.Instance);

            MergeToCoalescedTreeNode(sentinel, res);
            return(res);
        }
Example #5
0
        public static List <CoalescedTreeNode> CreateRootTree(out CoalescedTreeNode root)
        {
            root = new CoalescedTreeNode(AlwaysTrueCondition.Instance);
            List <CoalescedTreeNode> list = new List <CoalescedTreeNode>();

            list.Add(root);
            return(list);
        }
Example #6
0
        public static MatchCollection MatchAll(Signal output, Port port, CoalescedTreeNode tree)
        {
            if (tree == null)
            {
                throw new ArgumentNullException("tree");
            }

            return(tree.MatchAll(output, port, 1));
        }
Example #7
0
        public static bool TryMatchFirst(Signal output, Port port, CoalescedTreeNode tree, out Match match)
        {
            if (tree == null)
            {
                throw new ArgumentNullException("tree");
            }

            match = tree.MatchFirst(output, port);
            return(match != null);
        }
Example #8
0
        protected virtual void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List <CoalescedTreeNode> children)
        {
            CoalescedTreeNode child;

            if (!TryGetExistingNode(parent, this, out child))
            {
                child = new CoalescedTreeNode(this);
                parent.ConditionAxis.Add(child);
            }
            children.Add(child);
        }
Example #9
0
 protected override void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List <CoalescedTreeNode> children)
 {
     foreach (Condition condition in _conditions)
     {
         CoalescedTreeNode child;
         if (!TryGetExistingNode(parent, condition, out child))
         {
             child = new CoalescedTreeNode(condition);
             parent.ConditionAxis.Add(child);
         }
         children.Add(child);
     }
 }
Example #10
0
        public bool CouldMergeToCoalescedTree(CoalescedTreeNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.Condition.Equals(AlwaysTrueCondition.Instance))
            {
                return(true);
            }
            return(CouldMergeToCoalescedTreeNode(node.Condition));
        }
Example #11
0
 protected bool TryGetExistingNode(CoalescedTreeNode parent, Condition condition, out CoalescedTreeNode node)
 {
     foreach (CoalescedTreeNode child in parent.ConditionAxis)
     {
         if (Equals(child.Condition))
         {
             node = child;
             return(true);
         }
     }
     node = null;
     return(false);
 }
 protected override void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List<CoalescedTreeNode> children)
 {
     foreach(Condition condition in _conditions)
     {
         CoalescedTreeNode child;
         if(!TryGetExistingNode(parent, condition, out child))
         {
             child = new CoalescedTreeNode(condition);
             parent.ConditionAxis.Add(child);
         }
         children.Add(child);
     }
 }
Example #13
0
        public static Match MatchFirst(Signal output, Port port, CoalescedTreeNode tree)
        {
            if (tree == null)
            {
                throw new ArgumentNullException("tree");
            }

            Match res = tree.MatchFirst(output, port);

            if (res == null)
            {
                throw new MathNet.Symbolics.Exceptions.NotFoundException();
            }
            return(res);
        }
Example #14
0
        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 static List<CoalescedTreeNode> CreateRootTree(out CoalescedTreeNode root)
 {
     root = new CoalescedTreeNode(AlwaysTrueCondition.Instance);
     List<CoalescedTreeNode> list = new List<CoalescedTreeNode>();
     list.Add(root);
     return list;
 }
 protected override void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List <CoalescedTreeNode> children)
 {
     children.Add(parent);
 }
        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");
        }
 public void AddChild(CoalescedTreeNode node)
 {
     _childAxis.Add(node);
 }
 public void AddChild(CoalescedTreeNode node)
 {
     _childAxis.Add(node);
 }
        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);
                    }
                }
            }
        }
 protected override void MergeToCoalescedTreeNode(CoalescedTreeNode parent, List<CoalescedTreeNode> children)
 {
     children.Add(parent);
 }