private String PartToString(ExpressionTreeNode node, Int32 currentPrecedence)
		{
			Debug.Assert(node != null, "node != null");
			String part = node.ToString();

			if (node.Token.IsOperator)
			{
				switch (node.Token.OperatorDescriptor.PrimitiveOperator)
				{
					case PrimitiveOperator.Add:
					case PrimitiveOperator.Sub:
					case PrimitiveOperator.Mul:
					case PrimitiveOperator.Div:
					{
						if (currentPrecedence > LeftNode.Token.OperatorPrecedence)
						{
							part = String.Format(CultureInfo.CurrentCulture, "({0})", part);
						}

						break;
					}
				}
			}

			return part;
		}
示例#2
0
        private String PartToString(ExpressionTreeNode node, Int32 currentPrecedence)
        {
            Debug.Assert(node != null, "node != null");
            String part = node.ToString();

            if (node.Token.IsOperator)
            {
                switch (node.Token.OperatorDescriptor.PrimitiveOperator)
                {
                case PrimitiveOperator.Add:
                case PrimitiveOperator.Sub:
                case PrimitiveOperator.Mul:
                case PrimitiveOperator.Div:
                {
                    if (currentPrecedence > LeftNode.Token.OperatorPrecedence)
                    {
                        part = String.Format(CultureInfo.CurrentCulture, "({0})", part);
                    }

                    break;
                }
                }
            }

            return(part);
        }
示例#3
0
        // Numbers, constants, variables and brakets.
        private ExpressionTreeNode ExprLevel6()
        {
            ExpressionTreeNode temp = null;

            if (CurrentToken.IsNumber || CurrentToken.IsVariable)
            {
                temp = new ExpressionTreeNode(CurrentToken, null, null);
                NextToken();
            }
            else
            if (CurrentToken.IsLeftBracket)
            {
                NextToken();
                temp = ExprLevel1();

                if (!CurrentToken.IsRightBracket)
                {
                    throw new ExpressionSyntaxException(Resources.ExpressionSyntax_RightBracketExpected, _expression, _parsedExpression);
                }

                NextToken();
            }

            return(temp);
        }
示例#4
0
        private SortedList FindVariables(ExpressionTreeNode node)
        {
            SortedList list = new SortedList();

            if (node.Token.IsVariable)
            {
                list.Add(node.Token.TokenBody.ToLower(), "");
            }

            if (node.LeftNode != null)
            {
                foreach (DictionaryEntry e in FindVariables(node.LeftNode))
                {
                    if (!list.ContainsKey(e.Key))
                    {
                        list.Add(e.Key, e.Value);
                    }
                }
            }

            if (node.RightNode != null)
            {
                foreach (DictionaryEntry e in FindVariables(node.RightNode))
                {
                    if (!list.ContainsKey(e.Key))
                    {
                        list.Add(e.Key, e.Value);
                    }
                }
            }

            return(list);
        }
示例#5
0
        public static ExpressionTreeNode BuildExpressionNode(INuGenServiceProvider serviceProvider, NodeBase schemaNode)
        {
            if (schemaNode == null)
            {
                return(null);
            }

            if (NeedToSkip(schemaNode))
            {
                throw new InvalidOperationException("Expression node connected to unsupported node. Expression node cannot be recognized");
            }

            ExpressionTreeNode node = null;

            if (schemaNode is Operator)
            {
                ExpressionToken token = new ExpressionToken(serviceProvider, ((Operator)schemaNode).OperatorDescriptor.StringRepresentation);
                node = new ExpressionTreeNode(token);

                switch (token.OperatorDescriptor.PrimitiveOperator)
                {
                case PrimitiveOperator.Add:
                case PrimitiveOperator.Sub:
                case PrimitiveOperator.Mul:
                case PrimitiveOperator.Div:
                {
                    node.LeftNode  = BuildExpressionNode(serviceProvider, schemaNode.Inputs[0].Connection.Node);
                    node.RightNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[1].Connection.Node);
                    break;
                }
                }

                if (token.IsOneVariableFunction)
                {
                    node.LeftNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[0].Connection.Node);
                }

                if (token.IsTwoVariableFunction)
                {
                    node.LeftNode  = BuildExpressionNode(serviceProvider, schemaNode.Inputs[0].Connection.Node);
                    node.RightNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[1].Connection.Node);
                }

                return(node);
            }

            if (schemaNode is NumberParam)
            {
                return(new ExpressionTreeNode(new ExpressionToken(serviceProvider, ((NumberParam)schemaNode).Name)));
            }

            if (schemaNode is VisiTypes.Number)
            {
                return(new ExpressionTreeNode(new ExpressionToken(serviceProvider, ((VisiTypes.Number)schemaNode).Value.ToString())));
            }

            return(null);
        }
		private Int32 GetDeepLevelInternal(ExpressionTreeNode node, Int32 level)
		{
			if (node == null)
			{
				return level;
			}

			level++;

			return Math.Max(GetDeepLevelInternal(node.LeftNode, level), GetDeepLevelInternal(node.RightNode, level));
		}
