Ejemplo n.º 1
0
    public static CallProcedureBehaviorExecution startProcedure(string procedure)
    {
        string orgEntity = null;

        List <OrganisationalStructure> structs = VRApplication.Instance.AgentPlateform.Structures;

        foreach (OrganisationalStructure s in structs)
        {
            List <Procedure> procs = s.Procedures;
            foreach (Procedure p in procs)
            {
                if (p.name == procedure)
                {
                    orgEntity = s.Entities[0].name;
                }
            }
        }
        if (orgEntity == null)
        {
            PrintSingleton.Instance.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            PrintSingleton.Instance.log("PROCEDURE NOT FOUND : " + procedure);
            PrintSingleton.Instance.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            return(null);
        }
        PrintSingleton.Instance.log("RUNNING : " + procedure + " / " + orgEntity);
        List <Entity> entities = MascaretApplication.Instance.getEnvironment().getEntities();
        Entity        entity   = entities[0];

        Mascaret.Action action2 = null;
        action2 = new CallProcedureAction();
        ((CallProcedureAction)(action2)).Procedure            = procedure;
        ((CallProcedureAction)(action2)).OrganisationalEntity = orgEntity;

        PrintSingleton.Instance.log("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        PrintSingleton.Instance.log("NEW PROCEDURE STARTED : " + procedure);
        PrintSingleton.Instance.log("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        CallProcedureBehaviorExecution cpbe = (CallProcedureBehaviorExecution)BehaviorScheduler.Instance.executeBehavior(action2, entity, new Dictionary <string, ValueSpecification>(), false);

        return(cpbe);
    }
Ejemplo n.º 2
0
    public static void executeOperationInActivity(Agent agent, string operation, Dictionary <string, string> spec, int index, CallProcedureBehaviorExecution pbe)
    {
        if (pbe != null)
        {
            ActivityPartition   agentPartition = new ActivityPartition(agent.name);
            CallOperationAction act            = new CallOperationAction();
            act.Operation = MascaretUtils.getOperation(operation, agent);

            ActionNode an = new ActionNode(operation + "_" + index, "action");
            an.Action = act;
            foreach (KeyValuePair <string, string> kvp in spec)
            {
                act.Arguments.Add(kvp.Key, kvp.Value);
            }
            an.Partitions = new List <ActivityPartition>();
            an.Partitions.Add(agentPartition);

            AgentBehaviorExecution pbehavior = agent.getBehaviorExecutingByName("ProceduralBehavior");
            if (pbehavior != null)
            {
                ProceduralBehavior procBehave = (ProceduralBehavior)(pbehavior);
                PrintSingleton.Instance.log(procBehave.RunningProcedures.Count);
                OrganisationalEntity askedOrg  = MascaretApplication.Instance.AgentPlateform.Organisations.Find(x => x.name == pbe.action.OrganisationalEntity);
                Procedure            askedProc = askedOrg.Structure.Procedures.Find(x => x.name == pbe.action.Procedure);
                Role askedRole = askedOrg.RoleAssignement.Find(x => x.Role.name == agent.name).Role;
                if (procBehave.RunningProcedures.Count == 0) //No runningProcedure for this agent, need to create one
                {
                    Dictionary <string, ValueSpecification> procParams = new Dictionary <string, ValueSpecification>();
                    PrintSingleton.Instance.log("Launch procedure for " + agent.name);
                    askedProc.Activity.Partitions.Add(agentPartition);
                    procBehave.pushProcedureToDo(askedProc, askedOrg, askedRole, procParams);
                }
                //The new partition needs to be added to all agents..
                Dictionary <string, Agent> allAgents = VRApplication.Instance.AgentPlateform.Agents;
                foreach (KeyValuePair <string, Agent> kvp in allAgents)
                {
                    ProceduralBehavior pb = (ProceduralBehavior)kvp.Value.getBehaviorExecutingByName("ProceduralBehavior");
                    if (pb != null)
                    {
                        for (int iP = 0; iP < pb.runningProcedures.Count; iP++)
                        {
                            if (!pb.runningProcedures[iP].getAgentToPartition().ContainsKey(agent.Aid.toString()))
                            {
                                pb.runningProcedures[iP].getAgentToPartition().Add(agent.Aid.toString(), askedProc.Activity.Partitions.Find(x => x.name == agent.name));
                            }
                        }
                    }
                }

                executeOperation(an, agent);
            }
            else
            {
                PrintSingleton.Instance.log("Agent " + agent.name + " does not have a proceduralBehavior!");
            }
        }
        else
        {
            PrintSingleton.Instance.log("No procedure launched!");
        }
    }