Skip to content

Commit

Permalink
CleanCode + update Nuggets
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Oct 7, 2020
1 parent c9df75b commit 9616101
Show file tree
Hide file tree
Showing 172 changed files with 3,063 additions and 2,728 deletions.
5 changes: 2 additions & 3 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.Act.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Threading.Tasks;
using Symu.Classes.Task;
using Symu.Common.Classes;
using Symu.Environment;
using Symu.Messaging.Messages;
using Symu.Repository;

Expand Down Expand Up @@ -71,13 +70,13 @@ public override void Act(Message message)
private SymuTask ConvertMessageIntoTask(Message message)
{
var communication =
Environment.Organization.Communication.TemplateFromChannel(message.Medium);
Environment.MainOrganization.Communication.TemplateFromChannel(message.Medium);
var task = new SymuTask(Schedule.Step)
{
Type = message.Medium.ToString(),
TimeToLive = communication.TimeToLive,
Parent = message,
Weight = Environment.Organization.Communication.TimeSpent(message.Medium, false,
Weight = Environment.MainOrganization.Communication.TimeSpent(message.Medium, false,
Environment.RandomLevelValue),
Assigned = message.Receiver
//todo maybe define a specific KeyActivity to follow the time spent on messaging?
Expand Down
189 changes: 94 additions & 95 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
#region using directives

using System;
using System.Collections.Generic;
using Symu.Classes.Task.Manager;
using Symu.Common;
using Symu.Common.Interfaces;
using Symu.Common.Math.ProbabilityDistributions;
using Symu.Messaging.Manager;
using Symu.Messaging.Messages;
using Symu.OrgMod.Edges;
using Symu.Repository.Edges;

#endregion

Expand All @@ -44,6 +40,91 @@ public abstract partial class CognitiveAgent
.InteractionCharacteristics
.PreferredCommunicationMediums);

#region Send messages

/// <summary>
/// Send a message to another agent define by the message.Receiver
/// It count in the Mailbox.NumberMessagesPerStep
/// It will be effectively sent only if IsMessages is above Limits
/// </summary>
/// <param name="message"></param>
public override void Send(Message message)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

switch (message.Medium)
{
case CommunicationMediums.System:
OnBeforeSendMessage(message);
Environment.SendAgent(message);
OnAfterSendMessage(message);
break;
default:
{
if (Status == AgentStatus.Offline ||
!IsMessagesPerPeriodBelowLimit(message.Medium) ||
!IsMessagesSendPerPeriodBelowLimit(message.Medium))
{
return;
}

OnBeforeSendMessage(message);
Environment.SendAgent(message);
OnAfterSendMessage(message);
break;
}
}
}

#endregion

#region Reply message

/// <summary>
/// Reply to a message from another agent
/// It does count in the Mailbox.NumberMessagesPerStep
/// It doesn't count in the Mailbox.NumberSentPerPeriod
/// It will be effectively sent only if IsMessages is above Limits
/// </summary>
/// <param name="message"></param>
/// <param name="delayed"></param>
/// <param name="delay"></param>
public override void Reply(Message message, bool delayed, ushort delay)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

if (!IsMessagesPerPeriodBelowLimit(message.Medium))
{
return;
}

if (message.HasAttachments && message.Medium != CommunicationMediums.System)
{
var ma = message.Attachments;
var communication =
Environment.MainOrganization.Communication.TemplateFromChannel(message.Medium);
ma.KnowledgeBits = KnowledgeModel.FilterKnowledgeToSend(ma.KnowledgeId, ma.KnowledgeBit, communication,
Schedule.Step, out var knowledgeIndexToSend);
ma.BeliefBits =
BeliefsModel.FilterBeliefToSendFromKnowledgeId(ma.KnowledgeId, ma.KnowledgeBit, communication);
// The agent is asked for his knowledge, so he can't forget it
if (ma.KnowledgeBits != null)
{
ForgettingModel.UpdateForgettingProcess(ma.KnowledgeId, knowledgeIndexToSend);
}
}

base.Reply(message, delayed, delay);
}

#endregion

#region Post message

/// <summary>
Expand Down Expand Up @@ -117,7 +198,7 @@ public bool AcceptNewInteraction(IAgentId senderId)
return true;
}

