Skip to content

Commit dc71ecf

Browse files
committed
Refactoring Edge Constructors
1 parent 581cfb1 commit dc71ecf

File tree

21 files changed

+99
-83
lines changed

21 files changed

+99
-83
lines changed

SourceCode/Symu/Classes/Agents/CognitiveAgent.Messaging.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ public bool AcceptNewInteraction(IAgentId senderId)
229229
}
230230

231231
// Message.Sender is now part of agent interaction sphere
232-
var interaction = new ActorActor(AgentId, senderId);
233-
Environment.MainOrganization.MetaNetwork.ActorActor.Add(interaction);
232+
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, AgentId, senderId);
234233

235234
return true;
236235
}

SourceCode/Symu/Classes/Agents/Models/CognitiveModels/ActorTaskModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public IDictionary<ITask, IEnumerable<IKnowledge>> Knowledge
9292
/// <param name="taskId"></param>
9393
public void AddActorTask(IAgentId taskId)
9494
{
95-
var actorTask = new ActorTask(_agentId, taskId);
96-
_actorTaskNetwork.Add(actorTask);
95+
_ = new ActorTask(_actorTaskNetwork, _agentId, taskId);
9796
}
9897

9998
/// <summary>

SourceCode/Symu/Classes/Agents/Models/CognitiveModels/BeliefsModel.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ public void AddBelief(IAgentId beliefId, BeliefLevel beliefLevel)
157157
return;
158158
}
159159

160-
var actorBelief = new ActorBelief(_agentId, beliefId, beliefLevel);
161-
_actorBeliefNetwork.Add(actorBelief);
160+
_ = new ActorBelief(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
162161
}
163162

164163

@@ -181,8 +180,7 @@ public void AddBeliefFromKnowledgeId(IAgentId knowledgeId, BeliefLevel beliefLev
181180
throw new NullReferenceException(nameof(belief));
182181
}
183182

184-
var actorBelief = new ActorBelief(_agentId, belief.EntityId, beliefLevel);
185-
_actorBeliefNetwork.Add(actorBelief);
183+
_ = new ActorBelief(_actorBeliefNetwork, _agentId, belief.EntityId, beliefLevel);
186184
}
187185

188186
public ActorBelief GetActorBelief(IAgentId beliefId)
@@ -425,8 +423,7 @@ public void LearnNewBelief(IAgentId beliefId, BeliefLevel beliefLevel)
425423
return;
426424
}
427425

428-
var actorBelief = new ActorBelief(_agentId, beliefId, beliefLevel);
429-
_actorBeliefNetwork.Add(actorBelief);
426+
_ = new ActorBelief(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
430427
InitializeBeliefs(true);
431428
}
432429
}

SourceCode/Symu/Classes/Agents/Models/CognitiveModels/KnowledgeModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ public void AddKnowledge(IAgentId knowledgeId, KnowledgeLevel level, float minim
192192
return;
193193
}
194194

195-
var actorKnowledge = new ActorKnowledge(_agentId, knowledgeId, level, minimumKnowledge, timeToLive);
196-
_actorKnowledgeNetwork.Add(actorKnowledge);
195+
_ = new ActorKnowledge(_actorKnowledgeNetwork, _agentId, knowledgeId, level, minimumKnowledge, timeToLive);
197196
}
198197

199198
/// <summary>

SourceCode/Symu/Classes/Agents/Models/CognitiveModels/LearningModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,8 @@ public void LearnNewKnowledge(IAgentId agentId, IAgentId knowledgeId, float mini
365365
return;
366366
}
367367

368-
var agentKnowledge = new ActorKnowledge(agentId, knowledgeId, KnowledgeLevel.NoKnowledge, minimumKnowledge,
368+
var agentKnowledge = new ActorKnowledge(_entityKnowledgeNetwork, agentId, knowledgeId, KnowledgeLevel.NoKnowledge, minimumKnowledge,
369369
timeToLive);
370-
_entityKnowledgeNetwork.Add(agentKnowledge);
371370
var knowledge = _knowledgeNetwork.GetEntity<Knowledge>(knowledgeId);
372371
agentKnowledge.InitializeKnowledge(knowledge.Length, _model, KnowledgeLevel.NoKnowledge, step);
373372
}

