Exemple #1
0
        public void pushProcedureToDo(Procedure proc, OrganisationalEntity org, Role role, Dictionary <String, ValueSpecification> parameters)
        {
            Agent agt = (Agent)(this.Host);

            ProcedureExecution procInfo = new ProcedureExecution(proc, org, agt);

            procInfo.buildAffectations(parameters, this.Host);

            /* Search if the current agent have something to do in this procedure */
            List <ActivityPartition> parts = proc.Activity.Partitions;
            bool found = false;

            for (int iP = 0; iP < parts.Count; iP++)
            {
                if (parts[iP].name == role.name)
                {
                    //   file.WriteLine("[ProceduralBehavior Info] Found a role " + role.name + " in procedure " + proc.name); file.Flush();
                    found = true;
                }
            }

            runningProcedures.Add(procInfo);
            ispause = false;

            if (!found)
            {
                //inform all agents that this agent's role in this procedure has ended (used to unblock tokens in ProcedureExecution)
                sendProcedureDoneMessage(procInfo);
                runningProcedures.RemoveAt(runningProcedures.Count - 1); //remove it..
                // ispause = true;
            }
        }
Exemple #2
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);
        }
 public void addOrganisationalEntity(OrganisationalEntity entity)
 {
     entities.Add(entity);
 }
        protected OrganisationalEntity _createOrganisation(XElement node)
        {
            //StreamWriter file = new StreamWriter("Organisation.txt");

            string name = "";
            OrganisationalEntity organisation = null;
            XAttribute attr = (XAttribute)node.Attribute("name");
            if (attr != null) name = attr.Value;

            foreach (XElement struc in node.Elements())
            {
                if (struc.Name.LocalName == "Structure")
                {
                    string structName = struc.Value;

                    OrganisationalStructure structOrg = null;

                    foreach (OrganisationalStructure currentOrg in agentPlateform.Structures)
                    {
                        if (currentOrg.name == structName)
                        {
                            structOrg = currentOrg;
                            break;
                        }
                    }
                    if (structOrg == null)
                    {  }
                    else
                    {
                      //  MascaretApplication.Instance.logfile.WriteLine("Structure found"); MascaretApplication.Instance.logfile.Flush();
                        organisation = new OrganisationalEntity(name);
                        organisation.Structure = structOrg;

                        structOrg.Entities.Add(organisation);

                        foreach (XElement affects in node.Elements())
                        {
                            if (affects.Name.LocalName == "Affect")
                            {
                                attr = (XAttribute)affects.Attribute("type");

                                if (attr != null && attr.Value == "Role")
                                {

                                //    file.WriteLine("Affect Role"); file.Flush();
                                    XElement roleNode = null, agentNode = null, aidNode = null;

                                    foreach (XElement currentNode in affects.Elements())
                                    {
                                        if (currentNode.Name.LocalName == "Role")
                                            roleNode = currentNode;
                                        else if (currentNode.Name.LocalName == "Agent")
                                            agentNode = currentNode;
                                        else if (currentNode.Name.LocalName == "AID")
                                            aidNode = currentNode;
                                    }

                                    string agentName = "";
                                    string plateformeName = "localhost";
                                    int plateformePort = agentPlateform.PortNumber;
                                    if (aidNode != null)
                                    {
                                        foreach (XElement currentNode in aidNode.Elements())
                                        {
                                            if (currentNode.Name.LocalName == "Name")
                                            {
                                                agentName = currentNode.Value;
                                            }
                                            else if (currentNode.Name.LocalName == "Plateform")
                                            {
                                                plateformeName = currentNode.Value;
                                            }
                                            else if (currentNode.Name.LocalName == "Port")
                                            {
                                                string ports = currentNode.Value;
                                                plateformePort = int.Parse(ports);
                                            }
                                        }

                                    }
                                    else
                                    {
                                        agentName = agentNode.Value;
                                    }

                                    string roleName = roleNode.Value;

                                    if (plateformeName == "localhost") plateformeName = agentPlateform.name;

                                    AID aid = new AID(agentName, plateformeName, plateformePort);

                                    Role role = null;

                                //    file.WriteLine("Nb Roles : " + structOrg.Roles.Count); file.Flush();

                                    foreach (Role currentRole in structOrg.Roles)
                                    {
                                     //   file.WriteLine(currentRole.name + " == " + roleName); file.Flush();
                                        if (currentRole.name == roleName)
                                        {
                                            role = currentRole;
                                            break;
                                        }
                                    }
                                    if (role == null)
                                    {  }
                                    else
                                    {
                                     //   file.WriteLine("Assign"); file.Flush();
                                        RoleAssignement assignement = new RoleAssignement(organisation);
                                        assignement.Agent = aid;
                                        assignement.Role = role;
                                        organisation.RoleAssignement.Add(assignement);
                                    }

                                }
                                else if (attr != null && attr.Value == "Ressource")
                                {
                                    XElement ressourceNode = null, entityNode = null;
                                    string ressourceName = "";
                                    string entityName = "";

                                    foreach (XElement currentNode in affects.Elements())
                                    {
                                        if (currentNode.Name.LocalName == "Ressource")
                                            ressourceNode = currentNode;

                                        else if (currentNode.Name.LocalName == "Entity")
                                            entityNode = currentNode;
                                    }

                                    ressourceName = ressourceNode.Value;
                                    entityName = entityNode.Value;

                                    //Debug.Log( " Affectation Ressource : " + ressourceName + " = " + entityName );
                                    // Recherche de l'entite .....
                                  //  MascaretApplication.Instance.logfile.WriteLine("Ressource affect looking entity : " + entityName);
                                  //  MascaretApplication.Instance.logfile.Flush();
                                    InstanceSpecification entite = environment.getInstance(entityName);
                                    if (entite == null) {
                                     //   MascaretApplication.Instance.logfile.WriteLine("Not found");
                                     //   MascaretApplication.Instance.logfile.Flush();
                                    }

                                    // Recherche de la ressource
                                    Ressource res = null;
                                    //Debug.Log( " *** " + structOrg.Ressources.Count );
                                    foreach (Ressource currentRes in structOrg.Ressources)
                                    {
                                     //   MascaretApplication.Instance.logfile.WriteLine( "Resource search: comparing " + currentRes.name + " to " + ressourceName );
                                     //   MascaretApplication.Instance.logfile.Flush();
                                        if (currentRes.name == ressourceName)
                                        {
                                          //  MascaretApplication.Instance.logfile.WriteLine("found");
                                          //  MascaretApplication.Instance.logfile.Flush();
                                            res = currentRes;
                                            break;
                                        }
                                    }

                                    // Creation de l'assignement
                                    RessourceAssignement ra = new RessourceAssignement();
                                    ra.Entity = ((Entity)entite);
                                    ra.Ressource = res;

                                    organisation.RessourcesAssignement.Add(ra);

                                }
                            }
                        }
                    }
                }
            }

            return organisation;
        }
