예제 #1
0
bool AdaptiveLayer::handleSpeech()
{
    bool gotSignal = false;
    Bottle* speechCmd = iCub->getSpeechClient()->STT(false);
    if (speechCmd)
    {
        if (speechCmd->size() != 2)
        {
            std::cout << "in adaptativeLayer::handleSpeech | error: bottle received size !=2" << std::endl;
            return false;
        }
        gotSignal = true;
        std::cout << speechCmd->toString() << std::endl;
        std::cout << speechCmd->toString() << std::endl;
        cout<<"Raw sentence: |"<<speechCmd->get(0).toString()<<"|"<<endl;
        //cout<<"Semantic:"<<speechCmd->get(1).toString();

        Bottle* semanticBottle = speechCmd->get(1).asList();

        string sentenceType = semanticBottle->get(0).asString();

        if (sentenceType == "SUBNODE")
        {
            Bottle keyBot;
            keyBot.addString(semanticBottle->get(1).asList()->check("keyword",Value("none")).asString());
            pSpeechRecognizerKeywordOut.write(keyBot);
            return true;
        }

        if (sentenceType == "miscSentences")
        {
            string rawSentence = speechCmd->get(0).toString();
            cout<<"Catched a misc sentence : "<<semanticBottle->toString().c_str()<<endl;
            return true;
        }

        //We trigger a scenario from speech
        if (sentenceType == "GAME")
        {
            Bottle keyBot;
            string gameName = semanticBottle->get(1).asList()->find("gameName").asString();
            return true;
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////
        //Create a relation for information exchange
        Bottle bRelation;
        Relation relationForm = this->getRelationFromSemantic(*semanticBottle->get(1).asList());
        cout<< "Sentence type : "<<sentenceType<<endl;
        cout<< "Relation form : "<<relationForm.toString()<<endl;
        string answerFromRobot = "";

        if (sentenceType == "IMPERATIVE")
        {
            //Execute the action
        }
        else if (sentenceType == "AFFIRMATIVE")
        {
            if (!iCub->opc->containsRelation(relationForm))
            {
                iCub->opc->addRelation(relationForm);
                answerFromRobot = "Ok, I will know that " + relationForm.toString();
            }
            else
            {
                answerFromRobot = "I already knew that.";
            }
            //Update the other model to reflect his knowledge
            Agent* partner = dynamic_cast<Agent*>(iCub->opc->getEntity("partner"));
            partner->addBelief(relationForm);
            iCub->opc->commit(partner);
        }
        else //interrogative
        {
            if (sentenceType == "INTERROGATIVE_WHO")
                relationForm.m_subject = "?";
            if (sentenceType == "INTERROGATIVE_WHAT")
                relationForm.m_object = "?";
            if (sentenceType == "INTERROGATIVE_WHEN")
                relationForm.m_complement_time = "?";
            if (sentenceType == "INTERROGATIVE_WHERE")
                relationForm.m_complement_place = "?";
            if (sentenceType == "INTERROGATIVE_HOW")
                relationForm.m_complement_manner = "?";

            Relation relationReturn(relationForm);


            //Retrieve from OPC
            string matchingSubject;
            string matchingObject;
            string matchingVerb;
            string matchingPlace;
            string matchingTime;
            string matchingManner;
            relationForm.subject() != "?" && relationForm.subject() != "none" ?     matchingSubject = relationForm.subject()  :    matchingSubject = "any";
            relationForm.verb() != "?"  && relationForm.verb() != "none"    ?       matchingVerb = relationForm.verb()      :    matchingVerb = "any";
            relationForm.object() != "?" && relationForm.object() != "none"   ?     matchingObject = relationForm.object()    :    matchingObject = "any";
            relationForm.complement_place()   != "?" && relationForm.complement_place() != "none" ? matchingPlace =  relationForm.complement_place() :   matchingPlace = "any" ;
            relationForm.complement_time()    != "?" && relationForm.complement_time() != "none" ? matchingTime = relationForm.complement_time()   :   matchingTime = "any"   ;
            relationForm.complement_manner()  != "?" && relationForm.complement_manner() != "none" ? matchingManner = relationForm.complement_manner():  matchingManner = "any" ;
            list<Relation> allAnswers = iCub->opc->getRelationsMatching(matchingSubject,matchingVerb,matchingObject,matchingPlace,matchingTime,matchingManner);

            //We say the first thing recovered. Ideally we should see if our model of the other already contains it.
            list<Relation> realAnswers;
            for(list<Relation>::iterator pAns = allAnswers.begin(); pAns!= allAnswers.end(); pAns++)
            {
                //We check if this relation answer to the specific interrogation
                bool isAnswering = false;
                if (relationForm.subject() == "?" && pAns->subject() != "none")
                    isAnswering = true;
                if (relationForm.object() == "?" && pAns->object() != "none")
                    isAnswering = true;
                if (relationForm.complement_place() == "?" && pAns->complement_place() != "none")
                    isAnswering = true;
                if (relationForm.complement_time() == "?" && pAns->complement_time() != "none")
                    isAnswering = true;
                if (relationForm.complement_manner() == "?" && pAns->complement_manner() != "none")
                    isAnswering = true;

                if (isAnswering)
                    realAnswers.push_back(*pAns);
            }

            if (realAnswers.size() >0)
            {

                Agent* partner = dynamic_cast<Agent*>(iCub->opc->getEntity("partner"));
                Relation relationToBeStated = realAnswers.front();
                bool partnerShouldHaveKnown = true;

                //Go through all the possible answers and take the one that is not known by our modl of the other
                for(list<Relation>::iterator pAns = realAnswers.begin(); pAns!= realAnswers.end(); pAns++)
                {
                    //If we know that the partner does't know this relation we say it
                    if (!partner->checkBelief(*pAns))
                    {
                        relationToBeStated = *pAns;
                        partnerShouldHaveKnown = false;
                        break;
                    }
                }

                //Update the other model
                partner->addBelief(relationToBeStated);
                iCub->opc->commit(partner);

                //Create good answer from relation
                answerFromRobot = "I know that ";
                answerFromRobot += relationToBeStated.subject();
                answerFromRobot += " " ;
                answerFromRobot += relationToBeStated.verb();
                answerFromRobot += " " ;
                if (relationToBeStated.object() != "none")
                {
                    answerFromRobot += " the " ;
                    answerFromRobot += relationToBeStated.object();
                    answerFromRobot += " " ;
                }
                if (relationToBeStated.complement_place() != "none")
                {
                    answerFromRobot += "in the " ;
                    answerFromRobot += relationToBeStated.complement_place();
                    answerFromRobot += " " ;
                }
                if (relationToBeStated.complement_time() != "none")
                {
                    answerFromRobot += relationToBeStated.complement_time();
                    answerFromRobot += " " ;
                }
                if (relationToBeStated.complement_manner() != "none")
                {
                    answerFromRobot += relationToBeStated.complement_manner();
                    answerFromRobot += " " ;
                }
                answerFromRobot +=".";

                if( partnerShouldHaveKnown)
                {
                    answerFromRobot += " But I thought you already knew that.";
                }

            }
            else
                answerFromRobot = "I do not know...";
        }

        iCub->say(answerFromRobot);

        //////////////ABM//////////////////////////////////////////

        // save in the ABM the sentence of the Human.
        list<string> roles;
        list<string> arguments;

        roles.push_back("raw");
        arguments.push_back(speechCmd->get(0).toString().c_str());
        roles.push_back("speaker");
        arguments.push_back("partner");
        roles.push_back("semantic");
        arguments.push_back(bRelation.toString().c_str());

//        iCub->getABMClient()->sendActivity("action",sentenceType.c_str(),"mainLoop",arguments, roles);

        // Answer from the robot
        arguments.clear();
        roles.clear();

        roles.push_back("raw");
        arguments.push_back(answerFromRobot);
        roles.push_back("speaker");
        arguments.push_back("icub");

//        iCub->getABMClient()->sendActivity("action",sentenceType.c_str(),"mainLoop",arguments, roles);

    }
    return gotSignal;
}
예제 #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;
}