Skip to content

Commit

Permalink
Move Agents to MetaNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 28, 2020
1 parent a80d9ab commit fbc5b27
Show file tree
Hide file tree
Showing 22 changed files with 314 additions and 131 deletions.
2 changes: 1 addition & 1 deletion SourceCode/Symu/Classes/Agents/CognitiveAgent.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public override void PostMessage(Message message)
/// </summary>
/// <param name="senderId"></param>
/// <returns>True if the new interaction has been accepted</returns>
public bool AcceptNewInteraction(AgentId senderId)
public bool AcceptNewInteraction(IAgentId senderId)
{
if (AgentId.Equals(senderId))
{
Expand Down
3 changes: 2 additions & 1 deletion SourceCode/Symu/Classes/Agents/CognitiveAgent.Murphies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ public bool CheckBlockerIncompleteInformation(SymuTask task)
if (!murphy.On ||
Math.Abs(task.WorkToDo) < Tolerance || // Task is done
!task.IsAssigned ||
!task.HasCreator ||
task.Creator.Equals(AgentId)) // Worker can't be blocked by himself
{
return false;
Expand Down Expand Up @@ -871,7 +872,7 @@ public void TryRecoverBlockerIncompleteInformation(SymuTask task, Blocker blocke
blocker.InitialStep);

//TODO send to creator only if he has the right to communicate to cf. Network
if (askInternally && task.Creator.IsNotNull)
if (askInternally && task.HasCreator)
{
var messageType = murphy.AskOnWhichChannel(Cognitive.InteractionCharacteristics
.PreferredCommunicationMediums);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static float NextInfluenceability(InternalCharacteristics internalCharact
/// <param name="beliefBits">from agentAgentId beliefBits</param>
/// <param name="agentId"></param>
/// <param name="beliefLevel"></param>
public void BeInfluenced(IId beliefId, Bits beliefBits, AgentId agentId, BeliefLevel beliefLevel)
public void BeInfluenced(IId beliefId, Bits beliefBits, IAgentId agentId, BeliefLevel beliefLevel)
{
if (!On || beliefBits == null)
{
Expand Down
14 changes: 7 additions & 7 deletions SourceCode/Symu/Classes/Agents/ReactiveAgent.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,31 @@ public void PostDelayedMessages()
/// A conversation identifier, for the cases when a conversation involves multiple messages
/// that refer to the same topic
/// </param>
public void Send(AgentId receiverId, MessageAction action, byte content)
public void Send(IAgentId receiverId, MessageAction action, byte content)
{
var message = new Message(AgentId, receiverId, action, content);
Send(message);
}

public void Send(AgentId receiverId, MessageAction action, byte content, object parameter)
public void Send(IAgentId receiverId, MessageAction action, byte content, object parameter)
{
var message = new Message(AgentId, receiverId, action, content, parameter);
Send(message);
}

public void Send(AgentId receiverId, MessageAction action, byte content, CommunicationMediums mediums)
public void Send(IAgentId receiverId, MessageAction action, byte content, CommunicationMediums mediums)
{
var message = new Message(AgentId, receiverId, action, content, mediums);
Send(message);
}

public void Send(AgentId receiverId, MessageAction action, byte content, MessageAttachments parameter)
public void Send(IAgentId receiverId, MessageAction action, byte content, MessageAttachments parameter)
{
var message = new Message(AgentId, receiverId, action, content, parameter);
Send(message);
}

public void Send(AgentId receiverId, MessageAction action, byte content, MessageAttachments parameter,
public void Send(IAgentId receiverId, MessageAction action, byte content, MessageAttachments parameter,
CommunicationMediums communicationMedium)
{
var message = new Message(AgentId, receiverId, action, content, parameter, communicationMedium);
Expand Down Expand Up @@ -309,7 +309,7 @@ private void SendDelayed(Message message, ushort step)
Environment.SendDelayedMessage(message, step);
}

public void SendToMany(IEnumerable<AgentId> receivers, MessageAction action, byte content)
public void SendToMany(IEnumerable<IAgentId> receivers, MessageAction action, byte content)
{
if (receivers is null)
{
Expand All @@ -322,7 +322,7 @@ public void SendToMany(IEnumerable<AgentId> receivers, MessageAction action, byt
}
}

public void SendToMany(IEnumerable<AgentId> receivers, MessageAction action, byte content, object parameter)
public void SendToMany(IEnumerable<IAgentId> receivers, MessageAction action, byte content, object parameter)
{
if (receivers is null)
{
Expand Down
9 changes: 5 additions & 4 deletions SourceCode/Symu/Classes/Task/SymuTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ public SymuTask(ushort day)
/// AgentId that is assigned on the task and will performed it.
/// Only one agent can perform a task at the same time
/// </summary>
public AgentId Assigned { get; set; }
public IAgentId Assigned { get; set; }

/// <summary>
/// The creator of the task
/// </summary>
public AgentId Creator { get; set; }
public IAgentId Creator { get; set; }

/// <summary>
/// If only one agent can perform a task at the same time, agent can cancel a task and another agent can take the task.
/// </summary>
public List<AgentId> HasBeenCancelledBy { get; } = new List<AgentId>();
public List<IAgentId> HasBeenCancelledBy { get; } = new List<IAgentId>();

/// <summary>
/// The weight of the task
Expand Down Expand Up @@ -130,7 +130,8 @@ public float Weight
/// </summary>
public byte Priority { get; set; }

public bool IsAssigned => Assigned.IsNotNull;
public bool IsAssigned => Assigned!= null && Assigned.IsNotNull;
public bool HasCreator => Creator != null && Creator.IsNotNull;

public bool IsStarted { get; private set; }
public bool IsToDo => !IsStarted && !IsBlocked && !IsAssigned;
Expand Down
10 changes: 5 additions & 5 deletions SourceCode/Symu/Environment/SymuEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public void AddAgent(ReactiveAgent agent)
throw new ArgumentNullException(nameof(agent));
}

if (WhitePages.Agents.Exists(agent.AgentId))
if (WhitePages.ExistsAgent(agent.AgentId))
{
throw new ArgumentException("Trying to add an agent " + agent.AgentId.ClassId + " with an existing key: " +
agent.AgentId.Id);
}

WhitePages.Agents.Add(agent);
WhitePages.AddAgent(agent);
}

#endregion
Expand Down Expand Up @@ -166,7 +166,7 @@ public virtual IterationResult SetIterationResult(ushort iterationNumber)
/// </returns>
public bool StopIteration()
{
return WhitePages.FilteredAgentsByClassCount(SymuYellowPages.Scenario) == 0;
return WhitePages.FilteredAgentsByClassCount(new ClassId(SymuYellowPages.Scenario)) == 0;
}

/// <summary>
Expand Down Expand Up @@ -254,10 +254,10 @@ public void SendAgent(Message message)
throw new ArgumentNullException(nameof(message));
}

if (WhitePages.Agents.Exists(message.Receiver))
if (WhitePages.ExistsAgent(message.Receiver))
// Log is done within Agent.Post
{
WhitePages.Agents.Get(message.Receiver).Post(message);
WhitePages.GetAgent(message.Receiver).Post(message);
}
else
{
Expand Down
16 changes: 8 additions & 8 deletions SourceCode/Symu/Messaging/Messages/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public class Message
/// <summary>
/// The name of the agent that sends the message
/// </summary>
public AgentId Sender { get; set; }
public IAgentId Sender { get; set; }

/// <summary>
/// The name of the agent that needs to receive the message
/// </summary>
public AgentId Receiver { get; set; }
public IAgentId Receiver { get; set; }

/// <summary>
/// The State of the message
Expand Down Expand Up @@ -125,7 +125,7 @@ public Message()
/// <param name="receiverId">The name of the agent that needs to receive the message</param>
/// <param name="action"></param>
/// <param name="subject">The subject of the message</param>
public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte subject) : this()
public Message(IAgentId senderId, IAgentId receiverId, MessageAction action, byte subject) : this()
{
Sender = senderId;
Receiver = receiverId;
Expand All @@ -141,13 +141,13 @@ public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte
/// <param name="action"></param>
/// <param name="subject">The subject of the message</param>
/// <param name="medium"></param>
public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte subject,
public Message(IAgentId senderId, IAgentId receiverId, MessageAction action, byte subject,
CommunicationMediums medium) : this(senderId, receiverId, action, subject)
{
Medium = medium;
}

public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte subject, object attachment)
public Message(IAgentId senderId, IAgentId receiverId, MessageAction action, byte subject, object attachment)
: this(senderId, receiverId, action, subject)
{
if (attachment == null)
Expand All @@ -159,7 +159,7 @@ public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte
Attachments.Add(attachment);
}

public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte subject, object attachment,
public Message(IAgentId senderId, IAgentId receiverId, MessageAction action, byte subject, object attachment,
CommunicationMediums medium)
: this(senderId, receiverId, action, subject, medium)
{
Expand All @@ -172,7 +172,7 @@ public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte
Attachments.Add(attachment);
}

public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte subject,
public Message(IAgentId senderId, IAgentId receiverId, MessageAction action, byte subject,
MessageAttachments attachments)
: this(senderId, receiverId, action, subject)
{
Expand All @@ -184,7 +184,7 @@ public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte
Attachments = attachments;
}

public Message(AgentId senderId, AgentId receiverId, MessageAction action, byte subject,
public Message(IAgentId senderId, IAgentId receiverId, MessageAction action, byte subject,
MessageAttachments attachments, CommunicationMediums medium)
: this(senderId, receiverId, action, subject, medium)
{
Expand Down
4 changes: 2 additions & 2 deletions SourceCode/Symu/Messaging/Subscription/MessageSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Symu.Messaging.Subscription
/// </summary>
public struct MessageSubscription
{
public MessageSubscription(AgentId agentId, byte content)
public MessageSubscription(IAgentId agentId, byte content)
{
Content = content;
AgentId = agentId;
Expand All @@ -36,6 +36,6 @@ public MessageSubscription(AgentId agentId, byte content)
/// <summary>
/// The name of the agent
/// </summary>
public AgentId AgentId { get; set; }
public IAgentId AgentId { get; set; }
}
}
12 changes: 6 additions & 6 deletions SourceCode/Symu/Messaging/Subscription/MessageSubscriptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MessageSubscriptions
/// <summary>
/// Subscribe to the Message content
/// </summary>
public void Subscribe(AgentId agentId, byte content)
public void Subscribe(IAgentId agentId, byte content)
{
if (!HasSubscribed(agentId, content))
{
Expand All @@ -44,28 +44,28 @@ public void Subscribe(AgentId agentId, byte content)
/// <summary>
/// UnSubscribe to the Message content
/// </summary>
public void Unsubscribe(AgentId agentId, byte content)
public void Unsubscribe(IAgentId agentId, byte content)
{
_subscriptions.RemoveAll(s => s.AgentId.Equals(agentId) && s.Content == content);
}

/// <summary>
/// UnSubscribe to the all Message contents
/// </summary>
public void Unsubscribe(AgentId agentId)
public void Unsubscribe(IAgentId agentId)
{
_subscriptions.RemoveAll(s => s.AgentId.Equals(agentId));
}

/// <summary>
/// Subscribe to the Message content
/// </summary>
public bool HasSubscribed(AgentId agentId, byte content)
public bool HasSubscribed(IAgentId agentId, byte content)
{
return _subscriptions.Exists(s => s.AgentId.Equals(agentId) && s.Content == content);
}

public IEnumerable<AgentId> Subscribers(byte content)
public IEnumerable<IAgentId> Subscribers(byte content)
{
return _subscriptions.Where(s => s.Content == content).Select(s => s.AgentId).ToList();
}
Expand All @@ -77,7 +77,7 @@ public IEnumerable<AgentId> Subscribers(byte content)
/// <param name="content"></param>
/// <param name="stoppedAgentIds"></param>
/// <returns></returns>
public IEnumerable<AgentId> Subscribers(byte content, List<AgentId> stoppedAgentIds)
public IEnumerable<IAgentId> Subscribers(byte content, List<IAgentId> stoppedAgentIds)
{
_subscriptions.RemoveAll(x => stoppedAgentIds.Exists(stopped => stopped.Equals(x.AgentId)));
return _subscriptions.Where(s => s.Content == content).Select(s => s.AgentId).ToList();
Expand Down
4 changes: 2 additions & 2 deletions SourceCode/Symu/Messaging/Tracker/TimeStampedMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public List<Message> SentByAgent(ushort step, AgentId agentId)
return _messages.ContainsKey(step) ? _messages[step].ToList().FindAll(m => m.Sender.Equals(agentId)) : null;
}

public bool Exists(MessageAction action, byte content, byte senderClassId, byte receiverClassId)
public bool Exists(MessageAction action, byte content, IClassId senderClassId, IClassId receiverClassId)
{
return _messages.Any(m => m.Value.Exists(v => v.Action == action && v.Subject == content
&& v.Sender.Equals(senderClassId) &&
v.Receiver.Equals(receiverClassId)));
}

public bool Exists(MessageAction action, byte content, byte senderClassId)
public bool Exists(MessageAction action, byte content, IClassId senderClassId)
{
return _messages.Any(m => m.Value.Exists(v => v.Action == action && v.Subject == content
&& v.Sender.Equals(senderClassId)));
Expand Down

0 comments on commit fbc5b27

Please sign in to comment.