Exemple #5
0
 public RoleAssignement(OrganisationalEntity organisation)
 {
     role              = new Role("");
     agent             = new AID("", "", 0);
     this.organisation = organisation;
 }
 public void addOrganisationalEntity(OrganisationalEntity entity)
 {
     entities.Add(entity);
 }
        public ProcedureExecution(Procedure procedure, OrganisationalEntity organisation, Agent agent)
        {
            this.procedure = procedure;
            this.organisation = organisation;
            this.agent = agent;

            agentToPartition = new Dictionary<string, ActivityPartition>();
            partitionToAgent = new Dictionary<ActivityPartition, AID>();
            activeTokens = new List<ActivityExecutionToken>();
            affectations = new Dictionary<string, InstanceSpecification>();
            activityParams = new Dictionary<string, ValueSpecification>();
            actionsRunning = new List<KeyValuePair<string, ActionNode>>();
            actionsDone = new List<ActionNode>();
            allActionsDone = new Dictionary<string, List<ActionNode>>(); // key: AID
            allActionsDoneTimestamps = new Dictionary<string, List<TimeExpression>>();

            for (int i = 0; i < procedure.Activity.Partitions.Count; i++)
            {
                ActivityPartition partition = procedure.Activity.Partitions[i];
                List<RoleAssignement> assignements = organisation.RoleAssignement;
                for (int j = 0; j < assignements.Count; j++)
                {
                    if (partition.name == assignements[j].Role.name)
                    {
                        agentToPartition.Add(assignements[j].Agent.toString(), partition);
                        partitionToAgent.Add(partition, assignements[j].Agent);
                        Agent agt = MascaretApplication.Instance.AgentPlateform.Agents[assignements[j].Agent.toString()];
                        if (agt == null) MascaretApplication.Instance.VRComponentFactory.Log("CA VA TRANCHER");
                        affectations.Add(partition.name, agt);
                    }
                }
            }

            ActivityExecutionToken initialToken = new ActivityExecutionToken();
            initialToken.currentLocation = procedure.Activity.Initial;

            if (initialToken.currentLocation == null)
            {
                System.Console.WriteLine("ERROR: no initial node found, not allowed");
                System.Console.WriteLine("Procedure will not start.");
                return;
            }

            List<ActivityEdge> controlFlows = procedure.Activity.Initial.getOutgoingControlFlowEdges();

            if (controlFlows.Count == 1)
            {
                initialToken.outgoingEdge = controlFlows[0];
                activeTokens.Add(initialToken);

                ActionNode dummyActionVariable = null;
                tryToAdvanceToken(initialToken, dummyActionVariable,true); //first advancement
            }
            else
            {
                if (controlFlows.Count == 0)
                {
                    System.Console.WriteLine("WARNING: initial node has no outgoing edges");
                }
                else
                {
                    System.Console.WriteLine("ERROR: multiple outgoing edges for the initial node, not allowed");
                }
                System.Console.WriteLine("Procedure will not start.");
            }
        }
        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;
        }
 public RoleAssignement(OrganisationalEntity organisation)
 {
     role = new Role("");
     agent = new AID("", "", 0);
     this.organisation = organisation;
 }
        public override void manageRequest(HttpRequest req)
        {
            string orgName = req.parameters["alias"];

            req.response.write("<html>");
            req.response.write("<META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            req.response.write("<body>");

            OrganisationalEntity        org  = null;
            List <OrganisationalEntity> orgs = MascaretApplication.Instance.AgentPlateform.Organisations;
            bool found = false;
            int  i     = 0;

            while (!found && i < orgs.Count)
            {
                if (orgs[i].name == orgName)
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if (found)
            {
                org = orgs[i];
            }

            OrganisationalStructure struc = org.Structure;

            req.response.write("<H2>Description</H2>");
            req.response.write(org.name);

            req.response.write("<H2>Affectations</H2>");
            List <RoleAssignement> assignements = org.RoleAssignement;

            req.response.write("<ul>");
            for (int iA = 0; iA < assignements.Count; iA++)
            {
                string agentAid = assignements[iA].Agent.toString();
                Role   role     = assignements[iA].Role;
                req.response.write("<li>");
                req.response.write(agentAid);
                req.response.write(" / ");
                req.response.write(role.name);
                req.response.write(" : <a href=\"RoleClass?alias=");
                req.response.write(struc.name);
                req.response.write("&role=");
                req.response.write(role.name);
                req.response.write("\" target = \"Body\">");
                req.response.write(role.RoleClass.name);
                req.response.write("</a></li>");
            }
            req.response.write("</ul>");

            req.response.write("<H2>Procedures</H2>");
            List <Procedure> procs = struc.Procedures;

            req.response.write("<ul>");
            for (int iP = 0; iP < procs.Count; iP++)
            {
                req.response.write("<li>");
                req.response.write("<a href=\"Procedure?alias=");
                req.response.write(procs[iP].name);
                req.response.write("&org=");
                req.response.write(org.name);
                req.response.write("\" target = \"Body\">");
                req.response.write(procs[iP].name);
                req.response.write("</a></li>");
            }
            req.response.write("</ul>");

            req.response.write("<H2>Structure</H2>");
            req.response.write("<a href=\"OrgStruct?alias=");
            req.response.write(struc.name);
            req.response.write("\" target = \"Body\">");
            req.response.write(struc.name);
            req.response.write("</a>");

            req.response.write("</body>");
            req.response.write("</html>");
        }
        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);
        }
        public override void manageRequest(HttpRequest req)
        {
            string procName = req.parameters["alias"];
            string orgName  = req.parameters["org"];

            req.response.write("<html>");
            req.response.write("<META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            req.response.write("<body>");

            OrganisationalEntity        org  = null;
            List <OrganisationalEntity> orgs = MascaretApplication.Instance.AgentPlateform.Organisations;
            bool found = false;
            int  i     = 0;

            while (!found && i < orgs.Count)
            {
                if (orgs[i].name == orgName)
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if (found)
            {
                org = orgs[i];
            }

            OrganisationalStructure struc = org.Structure;

            List <Procedure> procs = struc.Procedures;
            Procedure        proc  = null;

            found = false;
            i     = 0;
            while (!found && i < procs.Count)
            {
                if (procs[i].name == procName)
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if (found)
            {
                proc = procs[i];
            }

            Environment env = MascaretApplication.Instance.getEnvironment();

            List <Parameter>    parameters = new List <Parameter>();
            List <ActivityNode> nodes      = proc.Activity.Nodes;

            for (int iNode = 0; iNode < nodes.Count; iNode++)
            {
                if (nodes[iNode].Kind == "parameter")
                {
                    parameters.Add(((ActivityParameterNode)(nodes[iNode])).Parameter);
                }
            }

            req.response.write("<HR>");
            req.response.write("<H2>Parametres</H2>");
            req.response.write("<FORM METHOD=GET action=\"launchProcedure\">");
            req.response.write("<INPUT type=\"hidden\" name=\"org\" value=\"");
            req.response.write(orgName);
            req.response.write("\"/>");
            req.response.write("<INPUT type=\"hidden\" name=\"alias\" value=\"");
            req.response.write(procName);
            req.response.write("\"/>");
            req.response.write("<ul>");
            for (int iParam = 0; iParam < parameters.Count; iParam++)
            {
                Parameter param = parameters[iParam];
                req.response.write("<li>");
                Classifier type = param.Type;
                req.response.write(type.name);
                req.response.write("\t");
                req.response.write(param.name);
                if (_isBaseType2(type))
                {
                    req.response.write(" = ");
                    req.response.write(" <INPUT name=_");
                    req.response.write(param.name);
                    req.response.write(">");
                    req.response.write("</li>");
                }
                else
                {
                    req.response.write(" <select name=\"_");
                    req.response.write(param.name);
                    req.response.write("\">");

                    foreach (KeyValuePair <string, InstanceSpecification> instance in env.InstanceSpecifications)
                    {
                        if (instance.Value.Classifier != null)
                        {
                            if (instance.Value.Classifier.isA(type))
                            {
                                req.response.write(" = <option value=\"");
                                req.response.write(instance.Key);
                                req.response.write("\">");
                                req.response.write(instance.Key);
                                req.response.write("</option>");
                            }
                        }
                    }
                    req.response.write("</select>");
                }
            }
            req.response.write("</ul>");
            req.response.write("<INPUT TYPE=\"submit\" VALUE=\"Lancer\">");
            req.response.write("</FORM>");

            req.response.write("</body>");
            req.response.write("</html>");
        }
        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);
            }
        }
        public ProcedureExecution(Procedure procedure, OrganisationalEntity organisation, Agent agent)
        {
            this.procedure    = procedure;
            this.organisation = organisation;
            this.agent        = agent;

            agentToPartition         = new Dictionary <string, ActivityPartition>();
            partitionToAgent         = new Dictionary <ActivityPartition, AID>();
            activeTokens             = new List <ActivityExecutionToken>();
            affectations             = new Dictionary <string, InstanceSpecification>();
            activityParams           = new Dictionary <string, ValueSpecification>();
            actionsRunning           = new List <KeyValuePair <string, ActionNode> >();
            actionsDone              = new List <ActionNode>();
            allActionsDone           = new Dictionary <string, List <ActionNode> >(); // key: AID
            allActionsDoneTimestamps = new Dictionary <string, List <TimeExpression> >();

            for (int i = 0; i < procedure.Activity.Partitions.Count; i++)
            {
                ActivityPartition      partition    = procedure.Activity.Partitions[i];
                List <RoleAssignement> assignements = organisation.RoleAssignement;
                for (int j = 0; j < assignements.Count; j++)
                {
                    if (partition.name == assignements[j].Role.name)
                    {
                        agentToPartition.Add(assignements[j].Agent.toString(), partition);
                        partitionToAgent.Add(partition, assignements[j].Agent);
                        Agent agt = MascaretApplication.Instance.AgentPlateform.Agents[assignements[j].Agent.toString()];
                        if (agt == null)
                        {
                            MascaretApplication.Instance.VRComponentFactory.Log("CA VA TRANCHER");
                        }
                        affectations.Add(partition.name, agt);
                    }
                }
            }

            ActivityExecutionToken initialToken = new ActivityExecutionToken();

            initialToken.currentLocation = procedure.Activity.Initial;

            if (initialToken.currentLocation == null)
            {
                System.Console.WriteLine("ERROR: no initial node found, not allowed");
                System.Console.WriteLine("Procedure will not start.");
                return;
            }

            List <ActivityEdge> controlFlows = procedure.Activity.Initial.getOutgoingControlFlowEdges();

            if (controlFlows.Count == 1)
            {
                initialToken.outgoingEdge = controlFlows[0];
                activeTokens.Add(initialToken);

                ActionNode dummyActionVariable = null;
                tryToAdvanceToken(initialToken, dummyActionVariable, true); //first advancement
            }
            else
            {
                if (controlFlows.Count == 0)
                {
                    System.Console.WriteLine("WARNING: initial node has no outgoing edges");
                }
                else
                {
                    System.Console.WriteLine("ERROR: multiple outgoing edges for the initial node, not allowed");
                }
                System.Console.WriteLine("Procedure will not start.");
            }
        }
