Skip to content

Commit

Permalink
Extract interface Id
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 24, 2020
1 parent fa7ef56 commit bf732e7
Show file tree
Hide file tree
Showing 87 changed files with 504 additions and 329 deletions.
9 changes: 5 additions & 4 deletions SourceCode/Symu/Classes/Agents/AgentEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#region using directives

using System;
using Symu.Common.Interfaces.Entity;

#endregion

Expand All @@ -24,12 +25,12 @@ public AgentEntity()
{
}

public AgentEntity(ushort key, byte classKey)
public AgentEntity(UId id, byte classId)
{
AgentId = new AgentId(key, classKey);
AgentId = new AgentId(id, classId);
}

public AgentEntity(ushort key, byte classKey, string name) : this(key, classKey)
public AgentEntity(UId id, byte classId, string name) : this(id, classId)
{
if (string.IsNullOrEmpty(name))
{
Expand All @@ -39,7 +40,7 @@ public AgentEntity(ushort key, byte classKey, string name) : this(key, classKey)
Name = name;
}

public AgentEntity(ushort key, byte classKey, string name, AgentId parent) : this(key, classKey, name)
public AgentEntity(UId id, byte classId, string name, AgentId parent) : this(id, classId, name)
{
Parent = parent;
}
Expand Down
34 changes: 23 additions & 11 deletions SourceCode/Symu/Classes/Agents/AgentId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

using System;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;

#endregion

Expand All @@ -24,7 +26,7 @@ public struct AgentId : IAgentId
/// <summary>
/// Unique Id of the agent
/// </summary>
public ushort Id { get; set; }
public UId Id { get; set; }

/// <summary>
/// ClassId of the agent
Expand All @@ -33,16 +35,23 @@ public struct AgentId : IAgentId

public byte Class => ((ClassId?) ClassId)?.Id ?? 0;

public bool IsNull => Id == 0;
public bool IsNotNull => Id != 0;
public bool IsNull => Id == null || Id.IsNull;
public bool IsNotNull => Id != null && Id.IsNotNull;

public AgentId(ushort id, byte classId)
IId IAgentId.Id => Id;

public bool Equals(IId id)
{
//if (classId == 0)
//{
// throw new ArgumentNullException(nameof(classId));
//}
return Id.Equals(id);
}

public AgentId(ushort id, byte classId)
{
Id = new UId(id);
ClassId = new ClassId(classId);
}
public AgentId(UId id, byte classId)
{
Id = id;
ClassId = new ClassId(classId);
}
Expand All @@ -54,12 +63,15 @@ public AgentId(ushort id, byte classId)
public override bool Equals(object obj)
{
return obj is AgentId id &&
Id == id.Id;
(id.Id != null && Id != null && Id.Equals(id.Id) ||
Id == null && id.Id == null);
}

public bool Equals(IAgentId agentId)
{
return Id == ((AgentId)agentId).Id;
return agentId is AgentId id &&
(id.Id != null && Id !=null && Id.Equals(id.Id) ||
Id == null && id.Id == null);
}

public bool Equals(IClassId classId)
Expand All @@ -79,7 +91,7 @@ public bool Equals(byte classId)
/// <returns>true if this is inferior to agentId </returns>
public bool CompareTo(IAgentId agentId)
{
return Id < ((AgentId)agentId).Id;
return agentId is AgentId agent && Id.Id < agent.Id.Id;
}

public override string ToString()
Expand Down
1 change: 1 addition & 0 deletions SourceCode/Symu/Classes/Agents/ClassId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endregion

using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;

namespace Symu.Classes.Agents
{
Expand Down
9 changes: 5 additions & 4 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.Tasking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using Symu.Classes.Agents.Models;
using Symu.Classes.Task;
using Symu.Common.Interfaces.Entity;
using Symu.Environment;
using Symu.Messaging.Messages;
using static Symu.Common.Constants;
Expand Down Expand Up @@ -210,7 +211,7 @@ public virtual void SwitchingContextModel()
/// Key => step
/// Value => time spent
/// </summary>
public Dictionary<ushort, float> TimeSpent { get; } = new Dictionary<ushort, float>();
public Dictionary<UId, float> TimeSpent { get; } = new Dictionary<UId, float>();

/// <summary>
/// Impact of the Communication channels on the time spent
Expand All @@ -221,9 +222,9 @@ public virtual void SwitchingContextModel()
/// <param name="send">If set, it is an ask help task, otherwise it is a reply help task</param>
/// <remarks>Impact on capacity is done in OnBeforeSendMessage and OnAfterPostMessage</remarks>
public void ImpactOfTheCommunicationMediumOnTimeSpent(CommunicationMediums medium, bool send,
ushort keyActivity)
UId keyActivity)
{
if (keyActivity == 0)
if (keyActivity == null || keyActivity.IsNull)
{
return;
}
Expand All @@ -239,7 +240,7 @@ public virtual void SwitchingContextModel()
/// </summary>
/// <param name="keyActivity"></param>
/// <param name="timeSpent"></param>
public void AddTimeSpent(ushort keyActivity, float timeSpent)
public void AddTimeSpent(UId keyActivity, float timeSpent)
{
if (!TimeSpent.ContainsKey(keyActivity))
{
Expand Down
6 changes: 3 additions & 3 deletions SourceCode/Symu/Classes/Agents/CognitiveAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ protected CognitiveAgent(AgentId agentId, SymuEnvironment environment, Cognitive
/// <summary>
/// If agent has an email, get the email database of the agent
/// </summary>
protected Database Email => Environment.WhitePages.MetaNetwork.Resources.GetResource<Database>(AgentId, AgentId) ;
protected Database Email => Environment.WhitePages.MetaNetwork.Resources.GetResource<Database>(AgentId, AgentId.Id) ;

/// <summary>
/// If agent has an email
/// </summary>
public bool HasEmail => Environment.WhitePages.MetaNetwork.Resources.Exists(AgentId, AgentId);
public bool HasEmail => Environment.WhitePages.MetaNetwork.Resources.Exists(AgentId, AgentId.Id);

//TODO => all the models should be included in the cognitive architecture
/// <summary>
Expand Down Expand Up @@ -132,7 +132,7 @@ protected override void CreateAgent(AgentId agentId, SymuEnvironment environment
foreach (var database in environment.Organization.Databases)
{
// Organization databases are used by every one
var agentResource = new AgentResource(database.AgentId, new ResourceUsage(0), 100);
var agentResource = new AgentResource(database.Id, new ResourceUsage(0), 100);
environment.WhitePages.MetaNetwork.Resources.Add(AgentId, agentResource);
}
}
Expand Down
7 changes: 4 additions & 3 deletions SourceCode/Symu/Classes/Organization/OrganizationEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Symu.Classes.Agents;
using Symu.Classes.Agents.Models.CognitiveTemplates;
using Symu.Classes.Murphies;
using Symu.Common.Interfaces.Entity;
using Symu.Messaging.Templates;
using Symu.Repository;
using Symu.Repository.Entity;
Expand All @@ -30,7 +31,7 @@ public class OrganizationEntity : AgentEntity
{
public const byte Class = SymuYellowPages.Organization;

public OrganizationEntity(string name) : base(0, Class, name)
public OrganizationEntity(string name) : base(new UId(0), Class, name)
{
}

Expand Down Expand Up @@ -73,9 +74,9 @@ public OrganizationEntity(string name) : base(0, Class, name)
/// </summary>
public MurphyCollection Murphies { get; } = new MurphyCollection();

public ushort NextEntityIndex()
public UId NextEntityId()
{
return EntityIndex++;
return new UId(EntityIndex++);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion SourceCode/Symu/Classes/Scenario/ScenarioEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#region using directives

using Symu.Classes.Agents;
using Symu.Common.Interfaces.Entity;
using Symu.Repository;

#endregion
Expand All @@ -20,7 +21,7 @@ public class ScenarioEntity : AgentEntity
{
public const byte Class = SymuYellowPages.Scenario;

public ScenarioEntity(ushort key) : base(key, Class)
public ScenarioEntity(UId id) : base(id, Class)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion SourceCode/Symu/Classes/Scenario/SimulationScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Symu.Classes.Scenario
public class SimulationScenario : ReactiveAgent
{
public SimulationScenario(object parent, SymuEnvironment environment) : base(
new AgentId(environment.Organization.NextEntityIndex(), ScenarioEntity.Class), environment)
new AgentId(environment.Organization.NextEntityId(), ScenarioEntity.Class), environment)
{
if (environment is null)
{
Expand Down
3 changes: 2 additions & 1 deletion SourceCode/Symu/Classes/Task/SymuTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Symu.Classes.Blockers;
using Symu.Classes.Task.Manager;
using Symu.Common;
using Symu.Common.Interfaces.Entity;
using Symu.Repository.Networks.Knowledges;
using Symu.Results.Blocker;
using static Symu.Common.Constants;
Expand Down Expand Up @@ -56,7 +57,7 @@ public SymuTask(ushort day)
/// <summary>
/// the Key of the ParentId (group, process, ...) in which the task must be performed
/// </summary>
public ushort KeyActivity { get; set; }
public UId KeyActivity { get; set; }

/// <summary>
/// Type of the task use to have specific behaviour
Expand Down
1 change: 1 addition & 0 deletions SourceCode/Symu/Environment/SymuEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Symu.Classes.Scenario;
using Symu.Common;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Environment.Events;
using Symu.Messaging.Messages;
using Symu.Messaging.Tracker;
Expand Down
5 changes: 3 additions & 2 deletions SourceCode/Symu/Repository/ConcurrentAgents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Collections.Generic;
using System.Linq;
using Symu.Classes.Agents;
using Symu.Common.Interfaces.Entity;

#endregion

Expand All @@ -38,9 +39,9 @@ internal void Add(T agent)
_list[agent.AgentId] = agent;
}

public bool Exists(ushort key, byte classKey)
public bool Exists(UId id, byte classId)
{
var agentId = new AgentId(key, classKey);
var agentId = new AgentId(id, classId);
return Exists(agentId);
}

Expand Down
4 changes: 3 additions & 1 deletion SourceCode/Symu/Repository/Entity/AgentDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#region using directives

using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.Repository.Networks.Resources;

#endregion
Expand All @@ -21,7 +23,7 @@ namespace Symu.Repository.Entity
/// </summary>
public class AgentDatabase : AgentResource
{
public AgentDatabase(IAgentId agentId, ResourceUsage resourceUsage, float resourceAllocation): base(agentId, resourceUsage, resourceAllocation)
public AgentDatabase(IId resourceId, IResourceUsage resourceUsage, float resourceAllocation): base(resourceId, resourceUsage, resourceAllocation)
{
}
}
Expand Down
1 change: 1 addition & 0 deletions SourceCode/Symu/Repository/Entity/AgentGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#region using directives

using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Repository.Networks.Groups;

#endregion
Expand Down
4 changes: 3 additions & 1 deletion SourceCode/Symu/Repository/Entity/AgentPortfolio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#region using directives

using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.Repository.Networks.Resources;

#endregion
Expand All @@ -21,7 +23,7 @@ namespace Symu.Repository.Entity
/// </summary>
public class AgentPortfolio : AgentResource
{
public AgentPortfolio(IAgentId agentId, ResourceUsage resourceUsage, float resourceAllocation): base(agentId, resourceUsage, resourceAllocation)
public AgentPortfolio(IId resourceId, IResourceUsage resourceUsage, float resourceAllocation): base(resourceId, resourceUsage, resourceAllocation)
{
}
}
Expand Down
23 changes: 10 additions & 13 deletions SourceCode/Symu/Repository/Entity/AgentResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

using System;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.Repository.Networks.Resources;

#endregion
Expand All @@ -22,7 +24,7 @@ namespace Symu.Repository.Entity
/// </summary>
public class AgentResource : IAgentResource
{
public AgentResource(IAgentId resourceId, ResourceUsage resourceUsage, float resourceAllocation=100)
public AgentResource(IId resourceId, IResourceUsage resourceUsage, float resourceAllocation=100)
{
ResourceId = resourceId;
ResourceUsage = resourceUsage;
Expand All @@ -32,7 +34,7 @@ public AgentResource(IAgentId resourceId, ResourceUsage resourceUsage, float res
/// <summary>
/// The unique agentId of the resource
/// </summary>
public IAgentId ResourceId { get; }
public IId ResourceId { get; }

/// <summary>
/// Define how the AgentId is using the resource
Expand Down Expand Up @@ -65,24 +67,19 @@ public IAgentResource Clone()
return new AgentResource(ResourceId, (ResourceUsage)ResourceUsage, ResourceAllocation);
}

public bool IsResourceUsage(IResourceUsage resourceUsage)
public bool Equals(IResourceUsage resourceUsage)
{
return ResourceUsage.IsResourceUsage((ResourceUsage)resourceUsage);
return ResourceUsage.Equals(resourceUsage);
}

public bool IsResourceUsageAndClassId(IResourceUsage resourceUsage, IClassId classId)
public bool Equals(IId resourceId)
{
return IsResourceUsage(resourceUsage) && ResourceId.Equals(classId);
}

public bool Equals(IAgentId resourceId, IResourceUsage resourceUsage)
{
return IsResourceUsage(resourceUsage) && ResourceId.Equals(resourceId);
return ResourceId.Equals(resourceId);
}

public bool Equals(IAgentId resourceId)
public bool Equals(IId resourceId, IResourceUsage resourceUsage)
{
return ResourceId.Equals(resourceId);
return Equals(resourceUsage) && ResourceId.Equals(resourceId);
}

public override bool Equals(object obj)
Expand Down

0 comments on commit bf732e7

Please sign in to comment.