Exemplo n.º 1
0
    def test_rule_if_clause_condition_if_clause_with_and(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            RULEBLOCK rule1
                RULE first_rule : IF something AND otherthing THEN conclusion IS final;
            END_RULEBLOCK
        END_FUNCTION_BLOCK
        """

        class FclListenerRules(FclListener):
            def enterIf_clause(_self, ctx):
                condition = ctx.condition()
                something = condition.getChild(0).getText()
                operator = condition.getChild(1).getText()
                otherthing = condition.getChild(2).getText()
                self.assertEqual(something, 'something')
                self.assertEqual(operator, 'AND')
                self.assertEqual(otherthing, 'otherthing')

        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = FclListenerRules()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
Exemplo n.º 2
0
    def test_rule_if_clause_condition_then_clause_with_x(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            RULEBLOCK rule1
                RULE first_rule : IF something AND otherthing THEN final IS final2 WITH 123;
            END_RULEBLOCK
        END_FUNCTION_BLOCK
        """

        class FclListenerRules(FclListener):
            def enterThen_clause(_self, ctx):
                conclusion = ctx.conclusion()
                subconclusion = conclusion.sub_conclusion()[0]
                final = subconclusion.ID()[0].getText()
                final2 = subconclusion.ID()[1].getText()
                self.assertEqual(final, 'final')
                self.assertEqual(final2, 'final2')

            def enterWith_x(_self, ctx):
                real = ctx.REAL().getText()

                self.assertEqual(real, '123')

        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = FclListenerRules()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
Exemplo n.º 3
0
    def test_consequent_define_universe_override_range_defined_in_var_if_defined_in_consequent(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            VAR_output
                consequent1 : REAL (1 .. 9);
            END_VAR
            DEFUZZIFY consequent1
                RANGE := (0 .. 30);
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        consequents = listener.consequents
        expected_universe = np.asarray([0., 30.])
        self.assertIn('consequent1', consequents)
        self.assertEqual('consequent1', consequents.get('consequent1').get('value').label)
        np.testing.assert_array_equal(expected_universe, consequents.get('consequent1').get('value').universe)
Exemplo n.º 4
0
    def test_antecedents_terms_have_correct_mf_values_using_singleton_and_piecewise(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            FUZZIFY antecedent1
                TERM mf1 := 4.0;
                TERM mf2 := (0, 0.2) (2, 0) (3, 1);
                TERM mf3 := 1.0;
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        antecedent = listener.antecedents.get('antecedent1').get('value')
        term = antecedent['mf1']
        expected_mf_value = np.asarray([0, 0, 0, 0, 1])  # fx[0], fx[1], fx[2], fx[3], f[4]
        np.testing.assert_array_equal(expected_mf_value, term.mf)

        term = antecedent['mf2']
        expected_mf_value = np.asarray([0.2, 0.1, 0, 1, 0])  # fx[0], fx[1], fx[2], fx[3], f[4]
        np.testing.assert_array_equal(expected_mf_value, term.mf)

        term = antecedent['mf3']
        expected_mf_value = np.asarray([0, 1, 0, 0, 0])  # fx[0], fx[1], fx[2], fx[3], f[4]
        np.testing.assert_array_equal(expected_mf_value, term.mf)
Exemplo n.º 5
0
    def test_var_input_and_output(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            VAR_INPUT
                input_id1 : REAL;
            END_VAR
            VAR_OUTPUT
                output_id1 : REAL;
            END_VAR
            VAR_INPUT
                input_id2 : REAL;
            END_VAR
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual(['output_id1', 'REAL'], listener.outputs[0])
        self.assertEqual(['input_id1', 'REAL'], listener.inputs[0])
        self.assertEqual(['input_id2', 'REAL'], listener.inputs[1])
Exemplo n.º 6
0
    def test_antecedents_terms_have_correct_mf_values_with_more_then_one_term(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            FUZZIFY antecedent1
                TERM mf1 := (0, 1) (0.5, 0);
                TERM mf2 := (1, 0.3) (2, 0) (3, 1);
                TERM mf3 := (2, 0.4) (4, 1) (5, 1);
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        antecedent = listener.antecedents.get('antecedent1').get('value')
        term = antecedent['mf1']
        expected_mf_value = np.asarray([1, 0, 0, 0, 0, 0, 0])
        np.testing.assert_array_equal(expected_mf_value, term.mf)

        term2 = antecedent['mf2']
        expected_mf_value = np.asarray([0, 0, 0.3, 0, 1, 0, 0])
        np.testing.assert_array_equal(expected_mf_value, term2.mf)

        term3 = antecedent['mf3']
        expected_mf_value = np.asarray([0, 0, 0, 0.4, 0.7, 1, 1])
        np.testing.assert_array_equal(expected_mf_value, term3.mf)
Exemplo n.º 7
0
    def test_should_raise_parser_exception_if_incorrect_fcl_text(self):
        fcl_text = "INCORRECT FCL TEXT"
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        with self.assertRaises(FclParserException):
            walker.walk(listener, tree)
Exemplo n.º 8
0
    def test_load_simple_function_block2(self):
        fcl_text = """
        FUNCTION_BLOCK
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual([], listener.function_blocks_ids)
Exemplo n.º 9
0
    def test_should_create_empyt_control_system_if_no_declaration(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        control_system = listener.control_system
        self.assertIsNot(control_system, None)
Exemplo n.º 10
0
    def test_rule_block(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            RULEBLOCK rule1
            END_RULEBLOCK
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual('rule1', listener.last_rule_block.get('id'))
Exemplo n.º 11
0
    def test_defuzzify_block_with_defuzzification_method(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            DEFUZZIFY defuzz_id
                METHOD : COG;
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual('COG', listener.last_deffuz_method)
Exemplo n.º 12
0
    def test_singletons(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            FUZZIFY fuzzyfy_id
                TERM sing := SINGLETONS (0, 1) (1, 2);
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual([['0', '1'], ['1', '2']], listener.last_singletons)
Exemplo n.º 13
0
    def test_piece_wise_linear(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            FUZZIFY fuzzyfy_id
                TERM term1 := (0, 1);
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual([['0', '1']], listener.last_piece_wise_liner)
Exemplo n.º 14
0
    def test_var_inputs_if_var_input_empty(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            VAR_INPUT
            END_VAR
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        loaded_vars = listener.vars
        self.assertEqual(loaded_vars, {})
Exemplo n.º 15
0
    def test_var_input_range(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            VAR_INPUT
                input_id : REAL ( 12 .. 34 );
            END_VAR
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual(['input_id', 'REAL', '12', '34'], listener.inputs[0])
Exemplo n.º 16
0
    def test_defuzzify_block_with_default_value_nc(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            DEFUZZIFY defuzz_id
                DEFAULT := NC;
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual('defuzz_id', listener.last_defuzz.get('id'))
        self.assertEqual('NC', listener.last_default_value)
Exemplo n.º 17
0
    def test_antecedents_created_when_fuzzify_block_described(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            FUZZIFY antecedent1
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        antecedents = listener.antecedents
        self.assertIn('antecedent1', antecedents)
        self.assertEqual('antecedent1', antecedents.get('antecedent1').get('value').label)
Exemplo n.º 18
0
    def test_tipper_fcl_file(self):
        fcl_text = ""
        fcl_file_path = FIXTURES_DIR.child('tipper.fcl')
        with open(fcl_file_path, 'r') as fcl_file:
            fcl_text = fcl_file.read()

        class FclListenerNoError(FclListener):
            def visitErrorNode(self, node):
                raise FclParserException(node)

        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = FclListenerNoError()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
Exemplo n.º 19
0
    def test_add_vars_if_var_input_simple(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            VAR_INPUT
                var1 : REAL;
            END_VAR
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        loaded_vars = listener.vars
        self.assertEqual(loaded_vars, {'var1': {'type': 'REAL', 'range': None}})
Exemplo n.º 20
0
    def test_linguistic_term(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            FUZZIFY fuzzyfy_id
                TERM term1 := mf ;
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        fb = listener.fuzzyfy_blocks[0]
        ling_term = fb.get('linguistic_term')[0]

        self.assertEqual('term1', ling_term.get('id'))
Exemplo n.º 21
0
 def test_rule(self):
     fcl_text = """
     FUNCTION_BLOCK f_block
         RULEBLOCK rule1
             RULE first_rule : IF something THEN finalthing IS conclusion;
             RULE 2 : IF something THEN finalthing IS conclusion;
         END_RULEBLOCK
     END_FUNCTION_BLOCK
     """
     # RULE first_rule : IF something IS otherthing OR something2 IS otherthing2 THEN finalthing IS conclusion;
     lexer = FclLexer(InputStream(fcl_text))
     stream = CommonTokenStream(lexer)
     parser = FclParser(stream)
     tree = parser.main()
     listener = FclListenerTester()
     walker = ParseTreeWalker()
     walker.walk(listener, tree)
     self.assertEqual('first_rule', listener.rules[0].get('id'))
     self.assertEqual('2', listener.rules[1].get('id'))
Exemplo n.º 22
0
    def test_defuzzify_block_with_range(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            DEFUZZIFY defuzz_id
                RANGE := ( 12 .. 34 );
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual('defuzz_id', listener.last_defuzz.get('id'))
        self.assertEqual(['RANGE:=(12..34);'],
                         listener.last_defuzz.get('items'))
Exemplo n.º 23
0
    def test_antecedents_universe_has_correct_value_based_on_term(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            FUZZIFY antecedent1
                TERM mf1 := (0, 1) (0.5, 0);
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        antecedent = listener.antecedents.get('antecedent1').get('value')
        expected_universe = np.asarray([0, 0.5])
        np.testing.assert_array_equal(expected_universe, antecedent.universe)
Exemplo n.º 24
0
    def test_consequents_defuzzify_method_mom_as_mom(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            DEFUZZIFY consequent1
                METHOD : MOM;
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        consequents = listener.consequents
        self.assertIn('consequent1', consequents)
        self.assertEqual('consequent1', consequents.get('consequent1').get('value').label)
        self.assertEqual('mom', consequents.get('consequent1').get('value').defuzzify_method)
Exemplo n.º 25
0
    def test_antecedents_terms_have_correct_mf_values_using_singleton(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            FUZZIFY antecedent1
                TERM mf1 := 1.0;
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        antecedent = listener.antecedents.get('antecedent1').get('value')
        term = antecedent['mf1']
        expected_mf_value = np.asarray([1])
        np.testing.assert_array_equal(expected_mf_value, term.mf)
Exemplo n.º 26
0
    def test_consequents_term_defined_if_present(self):
        fcl_text = """
        FUNCTION_BLOCK my_system
            DEFUZZIFY consequent1
                TERM mf1 := (0, 1) (1, 1);
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = ScikitFuzzyFclListener()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
        consequents = listener.consequents
        self.assertIn('consequent1', consequents)
        self.assertEqual('consequent1', consequents.get('consequent1').get('value').label)
        self.assertIn('mf1', consequents.get('consequent1').get('value').terms)
        self.assertIn('mf1', consequents.get('consequent1').get('value').terms.get('mf1').label)
Exemplo n.º 27
0
    def test_defuzzify_block_with_linguistic_term(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            DEFUZZIFY defuzz_id
                TERM term1 := (0,0) (5,1) (10,0);
                TERM term2 := (1,2);
            END_DEFUZZIFY
        END_FUNCTION_BLOCK
        """
        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()
        listener = FclListenerTester()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)

        self.assertEqual('defuzz_id', listener.last_defuzz.get('id'))
        self.assertEqual('term1', listener.last_linguistic_terms[0].get('id'))
        self.assertEqual([['1', '2']], listener.last_piece_wise_liner)
        self.assertEqual('term2', listener.last_linguistic_terms[1].get('id'))
Exemplo n.º 28
0
    def test_if_clause_complex(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            RULEBLOCK rule1
                RULE first_rule : IF (something IS NOT otherthing) THEN final IS final2;
            END_RULEBLOCK
        END_FUNCTION_BLOCK
        """

        class FclListenerRules(FclListener):
            def enterSubcondition_paren(_self, ctx):
                condition = ctx.condition().getText()
                self.assertEqual(condition, 'somethingISNOTotherthing')

        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = FclListenerRules()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
Exemplo n.º 29
0
    def test_mf_cosine(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            FUZZIFY service
                TERM mf := COSINE 123 345;
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """

        class FclListenerRules(FclListener):
            def enterCosine(_self, ctx):
                cosine_args = [arg.getText() for arg in ctx.atom()]
                self.assertEqual(cosine_args, ['123', '345'])

        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = FclListenerRules()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)
Exemplo n.º 30
0
    def test_mf_gbell(self):
        fcl_text = """
        FUNCTION_BLOCK f_block
            FUZZIFY service
                TERM mf := GBELL a b c;
            END_FUZZIFY
        END_FUNCTION_BLOCK
        """

        class FclListenerRules(FclListener):
            def enterGbell(_self, ctx):
                args = [arg.getText() for arg in ctx.atom()]
                self.assertEqual(args, ['a', 'b', 'c'])

        lexer = FclLexer(InputStream(fcl_text))
        stream = CommonTokenStream(lexer)
        parser = FclParser(stream)
        tree = parser.main()

        listener = FclListenerRules()
        walker = ParseTreeWalker()
        walker.walk(listener, tree)