예제 #1
0
        public override double execute(double dt)
        {
            MascaretApplication appli = MascaretApplication.Instance;

            //bool found = false;
            OrganisationalEntity askedOrg  = null;
            Procedure            askedProc = null;
            Role askedRole = null;

            List <OrganisationalEntity> orgs = appli.AgentPlateform.Organisations;

            appli.VRComponentFactory.Log("CallProcedure");

            for (int iOrg = 0; iOrg < orgs.Count; iOrg++)
            {
                appli.VRComponentFactory.Log(" Org " + orgs[iOrg].name + " ?");
                if (orgs[iOrg].name == action.OrganisationalEntity)
                {
                    appli.VRComponentFactory.Log("Org : " + orgs[iOrg].name + " found");
                    OrganisationalStructure os    = orgs[iOrg].Structure;
                    List <Procedure>        procs = os.Procedures;
                    askedOrg = orgs[iOrg];

                    for (int iP = 0; iP < procs.Count; iP++)
                    {
                        if (procs[iP].name == action.Procedure)
                        {
                            appli.VRComponentFactory.Log("Procedure " + procs[iP].name + " found");
                            askedProc = procs[iP];
                            List <RoleAssignement> assigns = orgs[iOrg].RoleAssignement;

                            appli.VRComponentFactory.Log("Assigns : " + assigns.Count);
                            for (int iAss = 0; iAss < assigns.Count; iAss++)
                            {
                                Agent agt = appli.AgentPlateform.Agents[assigns[iAss].Agent.toString()];
                                askedRole = assigns[iAss].Role;

                                appli.VRComponentFactory.Log("Role : " + assigns[iAss].Role.name + " == " + agt.name);

                                AgentBehaviorExecution pbehavior = agt.getBehaviorExecutingByName("ProceduralBehavior");

                                if (pbehavior != null)
                                {
                                    appli.VRComponentFactory.Log("Procedure launched for " + agt.name);
                                    ProceduralBehavior procBehave = (ProceduralBehavior)(pbehavior);

                                    Dictionary <string, ValueSpecification> procParams = new Dictionary <string, ValueSpecification>();

                                    procBehave.pushProcedureToDo(askedProc, askedOrg, askedRole, procParams);
                                }
                            }
                        }
                    }
                }
            }

            return(0);
        }
        protected void manageInform(ACLMessage msg)
        {
            Agent              agt     = (Agent)(this.Host);
            string             content = msg.Content;
            FIPASLParserResult result  = parseFipaSLExpression(content);

            if (result.isAction)
            {
                if (result.success)
                {
                    AgentBehaviorExecution be = agt.getBehaviorExecutingByName("ProceduralBehavior");
                    if (be != null)
                    {
                        ProceduralBehavior pbe = (ProceduralBehavior)(be);
                        AID aid = new AID(result.action.performer, agt.Aid.PlateformName, agt.Aid.PlateformPort);
                        if (result.isDone)
                        {
                            aid = new AID(result.action.performer, agt.Aid.PlateformName, agt.Aid.PlateformPort);
                            pbe.onActionDone(aid, result.action.actionName);
                        }
                        else
                        {
                            pbe.onActionRunning(aid, result.action.actionName);
                        }
                    }
                }
            }
            else if (result.isEqual)
            {
                string entityName = result.iota.paramName[2];
                string slotName   = result.iota.paramName[0];
                string value      = result.iota.value;

                MascaretApplication.Instance.VRComponentFactory.Log(entityName + "." + slotName + "=" + value);

                KnowledgeBase        kb       = ((Agent)(this.Host)).KnowledgeBase;
                Mascaret.Environment envKB    = kb.Environment;
                List <Entity>        entities = envKB.getEntities();
                foreach (Entity entity in entities)
                {
                    if (entity.name == entityName)
                    {
                        MascaretApplication.Instance.VRComponentFactory.Log("ENTITY ...");
                        foreach (KeyValuePair <string, Slot> s in entity.Slots)
                        {
                            if (s.Value.name == slotName)
                            {
                                s.Value.addValueFromString(value);
                            }
                        }
                    }
                }
            }
        }