示例#7
0
        private Int32 GetDeepLevelInternal(ExpressionTreeNode node, Int32 level)
        {
            if (node == null)
            {
                return(level);
            }

            level++;

            return(Math.Max(GetDeepLevelInternal(node.LeftNode, level), GetDeepLevelInternal(node.RightNode, level)));
        }
示例#8
0
        // Unary +- operators
        private ExpressionTreeNode ExprLevel4()
        {
            ExpressionTreeNode temp = null;

            if (CurrentToken.IsOperator)
            {
                switch (CurrentToken.OperatorDescriptor.PrimitiveOperator)
                {
                case PrimitiveOperator.Add:
                {
                    NextToken();
                    temp = ExprLevel5();
                    break;
                }

                case PrimitiveOperator.Sub:
                {
                    NextToken();

                    ExpressionTreeNode left  = new ExpressionTreeNode(new ExpressionToken(ServiceProvider, "-1"));
                    ExpressionTreeNode right = ExprLevel5();

                    if (right == null)
                    {
                        throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
                    }

                    temp = new ExpressionTreeNode(new ExpressionToken(ServiceProvider, "*"), left, right);
                    break;
                }

                default:
                {
                    temp = ExprLevel5();
                    break;
                }
                }
            }
            else
            {
                temp = ExprLevel5();
            }

            return(temp);
        }
示例#9
0
        // * / operators
        private ExpressionTreeNode ExprLevel2()
        {
            ExpressionTreeNode temp = ExprLevel3();
            ExpressionToken    token;

            while (true)
            {
                if (CurrentToken.IsOperator)
                {
                    switch (CurrentToken.OperatorDescriptor.PrimitiveOperator)
                    {
                    case PrimitiveOperator.Mul:
                    case PrimitiveOperator.Div:
                    {
                        if (temp == null)
                        {
                            throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
                        }

                        token = CurrentToken;
                        NextToken();
                        ExpressionTreeNode right = ExprLevel2();

                        if (right == null)
                        {
                            throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
                        }

                        temp = new ExpressionTreeNode(token, temp, right);
                        break;
                    }

                    default:
                    {
                        return(temp);
                    }
                    }
                }
                else
                {
                    return(temp);
                }
            }
        }
示例#10
0
        // Math functions
        private ExpressionTreeNode ExprLevel5()
        {
            ExpressionTreeNode left  = null;
            ExpressionTreeNode right = null;
            ExpressionTreeNode temp  = null;

            if (CurrentToken.IsOneVariableFunction)
            {
                temp = new ExpressionTreeNode(CurrentToken, null, null);
                NextToken();

                if (!CurrentToken.IsLeftBracket)
                {
                    throw new ExpressionSyntaxException(Resources.ExpressionSyntax_LeftBracketExpected, _expression, _parsedExpression);
                }

                NextToken();

                left = ExprLevel1();

                if (left == null)
                {
                    throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
                }

                temp.LeftNode = left;

                if (!CurrentToken.IsRightBracket)
                {
                    throw new ExpressionSyntaxException(Resources.ExpressionSyntax_RightBracketExpected, _expression, _parsedExpression);
                }

                NextToken();
            }
            else
            {
                if (CurrentToken.IsTwoVariableFunction)
                {
                    temp = new ExpressionTreeNode(CurrentToken, null, null);
                    NextToken();
                    if (!CurrentToken.IsLeftBracket)
                    {
                        throw new ExpressionSyntaxException(Resources.ExpressionSyntax_LeftBracketExpected, _expression, _parsedExpression);
                    }

                    NextToken();

                    left = ExprLevel1();
                    if (left == null)
                    {
                        throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
                    }

                    temp.LeftNode = left;

                    if (!CurrentToken.IsComma)
                    {
                        throw new ExpressionSyntaxException(Resources.ExpressionSyntax_CommaExpected, _expression, _parsedExpression);
                    }

                    NextToken();
                    right = ExprLevel1();

                    if (right == null)
                    {
                        throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
                    }

                    temp.RightNode = right;

                    if (!CurrentToken.IsRightBracket)
                    {
                        throw new ExpressionSyntaxException(Resources.ExpressionSyntax_RightBracketExpected, _expression, _parsedExpression);
                    }

                    NextToken();
                }
                else
                {
                    temp = ExprLevel6();
                }
            }

            return(temp);
        }
