Пример #1
0
        public void ConstraintInfoTest()
        {
            IBooleanOperator trueExpression = new GreaterThanOperator(Three, Two);
            var trueTree      = new BooleanExpressionTree("true", trueExpression);
            var constraintOne = new ConstraintInfo(trueTree);

            Console.Write("Created constraint: ");
            Console.WriteLine(constraintOne);
            Console.WriteLine("ConstraintInfoTest: testing name of anonymous constraint...");
            Expect(constraintOne.Name == string.Empty);
            Console.WriteLine("ConstraintInfoTest: testing Predicate property (constraintOne)...");
            Expect(constraintOne.Predicate == trueTree);

            const string     constraintName  = "ConstraintTwo";
            IBooleanOperator falseExpression = new LessThanOperator(Three, Two);
            var falseTree     = new BooleanExpressionTree("false", falseExpression);
            var constraintTwo = new ConstraintInfo(constraintName, falseTree);

            Console.Write("Created constraint: ");
            Console.WriteLine(constraintTwo);
            Console.WriteLine("ConstraintInfoTest: testing Name of named constraint...");
            Expect(constraintTwo.Name == constraintName);
            Console.WriteLine("ConstraintInfoTest: testing Predicate property (constraintTwo)...");
            Expect(constraintTwo.Predicate == falseTree);
        }
Пример #2
0
        public void Format_WhenNotInclusive_ExpectNotInclusiveQuery()
        {
            // Arrange
            var lessThanFormatter = new LessThanFormatter();

            var node = new LessThanOperator
            {
                Name      = "ColumnName",
                Value     = 23,
                Inclusive = false,
                Statement = "ColumnName:[23,*]",
            };

            // Act
            var stopwatch       = Stopwatch.StartNew();
            var sqlDataResponse = lessThanFormatter.Format(node, 0);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(sqlDataResponse.Sql, Is.EqualTo("ColumnName < @ColumnName0"));
            Assert.That(sqlDataResponse.Params.First().Value, Is.EqualTo(23));
        }
Пример #3
0
 public void Visit(LessThanOperator node)
 {
     builder.AppendLine($"LessThan");
     builder.Indent();
     node.Left.Accept(this);
     node.Right.Accept(this);
     builder.Outdent();
 }
Пример #4
0
        public void Visit(LessThanOperator node)
        {
            VisitBinary(node);

            ITerminalWrapper leftWrapper  = ReferenceTables.GetValue(node.Left);
            ITerminalWrapper rightWrapper = ReferenceTables.GetValue(node.Right);

            ReferenceTables.SetValue(node, ((dynamic)leftWrapper) < ((dynamic)rightWrapper));
        }