예제 #3
0
        void CallProceduralBehaviourExecution(ActionNode action)
        {
            MascaretApplication.Instance.VRComponentFactory.Log(Host.name + "+++++++++++++++++++++++++++########################################### CallProceduralBehaviourExecution  " + action.name);

            MascaretApplication appli = MascaretApplication.Instance;
            //bool found = false;
            OrganisationalEntity askedOrg = null;
            Procedure askedProc = null;
            Role askedRole = null;

            List<OrganisationalEntity> orgs = appli.AgentPlateform.Organisations;

            appli.VRComponentFactory.Log("ProceduralBehaviourExecution");

            for (int iOrg = 0; iOrg < orgs.Count; iOrg++)
            {
                appli.VRComponentFactory.Log("------ Org " + orgs[iOrg].name + " ?");
                if (orgs[iOrg].name == parameters["orgEntity"].getStringFromValue())
                {
                    appli.VRComponentFactory.Log("----------Org : " + orgs[iOrg].name + " found");
                    OrganisationalStructure os = orgs[iOrg].Structure;
                    List<Procedure> procs = os.Procedures;
                    askedOrg = orgs[iOrg];

                    for (int iP = 0; iP < procs.Count; iP++)
                    {
                        if (procs[iP].name == action.Action.name)
                        {
                            appli.VRComponentFactory.Log("Procedure " + procs[iP].name + " found");
                            askedProc = procs[iP];
                            List<RoleAssignement> assigns = orgs[iOrg].RoleAssignement;

                            appli.VRComponentFactory.Log("Assigns : " + assigns.Count);
                            for (int iAss = 0; iAss < assigns.Count; iAss++)
                            {
                                agt = appli.AgentPlateform.Agents[assigns[iAss].Agent.toString()];
                                askedRole = assigns[iAss].Role;
                                if (agt.name == Host.name)
                                {
                                    appli.VRComponentFactory.Log("Role : " + assigns[iAss].Role.name + " == " + agt.name);

                                    AgentBehaviorExecution pbehavior = agt.getBehaviorExecutingByName("ProceduralBehavior");

                                    if (pbehavior != null)
                                    {
                                        appli.VRComponentFactory.Log("Procedure " + askedProc.name + " launched for " + agt.name);
                                        ProceduralBehavior procBehave = (ProceduralBehavior)(pbehavior);

                                        Dictionary<string, ValueSpecification> procParams = new Dictionary<string, ValueSpecification>();

                                        procBehave.pushProcedureToDo(askedProc, askedOrg, askedRole, procParams);

                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
        public override double execute(double dt)
        {
            if (step == 0)
            {
                // StreamWriter file = new StreamWriter("CallBehaviorExecution.txt");
                // file.AutoFlush = true;
                // file.WriteLine("CallBehaviorExecution"); file.Flush();
                if (action.Behavior.GetType().ToString() == "Mascaret.Activity")
                {
                    string procedureName = action.Behavior.name;
                    Agent  agent         = (Agent)Host;
                    string owner         = action.Owner.name;

                    //     file.WriteLine("Agent : " + agent.name); file.Flush();
                    //     file.WriteLine("ProcedureToDo : " + procedureName); file.Flush();
                    //     file.WriteLine("From Action : " + action.name); file.Flush();
                    //     file.WriteLine("From Procedure : " + owner); file.Flush();

                    ProceduralBehavior        pbehavior         = (ProceduralBehavior)(agent.getBehaviorExecutingByName("ProceduralBehavior"));
                    List <ProcedureExecution> runningProcedures = pbehavior.runningProcedures;

                    //     file.WriteLine("NB Running procs : " + runningProcedures.Count); file.Flush();
                    for (int i = 0; i < runningProcedures.Count; i++)
                    {
                        //  file.WriteLine("Running proc : " + runningProcedures[i].procedure.name); file.Flush();
                        if (runningProcedures[i].procedure.name == owner)
                        {
                            organisation = runningProcedures[i].organisation;
                            //  file.WriteLine("Organisation : " + organisation.name); file.Flush();
                        }
                    }

                    OrganisationalStructure os    = organisation.Structure;
                    List <Procedure>        procs = os.Procedures;
                    for (int iP = 0; iP < procs.Count; iP++)
                    {
                        if (procs[iP].name == procedureName)
                        {
                            procedure = procs[iP];
                        }
                    }

                    List <RoleAssignement> assigns = organisation.RoleAssignement;
                    //        file.WriteLine("Assigns : " + assigns.Count); file.Flush();
                    for (int iAss = 0; iAss < assigns.Count; iAss++)
                    {
                        Agent agt = MascaretApplication.Instance.AgentPlateform.Agents[assigns[iAss].Agent.toString()];
                        AgentBehaviorExecution pbehavior2 = agt.getBehaviorExecutingByName("ProceduralBehavior");

                        if (pbehavior2 != null)
                        {
                            //   file.WriteLine("Procedure launched for " + agt.name); file.Flush();
                            ProceduralBehavior procBehave = (ProceduralBehavior)(pbehavior2);

                            Dictionary <string, ValueSpecification> procParams = new Dictionary <string, ValueSpecification>();

                            procBehave.pushProcedureToDo(procedure, organisation, assigns[iAss].Role, procParams);
                        }
                    }
                }
                //   file.Close();
                step++;
            }
            else
            {
                OrganisationalStructure os      = organisation.Structure;
                List <RoleAssignement>  assigns = organisation.RoleAssignement;
                for (int iAss = 0; iAss < assigns.Count; iAss++)
                {
                    Agent agt = MascaretApplication.Instance.AgentPlateform.Agents[assigns[iAss].Agent.toString()];
                    AgentBehaviorExecution pbehavior = agt.getBehaviorExecutingByName("ProceduralBehavior");

                    if (pbehavior != null)
                    {
                        ProceduralBehavior        procBehave        = (ProceduralBehavior)(pbehavior);
                        List <ProcedureExecution> runningProcedures = procBehave.runningProcedures;
                        bool foundP = false;
                        for (int iP = 0; iP < runningProcedures.Count; iP++)
                        {
                            if (runningProcedures[iP].procedure.name == procedure.name)
                            {
                                foundP = true;
                            }
                        }
                        if (!foundP)
                        {
                            return(0);
                        }
                    }
                }
            }
            return(0.1);
        }
        protected void manageRequestAction(ACLMessage msg)
        {
            Agent agt = (Agent)(this.Host);

            string content = msg.Content;

            FIPASLParserResult result = parseFipaSLExpression(content);

            if (result.success)
            {
                if (result.isAction)
                {
                    bool done = false;

                    AgentBehavior behavior = agt.getBehaviorByBame(result.action.actionName);

                    if (behavior != null)
                    {
                        Dictionary <string, ValueSpecification> parameters = new Dictionary <string, ValueSpecification>();

                        if (result.action.paramName.Count > 0)
                        {
                            for (int i = 0; i < result.action.paramName.Count; i++)
                            {
                                LiteralString stringValue = new LiteralString(result.action.paramValue[i]);
                                parameters.Add(result.action.paramName[i], (ValueSpecification)(stringValue));
                            }
                        }
                        else
                        {
                            for (int i = 0; i < result.action.paramName.Count; i++)
                            {
                                LiteralString stringValue = new LiteralString(result.action.paramValue[i]);
                                parameters.Add("param" + i + '0', (ValueSpecification)(stringValue));
                            }
                        }

                        BehaviorScheduler.Instance.executeBehavior(behavior, agt, parameters, false);
                        ACLMessage reponse = new ACLMessage(ACLPerformative.AGREE);
                        reponse.Content = msg.Content;
                        reponse.Receivers.Add(msg.Sender);
                        agt.send(reponse);
                        done = true;
                    }

                    Class     classifier = agt.Classifier;
                    Operation operation  = null;
                    if (classifier != null && classifier.Operations.ContainsKey(result.action.actionName))
                    {
                        operation = classifier.Operations[result.action.actionName];
                        //System.Console.WriteLine("Operation:" + operation.getFullName());
                    }
                    if (operation != null)
                    {
                        Dictionary <string, ValueSpecification> param = new Dictionary <string, ValueSpecification>();
                        List <Parameter> parameters = operation.Parameters;
                        List <string>    paramValue = result.action.paramValue;
                        List <string>    paramName  = result.action.paramName;

                        if (parameters.Count == paramValue.Count)
                        {
                            for (int i = 0; i < parameters.Count; i++)
                            {
                                // Pour tous les parametres issus de l'operation
                                string parameterName = parameters[i].name;

                                // Cherche l'indice de ce parameter dans paramsName
                                int indice = 0; bool found = false;
                                while (!found && indice < paramName.Count)
                                {
                                    if (paramName[indice] == parameterName)
                                    {
                                        found = true;
                                    }
                                    else
                                    {
                                        indice++;
                                    }
                                }

                                if (found)
                                {
                                    string strVal = paramValue[indice];

                                    System.Console.Write("Type : " + parameters[i].Type.getFullName());
                                    System.Console.Write(" - Name: " + parameters[i].name);
                                    System.Console.WriteLine(" - Value: " + strVal);

                                    string typeName = parameters[i].Type.getFullName();
                                    if (typeName == "boolean" || typeName == "integer" || typeName == "real" || typeName == "string")
                                    {
                                        param.Add(parameters[i].name, parameters[i].Type.createValueFromString(strVal.ToLower()));
                                    }
                                    else
                                    {
                                        try
                                        {
                                            InstanceSpecification inst = MascaretApplication.Instance.getEnvironment().getInstance(strVal.ToLower());
                                            param.Add(parameters[i].name, new InstanceValue(inst));
                                        }
                                        catch (NullReferenceException e)
                                        {
                                        }
                                    }
                                }
                            }

                            //BehaviorScheduler.Instance.executeBehavior(operation.Method, agt, param, false);

                            /*
                             *  List<ActionNode> possibleTranslation = _translateOperationToActions(operation);
                             *
                             *  shared_ptr<AgentBehaviorExecution> abe = agt->getBehaviorExecutionByName("ProceduralBehavior"); //hasslich... ja...
                             *  shared_ptr<ProceduralBehavior> pbe  = shared_dynamic_cast<ProceduralBehavior> (abe);
                             *
                             *  if (possibleTranslation.size() && pbe)
                             *  {
                             *      //add actionNode in ProceduralBehavior
                             *
                             *      pbe->pushActionToDo(possibleTranslation[0]);
                             *  }
                             *  else
                             *  {
                             *      //call method
                             */
                            BehaviorExecution be = BehaviorScheduler.Instance.executeBehavior(operation.Method, agt, param, false);

                            /*
                             * if (be != null)
                             * {
                             *  be.addCallbackOnBehaviorStop(bind(&SimpleCommunicationBehavior::_onBehaviorStop,this,_1));
                             *
                             *  _requestedAction[be->getSpecification()->getName()].push_back(msg->getSender());
                             * }*/
                            // }



                            ACLMessage reponse = new ACLMessage(ACLPerformative.AGREE);
                            reponse.Content = msg.Content;

                            System.Console.WriteLine("Content-Sent: " + reponse.Content);
                            reponse.Receivers.Add(msg.Sender);
                            agt.send(reponse);
                        }
                        else
                        {
                            ACLMessage reponse    = new ACLMessage(ACLPerformative.NOT_UNDERSTOOD);
                            string     contentRep = "";
                            contentRep     += content;
                            reponse.Content = contentRep;
                            reponse.Receivers.Add(msg.Sender);
                            agt.send(reponse);
                        }
                    }
                    else
                    {
                        string procName = result.action.actionName;
                        bool   found    = false;
                        OrganisationalEntity askedOrg  = null;
                        Procedure            askedProc = null;
                        Role askedRole = null;
                        List <OrganisationalEntity> orgs = agt.Plateform.Organisations;

                        for (int iOrg = 0; iOrg < orgs.Count; iOrg++)
                        {
                            List <RoleAssignement> assigns = orgs[iOrg].RoleAssignement;

                            for (int iAss = 0; iAss < assigns.Count; iAss++)
                            {
                                if (assigns[iAss].Agent.toString() == agt.Aid.toString())
                                {
                                    OrganisationalStructure os    = orgs[iOrg].Structure;
                                    List <Procedure>        procs = os.Procedures;

                                    for (int iP = 0; iP < procs.Count; iP++)
                                    {
                                        System.Console.WriteLine(procName + " / " + procs[iP].name);
                                        if (procs[iP].name == procName)
                                        {
                                            askedProc = procs[iP];
                                            askedOrg  = orgs[iOrg];
                                            askedRole = assigns[iAss].Role;
                                            found     = true;
                                        }
                                    }
                                }
                            }
                        }

                        if (found)
                        {
                            ACLMessage reponse = new ACLMessage(ACLPerformative.AGREE);
                            reponse.Content = msg.Content;
                            reponse.Receivers.Add(msg.Sender);
                            agt.send(reponse);

                            // Recherche du comportement procedural
                            // UGGLY
                            //System.Console.WriteLine("1");
                            AgentBehaviorExecution pbehavior = agt.getBehaviorExecutingByName("ProceduralBehavior");

                            //AgentBehaviorExecution> behavior2 = agt->getBehaviorExecutionByName("ActionListenerBehavior");
                            //System.Console.WriteLine("2");
                            if (pbehavior != null)
                            {
                                ProceduralBehavior procBehave = (ProceduralBehavior)(pbehavior);

                                Dictionary <string, ValueSpecification> procParams = new Dictionary <string, ValueSpecification>();
                                for (int i = 0; i < result.action.paramName.Count; i++)
                                {
                                    LiteralString stringValue = new LiteralString(result.action.paramValue[i]);
                                    procParams.Add(result.action.paramName[i], stringValue);
                                }

                                //System.Console.WriteLine("3");
                                Activity act = askedProc.Activity;
                                //List<ActivityNode>  nodes  = act.Nodes;

                                procBehave.pushProcedureToDo(askedProc, askedOrg, askedRole, procParams);
                                //procBehave.restart();

                                ACLMessage reponse2 = new ACLMessage(ACLPerformative.AGREE);
                                reponse2.Content = msg.Content;
                                reponse2.Receivers.Add(msg.Sender);
                                agt.send(reponse2);
                            }

                            /*
                             * else if (behavior2)
                             * {
                             *  shared_ptr<ActionListenerBehavior> procBehave  = shared_dynamic_cast<ActionListenerBehavior> (behavior2);
                             *
                             *  procBehave->pushProcedureToDo(askedProc, askedOrg, askedRole, Parameters());
                             *  procBehave->restart();
                             *
                             *  shared_ptr<ACLMessage> reponse = make_shared<ACLMessage>(AGREE);
                             *  reponse->setContent(msg->getContent());
                             *  reponse->addReceiver(msg->getSender());
                             *  agt->send(reponse);
                             * }*/
                            else
                            {
                                if (!done)
                                {
                                    ACLMessage reponse3   = new ACLMessage(ACLPerformative.NOT_UNDERSTOOD);
                                    string     contentRep = "";
                                    contentRep      += result.action.actionName;
                                    reponse3.Content = contentRep;
                                    reponse3.Receivers.Add(msg.Sender);
                                    agt.send(reponse3);
                                }
                            }
                        }
                        else
                        {
                            if (!done)
                            {
                                ACLMessage reponse    = new ACLMessage(ACLPerformative.NOT_UNDERSTOOD);
                                string     contentRep = "";
                                contentRep     += result.action.actionName;
                                reponse.Content = contentRep;
                                reponse.Receivers.Add(msg.Sender);
                                agt.send(reponse);
                            }
                        }
                    }
                }
                else
                {
                    // Proposition Non encore traite....
                }
            }
            else
            {
                System.Console.WriteLine("[SimpleCommunicationBehavior Warning] Parsing error. Message content parsing error: " + content);

                ACLMessage reponse    = new ACLMessage(ACLPerformative.NOT_UNDERSTOOD);
                string     contentRep = "";
                contentRep += content;

                reponse.Content = contentRep;
                reponse.Receivers.Add(msg.Sender);
                agt.send(reponse);
            }
        }