Fuzzy libray for embedded targets developed by zerokol. Read more on: http://zerokol.com/product/51e93616e84c5571b7000018/2/en edit by Bruno Alfano - corrected deallocation of FuzzyOutput

Fuzzy library by Zerokol. Read more on: http://zerokol.com/product/51e93616e84c5571b7000018/2/en

edit by Bruno Alfano - corrected deallocation bug for FuzzyOutput

FuzzyRuleConsequent.cpp

Committer:
astaff15
Date:
2015-06-24
Revision:
2:460b409e26e8
Parent:
0:66cd67db4f1b

File content as of revision 2:460b409e26e8:

/*
 * Robotic Research Group (RRG)
 * State University of Piaui (UESPI), Brazil - Piauí - Teresina
 *
 * FuzzyRuleConsequent.cpp
 *
 *      Author: Msc. Marvin Lemos <marvinlemos@gmail.com>
 *              AJ Alves <aj.alves@zerokol.com>
 *          Co authors: Douglas S. Kridi <douglaskridi@gmail.com>
 *                      Kannya Leal <kannyal@hotmail.com>
 */
#include "FuzzyRuleConsequent.h"

// CONSTRUTORES
FuzzyRuleConsequent::FuzzyRuleConsequent(){
    this->fuzzySetOutputs = NULL;
    this->fuzzySetOutputsCursor = NULL;
}

// DESTRUTOR
FuzzyRuleConsequent::~FuzzyRuleConsequent(){
    this->cleanFuzzySets(this->fuzzySetOutputs);
}

// MÉTODOS PÚBLICOS
bool FuzzyRuleConsequent::addOutput(FuzzySet* fuzzySet){
    fuzzySetOutputArray *aux;
    // Alocando espaço na memória
    if((aux = (fuzzySetOutputArray *) malloc(sizeof(fuzzySetOutputArray))) == NULL){
        return false;
    }
    aux->fuzzySet   = fuzzySet;
    aux->next       = NULL;

    if(this->fuzzySetOutputs == NULL){
        this->fuzzySetOutputs = aux;
        this->fuzzySetOutputsCursor = aux;
    }else{
        this->fuzzySetOutputsCursor->next = aux;
        this->fuzzySetOutputsCursor = aux;
    }
    return true;
}

bool FuzzyRuleConsequent::evaluate(float power){
    fuzzySetOutputArray *aux;
    aux = this->fuzzySetOutputs;
    while(aux != NULL){
        aux->fuzzySet->setPertinence(power);
        aux = aux->next;
    }
    return true;
}

// MÉTODOS PRIVADOS
void FuzzyRuleConsequent::cleanFuzzySets(fuzzySetOutputArray* aux){
    if(aux != NULL){
        // Esvaziando a memória alocada
        this->cleanFuzzySets(aux->next);
        free(aux);
    }
}