コード例 #1
0
int luaTrajectory::createObserver( lua_State *L )
{

#ifdef DEBUG_OBSERVER
    luaStackToQString(7);
#endif

    // recupero a referencia da celula
    lua_rawgeti(luaL, LUA_REGISTRYINDEX, ref);

    // flags para a definição do uso de compressão
    // na transmissão de datagramas e da visibilidade
    // dos observadores Udp Sender
    bool compressDatagram = false, obsVisible = true;

    // recupero a tabela de atributos da celula
    int top = lua_gettop(luaL);

    // Não modifica em nada a pilha recupera o enum referente ao tipo
    // do observer
    int typeObserver = (int)luaL_checkinteger(luaL, 1);

    if ((typeObserver !=  TObsMap) && (typeObserver !=  TObsImage))
    {
        QStringList allAttribs, obsAttribs, obsParams, cols;

        // qDebug() << "Recupera a tabela de parametros";
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 1) != 0)
        {
            QString key;

            if (lua_type(luaL, -2) == LUA_TSTRING)
            {
                key = luaL_checkstring(luaL, -2);
            }
            //else
            //{
            //    if (lua_type(luaL, -2) == LUA_TNUMBER)
            //    {
            //        char aux[100];
            //        double number = luaL_checknumber(luaL, -2);
            //        sprintf(aux, "%g", number);
            //        key = aux;
            //    }
            //}

            switch (lua_type(luaL, -1))
            {
            case LUA_TBOOLEAN:
            {
                bool val = lua_toboolean(luaL, -1);
                if (key == "visible")
                    obsVisible = val;
                else // if (key == "compress")
                    compressDatagram = val;
                break;
            }

            case LUA_TSTRING:
            {
                const char *strAux = luaL_checkstring(luaL, -1);
                cols.append(strAux);
                break;
            }
            case LUA_TTABLE:
            {
                int pos = lua_gettop(luaL);
                QString k;

                lua_pushnil(luaL);
                while(lua_next(luaL, pos) != 0)
                {

                    if (lua_type(luaL, -2) == LUA_TSTRING)
                    {
                        obsParams.append( luaL_checkstring(luaL, -2) );
                    }

                    switch (lua_type(luaL, -1))
                    {
                    case LUA_TSTRING:
                        k = QString(luaL_checkstring(luaL, -1));
                        break;

                    case LUA_TNUMBER:
                    {
                        char aux[100];
                        double number = luaL_checknumber(luaL, -1);
                        sprintf(aux, "%g", number);
                        k = QString(aux);
                        break;
                    }
                    default:
                        break;
                    }
                    cols.append(k);
                    lua_pop(luaL, 1);
                }
                break;
            }
            default:
                break;
            }
            lua_pop(luaL, 1);
        }

        // qDebug() << "Recupera a tabela de atributos";
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 2) != 0)
        {
            if (lua_type(luaL, -1) == LUA_TSTRING)
            {
                // QString key( luaL_checkstring(luaL, -1) );
                obsAttribs.push_back(luaL_checkstring(luaL, -1));
            }
            lua_pop(luaL, 1);
        }

        // Retrieves all subject attributes
        lua_pushnil(luaL);
        while(lua_next(luaL, top) != 0)
        {
            if (lua_type(luaL, -2) == LUA_TSTRING)
                allAttribs.append( luaL_checkstring(luaL, -2) );
            lua_pop(luaL, 1);
        }

        if (obsAttribs.empty())
        {
            obsAttribs = allAttribs;
            observedAttribs = allAttribs;
        }
        else
        {
            // Verifica se o atributo informado realmente existe na celula
            for (int i = 0; i < obsAttribs.size(); i++)
            {
                if (! observedAttribs.contains(obsAttribs.at(i)) )
                    observedAttribs.push_back(obsAttribs.at(i));

                if (! allAttribs.contains(obsAttribs.at(i)))
                {
                    qFatal("\nError: Attribute name '%s' not found.\n",
                           qPrintable(obsAttribs.at(i)) );
                    return -1;
                }
            }
        }

        ObserverTextScreen *obsText = 0;
        ObserverTable *obsTable = 0;
        ObserverGraphic *obsGraphic = 0;
        ObserverLogFile *obsLog = 0;
        ObserverUDPSender *obsUDPSender = 0;
        int obsId = -1;

        switch (typeObserver)
        {
        case TObsTextScreen:
            obsText = (ObserverTextScreen*)
                      TrajectorySubjectInterf::createObserver(TObsTextScreen);
            if (obsText)
            {
                obsId = obsText->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsLogFile:
            obsLog = (ObserverLogFile*)
                     TrajectorySubjectInterf::createObserver(TObsLogFile);
            if (obsLog)
            {
                obsId = obsLog->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsTable:
            obsTable = (ObserverTable *)
                       TrajectorySubjectInterf::createObserver(TObsTable);
            if (obsTable)
            {
                obsId = obsTable->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsDynamicGraphic:
            obsGraphic = (ObserverGraphic *)
                         TrajectorySubjectInterf::createObserver(TObsDynamicGraphic);
            if (obsGraphic)
            {
                obsGraphic->setObserverType(TObsDynamicGraphic);
                obsId = obsGraphic->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsGraphic:
            obsGraphic = (ObserverGraphic *)
                         TrajectorySubjectInterf::createObserver(TObsGraphic);
            if (obsGraphic)
            {
                obsId = obsGraphic->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsUDPSender:
            obsUDPSender = (ObserverUDPSender *)
                           TrajectorySubjectInterf::createObserver(TObsUDPSender);
            if (obsUDPSender)
            {
                obsId = obsUDPSender->getId();
                obsUDPSender->setCompressDatagram(compressDatagram);

                if (obsVisible)
                    obsUDPSender->show();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        default:
            if (! QUIET_MODE )
            {
                qWarning("Warning: In this context, the code '%s' does not correspond to a "
                         "valid type of Observer.",  getObserverName(typeObserver) );
            }
            return 0;
        }

#ifdef DEBUG_OBSERVER
        qDebug() << "obsParams: " << obsParams;
        qDebug() << "\nobsAttribs: " << obsAttribs;
        qDebug() << "\nallAttribs: " << allAttribs;
        qDebug() << "\ncols: " << cols;
#endif

        if (obsLog)
        {
            obsLog->setAttributes(obsAttribs);

            if (cols.at(0).isNull() || cols.at(0).isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Filename was not specified, using a "
                             "default \"%s\".", qPrintable(DEFAULT_NAME));
                obsLog->setFileName(DEFAULT_NAME + ".csv");
            }
            else
            {
                obsLog->setFileName(cols.at(0));
            }

            // caso não seja definido, utiliza o default ";"
            if ((cols.size() < 2) || cols.at(1).isNull() || cols.at(1).isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Separator not defined, using \";\".");
                obsLog->setSeparator();
            }
            else
            {
                obsLog->setSeparator(cols.at(1));
            }

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsText)
        {
            obsText->setAttributes(obsAttribs);
            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsTable)
        {
            if ((cols.size() < 2) || cols.at(0).isNull() || cols.at(0).isEmpty()
                    || cols.at(1).isNull() || cols.at(1).isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Column title not defined.");
            }

            obsTable->setColumnHeaders(cols);
            obsTable->setAttributes(obsAttribs);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsGraphic)
        {
            obsGraphic->setLegendPosition();

            // Takes titles of three first locations
            obsGraphic->setTitles(cols.at(0), cols.at(1), cols.at(2));
            cols.removeFirst(); // remove graphic title
            cols.removeFirst(); // remove axis x title
            cols.removeFirst(); // remove axis y title

            // Splits the attribute labels in the cols list
            obsGraphic->setAttributes(obsAttribs, cols.takeFirst().split(";", QString::SkipEmptyParts),
                                      obsParams, cols);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if(obsUDPSender)
        {
            obsUDPSender->setAttributes(obsAttribs);

            // if (cols.at(0).isEmpty())
            if (cols.isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Port not defined.");
            }
            else
            {
                obsUDPSender->setPort(cols.at(0).toInt());
            }

            // broadcast
            if ((cols.size() == 1) || ((cols.size() == 2) && cols.at(1).isEmpty()) )
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Observer will send to broadcast.");
                obsUDPSender->addHost(BROADCAST_HOST);
            }
            else
            {
                // multicast or unicast
                for(int i = 1; i < cols.size(); i++)
                {
                    if (! cols.at(i).isEmpty())
                        obsUDPSender->addHost(cols.at(i));
                }
            }
            lua_pushnumber(luaL, obsId);
            return 1;
        }
    }
    //   ((typeObserver !=  TObsMap) && (typeObserver !=  TObsImage))
    // Creation of spatial observers
    else
    {
        QStringList obsParams, obsParamsAtribs; // parametros/atributos da legenda

        bool getObserverId = false, isLegend = false;
        int obsId = -1;

        AgentObserverMap *obsMap = 0;
        AgentObserverImage *obsImage = 0;

        // Recupera os parametros
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 1) != 0)
        {
            // Recupera o ID do observer map
            if ( (lua_isnumber(luaL, -1) && (! getObserverId)) )
            {
                obsId = luaL_checknumber(luaL, -1);
                getObserverId = true;
                isLegend = true;
            }

            // recupera o espao celular
            if (lua_istable(luaL, -1))
            {
                int paramTop = lua_gettop(luaL);

                lua_pushnil(luaL);
                while(lua_next(luaL, paramTop) != 0)
                {
                    if (isudatatype(luaL, -1, "TeCellularSpace"))
                    {
                        cellSpace = Luna<luaCellularSpace>::check(L, -1);
                    }
                    else
                    {
                        if (isLegend)
                        {
                            QString key = luaL_checkstring(luaL, -2);

                            obsParams.push_back(key);

                            bool boolAux;
                            double numAux;
                            QString strAux;

                            switch( lua_type(luaL, -1) )
                            {
                            case LUA_TBOOLEAN:
                                boolAux = lua_toboolean(luaL, -1);
                                break;

                            case LUA_TNUMBER:
                                numAux = luaL_checknumber(luaL, -1);
                                obsParamsAtribs.push_back(QString::number(numAux));
                                break;

                            case LUA_TSTRING:
                                strAux = luaL_checkstring(luaL, -1);
                                obsParamsAtribs.push_back(QString(strAux));
                                break;

                            default:
                                break;
                            }
                        } // isLegend
                    }
                    lua_pop(luaL, 1);
                }
            }
            lua_pop(luaL, 1);
        }

        QString errorMsg = QString("\nError: The Observer ID \"%1\" was not found. "
                                   "Check the declaration of this observer.\n").arg(obsId);

        if (! cellSpace)
            qFatal("%s", qPrintable(errorMsg));

        if (typeObserver == TObsMap)
        {
            obsMap = (AgentObserverMap *)cellSpace->getObserver(obsId);

            if (! obsMap)
                qFatal("%s", qPrintable(errorMsg));

            obsMap->registry(this);
        }
        else
        {
            obsImage = (AgentObserverImage *)cellSpace->getObserver(obsId);

            if (! obsImage)
                qFatal("%s", qPrintable(errorMsg));

            obsImage->registry(this);
        }

        QStringList allAttribs, obsAttribs;

        // Recupera os atributos
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 2) != 0)
        {
            const char * key = luaL_checkstring(luaL, -1);
            obsAttribs.push_back(QString(key));
            lua_pop(luaL, 1);
        }

        for(int i = 0; i < obsAttribs.size(); i++)
        {
            if (! observedAttribs.contains(obsAttribs.at(i)) )
                observedAttribs.push_back(obsAttribs.at(i));
        }

        if (typeObserver == TObsMap)
        {
            // ao definir os valores dos atributos do agente,
            // redefino o tipo do atributos na super classe ObserverMap
            obsMap->setAttributes(obsAttribs, obsParams, obsParamsAtribs);
            obsMap->setSubjectAttributes(obsAttribs, TObsTrajectory);
        }
        else
        {
            obsImage->setAttributes(obsAttribs, obsParams, obsParamsAtribs);
            obsImage->setSubjectAttributes(obsAttribs, TObsTrajectory);
        }
        lua_pushnumber(luaL, obsId);
        return 1;
    }

    return 0;
}
コード例 #2
0
ファイル: luaCell.cpp プロジェクト: ufopterralab/terrame
/// Creates several types of observers
/// parameters: observer type, observeb attributes table, observer type parameters
// verif. ref (endereco na pilha lua)
// olhar a classe event
int luaCell::createObserver( lua_State * )
{
    // recupero a referencia da celula
    lua_rawgeti(luaL, LUA_REGISTRYINDEX, ref);

    // flags para a definição do uso de compressão
    // na transmissão de datagramas e da visibilidade
    // dos observadores Udp Sender
    bool compressDatagram = false, obsVisible = true;

    // recupero a tabela de
    // atributos da celula
    int top = lua_gettop(luaL);

    // Nao modifica em nada a pilha
    // recupera o enum referente ao tipo
    // do observer
    int typeObserver = (int)luaL_checkinteger(luaL, -4);

    //@RAIAN
    // Para o Observer do tipo Neighbohrood
    if( typeObserver != TObsNeigh )
    {
        bool isGraphicType = (typeObserver == TObsDynamicGraphic) || (typeObserver == TObsGraphic);

        //------------------------
        QStringList allAttribs, obsAttribs;

        // Pecorre a pilha lua recuperando todos os atributos celula
        lua_pushnil(luaL);
        while(lua_next(luaL, top) != 0)
        {
            QString key( luaL_checkstring(luaL, -2) );

            allAttribs.push_back(key);
            lua_pop(luaL, 1);
        }

        //------------------------
        // pecorre a pilha lua recuperando
        // os atributos celula que se quer observar
        lua_settop(luaL, top - 1);
        top = lua_gettop(luaL);

        // Verificacao da sintaxe da tabela Atributos
        if(! lua_istable(luaL, top) )
        {
            qFatal("Error: Attributes table not found. Incorrect sintax.\n");
            return -1;
        }

        bool attribTable = false;

        lua_pushnil(luaL);
        while(lua_next(luaL, top - 1 ) != 0)
        {
            QString key( luaL_checkstring(luaL, -1) );
            attribTable = true;

            // Verifica se o atributo informado não existe deve ter sido digitado errado
            if (allAttribs.contains(key))
            {
                obsAttribs.push_back(key);
                if (! observedAttribs.contains(key))
                    observedAttribs.push_back(key);
            }
            else
            {
                if ( ! key.isNull() || ! key.isEmpty())
                {
                    qFatal("Error: Attribute name '%s' not found.\n", qPrintable(key));
                    return -1;
                }
            }
            lua_pop(luaL, 1);
        }
        //------------------------

        // if ((obsAttribs.empty() ) && (! isGraphicType))
        if (obsAttribs.empty())
        {
            obsAttribs = allAttribs;
            observedAttribs = allAttribs;
        }

        //if(! lua_istable(luaL, top) )
        //{
        //    qWarning("Warning: Parameter table not found. Incorrect sintax.");
        //    return 0;
        //}

        QStringList cols, obsParams;

        // Recupera a tabela de parametros os observadores do tipo Table e Graphic
        // caso não seja um tabela a sintaxe do metodo esta incorreta
        lua_pushnil(luaL);
        while(lua_next(luaL, top) != 0)
        {
            QString key;
            if (lua_type(luaL, -2) == LUA_TSTRING)
                key = QString(luaL_checkstring(luaL, -2));

            switch (lua_type(luaL, -1))
            {
            case LUA_TSTRING:
            {
                QString value( luaL_checkstring(luaL, -1));
                cols.push_back(value);
                break;
            }

            case LUA_TBOOLEAN:
            {
                bool val = lua_toboolean(luaL, -1);
                if (key == "visible")
                    obsVisible = val;
                else // if (key == "compress")
                    compressDatagram = val;
                break;
            }

            case LUA_TTABLE:
            {
                int tableTop = lua_gettop(luaL);

                lua_pushnil(luaL);
                while(lua_next(luaL, tableTop) != 0)
                {
                    if (lua_type(luaL, -2) == LUA_TSTRING)
                        obsParams.append(luaL_checkstring(luaL, -2));

                    switch (lua_type(luaL, -1))
                    {
                    case LUA_TNUMBER:
                        cols.append(QString::number(luaL_checknumber(luaL, -1)) );
                        break;

                    case LUA_TSTRING:
                        cols.append(luaL_checkstring(luaL, -1));
                        break;
                    }
                    lua_pop(luaL, 1);
                }
            }

            default:
                break;
            }
            lua_pop(luaL, 1);
        }

        // Caso não seja definido nenhum parametro,
        // e o observador não é TextScreen então
        // lança um warning
        if ((cols.isEmpty()) && (typeObserver != TObsTextScreen))
        {
            if (! QUIET_MODE )
                qWarning("Warning: The Parameters Table is empty.");
        }
        //------------------------

        ObserverTextScreen *obsText = 0;
        ObserverTable *obsTable = 0;
        ObserverGraphic *obsGraphic = 0;
        ObserverLogFile *obsLog = 0;
        ObserverUDPSender *obsUDPSender = 0;

        int obsId = -1;

        switch (typeObserver)
        {
        case TObsTextScreen:
            obsText = (ObserverTextScreen*)
                      CellSubjectInterf::createObserver(TObsTextScreen);
            if (obsText)
            {
                obsId = obsText->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsLogFile:
            obsLog = (ObserverLogFile*)
                     CellSubjectInterf::createObserver(TObsLogFile);
            if (obsLog)
            {
                obsId = obsLog->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsTable:
            obsTable = (ObserverTable *)
                       CellSubjectInterf::createObserver(TObsTable);
            if (obsTable)
            {
                obsId = obsTable->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsDynamicGraphic:
            obsGraphic = (ObserverGraphic *)
                         CellSubjectInterf::createObserver(TObsDynamicGraphic);
            if (obsGraphic)
            {
                obsGraphic->setObserverType(TObsDynamicGraphic);
                obsId = obsGraphic->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsGraphic:
            obsGraphic = (ObserverGraphic *)
                         CellSubjectInterf::createObserver(TObsGraphic);
            if (obsGraphic)
            {
                obsId = obsGraphic->getId();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsUDPSender:
            obsUDPSender = (ObserverUDPSender *)
                           CellSubjectInterf::createObserver(TObsUDPSender);
            if (obsUDPSender)
            {
                obsId = obsUDPSender->getId();
                obsUDPSender->setCompressDatagram(compressDatagram);

                if (obsVisible)
                    obsUDPSender->show();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsMap:
        default:
            if (! QUIET_MODE )
            {
                qWarning("Warning: In this context, the code '%s' does not correspond to a "
                         "valid type of Observer.",  getObserverName(typeObserver) );
            }
            return 0;
        }

#ifdef DEBUG_OBSERVER
        qDebug() << "luaCell";
        qDebug() << "obsParams: " << obsParams;
        qDebug() << "obsAttribs: " << obsAttribs;
        qDebug() << "allAttribs: " << allAttribs;
        qDebug() << "cols: " << cols;
#endif

        //@RODRIGO
        //serverSession->add(obsKey);

        /// Define alguns parametros do observador instanciado ---------------------------------------------------

        if (obsLog)
        {
            obsLog->setAttributes(obsAttribs);

            if (cols.at(0).isNull() || cols.at(0).isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Filename was not specified, using a "
                             "default \"%s\".", qPrintable(DEFAULT_NAME));
                obsLog->setFileName(DEFAULT_NAME + ".csv");
            }
            else
            {
                obsLog->setFileName(cols.at(0));
            }

            // caso não seja definido, utiliza o default ";"
            if ((cols.size() < 2) || cols.at(1).isNull() || cols.at(1).isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Separator not defined, using \";\".");
                obsLog->setSeparator();
            }
            else
            {
                obsLog->setSeparator(cols.at(1));
            }

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsText)
        {
            obsText->setAttributes(obsAttribs);
            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsTable)
        {
            if ((cols.size() < 2) || cols.at(0).isNull() || cols.at(0).isEmpty()
                    || cols.at(1).isNull() || cols.at(1).isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Column title not defined.");
            }

            obsTable->setColumnHeaders(cols);
            obsTable->setAttributes(obsAttribs);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsGraphic)
        {
            obsGraphic->setLegendPosition();

            // Takes titles of three first locations
            obsGraphic->setTitles(cols.at(0), cols.at(1), cols.at(2));
            cols.removeFirst(); // remove graphic title
            cols.removeFirst(); // remove axis x title
            cols.removeFirst(); // remove axis y title

            // Splits the attribute labels in the cols list
            obsGraphic->setAttributes(obsAttribs, cols.takeFirst().split(";", QString::SkipEmptyParts),
                                      obsParams, cols);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if(obsUDPSender)
        {
            obsUDPSender->setAttributes(obsAttribs);

            // if (cols.at(0).isEmpty())
            if (cols.isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Port not defined.");
            }
            else
            {
                obsUDPSender->setPort(cols.at(0).toInt());
            }

            // broadcast
            if ((cols.size() == 1) || ((cols.size() == 2) && cols.at(1).isEmpty()) )
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Observer will send to broadcast.");
                obsUDPSender->addHost(BROADCAST_HOST);
            }
            else
            {
                // multicast or unicast
                for(int i = 1; i < cols.size(); i++) {
                    if (! cols.at(i).isEmpty())
                        obsUDPSender->addHost(cols.at(i));
                }
            }
            lua_pushnumber(luaL, obsId);
            return 1;
        }
    }
    //@RAIAN
    // Comeca a criacao do Observer do tipo Neighborhood
    else
    {
        QStringList obsParams, obsParamsAtribs;

        bool getObserverID = false, isLegend = false;
        int obsID = -1;

        AgentObserverMap *obsMap = 0;
        luaCellularSpace *cellSpace = 0;

        // Recupera os parametros
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 1) != 0)
        {
            // Recupera o ID do observer map
            if( lua_isnumber(luaL, -1) && (!getObserverID) )
            {
                obsID = luaL_checknumber(luaL, -1);
                getObserverID = true;
                isLegend = true;
            }

            //Recupera o espaco celular e a legenda
            if(lua_istable(luaL, -1))
            {
                int paramTop = lua_gettop(luaL);

                lua_pushnil(luaL);
                while(lua_next(luaL, paramTop) != 0)
                {
                    if(isudatatype(luaL, -1, "TeCellularSpace"))
                    {
                        cellSpace = Luna<luaCellularSpace>::check(luaL, -1);
                    }
                    else
                    {
                        if(isLegend)
                        {
                            QString key(luaL_checkstring(luaL, -2));
                            obsParams.push_back(key);

                            bool boolAux;
                            double numAux;
                            QString strAux;

                            switch(lua_type(luaL, -1))
                            {
                            case LUA_TBOOLEAN:
                                boolAux = lua_toboolean(luaL, -1);
                                break;

                            case LUA_TNUMBER:
                                numAux = luaL_checknumber(luaL, -1);
                                obsParamsAtribs.push_back(QString::number(numAux));
                                break;

                            case LUA_TSTRING:
                                strAux = luaL_checkstring(luaL, -1);
                                obsParamsAtribs.push_back(QString(strAux));

                            case LUA_TNIL:
                            case LUA_TTABLE:
                            default:
                                ;
                            }
                        }
                    }
                    lua_pop(luaL, 1);
                }
            }
            lua_pop(luaL, 1);
        }

        QString errorMsg = QString("\nError: The Observer ID \"%1\" was not found. "
                                   "Check the declaration of this observer.\n").arg(obsID);

        if(!cellSpace)
            qFatal("%s", qPrintable(errorMsg));

        QStringList neighIDs;
        QString exhibitionName;

        // Recupera os IDs das Vizinhancas a serem observadas
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 2) != 0)
        {
            const char* key = luaL_checkstring(luaL, -1);
            this->attrNeighName.append(key);
            exhibitionName = QString("neighborhood (%1)").arg(this->attrNeighName);

            neighIDs.push_back(exhibitionName);
            observedAttribs.push_back(exhibitionName);

#ifdef TME_BLACK_BOARD
            // Solucao provisoria para o observer do tipo Neighbohrood
            observedAttribs.push_front("@getNeighborhoodState");
#endif
            lua_pop(luaL, 1);
        }

        if(typeObserver == TObsNeigh)
        {
            obsMap = (AgentObserverMap *)cellSpace->getObserver(obsID);
            if(!obsMap)
                qFatal("%s", qPrintable(errorMsg));

            obsMap->registry(this, exhibitionName);
            obsMap->setAttributes(neighIDs, obsParams, obsParamsAtribs);
            obsMap->setSubjectAttributes(neighIDs, TObsNeighborhood, this->attrNeighName);
        }

        // @RAIAN: Acrescentei estas linhas para retornar o ID do observer, que estava faltando
        lua_pushnumber(luaL, obsID);
        return 1;
    }
    //@RAIAN - FIM
    return 0;
}
コード例 #3
0
int luaTrajectory::createObserver( lua_State *L )
{

#ifdef DEBUG_OBSERVER
    luaStackToQString(7);
#endif

    // recupero a referencia da celula
    lua_rawgeti(luaL, LUA_REGISTRYINDEX, ref);

    // flags para a definição do uso de compressão
    // na transmissão de datagramas e da visibilidade
    // dos observadores Udp Sender
    bool compressDatagram = false, obsVisible = true;

    // recupero a tabela de atributos da celula
    int top = lua_gettop(luaL);

    // Não modifica em nada a pilha recupera o enum referente ao tipo
    // do observer
    int typeObserver = (int)luaL_checkinteger(luaL, 1);

    if ((typeObserver !=  TObsMap) && (typeObserver !=  TObsImage))
    {
        QStringList allAttribs, obsAttribs, obsParams;

        // qDebug() << "Recupera a tabela de parametros";
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 1) != 0)
        {
            QString key;
            if (lua_type(luaL, -2) == LUA_TSTRING)
                key = QString(luaL_checkstring(luaL, -2));

            switch (lua_type(luaL, -1))
            {
            case LUA_TBOOLEAN:
            {
                bool val = lua_toboolean(luaL, -1);
                if (key == "visible")
                    obsVisible = val;
                else // if (key == "compress")
                    compressDatagram = val;
                break;
            }

            case LUA_TTABLE:
            {
                int pos = lua_gettop(luaL);

                lua_pushnil(luaL);
                while(lua_next(luaL, pos) != 0)
                {
                    QString k;

                    switch (lua_type(luaL, -1))
                    {
                    case LUA_TSTRING:
                        k = QString(luaL_checkstring(luaL, -1));
                        break;

                    case LUA_TNUMBER:
                    {
                        char aux[100];
                        double number = luaL_checknumber(luaL, -1);
                        sprintf(aux, "%g", number);
                        k = QString(aux);
                        break;
                    }
                    default:
                        break;
                    }
                    obsParams.push_back(k);
                    lua_pop(luaL, 1);
                }
                break;
            }
            default:
                break;
            }
            lua_pop(luaL, 1);
        }

        // qDebug() << "Recupera a tabela de atributos";
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 2) != 0)
        {
            if (lua_type(luaL, -1) == LUA_TSTRING)
            {
                QString key( luaL_checkstring(luaL, -1) );
                obsAttribs.push_back(key);
            }
            lua_pop(luaL, 1);
        }

        // Verifica se o atributo informado realmente existe na celula
        for (int i = 0; i < obsAttribs.size(); i++)
        {
            if (! observedAttribs.contains(obsAttribs.at(i)) )
                observedAttribs.push_back(obsAttribs.at(i));

            if (! allAttribs.contains(obsAttribs.at(i)))
            {
                qFatal("\nError: Attribute name '%s' not found.\n",
                       qPrintable(obsAttribs.at(i)) );
                return -1;
            }
        }

        ObserverUDPSender *obsUDPSender = 0;
        int obsId = -1;

        switch (typeObserver)
        {
        case TObsUDPSender:
            obsUDPSender = (ObserverUDPSender *)
                           TrajectorySubjectInterf::createObserver(TObsUDPSender);
            if (obsUDPSender)
            {
                obsId = obsUDPSender->getId();
                obsUDPSender->setCompressDatagram(compressDatagram);

                if (obsVisible)
                    obsUDPSender->show();
            }
            else
            {
                if (! QUIET_MODE)
                    qWarning("%s", TerraMEObserver::MEMORY_ALLOC_FAILED);
            }
            break;

        default:
            if (! QUIET_MODE )
            {
                qWarning("Warning: In this context, the code '%s' does not correspond to a "
                         "valid type of Observer.",  getObserverName(typeObserver) );
            }
            return 0;
        }

        //qDebug() << obsParams;
        //qDebug() << obsAttribs;
        //qDebug() << allAttribs;

        if (obsUDPSender)
        {
            obsUDPSender->setAttributes(obsAttribs);

            //if (obsParams.at(0).isEmpty())
            if (obsParams.isEmpty())
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Port not defined.");
            }
            else
            {
                obsUDPSender->setPort(obsParams.at(0).toInt());
            }

            // broadcast
            if ((obsParams.size() == 1)
                    || ((obsParams.size() == 2) && obsParams.at(1).isEmpty()) )
            {
                if (! QUIET_MODE )
                    qWarning("Warning: Observer will send to broadcast.");
                obsUDPSender->addHost(BROADCAST_HOST);
            }
            else
            {
                // multicast or unicast
                for(int i = 1; i < obsParams.size(); i++) {
                    if (! obsParams.at(i).isEmpty())
                        obsUDPSender->addHost(obsParams.at(i));
                }
            }
            lua_pushnumber(luaL, obsId);
            return 1;
        }
    }  //   ((typeObserver !=  TObsMap) && (typeObserver !=  TObsImage))
    else
    {
        QStringList obsParams, obsParamsAtribs; // parametros/atributos da legenda

        bool getObserverId = false, isLegend = false;
        int obsId = -1;

        AgentObserverMap *obsMap = 0;
        AgentObserverImage *obsImage = 0;

        // Recupera os parametros
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 1) != 0)
        {
            // Recupera o ID do observer map
            if ( (lua_isnumber(luaL, -1) && (! getObserverId)) )
            {
                obsId = luaL_checknumber(luaL, -1);
                getObserverId = true;
                isLegend = true;
            }

            // recupera o espao celular
            if (lua_istable(luaL, -1))
            {
                int paramTop = lua_gettop(luaL);

                lua_pushnil(luaL);
                while(lua_next(luaL, paramTop) != 0)
                {
                    if (isudatatype(luaL, -1, "TeCellularSpace"))
                    {
                        cellSpace = Luna<luaCellularSpace>::check(L, -1);
                    }
                    else
                    {
                        if (isLegend)
                        {
                            QString key = luaL_checkstring(luaL, -2);

                            obsParams.push_back(key);

                            bool boolAux;
                            double numAux;
                            QString strAux;

                            switch( lua_type(luaL, -1) )
                            {
                            case LUA_TBOOLEAN:
                                boolAux = lua_toboolean(luaL, -1);
                                break;

                            case LUA_TNUMBER:
                                numAux = luaL_checknumber(luaL, -1);
                                obsParamsAtribs.push_back(QString::number(numAux));
                                break;

                            case LUA_TSTRING:
                                strAux = luaL_checkstring(luaL, -1);
                                obsParamsAtribs.push_back(QString(strAux));
                                break;

                            default:
                                break;
                            }
                        } // isLegend
                    }
                    lua_pop(luaL, 1);
                }
            }
            lua_pop(luaL, 1);
        }

        QString errorMsg = QString("\nError: The Observer ID \"%1\" was not found. "
                                   "Check the declaration of this observer.\n").arg(obsId);

        if (! cellSpace)
            qFatal("%s", qPrintable(errorMsg));

        if (typeObserver == TObsMap)
        {
            obsMap = (AgentObserverMap *)cellSpace->getObserver(obsId);

            if (! obsMap)
                qFatal("%s", qPrintable(errorMsg));

            obsMap->registry(this);
        }
        else
        {
            obsImage = (AgentObserverImage *)cellSpace->getObserver(obsId);

            if (! obsImage)
                qFatal("%s", qPrintable(errorMsg));

            obsImage->registry(this);
        }

        QStringList allAttribs, obsAttribs;

        // Recupera os atributos
        lua_pushnil(luaL);
        while(lua_next(luaL, top - 2) != 0)
        {
            const char * key = luaL_checkstring(luaL, -1);
            obsAttribs.push_back(QString(key));
            lua_pop(luaL, 1);
        }

        for(int i = 0; i < obsAttribs.size(); i++)
        {
            if (! observedAttribs.contains(obsAttribs.at(i)) )
                observedAttribs.push_back(obsAttribs.at(i));
        }

        if (typeObserver == TObsMap)
        {
            // ao definir os valores dos atributos do agente,
            // redefino o tipo do atributos na super classe ObserverMap
            obsMap->setHeaders(obsAttribs, obsParams, obsParamsAtribs);
            obsMap->setSubjectAttributes(obsAttribs, TObsTrajectory);
        }
        else
        {
            obsImage->setHeaders(obsAttribs, obsParams, obsParamsAtribs);
            obsImage->setSubjectAttributes(obsAttribs, TObsTrajectory);
        }
        lua_pushnumber(luaL, obsId);
        return obsId;
    }

    return 0;
}
コード例 #4
0
ファイル: luaLocalAgent.cpp プロジェクト: hguerra/terrame
int luaLocalAgent::createObserver(lua_State *L)
{
    // recupero a referencia da celula
    Reference<luaAgent>::getReference(luaL);

    // flags para a defini(C)(C)o do uso de compress(C)o
    // na transmiss(C)o de datagramas e da visibilidade
    // dos observadores Udp Sender
    bool compressDatagram = false, obsVisible = true;

    // recupero a tabela de atributos da celula
    int top = lua_gettop(luaL);

    // No modifica em nada a pilha recupera o enum
    // referente ao tipo do observer
    int typeObserver =(int)luaL_checkinteger(luaL, 1); //top - 3);

    if ((typeObserver !=  TObsMap) && (typeObserver !=  TObsImage))
    {
        bool isGraphicType =(typeObserver == TObsDynamicGraphic)
            ||(typeObserver == TObsGraphic);

        //------------------------
        QStringList allAttribs, obsAttribs;
        QList<QPair<QString, QString> > allStates;

        // Pecorre a pilha lua recuperando
        // todos os atributos celula
        lua_pushnil(luaL);
        while (lua_next(luaL, top) != 0)
        {
            QString key;

            switch (lua_type(luaL, -2))
            {
            case LUA_TSTRING:
                key = QString(luaL_checkstring(luaL, -2));
                break;

            case LUA_TNUMBER:
                {
                    char aux[100];
                    double number = luaL_checknumber(luaL, -2);
                    sprintf(aux, "%g", number);
                    key = QString(aux);
                    break;
                }
            default:
                break;
            }

            // Recupero os estados do TeState
            if (isudatatype(luaL, -1, "TeState"))
            {
                ControlMode*  lcm =(ControlMode*)Luna<luaControlMode>::check(L, -1);

                QString state, transition;
                state.append(lcm->getControlModeName().c_str());

                // Adiciona o estado do atributo na lista de parametros
                // allAttribs.push_back(state);

                // Recupero a transi(C)(C)o dos estados
                ProcessCompositeInterf::iterator prIt;
                prIt = lcm->ProcessCompositeInterf::begin();

                JumpCompositeInterf::iterator jIt;
                jIt = prIt->JumpCompositeInterf::begin();

                while (jIt != prIt->JumpCompositeInterf::end())
                {
                    transition = QString((*jIt)->getTargetControlModeName().c_str());
                    jIt++;
                }

                // cria um par(estado, transi(C)(C)o) e adiciona na lista de estados
                allStates.push_back(qMakePair(state, transition));
            }
            allAttribs.push_back(key);
            lua_pop(luaL, 1);
        }

        // Adiciono o currentState no observador
        allAttribs.push_back("currentState"); // insere o estado atual

        //------------------------
        // pecorre a pilha lua recuperando
        // os atributos celula que se quer observar
        lua_settop(luaL, top - 1);
        top = lua_gettop(luaL);

        // Verificao da sintaxe da tabela Atributos
        if (!lua_istable(luaL, top))
        {
            qFatal("Error: Attributes table not found. Incorrect sintax.\n");
            return -1;
        }

        lua_pushnil(luaL);
        while (lua_next(luaL, top - 1) != 0)
        {
            QString key = luaL_checkstring(luaL, -1);

            // Verifica se o atributo informado existe
            // ou pode ter sido digitado errado
            if (allAttribs.contains(key))
            {
                obsAttribs.push_back(key);
                if (!observedAttribs.contains(key))
                    observedAttribs.push_back(key);
            }
            else
            {
                if (!key.isNull() || !key.isEmpty())
                {
					string err_out = string("Error: Attribute name '") + string(qPrintable(key)) + string("' not found.");
					lua_getglobal(L, "customError");
					lua_pushstring(L, err_out.c_str());
					lua_pushnumber(L, 4);
					lua_call(L, 2, 0);
                    return -1;
                }
            }
            lua_pop(luaL, 1);
        }
        //------------------------

        // Adiciono o currentState no observador
        if ((obsAttribs.empty()) && (!isGraphicType))
        {
            obsAttribs = allAttribs;
            observedAttribs = allAttribs;
        }

        //------------------------
        if (!lua_istable(luaL, top))
        {
            qFatal("Error: Parameter table not found. Incorrect sintax.\n");
            return -1;
        }

        QStringList obsParams, obsParamsAtribs; // parametros/atributos da legenda
        QStringList cols;
        bool isLegend = false;

        // Recupera a tabela de parametros
        lua_pushnil(luaL);
        while (lua_next(luaL, top) != 0)
        {
            QString key;
            if (lua_type(luaL, -2) == LUA_TSTRING)
                key = QString(luaL_checkstring(luaL, -2));

            switch (lua_type(luaL, -1))
            {
            case LUA_TSTRING:
            {
                QString value(luaL_checkstring(luaL, -1));
                cols.push_back(value);
                break;
            }
            case LUA_TBOOLEAN:
            {
                bool val = lua_toboolean(luaL, -1);
                if (key == "visible")
                    obsVisible = val;
                else // if (key == "compress")
                    compressDatagram = val;
                break;
            }

            // Recupera a celula que se deseja observar o automato
            case LUA_TTABLE:
                {
                    int paramTop = lua_gettop(luaL);
                    QString value;

                    lua_pushnil(luaL);
                    while (lua_next(luaL, paramTop) != 0)
                    {
                        if (lua_type(luaL, -2) == LUA_TSTRING)
                        {
                            value = QString(luaL_checkstring(luaL, -2));

                            if (value == "cObj_")
                            {
                                //int cellTop = lua_gettop(luaL);
                                //lua_pushstring(luaL, "cObj_");
                                //lua_gettable(luaL, cellTop);

                                whereCell =(luaCell*)Luna<luaCell>::check(L, -1);
                                // lua_pop(luaL, 1); // lua_pushstring
                            }

                            if (isLegend)
                            {
                                // bool boolAux;
                                const char *strAux;
                                double numAux = -1;

                                obsParams.push_back(value);

                                switch (lua_type(luaL, -1))
                                {
                                case LUA_TBOOLEAN:
                                    // boolAux = lua_toboolean(luaL, -1);
                                    // obsParamsAtribs.push_back(QString::number(boolAux));
                                    break;

                                case LUA_TNUMBER:
                                    numAux = luaL_checknumber(luaL, -1);
                                    obsParamsAtribs.push_back(QString::number(numAux));
                                    break;

                                case LUA_TSTRING:
                                    strAux = luaL_checkstring(luaL, -1);
                                    obsParamsAtribs.push_back(QString(strAux));
                                    break;

                                case LUA_TNIL:
                                case LUA_TTABLE:
                                default:
                                    break;
                                }
                            }
                        }
                        lua_pop(luaL, 1);
                    }
                    isLegend = true;
                }
            default:
                break;
            }
            lua_pop(luaL, 1);
        }

        ObserverTextScreen *obsText = 0;
        ObserverTable *obsTable = 0;
        ObserverGraphic *obsGraphic = 0;
        ObserverLogFile *obsLog = 0;
        ObserverUDPSender *obsUDPSender = 0;
        ObserverStateMachine *obsStateMachine = 0;

        int obsId = -1;
        // QStringList attrs;

        switch (typeObserver)
        {
        case TObsTextScreen:
            obsText =(ObserverTextScreen *)
                LocalAgentSubjectInterf::createObserver(TObsTextScreen);
            if (obsText)
            {
                obsId = obsText->getId();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsLogFile:
            obsLog =(ObserverLogFile *)
                LocalAgentSubjectInterf::createObserver(TObsLogFile);
            if (obsLog)
            {
                obsId = obsLog->getId();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsTable:
            obsTable =(ObserverTable *)
                LocalAgentSubjectInterf::createObserver(TObsTable);
            if (obsTable)
            {
                obsId = obsTable->getId();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsUDPSender:
            obsUDPSender =(ObserverUDPSender *)
                LocalAgentSubjectInterf::createObserver(TObsUDPSender);
            if (obsUDPSender)
            {
                obsId = obsUDPSender->getId();
                obsUDPSender->setCompressDatagram(compressDatagram);

                if (obsVisible)
                    obsUDPSender->show();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsStateMachine:
            obsStateMachine =(ObserverStateMachine *)
                LocalAgentSubjectInterf::createObserver(TObsStateMachine);
            if (obsStateMachine)
            {
                obsId = obsStateMachine->getId();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsDynamicGraphic:
            obsGraphic =(ObserverGraphic *)
                LocalAgentSubjectInterf::createObserver(TObsDynamicGraphic);
            if (obsGraphic)
            {
                obsGraphic->setObserverType(TObsDynamicGraphic);
                obsId = obsGraphic->getId();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        case TObsGraphic:
            obsGraphic =(ObserverGraphic *)
                LocalAgentSubjectInterf::createObserver(TObsGraphic);
            if (obsGraphic)
            {
                obsId = obsGraphic->getId();
            }
            else
            {
                if (execModes != Quiet)
                    qWarning("%s", qPrintable(TerraMEObserver::MEMORY_ALLOC_FAILED));
            }
            break;

        default:
            if (execModes != Quiet)
            {
                qWarning("Warning: In this context, the code '%s' does not correspond to a "
                "valid type of Observer.",  getObserverName(typeObserver));
            }
            return 0;
        }

        /// Define alguns parametros do observador instanciado -------------------------------------
        if (obsLog)
        {
            obsLog->setAttributes(obsAttribs);

            if (cols.at(0).isNull() || cols.at(0).isEmpty())
            {
                obsLog->setFileName(DEFAULT_NAME + ".csv");
            }
            else
            {
                obsLog->setFileName(cols.at(0));
            }

            // caso no seja definido, utiliza o default ";"
            if ((cols.size() < 2) || cols.at(1).isNull() || cols.at(1).isEmpty())
            {
                obsLog->setSeparator();
            }
            else
            {
                obsLog->setSeparator(cols.at(1));
            }

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsText)
        {
            obsText->setAttributes(obsAttribs);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsTable)
        {
            obsTable->setColumnHeaders(cols);
            obsTable->setAttributes(obsAttribs);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsUDPSender)
        {
            obsUDPSender->setAttributes(obsAttribs);
            obsUDPSender->setPort(cols.at(0).toInt());

            // broadcast
            if ((cols.size() == 1) ||((cols.size() == 2) && cols.at(1).isEmpty()))
            {
                obsUDPSender->addHost(BROADCAST_HOST);
            }
            else
            {
                // multicast or unicast
                for (int i = 1; i < cols.size(); i++){
                    if (!cols.at(i).isEmpty())
                        obsUDPSender->addHost(cols.at(i));
                }
            }
            lua_pushnumber(luaL, obsId);
            return 1;
        }

        if (obsGraphic)
        {
            obsGraphic->setLegendPosition();

            // if (obsAttribs.contains("currentState"))
            //    obsGraphic->setCurveStyle();

            // Takes titles of three first locations
            obsGraphic->setTitles(cols.at(0), cols.at(1), cols.at(2));
            cols.removeFirst(); // remove graphic title
            cols.removeFirst(); // remove axis x title
            cols.removeFirst(); // remove axis y title

            // Splits the attribute labels in the cols list
            obsGraphic->setAttributes(obsAttribs, cols.takeFirst().split(";", QString::SkipEmptyParts),
                obsParams, obsParamsAtribs);

            lua_pushnumber(luaL, obsId);
            return 1;
        }

        ///////////////////////////////////////////

        if (obsStateMachine)
        {
            obsStateMachine->addState(allStates);
            obsStateMachine->setAttributes(obsAttribs, obsParams, obsParamsAtribs);

            lua_pushnumber(luaL, obsId);
            return 1;
        }
    } // typeObserver !=  TerraMEObserver::TObsMap)
    else
    {
        QStringList obsParams, obsParamsAtribs; // parametros/atributos da legenda

        bool getObserverID = false, isLegend = false;
        int obsID = -1;

        AgentObserverMap *obsMap = 0;

        // Recupera os parametros
        lua_pushnil(luaL);
        while (lua_next(luaL, top - 1) != 0)
        {
            // Recupera o ID do observer map
            if ((lua_isnumber(luaL, -1) && (!getObserverID)))
            {
                // obsID = lua_tonumber(luaL, paramTop - 1);
                obsID = luaL_checknumber(luaL, -1);
                getObserverID = true;
                isLegend = true;
            }

            // recupera o espao celular
            if (lua_istable(luaL, -1))
            {
                int paramTop = lua_gettop(luaL);

                lua_pushnil(luaL);
                while (lua_next(luaL, paramTop) != 0)
                {
                    if (isudatatype(luaL, -1, "TeCellularSpace"))
                    {
                        cellSpace = Luna<luaCellularSpace>::check(L, -1);
                    }
                    else
                    {
                        if (isLegend)
                        {
                            QString key = luaL_checkstring(luaL, -2);
                            obsParams.push_back(key);

                            bool boolAux;
                            double numAux;
                            QString strAux;

                            switch (lua_type(luaL, -1))
                            {
                            case LUA_TBOOLEAN:
                                boolAux = lua_toboolean(luaL, -1);
                                break;

                            case LUA_TNUMBER:
                                numAux = luaL_checknumber(luaL, -1);
                                obsParamsAtribs.push_back(QString::number(numAux));
                                break;

                            case LUA_TSTRING:
                                strAux = luaL_checkstring(luaL, -1);
                                obsParamsAtribs.push_back(QString(strAux));
                                break;

                            default:
                                break;
                            }
                        } // isLegend
                    }

                    lua_pop(luaL, 1);
                }
            }
            lua_pop(luaL, 1);
        }

        QString errorMsg = QString("\nError: The Observer ID \"%1\" was not found. "
            "Check the declaration of this observer.\n").arg(obsID);

        if (!cellSpace)
            qFatal("%s", qPrintable(errorMsg));

        QStringList allAttribs, obsAttribs;

        // Recupera todos os atributos do agente
        // buscando apenas o classe do agente
        lua_pushnil(luaL);
        while (lua_next(luaL, top) != 0)
        {
            if (lua_type(luaL, -2) == LUA_TSTRING)
            {
                QString key;
                key = QString(luaL_checkstring(luaL, -2));

                if (key == "class")
                    attrClassName = QString(luaL_checkstring(luaL, -1));
            }
            lua_pop(luaL, 1);
        }

        attrClassName.push_front("(");
        attrClassName.push_back(")");

        if (typeObserver == TObsMap)
        {
            obsMap =(AgentObserverMap *)cellSpace->getObserver(obsID);

            if (!obsMap)
                qFatal("%s", qPrintable(errorMsg));

            obsMap->registry(this, attrClassName);
        }

        // Recupera os atributos
        lua_pushnil(luaL);
        while (lua_next(luaL, top - 2) != 0)
        {
            QString key = QString(luaL_checkstring(luaL, -1));

            if (key == "currentState")
                obsAttribs.push_back(key + attrClassName);
            else
                obsAttribs.push_back(key);

            lua_pop(luaL, 1);
        }

        for (int i = 0; i < obsAttribs.size(); i++)
        {
            if (!observedAttribs.contains(obsAttribs.at(i)))
                observedAttribs.push_back(obsAttribs.at(i));
        }

        if (typeObserver == TObsMap)
        {
            // ao definir os valores dos atributos do agente,
            // redefino o tipo do atributos na super classe ObserverMap
            obsMap->setAttributes(obsAttribs, obsParams, obsParamsAtribs);
            obsMap->setSubjectAttributes(obsAttribs, TObsAutomaton, attrClassName);
        }
        lua_pushnumber(luaL, obsID);
        return 1;
    }
    return 0;
}