Exemple #15
0
        public override void manageRequest(HttpRequest req)
        {
            string procName = req.parameters["alias"];
            string orgName  = req.parameters["org"];

            req.response.write("<html>");
            req.response.write("<META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            req.response.write("<body>");

            OrganisationalEntity        org  = null;
            List <OrganisationalEntity> orgs = MascaretApplication.Instance.AgentPlateform.Organisations;
            bool found = false;
            int  i     = 0;

            while (!found && i < orgs.Count)
            {
                if (orgs[i].name == orgName)
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if (found)
            {
                org = orgs[i];
            }

            OrganisationalStructure struc = org.Structure;

            List <Procedure> procs = struc.Procedures;
            Procedure        proc  = null;

            found = false;
            i     = 0;
            while (!found && i < procs.Count)
            {
                if (procs[i].name == procName)
                {
                    found = true;
                }
                else
                {
                    i++;
                }
            }
            if (found)
            {
                proc = procs[i];
            }

            Environment env = MascaretApplication.Instance.getEnvironment();

            List <Parameter>    parameters = new List <Parameter>();
            List <ActivityNode> nodes      = proc.Activity.Nodes;

            for (int iNode = 0; iNode < nodes.Count; iNode++)
            {
                if (nodes[iNode].Kind == "parameter")
                {
                    parameters.Add(((ActivityParameterNode)(nodes[iNode])).Parameter);
                }
            }

            string paramString = "";
            Dictionary <string, ValueSpecification> param = new Dictionary <string, ValueSpecification>();

            for (int iParam = 0; iParam < parameters.Count; iParam++)
            {
                string strVal = req.parameters[parameters[iParam].name];
                strVal.Replace("+", " ");

                //Debug.Log (strVal);
                param.Add(parameters[iParam].name, parameters[iParam].Type.createValueFromString(strVal));
                paramString += " :" + parameters[iParam].name + " " + strVal;
            }

            List <RoleAssignement> assignements = org.RoleAssignement;

            for (int iA = 0; iA < assignements.Count; iA++)
            {
                ACLMessage procMsg = new ACLMessage(ACLPerformative.REQUEST);
                procMsg.Receivers.Add(assignements[iA].Agent);

                AID agentAID = MascaretApplication.Instance.Agent.Aid;
                procMsg.Sender = agentAID;
                string content = "((action ";
                content        += assignements[iA].Agent.name;
                content        += " ";
                content        += "(" + proc.name;
                content        += paramString;
                content        += ")))";
                procMsg.Content = content;
                MascaretApplication.Instance.AgentPlateform.sendMessage(procMsg);

                if (agentAID.name == assignements[iA].Agent.name)
                {
                    req.response.write("<H2>Vous jouez le role ");
                    req.response.write(assignements[iA].Role.name);
                    req.response.write("</H2>");
                    req.response.write("<H3>Vous pouvez faire les actions suivantes : </H3>");

                    req.response.write("<ul>");
                    Activity act = proc.Activity;
                    List <ActivityPartition> partitions = act.Partitions;
                    for (int iPart = 0; iPart < partitions.Count; iPart++)
                    {
                        if (partitions[iPart].name == assignements[iA].Role.name)
                        {
                            nodes = partitions[iPart].Node;
                            for (int iNode = 0; iNode < nodes.Count; iNode++)
                            {
                                if (nodes[iNode].Kind == "action")
                                {
                                    req.response.write("<li>");
                                    req.response.write("<a href=\"Actions?alias=");
                                    req.response.write(nodes[iNode].name);
                                    req.response.write("&org=");
                                    req.response.write(org.name);
                                    req.response.write("&role=");
                                    req.response.write(assignements[iA].Role.name);
                                    req.response.write("&proc=");
                                    req.response.write(proc.name);
                                    req.response.write("\" target = \"Body\">");
                                    req.response.write(nodes[iNode].name);
                                    req.response.write("</a></li>");
                                }
                            }
                        }
                    }
                    req.response.write("</ul>");
                }
            }

            req.response.write("</body>");
            req.response.write("</html>");
        }
        public void pushProcedureToDo(Procedure proc, OrganisationalEntity org, Role role, Dictionary<String, ValueSpecification> parameters)
        {
            Agent agt = (Agent)(this.Host);

            ProcedureExecution procInfo = new ProcedureExecution(proc, org, agt);
            procInfo.buildAffectations(parameters, this.Host);

            /* Search if the current agent have something to do in this procedure */
            List<ActivityPartition> parts = proc.Activity.Partitions;
            bool found = false;
            for (int iP = 0; iP < parts.Count; iP++)
            {
                if (parts[iP].name == role.name)
                {
                    //   file.WriteLine("[ProceduralBehavior Info] Found a role " + role.name + " in procedure " + proc.name); file.Flush();
                    found = true;
                }
            }

            runningProcedures.Add(procInfo);
            ispause = false;

            if (!found)
            {
                //inform all agents that this agent's role in this procedure has ended (used to unblock tokens in ProcedureExecution)
                sendProcedureDoneMessage(procInfo);
                runningProcedures.RemoveAt(runningProcedures.Count - 1); //remove it..
                // ispause = true;
            }
        }