Ejemplo n.º 1
0
        public void StartAgent(string className, AgentBridge bridge)
        {
            var agent = (IAgent)Activator.CreateInstance(
                Type.GetType("simpleCDE." + className)
                );

            agent.EventCompleted  += bridge.OnEventCompleted;
            bridge.EventTriggered += agent.OnEventTriggered;
            activeAgents.Add(new Tuple <IAgent, AgentBridge>(agent, bridge));
        }
Ejemplo n.º 2
0
        public void HaltAgent(AgentBridge bridge)
        {
            activeAgents.RemoveAll(pair => {
                var candidateBridge = pair.Item2;

                if (bridge != candidateBridge)
                {
                    return(false);
                }

                var agent = pair.Item1;

                agent.Halt();
                agent.EventCompleted  -= bridge.OnEventCompleted;
                bridge.EventTriggered -= agent.OnEventTriggered;

                return(true);
            });
        }
Ejemplo n.º 3
0
        public void CreateServiceInstances(
            Specification specification,
            Service service,
            Agent agent,
            int amount
            )
        {
            if (!CheckRequirements(specification, agent))
            {
                throw new Exception("Can't meet requirements for an agent.");
            }

            for (int i = 0; i < amount; i++)
            {
                var bridge = new AgentBridge();
                bridge.EventCompleted += (eventName, actionName) => {
                    foreach (var rel in activeRelations)
                    {
                        if (rel.LeadingService == service.ServiceName &&
                            rel.LeadingEvent == eventName)
                        {
                            foreach (var pair in activeAgents)
                            {
                                var agn = pair.Item1;
                                agn.Events.ForEach(e => {
                                    if (agn.ServiceImplemented == rel.SubordinateService)
                                    {
                                        e.Preconditions?.RemoveAll(
                                            evt => evt == rel.SubordinateEvent
                                            );
                                    }
                                });
                            }
                        }
                    }

                    activeAgents.ForEach(pair => {
                        var brg = pair.Item2;
                        if (brg == bridge)
                        {
                            var agn = pair.Item1;
                            agn.Events.ForEach(evt => {
                                if (evt.Name == eventName)
                                {
                                    evt.State = EventState.Handled;
                                }
                                evt.Preconditions?.Remove(eventName);
                            });
                        }
                    });

                    UpdateState();
                };

                provider.StartAgent(agent.ImplementingClass, bridge);
                activeAgents.Add(
                    new Tuple <Agent, AgentBridge>(
                        agent.DeepClone(),
                        bridge
                        )
                    );
            }
        }