SourceCode/Symu/Classes/Agents/Models/CognitiveModels/ResourceTaskModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public void AddResourceTasks(IEnumerable<ITask> tasks)
8888

8989
foreach (var task in tasks)
9090
{
91-
var resourceTask = new ResourceTask(_resourceId, task.EntityId);
92-
_resourceTaskNetwork.Add(resourceTask);
91+
_ = new ResourceTask(_resourceTaskNetwork, _resourceId, task.EntityId);
9392
}
9493
}
9594
}

SourceCode/Symu/Repository/Edges/ActorBelief.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Symu.Common.Interfaces;
1616
using Symu.Common.Math.ProbabilityDistributions;
1717
using Symu.OrgMod.Edges;
18+
using Symu.OrgMod.GraphNetworks.TwoModesNetworks;
1819
using Symu.Repository.Entities;
1920
using static Symu.Common.Constants;
2021

@@ -44,6 +45,10 @@ public ActorBelief(IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel)
4445
{
4546
BeliefLevel = beliefLevel;
4647
}
48+
public ActorBelief(ActorBeliefNetwork network, IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel) : base(network, actorId, beliefId)
49+
{
50+
BeliefLevel = beliefLevel;
51+
}
4752

4853
/// <summary>
4954
/// The value used to feed the matrix network

SourceCode/Symu/Repository/Edges/ActorKnowledge.cs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Symu.Common.Interfaces;
1616
using Symu.Common.Math.ProbabilityDistributions;
1717
using Symu.OrgMod.Edges;
18+
using Symu.OrgMod.GraphNetworks.TwoModesNetworks;
1819
using Symu.Repository.Entities;
1920

2021
#endregion
@@ -26,21 +27,22 @@ namespace Symu.Repository.Edges
2627
/// KnowledgeId, KnowledgeLevel, KnowledgeBits
2728
/// </summary>
2829
/// <example>Dev Java, test, project management, sociology, ...</example>
29-
public class ActorKnowledge : EntityKnowledge //IActorKnowledge
30+
public class ActorKnowledge : EntityKnowledge
3031
{
3132
/// <summary>
3233
/// Constructor used by WorkerCognitiveAgent for ForgettingKnowledge
3334
/// </summary>
35+
/// <param name="network"></param>
3436
/// <param name="actorId"></param>
3537
/// <param name="knowledgeId"></param>
3638
/// <param name="knowledgeBits"></param>
37-
public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, KnowledgeBits knowledgeBits) : base(actorId,
39+
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, KnowledgeBits knowledgeBits) : base(network, actorId,
3840
knowledgeId)
3941
{
4042
KnowledgeBits = knowledgeBits;
4143
Length = KnowledgeBits?.Length ?? 0;
42-
}
43-
44+
}
45+
4446
/// <summary>
4547
/// Constructor used by Agent.Cognitive for ForgettingKnowledge
4648
/// </summary>
@@ -61,6 +63,27 @@ public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, float[] knowledgeB
6163
Length = KnowledgeBits.Length;
6264
}
6365

