Skip to content

Commit

Permalink
Extract interface RoleNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 25, 2020
1 parent 084e027 commit 422f32c
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 195 deletions.
57 changes: 57 additions & 0 deletions SourceCode/Symu/Repository/Networks/Roles/IAgentRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region Licence

// Description: SymuBiz - Symu
// Website: https://symu.org
// Copyright: (c) 2020 laurent morisseau
// License : the program is distributed under the terms of the GNU General Public License

#endregion

using Symu.Common.Interfaces.Agent;

namespace Symu.Repository.Networks.Roles
{
public interface IAgentRole
{
/// <summary>
/// Unique key of the agent
/// </summary>
IAgentId AgentId { get; }

/// <summary>
/// Unique key of the group
/// </summary>
IAgentId GroupId { get; set; }

/// <summary>
/// An agent may have different role type in a group
/// </summary>
IRole Role { get; set; }

bool IsMemberOfGroups(IAgentId teammateId, IClassId groupClassId);

/// <summary>
/// CHeck that there is a role of roleType for that groupId
/// </summary>
/// <param name="role"></param>
/// <param name="groupId"></param>
/// <returns></returns>
bool HasRoleInGroup(IRole role, IAgentId groupId);

bool HasRoleInGroup(IAgentId agentId, IRole role, IAgentId groupId);
bool HasRoleInGroup(IAgentId agentId, IAgentId groupId);

/// <summary>
/// CHeck that there is a role of roleType for that groupId
/// </summary>
/// <param name="role"></param>
/// <param name="agentId"></param>
/// <returns></returns>
bool HasRole(IAgentId agentId, IRole role);

bool HasRole(IRole role);
bool IsGroup(IAgentId groupId);
bool IsAgent(IAgentId agentId);
IAgentRole Clone();
}
}
19 changes: 19 additions & 0 deletions SourceCode/Symu/Repository/Networks/Roles/IRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#region Licence

// Description: SymuBiz - Symu
// Website: https://symu.org
// Copyright: (c) 2020 laurent morisseau
// License : the program is distributed under the terms of the GNU General Public License

#endregion

namespace Symu.Repository.Networks.Roles
{
/// <summary>
/// Defines the role of an agent for the RoleNetwork
/// </summary>
public interface IRole
{
bool Equals(IRole role);
}
}
33 changes: 9 additions & 24 deletions SourceCode/Symu/Repository/Networks/Roles/RoleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

using System;
using System.Collections.Generic;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Resources;

#endregion

Expand All @@ -27,37 +23,26 @@ namespace Symu.Repository.Networks.Roles
public class RoleCollection
{
/// <summary>
/// Key => DatabaseId
/// Values => List of Databases
/// List of all the roles used in the network
/// </summary>
public List<IResource> List { get; } = new List<IResource>();
public List<IRole> List { get; } = new List<IRole>();

public void Add(IResource resource)
public void Add(IRole role)
{
if (!Contains(resource))
if (!Contains(role))
{
List.Add(resource);
List.Add(role);
}
}

public bool Contains(IResource resource)
public bool Contains(IRole role)
{
if (resource is null)
if (role is null)
{
throw new ArgumentNullException(nameof(resource));
throw new ArgumentNullException(nameof(role));
}

return Exists(resource.Id);
}

public IResource Get(IId roleId)
{
return List.Find(k => k.Id.Equals(roleId));
}

public bool Exists(IId roleId)
{
return List.Exists(k => k.Id.Equals(roleId));
return List.Contains(role);
}

public void Clear()
Expand Down
53 changes: 28 additions & 25 deletions SourceCode/Symu/Repository/Networks/Roles/RoleNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Linq;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Repository.Entity;

#endregion

Expand All @@ -25,14 +26,14 @@ public class RoleNetwork
/// Repository of all the resources used during the simulation
/// </summary>
public RoleCollection Repository { get; } = new RoleCollection();
public List<RoleEntity> AgentRoles { get; } = new List<RoleEntity>();
public List<IAgentRole> AgentRoles { get; } = new List<IAgentRole>();

/// <summary>
/// Gets or sets the element at the specified index
/// </summary>
/// <param name="index">0 based</param>
/// <returns></returns>
public RoleEntity this[int index]
public IAgentRole this[int index]
{
get => AgentRoles[index];
set => AgentRoles[index] = value;
Expand Down Expand Up @@ -78,20 +79,20 @@ public bool IsMember(IAgentId agentId, IClassId groupClassId)
return AgentRoles.Exists(l => l != null && l.IsMemberOfGroups(agentId, groupClassId));
}

public bool ExistAgentForRoleType(byte roleType, IAgentId groupId)
public bool ExistAgentForRoleType(IRole role, IAgentId groupId)
{
return AgentRoles.Exists(l => l != null && l.HasRoleInGroup(roleType, groupId));
return AgentRoles.Exists(l => l != null && l.HasRoleInGroup(role, groupId));
}

public IAgentId GetAgentIdForRoleType(byte roleType, IAgentId groupId)
public IAgentId GetAgentIdForRoleType(IRole role, IAgentId groupId)
{
var group = AgentRoles.Find(l => l != null && l.HasRoleInGroup(roleType, groupId));
var group = AgentRoles.Find(l => l != null && l.HasRoleInGroup(role, groupId));
return group?.AgentId;
}

public IEnumerable<IAgentId> GetGroups(IAgentId agentId, byte roleType)
public IEnumerable<IAgentId> GetGroups(IAgentId agentId, IRole role)
{
return AgentRoles.FindAll(l => l != null && l.HasRole(agentId, roleType)).Select(x => x.GroupId);
return AgentRoles.FindAll(l => l != null && l.HasRole(agentId, role)).Select(x => x.GroupId);
}

/// <summary>
Expand All @@ -102,14 +103,14 @@ public bool HasRoles(IAgentId agentId)
return AgentRoles.Exists(l => l != null && l.AgentId.Equals(agentId));
}

