Skip to content

Commit

Permalink
starting the creation of the Consequent when parsing fcl refs #4
Browse files Browse the repository at this point in the history
  • Loading branch information
arruda committed Jun 5, 2017
1 parent 5999914 commit f744ac3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scikit_fuzzy_fcl/fcl_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f744ac3

Please sign in to comment.