Exemplo n.º 1
0
 public static BooleanExp Make(ref List <Token> .Enumerator polishNotationTokensEnumerator)
 {
     if (polishNotationTokensEnumerator.Current.type == Token.TokenType.LITERAL)
     {
         BooleanExp lit = new VariableExpression(Char.Parse(polishNotationTokensEnumerator.Current.value));
         Context.Assign(Char.Parse(polishNotationTokensEnumerator.Current.value), false);
         polishNotationTokensEnumerator.MoveNext();
         return(lit);
     }
     else
     {
         if (polishNotationTokensEnumerator.Current.value == "NOT")
         {
             polishNotationTokensEnumerator.MoveNext();
             BooleanExp operand = Make(ref polishNotationTokensEnumerator);
             return(new NotExpression(operand));
         }
         else if (polishNotationTokensEnumerator.Current.value == "AND")
         {
             polishNotationTokensEnumerator.MoveNext();
             BooleanExp left  = Make(ref polishNotationTokensEnumerator);
             BooleanExp right = Make(ref polishNotationTokensEnumerator);
             return(new AndExpression(left, right));
         }
         else if (polishNotationTokensEnumerator.Current.value == "OR")
         {
             polishNotationTokensEnumerator.MoveNext();
             BooleanExp left  = Make(ref polishNotationTokensEnumerator);
             BooleanExp right = Make(ref polishNotationTokensEnumerator);
             return(new OrExpression(left, right));
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Circuit.Reset();
            string       expr   = LogicalExpression.Text;
            List <Token> tokens = new List <Token>();
            StringReader reader = new StringReader(expr);

            try
            {
                //Tokenize the expression
                Token t = null;
                do
                {
                    t = new Token(reader);
                    tokens.Add(t);
                } while (t.type != Token.TokenType.EXPR_END);

                //Use a minimal version of the Shunting Yard algorithm to transform the token list to polish notation
                List <Token> polishNotation = MyParser.TransformToPolishNotation(tokens);

                var enumerator = polishNotation.GetEnumerator();
                enumerator.MoveNext();
                exp = MyParser.Make(ref enumerator);

                exp.Paint(ct);
                //LogicalExpression.Text = exp.GetStringExp();
                var result = exp.Evaluate(ct);
                Circuit.AddLamp();
                Circuit.SwitchLamp(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Gabim ne shprehje: {ex.Message}");
            }
        }
Exemplo n.º 3
0
        private int CalculateBoolean(BooleanExp exp, Dictionary <string, DefineExp> context)
        {
            Func <double, double, bool> compareFunction = exp.GetFunction();
            var left  = CalculateExp(exp.Children[0], context);
            var right = CalculateExp(exp.Children[1], context);

            return(compareFunction(left, right) ? 1 : 0);
        }
Exemplo n.º 4
0
 public QueryPharmacyLicenseModel()
 {
     Enabled          = new BooleanExp();
     StartDate        = new DateTimeRange();
     OutDate          = new DateTimeRange();
     IssuanceDate     = new DateTimeRange();
     Valid            = new BooleanExp();
     LicenseTypeValue = -1;
 }
Exemplo n.º 5
0
        public void CreateSyntaxTree()
        {
            string       expr   = @"(A&B)";
            List <Token> tokens = new List <Token>();
            StringReader reader = new StringReader(expr);

            //Tokenize the expression
            Token t = null;

            do
            {
                t = new Token(reader);
                tokens.Add(t);
            } while (t.type != Token.TokenType.EXPR_END);

            //Use a minimal version of the Shunting Yard algorithm to transform the token list to polish notation
            List <Token> polishNotation = MyParser.TransformToPolishNotation(tokens);

            var enumerator = polishNotation.GetEnumerator();

            enumerator.MoveNext();
            BooleanExp root = MyParser.Make(ref enumerator);
            var        str  = root.GetStringExp();
        }
Exemplo n.º 6
0
 public AndExp(BooleanExp exp1, BooleanExp exp2)
 {
     this.operand1 = exp1;
     this.operand2 = exp2;
 }
Exemplo n.º 7
0
        public static void Run()
        {
            bool    result;
            Context context;

            // (true and x)
            Console.WriteLine("Evaluating: (true and x) ...");
            VariableExp x = new VariableExp('X');

            BooleanExp expTrueAndX = new AndExp(new Constant(true), x);

            Console.WriteLine("Where x = true");
            context = new Context();
            context.Assign(x, true);

            result = expTrueAndX.Evaluate(context);
            Console.WriteLine("The answer should be 'True', and it evaluates as '{0}'\n", result);


            Console.WriteLine("Evaluating: (true and x) ...");
            Console.WriteLine("Where x = false");
            context = new Context();
            context.Assign(x, false);

            result = expTrueAndX.Evaluate(context);
            Console.WriteLine("The answer should be 'False', and it evaluates as '{0}'\n", result);


            // (not x)
            Console.WriteLine("Evaluating: (not x) ...");
            BooleanExp expNotX = new NotExp(x);

            Console.WriteLine("Where x = false");
            context = new Context();
            context.Assign(x, false);

            result = expNotX.Evaluate(context);
            Console.WriteLine("The answer should be 'True', and it evaluates as '{0}'\n", result);


            Console.WriteLine("Evaluating: (not x) ...");
            Console.WriteLine("Where x = true");
            context = new Context();
            context.Assign(x, true);

            result = expNotX.Evaluate(context);
            Console.WriteLine("The answer should be 'False', and it evaluates as '{0}'\n", result);


            // (y and (not x)
            Console.WriteLine("Evaluating: (y and (not x)) ...");
            VariableExp y = new VariableExp('Y');

            BooleanExp expYandNotX = new AndExp(y, new NotExp(x));

            Console.WriteLine("Where x = false, y = true.");
            context = new Context();
            context.Assign(x, false);
            context.Assign(y, true);

            result = expYandNotX.Evaluate(context);
            Console.WriteLine("The answer should be 'True', and it evaluates as '{0}'\n", result);


            // (true and x) or (y and (not x))
            Console.WriteLine("Evaluating: (true and x) or (y and (not x)) ...");
            BooleanExp expression = new OrExp(
                new AndExp(new Constant(true), x),
                new AndExp(y, new NotExp(x))
                );

            Console.WriteLine("Where x = false, y = true.");
            context = new Context();
            context.Assign(x, false);
            context.Assign(y, true);

            result = expression.Evaluate(context);
            Console.WriteLine("The answer should be 'True', and it evaluates as '{0}'\n", result);

            Console.WriteLine("Evaluating: (true and x) or (y and (not x)) ...");

            VariableExp z    = new VariableExp('Z');
            NotExp      notZ = new NotExp(z);

            Console.WriteLine("Where y is replaced with Not(z)");
            BooleanExp replacement = expression.Replace('Y', notZ);

            Console.WriteLine("Where x = false and z = true (making y = false)");
            context.Assign(z, true);
            result = replacement.Evaluate(context);
            Console.WriteLine("The answer should be 'False', and it evaluates as '{0}'\n", result);

            Console.WriteLine("Testing parser ...");
            Parser parser = new Parser();
            string input  = "";

            input = "(true and x) or (y and (not x)) where x = false, y = true";
            Console.WriteLine("Calculating: " + input);
            result = parser.ParseBlocks(input);
            Console.WriteLine("The answer should be 'True', and it evaluates as '{0}'\n", result);

            input = "(true and x) or (y and (not x)) where x = false, y = false";
            Console.WriteLine("Calculating: " + input);
            result = parser.ParseBlocks(input);
            Console.WriteLine("The answer should be 'False', and it evaluates as '{0}'\n", result);

            Console.WriteLine("User input test ...");
            string QUIT_CODE = "exit";

            Console.WriteLine("Enter '{0}' to stop.", QUIT_CODE);
            bool inSession = true;

            do
            {
                Console.Write("Define Operation: ");
                input = Console.ReadLine();
                if (input != QUIT_CODE)
                {
                    result = parser.ParseBlocks(input);
                    Console.WriteLine("Result is: '{0}'\n", result);
                }
                else
                {
                    inSession = false;
                }
            }while (inSession);
        }