示例#11
0
		// +- operators
		private ExpressionTreeNode ExprLevel1()
		{
			ExpressionTreeNode temp = ExprLevel2();
			ExpressionToken token;

			while (true)
			{
				if (CurrentToken.IsOperator)
				{
					switch (CurrentToken.OperatorDescriptor.PrimitiveOperator)
					{
						case PrimitiveOperator.Sub:
						case PrimitiveOperator.Add:
						{
							token = CurrentToken;
							NextToken();
							ExpressionTreeNode right = ExprLevel2();

							if (right == null)
							{
								throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
							}

							temp = new ExpressionTreeNode(token, temp, right);
							break;
						}
						default:
						{
							return temp;
						}
					}
				}
				else
				{
					return temp;
				}
			}
		}
示例#12
0
		private SortedList FindVariables(ExpressionTreeNode node)
		{
			SortedList list = new SortedList();

			if (node.Token.IsVariable)
			{
				list.Add(node.Token.TokenBody.ToLower(), "");
			}

			if (node.LeftNode != null)
			{
				foreach (DictionaryEntry e in FindVariables(node.LeftNode))
				{
					if (!list.ContainsKey(e.Key))
					{
						list.Add(e.Key, e.Value);
					}
				}
			}

			if (node.RightNode != null)
			{
				foreach (DictionaryEntry e in FindVariables(node.RightNode))
				{
					if (!list.ContainsKey(e.Key))
					{
						list.Add(e.Key, e.Value);
					}
				}
			}

			return list;
		}
示例#13
0
		// Numbers, constants, variables and brakets.
		private ExpressionTreeNode ExprLevel6()
		{
			ExpressionTreeNode temp = null;

			if (CurrentToken.IsNumber || CurrentToken.IsVariable)
			{
				temp = new ExpressionTreeNode(CurrentToken, null, null);
				NextToken();
			}
			else
				if (CurrentToken.IsLeftBracket)
				{
					NextToken();
					temp = ExprLevel1();

					if (!CurrentToken.IsRightBracket)
					{
						throw new ExpressionSyntaxException(Resources.ExpressionSyntax_RightBracketExpected, _expression, _parsedExpression);
					}

					NextToken();
				}

			return temp;
		}
		public ExpressionTreeNode(ExpressionToken token, ExpressionTreeNode leftNode)
			: this(token, leftNode, null)
		{
		}
示例#15
0
		// Unary +- operators
		private ExpressionTreeNode ExprLevel4()
		{
			ExpressionTreeNode temp = null;

			if (CurrentToken.IsOperator)
			{
				switch (CurrentToken.OperatorDescriptor.PrimitiveOperator)
				{
					case PrimitiveOperator.Add:
					{
						NextToken();
						temp = ExprLevel5();
						break;
					}
					case PrimitiveOperator.Sub:
					{
						NextToken();

						ExpressionTreeNode left = new ExpressionTreeNode(new ExpressionToken(ServiceProvider, "-1"));
						ExpressionTreeNode right = ExprLevel5();

						if (right == null)
						{
							throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
						}

						temp = new ExpressionTreeNode(new ExpressionToken(ServiceProvider, "*"), left, right);
						break;
					}
					default:
					{
						temp = ExprLevel5();
						break;
					}
				}
			}
			else
			{
				temp = ExprLevel5();
			}

			return temp;
		}
