Skip to content

Commit

Permalink
Extract interface for ResourceNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Aug 21, 2020
1 parent 688ba55 commit 279724e
Show file tree
Hide file tree
Showing 19 changed files with 454 additions and 187 deletions.
4 changes: 3 additions & 1 deletion SourceCode/Symu/Classes/Agents/CognitiveAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Symu.Messaging.Messages;
using Symu.Repository;
using Symu.Repository.Entity;
using Symu.Repository.Networks.Resources;
using Symu.Repository.Networks.Sphere;

#endregion
Expand Down Expand Up @@ -131,7 +132,8 @@ protected override void CreateAgent(AgentId agentId, SymuEnvironment environment
foreach (var database in environment.Organization.Databases)
{
// Organization databases are used by every one
environment.WhitePages.MetaNetwork.Resources.Add(AgentId, database.AgentId, 0);
var agentResource = new AgentResource(database.AgentId, new ResourceUsage(0), 100);
environment.WhitePages.MetaNetwork.Resources.Add(AgentId, agentResource);
}
}

Expand Down
4 changes: 2 additions & 2 deletions SourceCode/Symu/Repository/Entity/AgentDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Symu.Repository.Entity
/// <summary>
/// Define who is using a database and how
/// </summary>
public class AgentDatabase : IAgentResource
public class AgentDatabase : AgentResource
{
public AgentDatabase(IAgentId agentId, byte typeOfUse, float allocation): base(agentId, typeOfUse, allocation)
public AgentDatabase(IAgentId agentId, ResourceUsage resourceUsage, float resourceAllocation): base(agentId, resourceUsage, resourceAllocation)
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions SourceCode/Symu/Repository/Entity/AgentPortfolio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Symu.Repository.Entity
/// <summary>
/// Define who is using a component or a product and how
/// </summary>
public class AgentPortfolio : IAgentResource
public class AgentPortfolio : AgentResource
{
public AgentPortfolio(IAgentId agentId, byte typeOfUse, float allocation): base(agentId, typeOfUse, allocation)
public AgentPortfolio(IAgentId agentId, ResourceUsage resourceUsage, float resourceAllocation): base(agentId, resourceUsage, resourceAllocation)
{
}
}
Expand Down
95 changes: 95 additions & 0 deletions SourceCode/Symu/Repository/Entity/AgentResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#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

#region using directives

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

#endregion

namespace Symu.Repository.Entity
{
/// <summary>
/// Define who is using a resource and how
/// </summary>
public class AgentResource : IAgentResource
{
public AgentResource(IAgentId resourceId, ResourceUsage resourceUsage, float resourceAllocation=100)
{
ResourceId = resourceId;
ResourceUsage = resourceUsage;
ResourceAllocation = resourceAllocation;
}

/// <summary>
/// The unique agentId of the resource
/// </summary>
public IAgentId ResourceId { get; }

/// <summary>
/// Define how the AgentId is using the resource
/// </summary>
public IResourceUsage ResourceUsage { get; }


private float _resourceAllocation;

/// <summary>
/// Allocation of capacity per resource
/// capacity allocation ranging from [0; 100]
/// </summary>
public float ResourceAllocation
{
get => _resourceAllocation;
set
{
if (value < 0 || value > 100)
{
throw new ArgumentOutOfRangeException("Allocation should be between [0;100]");
}

_resourceAllocation = value;
}
}

public IAgentResource Clone()
{
return new AgentResource(ResourceId, (ResourceUsage)ResourceUsage, ResourceAllocation);
}

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

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

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

public bool Equals(IAgentId resourceId)
{
return ResourceId.Equals(resourceId);
}

public override bool Equals(object obj)
{
return obj is AgentResource agentResource &&
ResourceId.Equals(agentResource.ResourceId) &&
ResourceUsage == agentResource.ResourceUsage;
}
}
}
35 changes: 35 additions & 0 deletions SourceCode/Symu/Repository/Entity/ResourceUsage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#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 System;
using Symu.Repository.Networks.Resources;

namespace Symu.Repository.Entity
{
/// <summary>
/// Implement IResourceUsage
/// </summary>
public class ResourceUsage : IResourceUsage
{
public ResourceUsage(byte usage)
{
Usage = usage;
}
public byte Usage { get; }
public bool IsResourceUsage(IResourceUsage resourceUsage)
{
if (resourceUsage == null)
{
throw new ArgumentNullException(nameof(resourceUsage));
}

return Usage == ((ResourceUsage)resourceUsage).Usage;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void RemoveMember(IAgentId agentId, IAgentId groupId)
{
if (Exists(groupId))
{
AgentActivities[groupId].RemoveAll(g => g.AgentId.Equals(agentId));
AgentActivities[groupId].RemoveAll(g => g.Id.Equals(agentId));
}
}

Expand All @@ -266,12 +266,12 @@ public void AddActivity(IAgentId agentId, string activity, IAgentId groupId)
public bool AgentHasAnActivityOn(IAgentId agentId, IAgentId groupId, string activity)
{
return Exists(groupId) && AgentActivities[groupId]
.Exists(g => g.AgentId.Equals(agentId) && g.Activity == activity);
.Exists(g => g.Id.Equals(agentId) && g.Activity == activity);
}

public bool AgentHasActivitiesOn(IAgentId agentId, IAgentId groupId)
{
return Exists(groupId) && AgentActivities[groupId].Exists(g => g.AgentId.Equals(agentId));
return Exists(groupId) && AgentActivities[groupId].Exists(g => g.Id.Equals(agentId));
}


Expand All @@ -284,7 +284,7 @@ public bool AgentHasActivitiesOn(IAgentId agentId, IAgentId groupId)
public IEnumerable<string> GetAgentActivities(IAgentId agentId, IAgentId groupId)
{
return Exists(groupId)
? AgentActivities[groupId].FindAll(g => g.AgentId.Equals(agentId)).Select(x => x.Activity)
? AgentActivities[groupId].FindAll(g => g.Id.Equals(agentId)).Select(x => x.Activity)
: new List<string>();
}

Expand All @@ -298,7 +298,7 @@ public IEnumerable<string> GetAgentActivities(IAgentId agentId)
var activities = new List<string>();
foreach (var agentActivities in AgentActivities)
{
activities.AddRange(agentActivities.Value.Where(x => x.AgentId.Equals(agentId))
activities.AddRange(agentActivities.Value.Where(x => x.Id.Equals(agentId))
.Select(agentActivity => agentActivity.Activity));
}

Expand Down Expand Up @@ -366,7 +366,7 @@ public void TransferTo(IAgentId agentId, IAgentId groupSourceId, IAgentId groupT
/// <returns></returns>
public bool HasAgentActivities(IAgentId agentId)
{
return AgentActivities.Any(a => a.Value.Exists(v => v != null && v.AgentId.Equals(agentId)));
return AgentActivities.Any(a => a.Value.Exists(v => v != null && v.Id.Equals(agentId)));
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace Symu.Repository.Networks.Activities
{
public class AgentActivity
{
public AgentActivity(IAgentId agentId, string activity)
public AgentActivity(IAgentId id, string activity)
{
AgentId = agentId;
Id = id;
Activity = activity;
}

public IAgentId AgentId { get; }
public IAgentId Id { get; }
public string Activity { get; set; }
}
}
17 changes: 11 additions & 6 deletions SourceCode/Symu/Repository/Networks/Link/LinkNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ public bool Any()
/// Reinitialize links between members of a group :
/// Add a bi directional link between every member of a group
/// </summary>
public void AddLinks(List<IAgentId> members)
public void AddLinks(List<IAgentId> agentIds)
{
if (members == null)
if (agentIds == null)
{
throw new ArgumentNullException(nameof(members));
throw new ArgumentNullException(nameof(agentIds));
}

var count = members.Count;
var count = agentIds.Count;
for (var i = 0; i < count; i++)
{
var agentId1 = members[i];
var agentId1 = agentIds[i];
for (var j = i + 1; j < count; j++)
{
var agentId2 = members[j];
var agentId2 = agentIds[j];
AddLink(agentId1, agentId2);
}
}
Expand All @@ -84,6 +84,11 @@ public void Clear()
/// <param name="agentId2"></param>
public void AddLink(IAgentId agentId1, IAgentId agentId2)
{
if (agentId1 == null)
{
throw new ArgumentNullException(nameof(agentId1));
}

if (agentId1.Equals(agentId2))
{
return;
Expand Down
62 changes: 15 additions & 47 deletions SourceCode/Symu/Repository/Networks/Resources/IAgentResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,36 @@

#endregion

#region using directives

using Symu.Common.Interfaces;

#endregion

namespace Symu.Repository.Networks.Resources
{

/// <summary>
/// Define who is using a resource and how
/// Interface to define the node Agent/resource : define who is using a resource and how
/// By default, an agent uses a resourceId, with an allocation from 0 to 100adn a certain ResourceUsage
/// </summary>
public class IAgentResource
public interface IAgentResource
{
public IAgentResource(IAgentId resourceId, byte typeOfUse, float allocation)
{
ResourceId = resourceId;
TypeOfUse = typeOfUse;
Allocation = allocation;
}

/// <summary>
/// The unique agentId of the resource
/// </summary>
public IAgentId ResourceId { get; }

/// <summary>
/// Define how the AgentId is using the resource
/// </summary>
public byte TypeOfUse { get; }

IAgentId ResourceId { get; }
/// <summary>
/// Allocation of capacity per resource
/// capacity allocation ranging from [0; 100]
/// </summary>
public float Allocation { get; set; }

public bool IsTypeOfUse(byte typeOfUse)
{
return TypeOfUse == typeOfUse;
}

public bool IsTypeOfUseAndClassId(byte typeOfUse, IClassId classId)
{
return IsTypeOfUse(typeOfUse) && ResourceId.Equals(classId);
}

public bool Equals(IAgentId resourceId, byte typeOfUse)
{
return IsTypeOfUse(typeOfUse) && ResourceId.Equals(resourceId);
}
float ResourceAllocation { get; set; }
/// <summary>
/// Define how the AgentId is using the resource
/// </summary>
IResourceUsage ResourceUsage { get; }

public bool Equals(IAgentId resourceId)
{
return ResourceId.Equals(resourceId);
}
IAgentResource Clone();

public override bool Equals(object obj)
{
return obj is IAgentResource agentResource &&
ResourceId.Equals(agentResource.ResourceId) &&
TypeOfUse == agentResource.TypeOfUse;
}
bool IsResourceUsage(IResourceUsage resourceUsage);
bool IsResourceUsageAndClassId(IResourceUsage resourceUsage, IClassId classId);
bool Equals(IAgentId resourceId, IResourceUsage resourceUsage);
bool Equals(IAgentId resourceId);
bool Equals(object obj);
}
}
7 changes: 6 additions & 1 deletion SourceCode/Symu/Repository/Networks/Resources/IResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@

namespace Symu.Repository.Networks.Resources
{

/// <summary>
/// The interface that let you define a resource
/// </summary>
public interface IResource
{
/// <summary>
/// Unique identifier of the resource
/// </summary>
IAgentId Id { get; }
}
}

0 comments on commit 279724e

Please sign in to comment.