From f744ac3a5089667afed01a602a4da4eeb4081b9f Mon Sep 17 00:00:00 2001 From: Felipe Arruda Pontes Date: Mon, 5 Jun 2017 19:41:24 -0300 Subject: [PATCH] starting the creation of the Consequent when parsing fcl refs #4 --- scikit_fuzzy_fcl/fcl_listener.py | 12 +++++++++++- tests/test_listener.py | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/scikit_fuzzy_fcl/fcl_listener.py b/scikit_fuzzy_fcl/fcl_listener.py index 8a04f7e..b094ebe 100644 --- a/scikit_fuzzy_fcl/fcl_listener.py +++ b/scikit_fuzzy_fcl/fcl_listener.py @@ -5,7 +5,7 @@ import numpy as np from skfuzzy.control.controlsystem import ControlSystem -from skfuzzy.control.antecedent_consequent import Antecedent +from skfuzzy.control.antecedent_consequent import Antecedent, Consequent from .fcl_parser import FclParserException @@ -65,6 +65,7 @@ def __init__(self): self.control_system = None self.vars = {} self.antecedents = {} + self.consequents = {} self.num_steps = 100 def visitErrorNode(self, node): @@ -93,6 +94,15 @@ def enterFuzzify_block(self, ctx): 'terms': OrderedDict(), } + def enterDefuzzify_block(self, ctx): + label = ctx.ID().getText() + # if variable already defined a range then use this to instantiate the consequent + universe_range = self.vars.get(label, {}).get('range', None) + self.consequents[label] = { + 'value': Consequent(universe_range, label=label), + 'terms': OrderedDict(), + } + def getLinguistic_term_var_dict(self, linguistic_term_ctx): var_dict = None if hasattr(linguistic_term_ctx.parentCtx, 'ID'): diff --git a/tests/test_listener.py b/tests/test_listener.py index e926436..79695c1 100644 --- a/tests/test_listener.py +++ b/tests/test_listener.py @@ -345,3 +345,22 @@ def test_antecedents_terms_have_correct_mf_values_using_singleton_and_piecewise( 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) + + def test_consequents_created_when_defuzzify_block_described(self): + fcl_text = """ + FUNCTION_BLOCK my_system + DEFUZZIFY consequent1 + 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)