Exemplo n.º 1
0
/*
*   Return the list of beliefs of an agent in an OPC given
*   bInput format : getBeliefs real/mental  agent
*/
Bottle opcManager::getBeliefs(Bottle bInput)
{
    Bottle bOutput;
    if (bInput.size() != 3)
    {
        cout << "Error in opcManager::getBeliefs | wrong size of input" << endl;
        bOutput.addString("Error in opcManager::getBeliefs | wrong size of input");
        return bOutput;
    }

    if (bInput.get(1).toString() != "real" && bInput.get(1).toString() != "mental")
    {
        cout << "Error in opcManager::getBeliefs | unknown OPC (real/mental)" << endl;
        bOutput.addString("Error in opcManager::getBeliefs | unknown OPC (real/mental)");
        return bOutput;
    }

    Agent *agent;
    if (bInput.get(1).toString() == "real")
    {
        agent = dynamic_cast<Agent*>(realOPC->getEntity(bInput.get(2).toString().c_str()));
    }
    else
    {
        agent = dynamic_cast<Agent*>(mentalOPC->getEntity(bInput.get(2).toString().c_str()));
    }

    list<Relation> lRelation = agent->beliefs();

    cout << endl << agent->name() << " has the following beliefs in the " << bInput.get(1).toString() << " OPC (" << lRelation.size() << ") : " << endl;

    for (list<Relation>::iterator it_R = lRelation.begin(); it_R != lRelation.end(); it_R++)
    {
        Bottle bTemp = it_R->asLightBottle();
        cout << bTemp.toString() << endl;
        bOutput.addList() = bTemp;
    }

    cout << endl;

    return bOutput;
}
Exemplo n.º 2
0
Bottle opcManager::updateBelief(string sOPCname)
{
    cout << "Updating the beliefs in OPC ... ";

    Bottle bOutput;
    // Create the beliefs
    bool bReal = sOPCname == s_realOPC;

    if (bReal)
    {
        realOPC->checkout();
        realOPC->update();
    }
    else
    {
        mentalOPC->checkout();
        mentalOPC->update();
    }

    Bottle bMessenger;
    bMessenger.addString("updateObjectLocation");
    bMessenger.addString(sOPCname.c_str());
    portToAbmReasoning.write(bMessenger, bMessenger);

    // Set the Bottles for queryOPC to the 

    Bottle isRtobject, conditionAgent, conditionRTO;
    isRtobject.addString(EFAA_OPC_ENTITY_TAG);
    isRtobject.addString("==");
    isRtobject.addString(EFAA_OPC_ENTITY_RTOBJECT);

    Bottle isAgent;
    isAgent.addString(EFAA_OPC_ENTITY_TAG);
    isAgent.addString("==");
    isAgent.addString(EFAA_OPC_ENTITY_AGENT);

    Bottle isPresent;
    isPresent.addString(EFAA_OPC_OBJECT_PRESENT_TAG);
    isPresent.addString("==");
    isPresent.addInt(1);

    conditionAgent.addList() = isAgent;
    conditionAgent.addString("&&");
    conditionAgent.addList() = isPresent;

    conditionRTO.addList() = isRtobject;
    conditionRTO.addString("&&");
    conditionRTO.addList() = isPresent;


    list<Entity*> PresentEntities;
    list<Relation> listRelations;


    // Create the Relations
    Adjective* present;
    Action* is;
    //Action* isDoing;

    if (bReal)
    {
        PresentEntities = realOPC->Entities(conditionAgent);
        list<Entity*> tmp = realOPC->Entities(conditionRTO);
        PresentEntities.splice(PresentEntities.end(), tmp);
        listRelations = realOPC->getRelations();
        present = realOPC->addOrRetrieveEntity<Adjective>("isPresent");
        present->m_quality = "presence";
        is = realOPC->addOrRetrieveEntity<Action>("is");
        //isDoing = realOPC->addOrRetrieveEntity<Action>("isDoing");
    }
    else
    {
        PresentEntities = mentalOPC->Entities(conditionAgent);
        list<Entity*> tmp = mentalOPC->Entities(conditionRTO);
        PresentEntities.splice(PresentEntities.end(), tmp);
        listRelations = mentalOPC->getRelations();
        present = mentalOPC->addOrRetrieveEntity<Adjective>("isPresent");
        present->m_quality = "presence";
        is = mentalOPC->addOrRetrieveEntity<Action>("is");
        //isDoing = mentalOPC->addOrRetrieveEntity<Action>("isDoing");
    }

    for (list<Entity*>::iterator it_E = PresentEntities.begin(); it_E != PresentEntities.end(); it_E++)
    {
        if (bReal)
            realOPC->addRelation(*it_E, is, present, time_relation);
        else
            mentalOPC->addRelation(*it_E, is, present, time_relation);
        listRelations.push_back(Relation(*it_E, is, present));
    }

    // get the Agents present

    vector<Relation> vRelToAdd,
        vRelToRemove;

    list<Entity*> PresentAgents;
    if (bReal)
        PresentAgents = (realOPC->Entities(conditionAgent));
    else
        PresentAgents = (mentalOPC->Entities(conditionAgent));

    for (list<Entity*>::iterator it_E = PresentAgents.begin(); it_E != PresentAgents.end(); it_E++)
    {
        Agent* TempAgent = dynamic_cast<Agent*>(*it_E);

        list<Relation> AgentBeliefs = TempAgent->beliefs();
        vRelToAdd.clear();
        vRelToRemove.clear();
        bool bRelPresent = false;

        // Searching the relations to Remove
        // for each previous beliefs of an agent
        for (list<Relation>::iterator it_RAg = AgentBeliefs.begin(); it_RAg != AgentBeliefs.end(); it_RAg++)
        {
            // search is the relation is present in the world
            bRelPresent = false;

            //for each relation in the world
            for (list<Relation>::iterator it_RWorl = listRelations.begin(); it_RWorl != listRelations.end(); it_RWorl++)
            {
                if (!bRelPresent)
                {

                    // is the new relation is already known
                    if (it_RAg->toString() == it_RWorl->toString())
                    {
                        bRelPresent = true;
                    }
                }
            }
            // if the previous relation is no more present
            if (!bRelPresent)
                vRelToRemove.push_back(*it_RAg);
        }


        // Searching the relations to Add
        //for each relation in the world
        for (list<Relation>::iterator it_RWorl = listRelations.begin(); it_RWorl != listRelations.end(); it_RWorl++)
        {
            bRelPresent = false;

            // for each previous beliefs of an agent
            for (list<Relation>::iterator it_RAg = AgentBeliefs.begin(); it_RAg != AgentBeliefs.end(); it_RAg++)
            {
                // search is the relation has to be added               
                if (!bRelPresent)
                {
                    // is the new relation is already known
                    if (it_RAg->toString() == it_RWorl->toString())
                        bRelPresent = true;
                }
            }
            // if the previous relation is no more present
            if (!bRelPresent)
                vRelToAdd.push_back(*it_RWorl);
        }


        // Removing the old relations :
        for (vector<Relation>::iterator it_R = vRelToRemove.begin(); it_R != vRelToRemove.end(); it_R++)
        {
            TempAgent->removeBelief(*it_R);
        }


        // Adding the new relations :
        for (vector<Relation>::iterator it_R = vRelToAdd.begin(); it_R != vRelToAdd.end(); it_R++)
        {
            TempAgent->addBelief(*it_R);
        }
        if (bReal)
            realOPC->commit(*it_E);
        else
            mentalOPC->commit(*it_E);
    }

    if (bReal)
        realOPC->commit();
    else
        mentalOPC->commit();

    cout << "done" << endl << endl;
    return bOutput;
}