Skip to content

Commit

Permalink
Extract interface from GroupNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 21, 2020
1 parent 279724e commit fa7ef56
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#region using directives

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

#endregion

namespace Symu.Repository.Networks.Group
namespace Symu.Repository.Entity
{
public class GroupAllocation
public class AgentGroup : IAgentGroup
{
public GroupAllocation(IAgentId agentId, float allocation)
public AgentGroup(IAgentId agentId, float allocation)
{
AgentId = agentId;
Allocation = allocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
using System.Collections.Generic;
using System.Linq;
using Symu.Common.Interfaces;
using Symu.Repository.Entity;

#endregion

namespace Symu.Repository.Networks.Group
namespace Symu.Repository.Networks.Groups
{
/// <summary>
/// Dictionary of all the group of the network
Expand All @@ -32,8 +33,8 @@ public class GroupNetwork
/// Key => groupId
/// Value => list of group allocation : AgentId, Allocation of the agentId to the groupId
/// </summary>
public ConcurrentDictionary<IAgentId, List<GroupAllocation>> List { get; } =
new ConcurrentDictionary<IAgentId, List<GroupAllocation>>();
public ConcurrentDictionary<IAgentId, List<IAgentGroup>> AgentGroups { get; } =
new ConcurrentDictionary<IAgentId, List<IAgentGroup>>();

/// <summary>
/// Remove agent from network,
Expand Down Expand Up @@ -62,13 +63,13 @@ public void RemoveMember(IAgentId agentId, IAgentId groupId)
{
if (Exists(groupId))
{
List[groupId].RemoveAll(g => g.AgentId.Equals(agentId));
AgentGroups[groupId].RemoveAll(g => g.AgentId.Equals(agentId));
}
}

public IEnumerable<IAgentId> GetGroups()
{
return List.Any() ? List.Keys : new List<IAgentId>();
return AgentGroups.Any() ? AgentGroups.Keys : new List<IAgentId>();
}

/// <summary>
Expand All @@ -78,29 +79,29 @@ public IEnumerable<IAgentId> GetGroups()
/// <returns></returns>
public bool Exists(IAgentId groupId)
{
return List.ContainsKey(groupId);
return AgentGroups.ContainsKey(groupId);
}

public void RemoveGroup(IAgentId groupId)
{
List.TryRemove(groupId, out _);
AgentGroups.TryRemove(groupId, out _);
}

public bool Any()
{
return List.Any();
return AgentGroups.Any();
}

public void Clear()
{
List.Clear();
AgentGroups.Clear();
}

public void AddGroup(IAgentId groupId)
{
if (!Exists(groupId))
{
List.TryAdd(groupId, new List<GroupAllocation>());
AgentGroups.TryAdd(groupId, new List<IAgentGroup>());
}
}

Expand All @@ -115,8 +116,8 @@ public void AddAgent(IAgentId agentId, float allocation, IAgentId groupId)
AddGroup(groupId);
if (!IsMemberOfGroup(agentId, groupId))
{
var groupAllocation = new GroupAllocation(agentId, allocation);
List[groupId].Add(groupAllocation);
var agentGroup = new AgentGroup(agentId, allocation);
AgentGroups[groupId].Add(agentGroup);
}
else
{
Expand All @@ -138,7 +139,7 @@ public void AddAgent(IAgentId agentId, float allocation, IAgentId groupId)
public IEnumerable<IAgentId> GetAgents(IAgentId groupId)
{
return Exists(groupId)
? List[groupId].Select(x => x.AgentId)
? AgentGroups[groupId].Select(x => x.AgentId)
: null;
}

Expand All @@ -151,7 +152,7 @@ public IEnumerable<IAgentId> GetAgents(IAgentId groupId)
public IEnumerable<IAgentId> GetAgents(IAgentId groupId, IClassId classId)
{
return Exists(groupId)
? List[groupId].FindAll(x => x.AgentId.ClassId.Equals(classId)).Select(x => x.AgentId)
? AgentGroups[groupId].FindAll(x => x.AgentId.ClassId.Equals(classId)).Select(x => x.AgentId)
: null;
}

Expand All @@ -162,7 +163,7 @@ public IEnumerable<IAgentId> GetAgents(IAgentId groupId, IClassId classId)
/// <returns></returns>
public byte GetAgentsCount(IAgentId groupId)
{
return Exists(groupId) ? Convert.ToByte(List[groupId].Count) : (byte) 0;
return Exists(groupId) ? Convert.ToByte(AgentGroups[groupId].Count) : (byte) 0;
}

/// <summary>
Expand All @@ -178,27 +179,27 @@ public byte GetAgentsCount(IAgentId groupId, IClassId classId)
return 0;
}

lock (List[groupId])
lock (AgentGroups[groupId])
{
return Convert.ToByte(List[groupId].Count(x => x.AgentId.ClassId.Equals(classId)));
return Convert.ToByte(AgentGroups[groupId].Count(x => x.AgentId.ClassId.Equals(classId)));
}
}

public bool IsMemberOfGroup(IAgentId agentId, IAgentId groupId)
{
return Exists(groupId) && List[groupId].Exists(g => g != null && g.AgentId.Equals(agentId));
return Exists(groupId) && AgentGroups[groupId].Exists(g => g != null && g.AgentId.Equals(agentId));
}

public GroupAllocation GetGroupAllocation(IAgentId agentId, IAgentId groupId)
public IAgentGroup GetGroupAllocation(IAgentId agentId, IAgentId groupId)
{
return Exists(groupId) ? List[groupId].Find(g => g != null && g.AgentId.Equals(agentId)) : null;
return Exists(groupId) ? AgentGroups[groupId].Find(g => g != null && g.AgentId.Equals(agentId)) : null;
}

public float GetAllocation(IAgentId agentId, IAgentId groupId)
{
if (IsMemberOfGroup(agentId, groupId))
{
return List[groupId].Find(g => g != null && g.AgentId.Equals(agentId)).Allocation;
return AgentGroups[groupId].Find(g => g != null && g.AgentId.Equals(agentId)).Allocation;
}

return 0;
Expand All @@ -213,7 +214,7 @@ public float GetAllocation(IAgentId agentId, IAgentId groupId)
public IEnumerable<IAgentId> GetGroups(IAgentId agentId, IClassId groupClassId)
{
var groupIds = new List<IAgentId>();
if (!List.Any())
if (!AgentGroups.Any())
{
return groupIds;
}
Expand All @@ -231,7 +232,7 @@ public IEnumerable<IAgentId> GetGroups(IAgentId agentId, IClassId groupClassId)
public IEnumerable<IAgentId> GetGroups(IClassId groupClassId)
{
var groupIds = new List<IAgentId>();
if (!List.Any())
if (!AgentGroups.Any())
{
return groupIds;
}
Expand All @@ -258,7 +259,7 @@ public IEnumerable<IAgentId> GetCoMemberIds(IAgentId agentId, IClassId groupClas

foreach (var groupId in groupIds)
{
coMemberIds.AddRange(List[groupId].FindAll(x => !x.AgentId.Equals(agentId)).Select(x => x.AgentId));
coMemberIds.AddRange(AgentGroups[groupId].FindAll(x => !x.AgentId.Equals(agentId)).Select(x => x.AgentId));
}

return coMemberIds.Distinct();
Expand All @@ -270,18 +271,18 @@ public IEnumerable<IAgentId> GetCoMemberIds(IAgentId agentId, IClassId groupClas
/// <param name="agentId"></param>
/// <param name="classId"></param>
/// <returns>List of groupAllocations (groupId, Allocation)</returns>
public IEnumerable<GroupAllocation> GetGroupAllocationsOfAnAgentId(IAgentId agentId, IClassId classId)
public IEnumerable<AgentGroup> GetAgentGroupsOfAnAgentId(IAgentId agentId, IClassId classId)
{
var groupAllocations = new List<GroupAllocation>();
if (!List.Any())
var groupAllocations = new List<AgentGroup>();
if (!AgentGroups.Any())
{
return groupAllocations;
}

// convert To List, because the collection can be modified during the method
groupAllocations.AddRange(GetGroups().ToList()
.Where(g => g.ClassId.Equals(classId) && IsMemberOfGroup(agentId, g))
.Select(groupId => new GroupAllocation(groupId, GetAllocation(agentId, groupId))));
.Select(groupId => new AgentGroup(groupId, GetAllocation(agentId, groupId))));

return groupAllocations;
}
Expand All @@ -292,11 +293,11 @@ public IEnumerable<GroupAllocation> GetGroupAllocationsOfAnAgentId(IAgentId agen
/// <param name="groupId"></param>
/// <param name="classId"></param>
/// <returns>List of groupAllocations (groupId, Allocation)</returns>
public IEnumerable<GroupAllocation> GetGroupAllocationsOfAGroupId(IAgentId groupId, IClassId classId)
public IEnumerable<IAgentGroup> GetAgentGroupsOfAGroupId(IAgentId groupId, IClassId classId)
{
return Exists(groupId)
? List[groupId].FindAll(x => x.AgentId.ClassId.Equals(classId))
: new List<GroupAllocation>();
? AgentGroups[groupId].FindAll(x => x.AgentId.ClassId.Equals(classId))
: new List<IAgentGroup>();
}

/// <summary>
Expand All @@ -306,7 +307,7 @@ public IEnumerable<GroupAllocation> GetGroupAllocationsOfAGroupId(IAgentId group
/// <returns>allocation</returns>
public float GetAgentAllocations(IAgentId groupId)
{
return Exists(groupId) ? List[groupId].Sum(a => a.Allocation) : 0;
return Exists(groupId) ? AgentGroups[groupId].Sum(a => a.Allocation) : 0;
}

/// <summary>
Expand Down Expand Up @@ -336,7 +337,7 @@ public void UpdateGroupAllocation(IAgentId agentId, IAgentId groupId, float allo
/// <param name="fullAlloc">true if all groupAllocations are added, false if we are in modeling phase</param>
public void UpdateGroupAllocations(IAgentId agentId, IClassId classId, bool fullAlloc)
{
var groupAllocations = GetGroupAllocationsOfAnAgentId(agentId, classId).ToList();
var groupAllocations = GetAgentGroupsOfAnAgentId(agentId, classId).ToList();

if (!groupAllocations.Any())
{
Expand Down Expand Up @@ -376,9 +377,9 @@ public void UpdateGroupAllocations(IAgentId agentId, IClassId classId, bool full
/// </returns>
public IAgentId GetMainGroupOrDefault(IAgentId agentId, IClassId classId)
{
var groups = GetGroupAllocationsOfAnAgentId(agentId, classId);
var groups = GetAgentGroupsOfAnAgentId(agentId, classId);
return groups.Any()
? GetGroupAllocationsOfAnAgentId(agentId, classId).OrderByDescending(ga => ga.Allocation).First()
? GetAgentGroupsOfAnAgentId(agentId, classId).OrderByDescending(ga => ga.Allocation).First()
.AgentId
: null;
}
Expand All @@ -391,9 +392,9 @@ public IAgentId GetMainGroupOrDefault(IAgentId agentId, IClassId classId)
public void CopyTo(IAgentId groupSourceId, IAgentId groupTargetId)
{
AddGroup(groupTargetId);
foreach (var groupAllocation in List[groupSourceId])
foreach (var groupAllocation in AgentGroups[groupSourceId])
{
List[groupTargetId].Add(groupAllocation);
AgentGroups[groupTargetId].Add(groupAllocation);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions SourceCode/Symu/Repository/Networks/Groups/IAgentGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#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;

namespace Symu.Repository.Networks.Groups
{
/// <summary>
/// Interface to define who is member of a group and how
/// By default how is characterized by an allocation of capacity to define part-time membership
///
/// </summary>
public interface IAgentGroup
{
IAgentId AgentId { get; }

/// <summary>
/// Range 0 - 100
/// </summary>
float Allocation { get; set; }
}
}
2 changes: 1 addition & 1 deletion SourceCode/Symu/Repository/Networks/MetaNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Symu.Repository.Networks.Activities;
using Symu.Repository.Networks.Beliefs;
using Symu.Repository.Networks.Enculturation;
using Symu.Repository.Networks.Group;
using Symu.Repository.Networks.Groups;
using Symu.Repository.Networks.Influences;
using Symu.Repository.Networks.Knowledges;
using Symu.Repository.Networks.Link;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Symu.Classes.Agents;
using Symu.Repository.Networks.Group;
using Symu.Repository.Networks.Groups;

#endregion

namespace SymuTests.Repository.Networks.Group
namespace SymuTests.Repository.Networks.Groups
{
[TestClass]
public class NetworkGroupsTests
Expand Down Expand Up @@ -62,7 +62,7 @@ public void AddTeammateTest()
Assert.IsFalse(_group.Any());
_group.AddAgent(_teammateId, 100, _teamId);
Assert.IsTrue(_group.Any());
Assert.IsTrue(_group.List[_teamId][0].AgentId.Equals(_teammateId));
Assert.IsTrue(_group.AgentGroups[_teamId][0].AgentId.Equals(_teammateId));
}

[TestMethod]
Expand Down Expand Up @@ -178,9 +178,9 @@ public void GetCoMemberIds()
[TestMethod]
public void GetGroupAllocationsTest()
{
Assert.AreEqual(0, _group.GetGroupAllocationsOfAnAgentId(_teammateId, _teamId.ClassId).Count());
Assert.AreEqual(0, _group.GetAgentGroupsOfAnAgentId(_teammateId, _teamId.ClassId).Count());
_group.AddAgent(_teammateId, 100, _teamId);
Assert.AreEqual(1, _group.GetGroupAllocationsOfAnAgentId(_teammateId, _teamId.ClassId).Count());
Assert.AreEqual(1, _group.GetAgentGroupsOfAnAgentId(_teammateId, _teamId.ClassId).Count());
}

[TestMethod]
Expand Down

0 comments on commit fa7ef56

Please sign in to comment.