public override IExpressionTreeNode InjectInto(IExpressionTreeNode targetNode, Func<IPredicateConnectiveNode> createConnectiveNode)
 {
     if (targetNode is PredicateNode)
     {
         return InjectInto(targetNode as PredicateNode, createConnectiveNode);
     }
     else
     {
         return InjectInto(targetNode as PredicateConnectiveNode, createConnectiveNode);
     }
 }
        public virtual void InsertRight(IExpressionTreeNode rightNode)
        {
            if (rightNode == null)
                throw new ArgumentNullException("rightNode");

            if (Right != null)
                throw new InvalidOperationException("This node already contains right child node.");

            Right = rightNode;
            Right.AssignParent(this);

            RaiseTreeChanged();
        }
        public virtual void InsertLeft(IExpressionTreeNode leftNode)
        {
            if (leftNode == null)
                throw new ArgumentNullException("leftNode");

            if (Left != null)
                throw new InvalidOperationException("This node already contains left child node.");

            Left = leftNode;
            Left.AssignParent(this);

            RaiseTreeChanged();
        }
 public void ClearChildAssignment(IExpressionTreeNode existingChild, out ChildNodePosition childPosition)
 {
     if (object.Equals(Left, existingChild))
     {
         Left = null;
         childPosition = ChildNodePosition.Left;
     }
     else if (object.Equals(Right, existingChild))
     {
         Right = null;
         childPosition = ChildNodePosition.Right;
     }
     else
     {
         throw new ArgumentException("Specified node is not a child of this node.");
     }
 }
        public virtual void ReplaceChildNode(IExpressionTreeNode oldNode, IExpressionTreeNode newNode)
        {
            if (Left == oldNode)
            {
                Left = newNode;
            }
            else if (Right == oldNode)
            {
                Right = newNode;
            }
            else
            {
                throw new InvalidOperationException("old node is not a child of this element");
            }

            if (newNode != null)
            {
                newNode.AssignParent(this);
            }
        }
 public override bool Evaluate(object payload, IExpressionTreeNode left, IExpressionTreeNode right)
 {
     return
         left.Evaluate(payload)
         ||
         right.Evaluate(payload);
 }
 public void AppendNode(IExpressionTreeNode newNode)
 {
     if (Root == null)
     {
         ReplaceRootAndSubtree(newNode);
     }
     else
     {
         InjectInto(newNode, Root);
     }
 }
        public void RemoveNode(IExpressionTreeNode node)
        {
            var nodeAsPredicate = node as PredicateNode;

            if (nodeAsPredicate == null)
                throw new NotSupportedException();

            RemoveNodeInternal(nodeAsPredicate);

            CleanUpTreeStructure();
            RaiseTreeChanged();
        }
Beispiel #9
0
 public MultiplicationTreeNode(IExpressionTreeNode <double> leftExpression, IExpressionTreeNode <double> rightExpression)
 {
     _leftExpression  = leftExpression;
     _rightExpression = rightExpression;
 }
        public void ClearChildAssignment(IExpressionTreeNode existingChild)
        {
            ChildNodePosition childPosition_ignored = default(ChildNodePosition);

            ClearChildAssignment(existingChild, out childPosition_ignored);
        }
