Exemple #1
0
Relation mapRelation(map<string,string> dico, Relation child, Relation current)
{

    string newSubject = child.subject();
    map<string,string>::iterator place = dico.find(newSubject);
        if (place != dico.end() )
            newSubject = place->second;

        string newObject =child.object();
        place = dico.find(newObject);
        if (place != dico.end() )
            newObject = place->second;

        string newVerb = child.verb();
        //place = dico.find(newVerb);
        //if (place != dico.end() )
        //    newVerb = place->second;
        
        string newCTime = child.complement_time();
        place = dico.find(newCTime);
        if (place != dico.end() )
            newCTime = place->second;

        string newCManner = child.complement_manner();
        place = dico.find(newCManner);
        if (place != dico.end() )
            newCManner = place->second;

        string newCPlace = child.complement_place();
        place = dico.find(newCPlace);
        if (place != dico.end() )
            newCPlace = place->second;

        return Relation(newSubject,newVerb,newObject,newCPlace,newCTime,newCManner);
}
Exemple #2
0
bool OPCClient::containsRelation(Relation r)
{    
    Entity* subject = getEntity(r.subject());
    Entity* object = getEntity(r.object());
    Entity* cPlace = getEntity(r.complement_place());
    Entity* cTime = getEntity(r.complement_time());
    Entity* cManner = getEntity(r.complement_manner());
    Entity* verb = getEntity(r.verb());
    return containsRelation(subject,verb,object,cPlace,cTime,cManner);
}
Exemple #3
0
//Adds a relation between 2 entities
bool OPCClient::addRelation(Relation r, double lifeTime)
{   
    Entity* subject = getEntity(r.subject());
    Entity* object = getEntity(r.object());
    Entity* cPlace = getEntity(r.complement_place());
    Entity* cTime = getEntity(r.complement_time());
    Entity* cManner = getEntity(r.complement_manner());
    Entity* verb = getEntity(r.verb());
    if (subject == NULL || verb == NULL)
    {
        cerr<<"Verb and subject should exist before you try to add a relation"<<endl;
        return false;
    }

    return addRelation(subject,verb,object,lifeTime,cPlace,cTime,cManner);
}
Exemple #4
0
Action Action::express(Relation r)
{
    Action a;
    string initial = this->asBottle().toString().c_str();
    string final = initial.c_str();
    replace_all(final,this->initialDescription.subject().c_str(),"___subject");
    replace_all(final,this->initialDescription.object().c_str(),"___object");
    replace_all(final,this->initialDescription.complement_time().c_str(),"___compTime");
    replace_all(final,this->initialDescription.complement_place().c_str(),"___compPlace");
    replace_all(final,this->initialDescription.complement_manner().c_str(),"___compManner");
    replace_all(final,"___subject", r.subject());
    replace_all(final,"___object", r.object());
    replace_all(final,"___compTime", r.complement_time());
    replace_all(final,"___compPlace", r.complement_place());
    replace_all(final,"___compManner", r.complement_manner());
    Bottle b(final.c_str());
    a.fromBottle(b);
    return a;
}
Exemple #5
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;
}