From fae1ba3c71e2f5219f40e062102956da8dfa9a1f Mon Sep 17 00:00:00 2001 From: Justin Hammond Date: Tue, 26 Aug 2014 00:00:59 +0800 Subject: [PATCH] Fix up Parsing Terms with Variables --- src/main.cpp | 16 ++++++++++++++-- src/rule/Antecedent.cpp | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0594276..5afa652 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -194,6 +194,8 @@ int main(int argc, char** argv) { signal(SIGILL, fl::Exception::signalHandler); signal(SIGSEGV, fl::Exception::signalHandler); signal(SIGFPE, fl::Exception::signalHandler); + fl::fuzzylite::setLogging(true); + fl::fuzzylite::setDebug(true); #ifdef FL_UNIX signal(SIGBUS, fl::Exception::signalHandler); signal(SIGPIPE, fl::Exception::signalHandler); @@ -221,6 +223,15 @@ fl::Engine* engine = new fl::Engine("simple-dimmer"); ambient->addTerm(new fl::DateTime("Now", DateTime::TIME_GREATERTHAN)); engine->addInputVariable(ambient); + fl::InputVariable* ambient1 = new fl::InputVariable; + ambient1->setName("Ambient1"); + ambient1->setRange(0.000, 1.000); + ambient1->addTerm(new fl::Triangle("DARK", 0.000, 0.500)); + ambient1->addTerm(new fl::Triangle("MEDIUM", 0.250, 0.750)); + ambient1->addTerm(new fl::Triangle("BRIGHT", 0.500, 1.000)); + engine->addInputVariable(ambient1); + + fl::OutputVariable* power = new fl::OutputVariable; power->setName("Power"); power->setRange(0.000, 2.000); @@ -241,7 +252,7 @@ fl::Engine* engine = new fl::Engine("simple-dimmer"); engine->addOutputVariable(power1); fl::RuleBlock* ruleblock = new fl::RuleBlock; - ruleblock->addRule(fl::Rule::parse("if Ambient is Now(2:27pm) then Power is very HIGH", engine)); + ruleblock->addRule(fl::Rule::parse("if Ambient1 is DARK and Ambient is Now(2:27pm) then Power is very HIGH", engine)); ruleblock->addRule(fl::Rule::parse("if Ambient is DARK then Power is HIGH", engine)); ruleblock->addRule(fl::Rule::parse("if Ambient is MEDIUM then Power is MEDIUM", engine)); ruleblock->addRule(fl::Rule::parse("if Ambient is BRIGHT then in 5m set Power is LOW", engine)); @@ -250,7 +261,7 @@ fl::Engine* engine = new fl::Engine("simple-dimmer"); engine->addRuleBlock(ruleblock); //No Conjunction or Disjunction is needed - engine->configure("", "", "AlgebraicProduct", "AlgebraicSum", "Centroid"); + engine->configure("AlgebraicProduct", "", "AlgebraicProduct", "AlgebraicSum", "Centroid"); std::string status; if (not engine->isReady(&status)) @@ -260,6 +271,7 @@ fl::Engine* engine = new fl::Engine("simple-dimmer"); struct timeval tv; gettimeofday (&tv, NULL); ambient->setInputValue(tv.tv_sec); + ambient1->setInputValue(0.1); engine->process(); FL_LOG("Ambient.input = " << tv.tv_sec << " -> " << "Power.output = " << fl::Op::str(power->defuzzify()) << diff --git a/src/rule/Antecedent.cpp b/src/rule/Antecedent.cpp index 0ff43fc..92c0784 100644 --- a/src/rule/Antecedent.cpp +++ b/src/rule/Antecedent.cpp @@ -180,6 +180,7 @@ namespace fl { } if (state bitand S_ARGS) { proposition->term->setArgs(token); + state = S_VARIABLE bitor S_AND_OR; continue; } if (state bitand S_AND_OR) {