public void addProcedure(Procedure proc)
 {
     procedures.Add(proc);
 }
        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 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 #4
0
        public void addOrganisationalStructure(XElement orgNode)
        {
            XAttribute orgName = (XAttribute)orgNode.Attribute("name");

            OrganisationalStructure organisation = new OrganisationalStructure(orgName.Value);
            organisation.Description = getComment(orgNode);
            organisation.Summary = getSummary(orgNode);
            organisation.Tags = getTags(orgNode);

            //Debug.Log(MascaretApplication.Instance.Model.name);

            MascaretApplication.Instance.AgentPlateform.Structures.Add(organisation);

            foreach (XElement child in orgNode.Elements())
            {
                if (child.Name.LocalName.CompareTo("ownedAttribute") == 0)
                {

                    string childType = "", roleName = "";
                    Classifier classe = null;

                    XAttribute attr = (XAttribute)child.Attribute("type");

                    if (attr != null)
                    {
                        childType = attr.Value;
                        classe = this._classifiers[childType];
                    }

                    attr = (XAttribute)child.Attribute("name");
                    if (attr != null) roleName = attr.Value;

                    if (classe != null)
                    {
                        try
                        {

                            Role role = new Role(roleName);
                            role.RoleClass = (RoleClass)classe;
                            organisation.addRole(role);

                        }
                        catch (InvalidCastException)
                        {

                            try
                            {
                                Ressource ressource = new Ressource(roleName);
                                ressource.EntityClass = (EntityClass)classe;
                                organisation.addResource(ressource);

                            }
                            catch (InvalidCastException)
                            {
                            }
                        }
                    }
                }
            }

            foreach (XElement child in orgNode.Elements())
            {
                if (child.Name.LocalName.CompareTo("ownedBehavior") == 0)
                {

                    string childType = "", childName = "", childId = "";

                    XAttribute attr = (XAttribute)child.Attribute("{http://schema.omg.org/spec/XMI/2.1}type");
                    if (attr == null) attr = (XAttribute)child.Attribute("{http://www.omg.org/spec/XMI/20131001}type");

                    if (attr != null) childType = attr.Value;

                    attr = (XAttribute)child.Attribute("name");
                    if (attr != null) childName = attr.Value;

                    attr = (XAttribute)child.Attribute("{http://schema.omg.org/spec/XMI/2.1}id");
                    if (attr == null) attr = (XAttribute)child.Attribute("{http://www.omg.org/spec/XMI/20131001}id");

                    if (attr != null) childId = attr.Value;

                    if (childType.CompareTo("uml:Activity") == 0)
                    {
                        Activity activity = addActivity(child);
                        Procedure procedure = null;
                        MascaretApplication.Instance.VRComponentFactory.Log(" ?????? Procedure : " + childId);
                        if (isStereotypedScenarioPedagogique(child))
                        {
                            procedure = new PedagogicalScenario(childName);

                            XAttribute attr2 = child.Attribute("{http://schema.omg.org/spec/XMI/2.1}id");
                            if (attr2 == null)
                                attr2 = child.Attribute("{http://www.omg.org/spec/XMI/20131001}id");
                            ((PedagogicalScenario)procedure).Scene = _scenarioToScene[attr2.Value];
                        }
                        else
                            procedure = new Procedure(childName);
                        procedure.Activity = activity;

                        string stereo = getStereotype(childId);
                        MascaretApplication.Instance.VRComponentFactory.Log("procedure;;;;;;;;;;;;;;;;;;;;;;;;;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + childName + " : " + childId + " " + stereo);
                        if (stereo != "")
                        {
                            procedure.Stereotype = stereo;
                        }

                        organisation.addProcedure(procedure);
                        _idBehaviors.Add(childId, activity);
                    }
                }

            }
        }
        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>");
        }
        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 void stop(Procedure procedure)
        {
            List<ProcedureExecution> exec = new List<ProcedureExecution>(); // Procedure still running
            for (int iP = 0; iP < runningProcedures.Count; iP++)
            {
            ProcedureExecution procInfo = runningProcedures[iP];
            if (procInfo.procedure == procedure)
            {
                procInfo.stop();
            }
            else
            {
                exec.Add(procInfo);
            }
            }

            // We put back still running procedure in the vector
            runningProcedures.Clear();
            for (int iP = 0; iP < exec.Count ; iP++)
            runningProcedures.Add(exec[iP]);
        }
        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;
            }
        }