public bool HasRole(IAgentId agentId, byte roleType)
public bool HasRole(IAgentId agentId, IRole role)
{
return AgentRoles.Exists(l => l != null && l.HasRole(agentId, roleType));
return AgentRoles.Exists(l => l != null && l.HasRole(agentId, role));
}

public bool HasARoleIn(IAgentId agentId, byte roleType, IAgentId groupId)
public bool HasARoleIn(IAgentId agentId, IRole role, IAgentId groupId)
{
return AgentRoles.Exists(l => l != null && l.HasRoleInGroup(agentId, roleType, groupId));
return AgentRoles.Exists(l => l != null && l.HasRoleInGroup(agentId, role, groupId));
}

public bool HasARoleIn(IAgentId agentId, IAgentId groupId)
Expand All @@ -128,7 +129,7 @@ public void RemoveMember(IAgentId agentId, IAgentId groupId)
/// <param name="agentId"></param>
/// <param name="groupId"></param>
/// <returns></returns>
public IEnumerable<RoleEntity> GetRoles(IAgentId agentId, IAgentId groupId)
public IEnumerable<IAgentRole> GetRoles(IAgentId agentId, IAgentId groupId)
{
lock (AgentRoles)
{
Expand All @@ -141,19 +142,19 @@ public IEnumerable<RoleEntity> GetRoles(IAgentId agentId, IAgentId groupId)
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
public IEnumerable<RoleEntity> GetRoles(IAgentId groupId)
public IEnumerable<IAgentRole> GetRoles(IAgentId groupId)
{
return AgentRoles.Where(r => r.IsGroup(groupId));
}

/// <summary>
/// Get the roles of the agentId for the groupId
/// </summary>
/// <param name="roleType"></param>
/// <param name="role"></param>
/// <returns></returns>
public IEnumerable<IAgentId> GetAgents(byte roleType)
public IEnumerable<IAgentId> GetAgents(IRole role)
{
return AgentRoles.Where(r => r.HasRole(roleType)).Select(x => x.AgentId);
return AgentRoles.Where(r => r.HasRole(role)).Select(x => x.AgentId);
}

/// <summary>
Expand All @@ -179,31 +180,33 @@ public void TransferTo(IAgentId agentId, IAgentId groupSourceId, IAgentId groupT
var roles = GetRoles(agentId, groupSourceId).ToList();
foreach (var role in roles)
{
AgentRoles.Add(new RoleEntity(agentId, groupTargetId, role.RoleType));
var agentRole = role.Clone();
agentRole.GroupId = groupTargetId;
AgentRoles.Add(agentRole);
}

RemoveMember(agentId, groupSourceId);
}
}

public void Add(RoleEntity roleEntity)
public void Add(IAgentRole agentRole)
{
if (Exists(roleEntity))
if (Exists(agentRole))
{
return;
}

AgentRoles.Add(roleEntity);
AgentRoles.Add(agentRole);
}

public bool Exists(RoleEntity roleEntity)
public bool Exists(IAgentRole agentRole)
{
return AgentRoles.Contains(roleEntity);
return AgentRoles.Contains(agentRole);
}

public void RemoveMembersByRoleTypeFromGroup(byte roleType, IAgentId groupId)
public void RemoveMembersByRoleTypeFromGroup(IRole role, IAgentId groupId)
{
AgentRoles.RemoveAll(l => l.HasRoleInGroup(roleType, groupId));
AgentRoles.RemoveAll(l => l.HasRoleInGroup(role, groupId));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region Licence

// Description: SymuBiz - Symu
// Description: SymuBiz - SymuTests
// Website: https://symu.org
// Copyright: (c) 2020 laurent morisseau
// License : the program is distributed under the terms of the GNU General Public License
Expand All @@ -9,20 +9,30 @@

#region using directives

using System;
using Symu.Classes.Agents;
using Symu.Common.Interfaces;
using Symu.Common.Interfaces.Agent;
using Symu.Common.Interfaces.Entity;
using Symu.DNA.Resources;
using Symu.Environment;
using Symu.Repository;
using Symu.Repository.Networks.Roles;

#endregion

namespace Symu.Repository.Networks.Roles
namespace SymuTests.Helpers
{
public class RoleEntity
/// <summary>
/// Class for tests
/// </summary>
internal sealed class TestAgentRole : IAgentRole
{
public RoleEntity(IAgentId agentId, IAgentId groupId, byte roleType)
public TestAgentRole(IAgentId agentId, IAgentId groupId, byte role)
{
AgentId = agentId;
GroupId = groupId;
RoleType = roleType;
Role = new TestRole(role);
}

/// <summary>
Expand All @@ -38,8 +48,7 @@ public RoleEntity(IAgentId agentId, IAgentId groupId, byte roleType)
/// <summary>
/// An agent may have different role type in a group
/// </summary>
public byte RoleType { get; set; }

public IRole Role { get; set; }
public bool IsMemberOfGroups(IAgentId teammateId, IClassId groupClassId)
{
return GroupId.Equals(groupClassId) && IsAgent(teammateId);
Expand All @@ -48,17 +57,17 @@ public bool IsMemberOfGroups(IAgentId teammateId, IClassId groupClassId)
/// <summary>
/// CHeck that there is a role of roleType for that groupId
/// </summary>
/// <param name="roleType"></param>
/// <param name="role"></param>
/// <param name="groupId"></param>
/// <returns></returns>
public bool HasRoleInGroup(byte roleType, IAgentId groupId)
public bool HasRoleInGroup(IRole role, IAgentId groupId)
{
return RoleType == roleType && IsGroup(groupId);
return Role.Equals(role) && IsGroup(groupId);
}

public bool HasRoleInGroup(IAgentId agentId, byte roleType, IAgentId groupId)
public bool HasRoleInGroup(IAgentId agentId, IRole role, IAgentId groupId)
{
return RoleType == roleType && IsAgent(agentId) && IsGroup(groupId);
return Role.Equals(role) && IsAgent(agentId) && IsGroup(groupId);
}

public bool HasRoleInGroup(IAgentId agentId, IAgentId groupId)
Expand All @@ -69,17 +78,17 @@ public bool HasRoleInGroup(IAgentId agentId, IAgentId groupId)
/// <summary>
/// CHeck that there is a role of roleType for that groupId
/// </summary>
/// <param name="roleType"></param>
/// <param name="role"></param>
/// <param name="agentId"></param>
/// <returns></returns>
public bool HasRole(IAgentId agentId, byte roleType)
public bool HasRole(IAgentId agentId, IRole role)
{
return RoleType == roleType && IsAgent(agentId);
return Role.Equals(role) && IsAgent(agentId);
}

public bool HasRole(byte roleType)
public bool HasRole(IRole role)
{
return RoleType == roleType;
return Role.Equals(role);
}

public bool IsGroup(IAgentId groupId)
Expand All @@ -91,5 +100,9 @@ public bool IsAgent(IAgentId agentId)
{
return AgentId.Equals(agentId);
}
public IAgentRole Clone()
{
return new TestAgentRole(AgentId, GroupId, ((TestRole)Role).Role);
}
}
}

0 comments on commit 422f32c

Please sign in to comment.