66+
/// <summary>
67+
/// Constructor used by Agent.Cognitive for ForgettingKnowledge
68+
/// </summary>
69+
/// <param name="network"></param>
70+
/// <param name="actorId"></param>
71+
/// <param name="knowledgeId"></param>
72+
/// <param name="knowledgeBits"></param>
73+
/// <param name="minimumKnowledge"></param>
74+
/// <param name="timeToLive"></param>
75+
/// <param name="step"></param>
76+
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, float[] knowledgeBits, float minimumKnowledge,
77+
short timeToLive,
78+
ushort step = 0) : base(network, actorId, knowledgeId)
79+
{
80+
MinimumKnowledge = minimumKnowledge;
81+
TimeToLive = timeToLive;
82+
KnowledgeBits = new KnowledgeBits(minimumKnowledge, timeToLive);
83+
KnowledgeBits.SetBits(knowledgeBits, step);
84+
Length = KnowledgeBits.Length;
85+
}
86+
6487
/// <summary>
6588
/// Constructor based on the knowledge Id and the knowledge Level.
6689
/// KnowledgeBits is not yet initialized.
@@ -81,6 +104,27 @@ public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, KnowledgeLevel lev
81104
Length = KnowledgeBits.Length;
82105
}
83106

107+
/// <summary>
108+
/// Constructor based on the knowledge Id and the knowledge Level.
109+
/// KnowledgeBits is not yet initialized.
110+
/// NetworkKnowledges.InitializeAgentKnowledge must be called to initialized KnowledgeBits
111+
/// </summary>
112+
/// <param name="network"></param>
113+
/// <param name="actorId"></param>
114+
/// <param name="knowledgeId"></param>
115+
/// <param name="level"></param>
116+
/// <param name="minimumKnowledge"></param>
117+
/// <param name="timeToLive"></param>
118+
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, KnowledgeLevel level, float minimumKnowledge,
119+
short timeToLive) : base(network, actorId, knowledgeId)
120+
{
121+
KnowledgeLevel = level;
122+
MinimumKnowledge = minimumKnowledge;
123+
TimeToLive = timeToLive;
124+
KnowledgeBits = new KnowledgeBits(minimumKnowledge, timeToLive);
125+
Length = KnowledgeBits.Length;
126+
}
127+
84128
/// <summary>
85129
/// The value used to feed the matrix network
86130
/// For a binary matrix network, the value is 1

SourceCode/Symu/Repository/Entities/Database.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,8 @@ public void InitializeKnowledge(IAgentId knowledgeId, ushort step)
143143
}
144144

145145
var knowledge = MetaNetwork.Knowledge.GetEntity<Knowledge>(knowledgeId);
146-
var resourceKnowledge =
147-
new ActorKnowledge(EntityId, knowledgeId, KnowledgeLevel.NoKnowledge, 0, TimeToLive);
146+
var resourceKnowledge = new ActorKnowledge(MetaNetwork.ResourceKnowledge, EntityId, knowledgeId, KnowledgeLevel.NoKnowledge, 0, TimeToLive);
148147
resourceKnowledge.InitializeWith0(knowledge.Length, step);
149-
Add(resourceKnowledge);
150148
}
151149

152150
/// <summary>

SourceCode/SymuTests/Classes/Agents/CognitiveAgentTests.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace SymuTests.Classes.Agents
3737
[TestClass]
3838
public class CognitiveAgentTests : BaseTestClass
3939
{
40-
private IActorActor _actorActor;
4140
private TestCognitiveAgent _agent;
4241
private TestCognitiveAgent _agent2;
4342
private Belief _belief;
@@ -76,7 +75,6 @@ public void Initialize()
7675
_agent.Start();
7776

7877
_agent2 = TestCognitiveAgent.CreateInstance(Environment);
79-
_actorActor = new ActorActor(_agent.AgentId, _agent2.AgentId);
8078
_agent2.Start();
8179
}
8280