Пример #5
0
        public void LessThanOperator_ShouldPrint()
        {
            var ast = new LessThanOperator
                      (
                position: 0,
                left: new Identifier(0, "x"),
                right: new Identifier(0, "y")
                      );

            Assert.That(PrettyPrinter.ToString(ast), Is.EqualTo(
                            @"LessThan
    Identifier(x)
    Identifier(y)
"));
        }
Пример #6
0
        public void ExitComparison(QueryParser.ComparisonContext context)
        {
            var opToken = context.REL_OP();

            if (opToken == null)
            {
                return;
            }

            var op    = opToken.Symbol;
            var right = _expressions.Pop();
            var left  = _expressions.Pop();

            BinaryOperatorExpression value = null;

            switch (op.Text)
            {
            case "<":
                value = new LessThanOperator(op.Line, op.Column, left, right);
                break;

            case "<=":
                value = new LessThanOrEqualOperator(op.Line, op.Column, left, right);
                break;

            case "<>":
            case "!=":
                value = new NotEqualOperator(op.Line, op.Column, left, right);
                break;

            case "==":
            case "=":
                value = new EqualOperator(op.Line, op.Column, left, right);
                break;

            case ">":
                value = new GreaterThanOperator(op.Line, op.Column, left, right);
                break;

            case ">=":
                value = new GreaterThanOrEqualOperator(op.Line, op.Column, left, right);
                break;
            }

            Trace.Assert(value != null, "Invalid comparison operator " + op.Text);

            _expressions.Push(value);
        }
Пример #7
0
        public void CreateExpressionTest1()
        {
            // Arrange
            Expression parameterExpression = Expression.Parameter(typeof(Employee), "sample");
            Expression expressionProperty  = Expression.Property(parameterExpression, nameof(Employee.Age));
            Expression expressionConstant  = Expression.Constant(20);

            Expression expectedExpression = Expression.LessThan(expressionProperty, expressionConstant);

            var _operator = new LessThanOperator();

            // Act
            Expression resultExpression = _operator.CreateExpression(expressionProperty, expressionConstant);

            // Assert
            Assert.AreEqual(expectedExpression.ToString(), resultExpression.ToString(), "Expression not correct");
        }
        public void Visit(LessThanOperator lessThan)
        {
            lessThan.Left.Accept(this);
            var leftOperand = tacs.Last().Result;

            lessThan.Right.Accept(this);
            var rightOperand = tacs.Last().Result;
            var result       = MakeNewTemp();
            var label1       = MakeNewLabel();
            var label2       = MakeNewLabel();

            tacs.Add(Tac.IfLessThan(leftOperand, rightOperand, label1));
            tacs.Add(Tac.Assign("0", result));
            tacs.Add(Tac.Goto(label2));
            tacs.Add(Tac.Label(label1, result));
            tacs.Add(Tac.Assign("1", result));
            tacs.Add(Tac.Label(label2, result));
        }
Пример #9
0
        private static ExpTreeNode GetAsExpressionTree(this ParseTreeNode parsedNode, ExpTreeNode parentExpNode)
        {
            //The current node is a variable / value token. Create a value node and return it back
            if (!parsedNode.ChildNodes.Any())
            {
                switch (parsedNode.Term.Name)
                {
                case "variable":
                {
                    var varName = parsedNode.FindTokenAndGetValue <string>();
                    return(new VariableValue(varName, parentExpNode));
                }

                case "boolean":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <bool>(), parentExpNode));

                case "integer":
                case "decimal":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <double>(), parentExpNode));

                case "SingleQuoteString":
                case "DoubleQuoteString":
                    return(new LiteralValue(parsedNode.FindTokenAndGetValue <string>(), parentExpNode));

                default:
                    throw parsedNode.GetException($"Invalid token type '{parsedNode.Term.Name}' in expression");
                }
            }

            // Look on the next node down
            else if (parsedNode.ChildNodes.Count == 1)
            {
                return(GetAsExpressionTree(parsedNode.ChildNodes[0], parentExpNode));
            }

            //Ignore parenthesis, the middle non-terminal is what we want
            // Look on the next node down
            else if (parsedNode.ChildNodes.Count == 3 && parsedNode.ChildNodes[0]?.Token?.Text == "(")
            {
                return(GetAsExpressionTree(parsedNode.ChildNodes[1], parentExpNode));
            }

            //Binary operator
            else if (parsedNode.ChildNodes.Count == 3)
            {
                BinaryOperatorBase binaryOp;
                var opStr = parsedNode.ChildNodes[1].FindToken().ValueString;
                switch (opStr)
                {
                // Math
                case "+":
                    binaryOp = new AdditionOperator(parentExpNode);
                    break;

                case "-":
                    binaryOp = new SubtractionOperator(parentExpNode);
                    break;

                case "*":
                    binaryOp = new MultiplacationOperator(parentExpNode);
                    break;

                case "/":
                    binaryOp = new DivisionOperator(parentExpNode);
                    break;

                case "%":
                    binaryOp = new ModulusOperator(parentExpNode);
                    break;

                case "^":
                    binaryOp = new PowerOperator(parentExpNode);
                    break;

                // Bool
                case "~==":
                    binaryOp = new LooseEqualsOperator(parentExpNode);
                    break;

                case "~!=":
                    binaryOp = new LooseNotEqualsOperator(parentExpNode);
                    break;

                case "==":
                    binaryOp = new EqualsOperator(parentExpNode);
                    break;

                case "!=":
                    binaryOp = new NotEqualsOperator(parentExpNode);
                    break;

                case "like":
                    binaryOp = new LikeOperator(parentExpNode);
                    break;

                case "match":
                    binaryOp = new MatchOperator(parentExpNode);
                    break;

                case ">":
                    binaryOp = new GreaterThanOperator(parentExpNode);
                    break;

                case ">=":
                    binaryOp = new GreaterThanOrEqualOperator(parentExpNode);
                    break;

                case "<":
                    binaryOp = new LessThanOperator(parentExpNode);
                    break;

                case "<=":
                    binaryOp = new LessThanOrEqualOperator(parentExpNode);
                    break;

                case "&&":
                    binaryOp = new AndOperator(parentExpNode);
                    break;

                case "||":
                    binaryOp = new OrOperator(parentExpNode);
                    break;

                default:
                    throw parsedNode.ChildNodes[1].GetException($"Unrecognized operator '{opStr}'");
                }

                binaryOp.LeftOperand  = GetAsExpressionTree(parsedNode.ChildNodes[0], binaryOp);
                binaryOp.RightOperand = GetAsExpressionTree(parsedNode.ChildNodes[2], binaryOp);

                //Optimize
                if (binaryOp.LeftOperand is LiteralValue && binaryOp.RightOperand is LiteralValue)
                {
                    return(new LiteralValue(binaryOp.Eval(), parentExpNode));
                }

                return(binaryOp);
            }

            // Unary operator
            else if (parsedNode.ChildNodes.Count == 2)
            {
                var opVal = parsedNode.ChildNodes[0].FindToken().Value;
                UnaryOperatorBase unaryOp;
                if (parsedNode.ChildNodes[0].FindToken().Value is TypeCode convertType)
                {
                    unaryOp = new TypeConversionOperator(convertType, parentExpNode);
                }
                else
                {
                    var opStr = opVal.ToString();
                    switch (opStr)
                    {
                    case "!":
                        unaryOp = new NotOperator(parentExpNode);
                        break;

                    //Property Checking
                    case "lengthof":
                        unaryOp = new LengthOperator(parentExpNode);
                        break;

                    case "typeof":
                        unaryOp = new TypeOperator(parentExpNode);
                        break;

                    default:
                        unaryOp = new GenericUnaryMathFunctionOperator(opStr, parentExpNode);
                        break;
                    }
                }

                unaryOp.Operand = GetAsExpressionTree(parsedNode.ChildNodes[1], unaryOp);

                //Optimize
                if (unaryOp.Operand is LiteralValue)
                {
                    return(new LiteralValue(unaryOp.Eval(), parentExpNode));
                }

                return(unaryOp);
            }

            else
            {
                throw parsedNode.GetException($"Invalid number of tokens ({parsedNode.ChildNodes.Count})");
            }
        }
