Пример #1
0
 def test_complex2(self):
     context = InterpretterContext()
     statement = BooleanAndExpression(
         EqualsExpression(LiteralExpression(9), LiteralExpression(9)),
         GreaterThanExpression(LiteralExpression(5), LiteralExpression(9)))
     statement.interpret(context)
     self.assertEqual(context.lookup(statement), False)
Пример #2
0
 def test_with_logical_or_true(self):
     context = InterpretterContext()
     statement = self.__get_simple_boolean_or(True)
     statement.interpret(context)
     self.assertEqual(context.lookup(statement), True)
     statement = self.__get_simple_boolean_or(False)
     statement.interpret(context)
     self.assertEqual(context.lookup(statement), False)
Пример #3
0
    def interpret(self, context: InterpretterContext):
        self.__reference.interpret(context)
        self.__left_operator.interpret(context)
        self.__right_operator.interpret(context)
        reference = context.lookup(self.__reference)
        left_result = context.lookup(self.__left_operator)
        right_result = context.lookup(self.__right_operator)

        context.set(self, left_result <= reference <= right_result)
Пример #4
0
 def test_between(self):
     context = InterpretterContext()
     statement = BetweenExpression(LiteralExpression(9),
                                   VariableExpression("test1", 1),
                                   LiteralExpression(10))
     statement.interpret(context)
     self.assertEqual(context.lookup(statement), True)
     statement = BetweenExpression(LiteralExpression(9),
                                   VariableExpression("test1", 1),
                                   LiteralExpression(8))
     statement.interpret(context)
     self.assertEqual(context.lookup(statement), False)
Пример #5
0
 def test_complex1(self):
     context = InterpretterContext()
     statement = BooleanAndExpression(
         BooleanOrExpression(
             GreaterThanExpression(LiteralExpression(5),
                                   LiteralExpression(9)),
             EqualsExpression(VariableExpression("test1", 150),
                              VariableExpression("test2", 150))),
         LessThanExpression(LiteralExpression(9),
                            VariableExpression("test3", 10)))
     statement.interpret(context)
     self.assertEqual(context.lookup(statement), True)
Пример #6
0
    def __check_rule(self, rule: str) -> bool:
        context = InterpretterContext()
        expression_builder = ExpressionBuilder(self.__tokenizer,
                                               self.__logging)
        expression_builder.set_text(rule)
        try:
            expression_builder.build()
        except Exception as e:
            self.__logging.debug('Error building rule: {0}'.format(e.message))
            return False
        statement = expression_builder.get_expression()
        statement.interpret(context)

        return context.lookup(statement)
Пример #7
0
    def __check_rule(self, rule: Rule) -> bool:
        if self.__rule_lock.has_lock(rule):
            return False
        context = InterpretterContext()
        expression_builder = ExpressionBuilder(self.__tokenizer,
                                               self.__logging)
        expression_builder.set_text(rule.text)
        try:
            expression_builder.build()
        except Exception as e:
            self.__logging.error('Error building rule: {0}'.format(str(e)))
            return False
        statement = expression_builder.get_expression()
        statement.interpret(context)

        return context.lookup(statement)
Пример #8
0
    def test_elaborate_expression(self):
        rule = "and  ( eq(S[phoneIsHome:False], True), or (gt(TIME, 08:52)), eq(A[homeAlarm, On], True) )"
        buildt_statement = self.__get_expresion_builder(rule)
        expected_statement = BooleanAndExpression(
            EqualsExpression(LiteralExpression(False), LiteralExpression(True)),
            BooleanOrExpression(
            GreaterThanExpression(LiteralExpression('08:49'), LiteralExpression('08:52')),
                EqualsExpression(LiteralExpression(True), LiteralExpression(True))
            )
        )

        context = InterpretterContext()
        expected_statement.interpret(context)
        self.assertEqual(buildt_statement, expected_statement)
Пример #9
0
 def do_interpret(self, context: InterpretterContext, left_result, right_result) -> None:
     context.set(self, left_result and right_result)
Пример #10
0
 def do_interpret(self, context: InterpretterContext, left_result,
                  right_result):
     context.set(self, left_result == right_result)
Пример #11
0
 def interpret(self, context: InterpretterContext):
     self.__left_operator.interpret(context)
     self.__right_operator.interpret(context)
     left_result = context.lookup(self.__left_operator)
     right_result = context.lookup(self.__right_operator)
     self.do_interpret(context, left_result, right_result)
Пример #12
0
 def interpret(self, context: InterpretterContext):
     context.set(self, self.__value)