示例#16
0
        private static void AddNode(Canvas canvas, NodePort parentInput, ExpressionTreeNode expressionNode, List <NumberParam> variables, Point location, Int32 levelStep, Int32 minY, Int32 maxY)
        {
            Int32    leafs = (expressionNode.LeftNode != null ? 1 : 0) + (expressionNode.RightNode != null ? 1 : 0);
            NodeBase node  = null;

            if (expressionNode.Token.IsNumber)
            {
                node = new VisiTypes.Number(Double.Parse(expressionNode.Token.TokenBody));
                canvas.AddNode(node, location);
            }

            if (expressionNode.Token.IsVariable)
            {
                Boolean exist = false;
                foreach (NumberParam param in variables)
                {
                    if (param.Name == expressionNode.Token.TokenBody)
                    {
                        exist = true;
                        node  = param;
                        break;
                    }
                }

                if (!exist)
                {
                    node = new NumberParam();
                    ((NumberParam)node).Name   = expressionNode.Token.TokenBody;
                    ((NumberParam)node).Header = expressionNode.Token.TokenBody;
                    variables.Add((NumberParam)node);
                    canvas.AddNode(node, location);
                }
            }

            if (expressionNode.Token.IsOperator)
            {
                node = new Operator(expressionNode.Token.OperatorDescriptor);
                canvas.AddNode(node, location);

                switch (leafs)
                {
                case 1:
                {
                    AddNode(canvas, node.GetInput(0), expressionNode.LeftNode, variables, new Point(location.X - levelStep, location.Y), levelStep, minY, maxY);
                    break;
                }

                case 2:
                {
                    Int32 y2 = (maxY - minY) / 2;
                    AddNode(canvas, node.GetInput(0), expressionNode.LeftNode, variables, new Point(location.X - levelStep, minY + y2 / 2), levelStep, minY, minY + y2);
                    AddNode(canvas, node.GetInput(1), expressionNode.RightNode, variables, new Point(location.X - levelStep, maxY - y2 / 2), levelStep, maxY - y2, maxY);
                    break;
                }
                }
            }

            if (parentInput != null && node != null)
            {
                parentInput.Connection = node.GetOutput(0);
            }
        }
		public static ExpressionTreeNode BuildExpressionNode(INuGenServiceProvider serviceProvider, NodeBase schemaNode)
		{
			if (schemaNode == null)
			{
				return null;
			}

			if (NeedToSkip(schemaNode))
			{
				throw new InvalidOperationException("Expression node connected to unsupported node. Expression node cannot be recognized");
			}

			ExpressionTreeNode node = null;

			if (schemaNode is Operator)
			{
				ExpressionToken token = new ExpressionToken(serviceProvider, ((Operator)schemaNode).OperatorDescriptor.StringRepresentation);
				node = new ExpressionTreeNode(token);

				switch (token.OperatorDescriptor.PrimitiveOperator)
				{
					case PrimitiveOperator.Add:
					case PrimitiveOperator.Sub:
					case PrimitiveOperator.Mul:
					case PrimitiveOperator.Div:
					{
						node.LeftNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[0].Connection.Node);
						node.RightNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[1].Connection.Node);
						break;
					}
				}

				if (token.IsOneVariableFunction)
				{
					node.LeftNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[0].Connection.Node);
				}

				if (token.IsTwoVariableFunction)
				{
					node.LeftNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[0].Connection.Node);
					node.RightNode = BuildExpressionNode(serviceProvider, schemaNode.Inputs[1].Connection.Node);
				}

				return node;
			}

			if (schemaNode is NumberParam)
			{
				return new ExpressionTreeNode(new ExpressionToken(serviceProvider, ((NumberParam)schemaNode).Name));
			}

			if (schemaNode is VisiTypes.Number)
			{
				return new ExpressionTreeNode(new ExpressionToken(serviceProvider, ((VisiTypes.Number)schemaNode).Value.ToString()));
			}

			return null;
		}
示例#18
0
 public ExpressionTreeNode(ExpressionToken token, ExpressionTreeNode leftNode, ExpressionTreeNode rightNode)
 {
     _token     = token;
     _leftNode  = leftNode;
     _rightNode = rightNode;
 }
		public ExpressionTreeNode(ExpressionToken token, ExpressionTreeNode leftNode, ExpressionTreeNode rightNode)
		{
			_token = token;
			_leftNode = leftNode;
			_rightNode = rightNode;
		}