@@ -122,8 +120,7 @@ public void HasEmailTest()
122120
Assert.IsFalse(_agent.HasEmail);
123121
var email = EmailEntity.CreateInstance(Environment.MainOrganization.MetaNetwork, MainOrganization.Models);
124122
var usage = new ResourceUsage(0);
125-
var actorResource = new ActorResource(_agent.AgentId, email.EntityId, usage);
126-
Environment.MainOrganization.MetaNetwork.ActorResource.Add(actorResource);
123+
_ = new ActorResource(Environment.MainOrganization.MetaNetwork.ActorResource, _agent.AgentId, email.EntityId, usage);
127124
_agent.EmailId = email.EntityId;
128125
Assert.IsTrue(_agent.HasEmail);
129126
Assert.IsNotNull(_agent.Email);
@@ -272,9 +269,8 @@ public void LearnBeliefsFromPostMessageTest()
272269
private void SetExpertise(KnowledgeBits bit0S)
273270
{
274271
_agent.Cognitive.KnowledgeAndBeliefs.HasKnowledge = true;
275-
var knowledge = new Knowledge(MainOrganization.MetaNetwork, MainOrganization.Models, "1", 1);
276-
var actorKnowledge = new ActorKnowledge(_agent.AgentId, knowledge.EntityId, bit0S);
277-
Environment.MainOrganization.MetaNetwork.ActorKnowledge.Add(actorKnowledge);
272+
var knowledge = new Knowledge(Environment.MainOrganization.MetaNetwork, MainOrganization.Models, "1", 1);
273+
_ = new ActorKnowledge(Environment.MainOrganization.MetaNetwork.ActorKnowledge, _agent.AgentId, knowledge.EntityId, bit0S);
278274
}
279275

280276
#endregion
@@ -836,7 +832,7 @@ public void AcceptNewInteractionTest()
836832
public void AcceptNewInteractionTest1()
837833
{
838834
_agent.Cognitive.InteractionPatterns.IsPartOfInteractionSphere = true;
839-
Environment.MainOrganization.MetaNetwork.ActorActor.Add(_actorActor);
835+
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
840836
Assert.IsTrue(_agent.AcceptNewInteraction(_agent2.AgentId));
841837
}
842838

@@ -962,7 +958,7 @@ public void GetAgentIdsForInteractionsTest1()
962958
[TestMethod]
963959
public void GetAgentIdsForInteractionsTest2()
964960
{
965-
Environment.MainOrganization.MetaNetwork.ActorActor.Add(_actorActor);
961+
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
966962
Environment.MainOrganization.MetaNetwork.InteractionSphere.SetSphere(true,
967963
Environment.WhitePages.AllAgentIds().ToList(), Environment.MainOrganization.MetaNetwork);
968964
Assert.IsTrue(_agent.GetAgentIdsForInteractions(InteractionStrategy.Homophily).Any());
@@ -1362,12 +1358,9 @@ public void TryRecoverBlockerIncompleteKnowledgeTest()
13621358
var teammate = AddAgent();
13631359
var group = TestCognitiveAgent.CreateInstance(2, Environment);
13641360
group.Start();
1365-
var actorOrganization = new ActorOrganization(_agent.AgentId, group.AgentId);
1366-
var teammateOrganization = new ActorOrganization(teammate.AgentId, group.AgentId);
1367-
Environment.MainOrganization.MetaNetwork.ActorOrganization.Add(actorOrganization);
1368-
Environment.MainOrganization.MetaNetwork.ActorOrganization.Add(teammateOrganization);
1369-
var interaction = new ActorActor(_agent.AgentId, teammate.AgentId);
1370-
Environment.MainOrganization.MetaNetwork.ActorActor.Add(interaction);
1361+
_ = new ActorOrganization(Environment.MainOrganization.MetaNetwork.ActorOrganization, _agent.AgentId, group.AgentId);
1362+
_ = new ActorOrganization(Environment.MainOrganization.MetaNetwork.ActorOrganization, teammate.AgentId, group.AgentId);
1363+
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, teammate.AgentId);
13711364
teammate.KnowledgeModel.AddKnowledge(_knowledge.EntityId, KnowledgeLevel.FullKnowledge, 0, -1);
13721365
teammate.KnowledgeModel.InitializeExpertise(0);
13731366
_agent.KnowledgeModel.SetKnowledge(_knowledge.EntityId, 0, 1, 0);

0 commit comments

Comments
 (0)