if (Environment.Organization.MetaNetwork.ActorActor.HasActiveInteraction(AgentId, senderId))
if (Environment.MainOrganization.MetaNetwork.ActorActor.HasActiveInteraction(AgentId, senderId))
{
return true;
}
Expand All @@ -142,13 +223,15 @@ public bool AcceptNewInteraction(IAgentId senderId)
_newInteractionCounter++;

// Decide to positively answer to this new interaction
if (Environment.Organization.Models.InteractionSphere.SphereUpdateOverTime)
if (!Environment.MainOrganization.Models.InteractionSphere.SphereUpdateOverTime)
{
// Message.Sender is now part of agent interaction sphere
var interaction = new ActorActor(AgentId, senderId);
Environment.Organization.MetaNetwork.ActorActor.Add(interaction);
return true;
}

// Message.Sender is now part of agent interaction sphere
var interaction = new ActorActor(AgentId, senderId);
Environment.MainOrganization.MetaNetwork.ActorActor.Add(interaction);

return true;
}

Expand All @@ -169,7 +252,7 @@ public override void OnBeforeSendMessage(Message message)
base.OnBeforeSendMessage(message);
// Impact of the Communication channels on the remaining capacity
var cost =
Environment.Organization.Communication.TimeSpent(message.Medium, true,
Environment.MainOrganization.Communication.TimeSpent(message.Medium, true,
Environment.RandomLevelValue);
Capacity.Decrement(cost);
}
Expand Down Expand Up @@ -212,7 +295,7 @@ public void LearnKnowledgesFromPostMessage(Message message)
}

var communication =
Environment.Organization.Communication.TemplateFromChannel(message.Medium);
Environment.MainOrganization.Communication.TemplateFromChannel(message.Medium);
LearningModel.Learn(message.Attachments.KnowledgeId,
message.Attachments.KnowledgeBits, communication.MaxRateLearnable, Schedule.Step);
if (message.Medium == CommunicationMediums.Email && HasEmail)
Expand Down Expand Up @@ -248,90 +331,6 @@ public void LearnBeliefsFromPostMessage(Message message)

#endregion

#region Send messages

/// <summary>
/// Send a message to another agent define by the message.Receiver
/// It count in the Mailbox.NumberMessagesPerStep
/// It will be effectively sent only if IsMessages is above Limits
/// </summary>
/// <param name="message"></param>
public override void Send(Message message)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

switch (message.Medium)
{
case CommunicationMediums.System:
OnBeforeSendMessage(message);
Environment.SendAgent(message);
OnAfterSendMessage(message);
break;
default:
{
if (Status == AgentStatus.Offline ||
!IsMessagesPerPeriodBelowLimit(message.Medium) ||
!IsMessagesSendPerPeriodBelowLimit(message.Medium))
{
return;
}

OnBeforeSendMessage(message);
Environment.SendAgent(message);
OnAfterSendMessage(message);
break;
}
}
}

#endregion

#region Reply message

/// <summary>
/// Reply to a message from another agent
/// It does count in the Mailbox.NumberMessagesPerStep
/// It doesn't count in the Mailbox.NumberSentPerPeriod
/// It will be effectively sent only if IsMessages is above Limits
/// </summary>
/// <param name="message"></param>
/// <param name="delayed"></param>
/// <param name="delay"></param>
public override void Reply(Message message, bool delayed, ushort delay)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

if (!IsMessagesPerPeriodBelowLimit(message.Medium))
{
return;
}

if (message.HasAttachments && message.Medium != CommunicationMediums.System)
{
var ma = message.Attachments;
var communication =
Environment.Organization.Communication.TemplateFromChannel(message.Medium);
ma.KnowledgeBits = KnowledgeModel.FilterKnowledgeToSend(ma.KnowledgeId, ma.KnowledgeBit, communication,
Schedule.Step, out var knowledgeIndexToSend);
ma.BeliefBits = BeliefsModel.FilterBeliefToSendFromKnowledgeId(ma.KnowledgeId, ma.KnowledgeBit, communication);
// The agent is asked for his knowledge, so he can't forget it
if (ma.KnowledgeBits != null)
{
ForgettingModel.UpdateForgettingProcess(ma.KnowledgeId, knowledgeIndexToSend);
}
}

base.Reply(message, delayed, delay);
}

#endregion

#region Limit MessagesManager par period

/// <summary>
Expand Down

0 comments on commit 9616101

Please sign in to comment.