Пример #10
0
 public void Visit(LessThanOperator node)
 {
 }
Пример #11
0
 public Bitmap Visit(LessThanOperator expr)
 {
     return(ComputeFunction(expr));
 }
 public virtual void Visit(LessThanOperator binaryOperator)
 {
     Visit(binaryOperator.Left);
     Visit(binaryOperator.Right);
 }
Пример #13
0
 public override void Visit(LessThanOperator binaryOperator)
 {
     WriteBinaryExpression(binaryOperator, "<");
 }
 public override void Visit(LessThanOperator binaryOperator)
 {
     Visit(binaryOperator.Left);
     Write(" < ");
     Visit(binaryOperator.Right);
 }
Пример #15
0
        public void ModelInfoTest()
        {
            const string modelName = "test model";
            var          builder   = new ModelInfo.ModelBuilder(modelName);

            const string constraintName          = "test constraint";
            const string constraintPredicateName = "constraint predicate";
            var          constraintExpression    = new EqualToOperator(Two, Two);
            var          constraintPredicate     = new BooleanExpressionTree(constraintPredicateName, constraintExpression);
            var          constraint = new ConstraintInfo(constraintName, constraintPredicate);

            builder.AddConstraint(constraint);

/*
 *          const string eventName = "test event";
 *          const string eventPredicateName = "event predicate";
 *          var eventExpression = new GreaterThanOperator(Three, Two);
 *          var eventPredicate = new BooleanExpressionTree(eventPredicateName, eventExpression);
 *          int eventCount = 0;
 *          var testEvent = new EventInfo(eventName, eventPredicate, () => { eventCount++; });
 *          builder.AddEvent(testEvent);
 */

            const string expressionName = "test expression";
            var          expression     = new AddOperator(Three, Two);
            var          expressionTree = new NumericExpressionTree(expressionName, expression);

            builder.AddExpression(expressionTree);

            const string localeName = "test locale";
            var          locale     = new LocaleInfo(localeName);

            builder.AddLocale(locale);

            const string observableName           = "test observable";
            const string observableExpressionName = "observable expression";
            var          rootOperator             = new PowerOperator(Two, Three);
            var          observableExpressionTree = new NumericExpressionTree(observableExpressionName, rootOperator);
            var          observable = new ObservableInfo(observableName, observableExpressionTree);

            builder.AddObservable(observable);

            const string parameterName = "test parameter";
            var          parameter     = new ParameterInfo(parameterName, 3.14159265f);

            builder.AddParameter(parameter);

            const string predicateName       = "test predicate";
            var          predicateExpression = new LessThanOperator(Two, Three);
            var          predicate           = new BooleanExpressionTree(predicateName, predicateExpression);

            builder.AddPredicate(predicate);

            const string speciesName = "reactant";
            var          reactant    = new SpeciesDescription(speciesName, 2012, locale);

            builder.AddSpecies(reactant);

            const string reactionName = "test reaction";
            var          rBuilder     = new ReactionInfo.ReactionBuilder(reactionName);

            rBuilder.AddReactant(reactant);
            var rateExpression = new MultiplyOperator(Ten, new SymbolReference(speciesName));
            var reactionRate   = new NumericExpressionTree(null, rateExpression);

            rBuilder.SetRate(reactionRate);
            var reaction = rBuilder.Reaction;

            builder.AddReaction(reaction);

            var modelInfo = builder.Model;

            Console.WriteLine("ModelInfoTests: checking model name...");
            Expect(modelInfo.Name == modelName);
            Console.WriteLine("ModelInfoTests: checking constraint count (1)...");
            Expect(modelInfo.Constraints.Count() == 1);
            Console.WriteLine("ModelInfoTests: checking constraint...");
            Expect(modelInfo.Constraints.First() == constraint);

/*
 *          Console.WriteLine("ModelInfoTests: checking event count (1)...");
 *          Expect(modelInfo.Events.Count() == 1);
 *          Console.WriteLine("ModelInfoTests: checking event...");
 *          Expect(modelInfo.Events.First() == testEvent);
 */
            Console.WriteLine("ModelInfoTests: checking expression count (3)...");
            Expect(modelInfo.Expressions.Count() == 3);
            Console.WriteLine("ModelInfoTests: checking expressions...");
            var expressions = modelInfo.Expressions.Where(net => net.Name == expressionName).ToList();

            Expect(expressions.Count == 1);
            Expect(expressions[0] == expressionTree);
            Console.WriteLine("ModelInfoTests: checking locale count (1)...");
            Expect(modelInfo.Locales.Count() == 1);
            Console.WriteLine("ModelInfoTests: checking locale...");
            Expect(modelInfo.Locales.First() == locale);
            Console.WriteLine("ModelInfoTests: checking observable count (1)...");
            Expect(modelInfo.Observables.Count() == 1);
            Console.WriteLine("ModelInfoTests: checking observable...");
            Expect(modelInfo.Observables.First() == observable);
            Console.WriteLine("ModelInfoTests: checking parameter count (1)...");
            Expect(modelInfo.Parameters.Count() == 1);
            Console.WriteLine("ModelInfoTests: checking parameter...");
            Expect(modelInfo.Parameters.First() == parameter);

/*
 *          Console.WriteLine("ModelInfoTests: checking predicate count (3)...");
 *          Expect(modelInfo.Predicates.Count() == 3);
 */
            Console.WriteLine("ModelInfoTests: checking predicate count (2)...");
            Expect(modelInfo.Predicates.Count() == 2);
            Console.WriteLine("ModelInfoTests: checking predicate...");
            var predicates = modelInfo.Predicates.Where(pi => pi.Name == predicateName).ToList();

            Expect(predicates.Count == 1);
            Expect(predicates[0] == predicate);
            Console.WriteLine("ModelInfoTests: checking reaction count (1)...");
            Expect(modelInfo.Reactions.Count() == 1);
            Console.WriteLine("ModelInfoTests: checking reaction...");
            Expect(modelInfo.Reactions.First() == reaction);
            Console.WriteLine("ModelInfoTests: checking species count (1)...");
            Expect(modelInfo.Species.Count() == 1);
            Console.WriteLine("ModelInfoTests: checking species...");
            Expect(modelInfo.Species.First() == reactant);
        }
Пример #16
0
 public void Visit(LessThanOperator node)
 {
     VisitBinary(node);
     CheckOperandsAreOfSameType(node);
 }