示例#20
0
 public ExpressionTreeNode(ExpressionToken token, ExpressionTreeNode leftNode)
     : this(token, leftNode, null)
 {
 }
		private static void AddNode(Canvas canvas, NodePort parentInput, ExpressionTreeNode expressionNode, List<NumberParam> variables, Point location, Int32 levelStep, Int32 minY, Int32 maxY)
		{
			Int32 leafs = (expressionNode.LeftNode != null ? 1 : 0) + (expressionNode.RightNode != null ? 1 : 0);
			NodeBase node = null;

			if (expressionNode.Token.IsNumber)
			{
				node = new VisiTypes.Number(Double.Parse(expressionNode.Token.TokenBody));
				canvas.AddNode(node, location);
			}

			if (expressionNode.Token.IsVariable)
			{
				Boolean exist = false;
				foreach (NumberParam param in variables)
				{
					if (param.Name == expressionNode.Token.TokenBody)
					{
						exist = true;
						node = param;
						break;
					}
				}

				if (!exist)
				{
					node = new NumberParam();
					((NumberParam)node).Name = expressionNode.Token.TokenBody;
					((NumberParam)node).Header = expressionNode.Token.TokenBody;
					variables.Add((NumberParam)node);
					canvas.AddNode(node, location);
				}
			}

			if (expressionNode.Token.IsOperator)
			{
				node = new Operator(expressionNode.Token.OperatorDescriptor);
				canvas.AddNode(node, location);

				switch (leafs)
				{
					case 1:
					{
						AddNode(canvas, node.GetInput(0), expressionNode.LeftNode, variables, new Point(location.X - levelStep, location.Y), levelStep, minY, maxY);
						break;
					}
					case 2:
					{
						Int32 y2 = (maxY - minY) / 2;
						AddNode(canvas, node.GetInput(0), expressionNode.LeftNode, variables, new Point(location.X - levelStep, minY + y2 / 2), levelStep, minY, minY + y2);
						AddNode(canvas, node.GetInput(1), expressionNode.RightNode, variables, new Point(location.X - levelStep, maxY - y2 / 2), levelStep, maxY - y2, maxY);
						break;
					}
				}
			}

			if (parentInput != null && node != null)
			{
				parentInput.Connection = node.GetOutput(0);
			}
		}
示例#22
0
		// Math functions
		private ExpressionTreeNode ExprLevel5()
		{
			ExpressionTreeNode left = null;
			ExpressionTreeNode right = null;
			ExpressionTreeNode temp = null;

			if (CurrentToken.IsOneVariableFunction)
			{
				temp = new ExpressionTreeNode(CurrentToken, null, null);
				NextToken();

				if (!CurrentToken.IsLeftBracket)
				{
					throw new ExpressionSyntaxException(Resources.ExpressionSyntax_LeftBracketExpected, _expression, _parsedExpression);
				}

				NextToken();

				left = ExprLevel1();

				if (left == null)
				{
					throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
				}

				temp.LeftNode = left;
				
				if (!CurrentToken.IsRightBracket)
				{
					throw new ExpressionSyntaxException(Resources.ExpressionSyntax_RightBracketExpected, _expression, _parsedExpression);
				}

				NextToken();
			}
			else
			{
				if (CurrentToken.IsTwoVariableFunction)
				{
					temp = new ExpressionTreeNode(CurrentToken, null, null);
					NextToken();
					if (!CurrentToken.IsLeftBracket)
					{
						throw new ExpressionSyntaxException(Resources.ExpressionSyntax_LeftBracketExpected, _expression, _parsedExpression);
					}

					NextToken();

					left = ExprLevel1();
					if (left == null)
					{
						throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
					}

					temp.LeftNode = left;

					if (!CurrentToken.IsComma)
					{
						throw new ExpressionSyntaxException(Resources.ExpressionSyntax_CommaExpected, _expression, _parsedExpression);
					}

					NextToken();
					right = ExprLevel1();

					if (right == null)
					{
						throw new ExpressionSyntaxException(Resources.ExpressionSyntax_ExpressionExpected, _expression, _parsedExpression);
					}

					temp.RightNode = right;

					if (!CurrentToken.IsRightBracket)
					{
						throw new ExpressionSyntaxException(Resources.ExpressionSyntax_RightBracketExpected, _expression, _parsedExpression);
					}

					NextToken();
				}
				else
				{
					temp = ExprLevel6();
				}
			}

			return temp;
		}