Beispiel #11
0
 public IExpressionTreeNode <double> GetExpressionTreeNode(IExpressionTreeNode <double> leftOperand,
                                                           IExpressionTreeNode <double> rightOperand)
 {
     return(new MultiplicationTreeNode(leftOperand, rightOperand));
 }
 public AdditionTreeNode(IExpressionTreeNode <double> leftExpression, IExpressionTreeNode <double> rightExpression)
 {
     _leftExpression  = leftExpression;
     _rightExpression = rightExpression;
 }
 public bool Evaluate(object payload, IExpressionTreeNode left, IExpressionTreeNode right)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source">Node to be injected</param>
        /// <param name="target">Target Node which will be replaced by Source, and joined with source's new sub-tree</param>
        static IExpressionTreeNode InjectInto(IExpressionTreeNode source, IExpressionTreeNode target, Func<IPredicateConnectiveNode> createConnectiveNode)
        {
            var sourceParent = source.Parent;

            var sourceParentAsConnective = sourceParent as PredicateConnectiveNode;

            var sourceSibling = (IExpressionTreeNode)null;

            if (sourceParentAsConnective != null)
            {
                var sourcePosition = sourceParentAsConnective.GetChildPosition(source);

                if (sourcePosition == ChildNodePosition.Left)
                    sourceSibling = sourceParentAsConnective.Right;
                else
                    sourceSibling = sourceParentAsConnective.Left;
            }
            var sourceSiblingAsConnective = sourceSibling as PredicateConnectiveNode;
            
            var targetParent = target.Parent;

            var targetParentAsConnective = targetParent as PredicateConnectiveNode;

            var targetSibling = (IExpressionTreeNode)null;

            var targetPosition = ChildNodePosition.Right; // default should be right, if target has no parent

            if(targetParent != null)
            {
                targetPosition = targetParent.GetChildPosition(target);
            }

            if (targetParentAsConnective != null)
            {
                if (targetPosition == ChildNodePosition.Left)
                    targetSibling = targetParentAsConnective.Right;
                else
                    targetSibling = targetParentAsConnective.Left;
            }

            var targetSiblingAsConnective = targetSibling as PredicateConnectiveNode;

            var newConnective = createConnectiveNode();

            if (targetParent != null)
            {
                //# remove target from its parent
                target.AssignParent(newParent: null);
                targetParent.ClearChildAssignment(target);

                //# assign new Connective in place of target
                targetParent.AssignChild(newConnective, targetPosition);
                newConnective.AssignParent(targetParent);
            }
            else
            {

            }

            //# assign old target as left child node of new connective
            if (targetPosition == ChildNodePosition.Left)
                newConnective.AssignChild(target, ChildNodePosition.Right);
            else
                newConnective.AssignChild(target, ChildNodePosition.Left);

            //# copy connective mode from target parent (if exists)
            if (targetParentAsConnective != null)
                newConnective.CopyFrom(targetParentAsConnective);
            else if (targetSiblingAsConnective != null)
                newConnective.CopyFrom(targetSiblingAsConnective);

            target.AssignParent(newConnective);

            //# assign source node as right child node of new connective
            newConnective.AssignChild(source, targetPosition);
            source.AssignParent(newConnective);

            //# fix original parent tree
            //  parent is an empty connective or connective with one child now
            //  replace parent connective with its child (removing the connective completely from the tree)
            if (sourceParent != null)
            {
                //# remove source from its parent
                var sourcePosition = default(ChildNodePosition);
                sourceParent.ClearChildAssignment(source, out sourcePosition);

                //# get other source parent child
                var oldSibling = (IExpressionTreeNode)null;
                if (sourcePosition == ChildNodePosition.Left)
                    oldSibling = sourceParent.Right;
                else
                    oldSibling = sourceParent.Left;

                //# in grand parent, replace source parent with its other sibling
                var grandParent = sourceParent.Parent;

                if (grandParent != null)
                    grandParent.ReplaceChildNode(sourceParent, oldSibling);
            }

            //# get new root element of a tree

            var root_candidate = (IExpressionTreeNode)newConnective;
            while (root_candidate.Parent != null)
                root_candidate = root_candidate.Parent;

            return root_candidate;
        }
 public void RemoveNode(IExpressionTreeNode o)
 {
     ExpTree.RemoveNode(o);
 }
 public void AssignParent(IExpressionTreeNode newParent)
 {
     Parent = newParent;
 }
 public abstract IExpressionTreeNode InjectInto(IExpressionTreeNode node, Func<IPredicateConnectiveNode> createConnectiveNode);
 public ChildNodePosition GetChildPosition(IExpressionTreeNode existingChild)
 {
     if (object.Equals(Left, existingChild))
     {
         return ChildNodePosition.Left;
     }
     else if (object.Equals(Right, existingChild))
     {
         return ChildNodePosition.Right;
     }
     else
     {
         throw new ArgumentException("Specified node is not a child of this node.");
     }
 }
 public IExpressionTreeNode <double> GetExpressionTreeNode(IExpressionTreeNode <double> leftOperand,
                                                           IExpressionTreeNode <double> rightOperand)
 {
     return(new SubtractionTreeNode(leftOperand, rightOperand));
 }
 public void AssignChild(IExpressionTreeNode childNode, ChildNodePosition position)
 {
     if(position == ChildNodePosition.Left)
     {
         Left = childNode;
     }
     else
     {
         Right = childNode;
     }
 }
        public void InjectInto(IExpressionTreeNode source, IExpressionTreeNode target)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (target == null)
                throw new ArgumentNullException("target");

            //# cannot insert parent into its child
            if (target.Parent == source)
                return;

            Root = source.InjectInto(target, GetDefaultConnectiveNode);

            CleanUpTreeStructure();

            RaiseTreeChanged();
        }
        public void SwapChildren()
        {
            var left = Left;
            var right = Right;

            Left = right;
            Right = left;
        }
 public void ReplaceRootAndSubtree(IExpressionTreeNode newRootAndSubTree)
 {
     Root = newRootAndSubTree;
     CleanUpTreeStructure();
     RaiseTreeChanged();
 }
        public bool IsDescendantOf(IExpressionTreeNode potentialAncestor)
        {
            var ancestor = (IExpressionTreeNode)Parent;

            while (ancestor != null)
            {
                if (ancestor == potentialAncestor)
                    return true;

                ancestor = ancestor.Parent;
            }

            return false;
        }
        /// <summary>
        /// Cleans up the tree structure to ensure that all Connective Nodes have both children assigned.
        /// If a Connective has no children assigned, it will be removed.
        /// If a Connective has one child assigned, it will be removed and child moved in its place.
        /// </summary>
        void CleanUpTreeStructure()
        {
            if (Root == null)
                return;

            //# find te root

            var root_candidate = Root;

            foreach (var node in Root.TraverseTreePostOrder())
            {
                if (node is PredicateNode)
                    continue;

                var connectiveNode = node as PredicateConnectiveNode;

                //# Connective Node with both children => do nothing
                if (connectiveNode.Left != null && connectiveNode.Right != null)
                    continue;

                //# Connective Node without any children => REMOVE
                if (connectiveNode.Left == null && connectiveNode.Right == null)
                {
                    if (connectiveNode.Parent == null)
                    {
                        // this is the root, replace it with null
                        Root = null;
                        break;
                    }
                    else
                    {
                        connectiveNode.Parent.ReplaceChildNode(connectiveNode, null);
                        continue;
                    }
                }

                //# Connective Node with only one child => replace connective node with the child
                if (connectiveNode.Left == null)
                {
                    if (connectiveNode.Parent == null)
                    {
                        // this is the root, replace it with Right Child
                        Root = connectiveNode.Right;
                        Root.AssignParent(newParent: null);

                    }
                    else
                    {
                        connectiveNode.Parent.ReplaceChildNode(connectiveNode, connectiveNode.Right);
                        continue;
                    }
                }

                if (connectiveNode.Right == null)
                {
                    if (connectiveNode.Parent == null)
                    {
                        // this is the root, replace it with Left Child
                        Root = connectiveNode.Left;
                        Root.AssignParent(newParent: null);
                    }
                    else
                    {
                        connectiveNode.Parent.ReplaceChildNode(connectiveNode, connectiveNode.Left);
                        continue;
                    }
                }
            }

            RaiseTreeChanged();
        }
 public abstract bool Evaluate(object payload, IExpressionTreeNode left, IExpressionTreeNode right);