Skip to content

Commit

Permalink
Add factory constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Oct 8, 2020
1 parent dc71ecf commit 015cc51
Show file tree
Hide file tree
Showing 35 changed files with 117 additions and 132 deletions.
2 changes: 1 addition & 1 deletion SourceCode/Symu/Classes/Agents/CognitiveAgent.Messaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public bool AcceptNewInteraction(IAgentId senderId)
}

// Message.Sender is now part of agent interaction sphere
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, AgentId, senderId);
ActorActor.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorActor, AgentId, senderId);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public IDictionary<ITask, IEnumerable<IKnowledge>> Knowledge
/// <param name="taskId"></param>
public void AddActorTask(IAgentId taskId)
{
_ = new ActorTask(_actorTaskNetwork, _agentId, taskId);
ActorTask.CreateInstance(_actorTaskNetwork, _agentId, taskId);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void AddBelief(IAgentId beliefId, BeliefLevel beliefLevel)
return;
}

_ = new ActorBelief(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
ActorBelief.CreateInstance(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
}


Expand All @@ -180,7 +180,7 @@ public void AddBeliefFromKnowledgeId(IAgentId knowledgeId, BeliefLevel beliefLev
throw new NullReferenceException(nameof(belief));
}

_ = new ActorBelief(_actorBeliefNetwork, _agentId, belief.EntityId, beliefLevel);
ActorBelief.CreateInstance(_actorBeliefNetwork, _agentId, belief.EntityId, beliefLevel);
}

public ActorBelief GetActorBelief(IAgentId beliefId)
Expand Down Expand Up @@ -423,7 +423,7 @@ public void LearnNewBelief(IAgentId beliefId, BeliefLevel beliefLevel)
return;
}

_ = new ActorBelief(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
ActorBelief.CreateInstance(_actorBeliefNetwork, _agentId, beliefId, beliefLevel);
InitializeBeliefs(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void AddKnowledge(IAgentId knowledgeId, KnowledgeLevel level, float minim
return;
}

_ = new ActorKnowledge(_actorKnowledgeNetwork, _agentId, knowledgeId, level, minimumKnowledge, timeToLive);
ActorKnowledge.CreateInstance(_actorKnowledgeNetwork, _agentId, knowledgeId, level, minimumKnowledge, timeToLive);
}

/// <summary>
Expand All @@ -209,8 +209,7 @@ public void AddKnowledge(IAgentId knowledgeId, float[] knowledgeBits, float mini
return;
}

var actorKnowledge = new ActorKnowledge(_agentId, knowledgeId, knowledgeBits, minimumKnowledge, timeToLive);
_actorKnowledgeNetwork.Add(actorKnowledge);
ActorKnowledge.CreateInstance(_actorKnowledgeNetwork, _agentId, knowledgeId, knowledgeBits, minimumKnowledge, timeToLive);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void AddResourceTasks(IEnumerable<ITask> tasks)

foreach (var task in tasks)
{
_ = new ResourceTask(_resourceTaskNetwork, _resourceId, task.EntityId);
ResourceTask.CreateInstance(_resourceTaskNetwork, _resourceId, task.EntityId);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions SourceCode/Symu/Classes/Organization/MainOrganization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ namespace Symu.Classes.Organization
/// You must define your own organization derived classes.
/// </summary>
//TODO should be an abstract class
public class MainOrganization //: Entity
public class MainOrganization
{
public const byte Class = ClassIdCollection.Organization;
public static IClassId ClassId => new ClassId(Class);

public MainOrganization(string name) //: base(new MetaNetwork(), Class, name)
public MainOrganization(string name)
{
Name = name;
MetaNetwork = new GraphMetaNetwork(Models.InteractionSphere);
}

public static IClassId ClassId => new ClassId(Class);

public string Name { get; set; }

Expand Down
13 changes: 13 additions & 0 deletions SourceCode/Symu/Repository/Edges/ActorBelief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ public class ActorBelief : OrgMod.Edges.ActorBelief
/// </summary>
private const int RangeMax = 1;


public ActorBelief(IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel) : base(actorId, beliefId)
{
BeliefLevel = beliefLevel;
}
/// <summary>
/// Factory
/// </summary>
/// <param name="network"></param>
/// <param name="actorId"></param>
/// <param name="beliefId"></param>
/// <param name="beliefLevel"></param>
/// <returns></returns>
public static ActorBelief CreateInstance(ActorBeliefNetwork network, IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel)
{
return new ActorBelief(network, actorId, beliefId, beliefLevel);
}
public ActorBelief(ActorBeliefNetwork network, IAgentId actorId, IAgentId beliefId, BeliefLevel beliefLevel) : base(network, actorId, beliefId)
{
BeliefLevel = beliefLevel;
Expand Down
23 changes: 16 additions & 7 deletions SourceCode/Symu/Repository/Edges/ActorKnowledge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ namespace Symu.Repository.Edges
/// <example>Dev Java, test, project management, sociology, ...</example>
public class ActorKnowledge : EntityKnowledge
{
public static ActorKnowledge CreateInstance(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, KnowledgeBits knowledgeBits)
{
return new ActorKnowledge(network, actorId, knowledgeId, knowledgeBits);
}
/// <summary>
/// Constructor used by WorkerCognitiveAgent for ForgettingKnowledge
/// </summary>
Expand All @@ -41,8 +45,7 @@ public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorI
{
KnowledgeBits = knowledgeBits;
Length = KnowledgeBits?.Length ?? 0;
}

}
/// <summary>
/// Constructor used by Agent.Cognitive for ForgettingKnowledge
/// </summary>
Expand All @@ -62,7 +65,11 @@ public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, float[] knowledgeB
KnowledgeBits.SetBits(knowledgeBits, step);
Length = KnowledgeBits.Length;
}

public static ActorKnowledge CreateInstance(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, float[] knowledgeBits, float minimumKnowledge,
short timeToLive, ushort step = 0)
{
return new ActorKnowledge(network, actorId, knowledgeId, knowledgeBits, minimumKnowledge, timeToLive, step);
}
/// <summary>
/// Constructor used by Agent.Cognitive for ForgettingKnowledge
/// </summary>
Expand All @@ -74,16 +81,14 @@ public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, float[] knowledgeB
/// <param name="timeToLive"></param>
/// <param name="step"></param>
public ActorKnowledge(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, float[] knowledgeBits, float minimumKnowledge,
short timeToLive,
ushort step = 0) : base(network, actorId, knowledgeId)
short timeToLive, ushort step = 0) : base(network, actorId, knowledgeId)
{
MinimumKnowledge = minimumKnowledge;
TimeToLive = timeToLive;
KnowledgeBits = new KnowledgeBits(minimumKnowledge, timeToLive);
KnowledgeBits.SetBits(knowledgeBits, step);
Length = KnowledgeBits.Length;
}

/// <summary>
/// Constructor based on the knowledge Id and the knowledge Level.
/// KnowledgeBits is not yet initialized.
Expand All @@ -103,7 +108,11 @@ public ActorKnowledge(IAgentId actorId, IAgentId knowledgeId, KnowledgeLevel lev
KnowledgeBits = new KnowledgeBits(minimumKnowledge, timeToLive);
Length = KnowledgeBits.Length;
}

public static ActorKnowledge CreateInstance(TwoModesNetwork<IEntityKnowledge> network, IAgentId actorId, IAgentId knowledgeId, KnowledgeLevel level, float minimumKnowledge,
short timeToLive)
{
return new ActorKnowledge(network, actorId, knowledgeId, level, minimumKnowledge, timeToLive);
}
/// <summary>
/// Constructor based on the knowledge Id and the knowledge Level.
/// KnowledgeBits is not yet initialized.
Expand Down
30 changes: 0 additions & 30 deletions SourceCode/Symu/Repository/Edges/ActorPortfolio.cs

This file was deleted.

19 changes: 19 additions & 0 deletions SourceCode/Symu/Repository/Entities/Belief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ public Belief()
{
}


public static Belief CreateInstance(GraphMetaNetwork metaNetwork, byte length, RandomGenerator model,
BeliefWeightLevel beliefWeightLevel)
{
return new Belief(metaNetwork, length, model, beliefWeightLevel);
}

public static Belief CreateInstance(GraphMetaNetwork metaNetwork, byte length, RandomGenerator model,
BeliefWeightLevel beliefWeightLevel, string name)
{
return new Belief(metaNetwork, length, model, beliefWeightLevel, name);
}

public static Belief CreateInstance(GraphMetaNetwork metaNetwork, IKnowledge knowledge, byte length, RandomGenerator model,
BeliefWeightLevel beliefWeightLevel)
{
return new Belief(metaNetwork, knowledge, length, model, beliefWeightLevel);
}

public Belief(GraphMetaNetwork metaNetwork, byte length, RandomGenerator model,
BeliefWeightLevel beliefWeightLevel) : base(metaNetwork)
{
Expand Down
4 changes: 4 additions & 0 deletions SourceCode/Symu/Repository/Entities/CyclicalEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class CyclicalEvent : EventEntity
public CyclicalEvent()
{
}
public static CyclicalEvent CreateInstance(GraphMetaNetwork metaNetwork)
{
return new CyclicalEvent(metaNetwork);
}

public CyclicalEvent(GraphMetaNetwork metaNetwork) : base(metaNetwork)
{
Expand Down
6 changes: 6 additions & 0 deletions SourceCode/Symu/Repository/Entities/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public Database()
CognitiveArchitecture = new CognitiveArchitecture();
}

public static Database CreateInstance(GraphMetaNetwork metaNetwork, MainOrganizationModels models, CommunicationTemplate medium,
byte classId)
{
return new Database(metaNetwork, models, medium, classId);
}

public Database(GraphMetaNetwork metaNetwork, MainOrganizationModels models, CommunicationTemplate medium,
byte classId) : base(metaNetwork, classId)
{
Expand Down
5 changes: 4 additions & 1 deletion SourceCode/Symu/Repository/Entities/EventEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public class EventEntity : OrgMod.Entities.EventEntity
public EventEntity()
{
}

public new static EventEntity CreateInstance(GraphMetaNetwork metaNetwork)
{
return new EventEntity(metaNetwork);
}
public EventEntity(GraphMetaNetwork metaNetwork) : base(metaNetwork)
{
}
Expand Down
4 changes: 4 additions & 0 deletions SourceCode/Symu/Repository/Entities/Knowledge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class Knowledge : KnowledgeEntity //IKnowledge
public Knowledge()
{
}
public static Knowledge CreateInstance(GraphMetaNetwork metaNetwork, MainOrganizationModels models, string name, byte length)
{
return new Knowledge(metaNetwork, models, name, length);
}

public Knowledge(GraphMetaNetwork metaNetwork, MainOrganizationModels models, string name, byte length) : base(
metaNetwork, name)
Expand Down
5 changes: 4 additions & 1 deletion SourceCode/Symu/Repository/Entities/RandomEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public class RandomEvent : EventEntity
public RandomEvent()
{
}

public new static RandomEvent CreateInstance(GraphMetaNetwork metaNetwork)
{
return new RandomEvent(metaNetwork);
}
public RandomEvent(GraphMetaNetwork metaNetwork) : base(metaNetwork)
{
}
Expand Down
16 changes: 8 additions & 8 deletions SourceCode/SymuTests/Classes/Agents/CognitiveAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void HasEmailTest()
Assert.IsFalse(_agent.HasEmail);
var email = EmailEntity.CreateInstance(Environment.MainOrganization.MetaNetwork, MainOrganization.Models);
var usage = new ResourceUsage(0);
_ = new ActorResource(Environment.MainOrganization.MetaNetwork.ActorResource, _agent.AgentId, email.EntityId, usage);
ActorResource.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorResource, _agent.AgentId, email.EntityId, usage);
_agent.EmailId = email.EntityId;
Assert.IsTrue(_agent.HasEmail);
Assert.IsNotNull(_agent.Email);
Expand Down Expand Up @@ -270,7 +270,7 @@ private void SetExpertise(KnowledgeBits bit0S)
{
_agent.Cognitive.KnowledgeAndBeliefs.HasKnowledge = true;
var knowledge = new Knowledge(Environment.MainOrganization.MetaNetwork, MainOrganization.Models, "1", 1);
_ = new ActorKnowledge(Environment.MainOrganization.MetaNetwork.ActorKnowledge, _agent.AgentId, knowledge.EntityId, bit0S);
ActorKnowledge.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorKnowledge, _agent.AgentId, knowledge.EntityId, bit0S);
}

#endregion
Expand Down Expand Up @@ -832,7 +832,7 @@ public void AcceptNewInteractionTest()
public void AcceptNewInteractionTest1()
{
_agent.Cognitive.InteractionPatterns.IsPartOfInteractionSphere = true;
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
ActorActor.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
Assert.IsTrue(_agent.AcceptNewInteraction(_agent2.AgentId));
}

Expand Down Expand Up @@ -958,7 +958,7 @@ public void GetAgentIdsForInteractionsTest1()
[TestMethod]
public void GetAgentIdsForInteractionsTest2()
{
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
ActorActor.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, _agent2.AgentId);
Environment.MainOrganization.MetaNetwork.InteractionSphere.SetSphere(true,
Environment.WhitePages.AllAgentIds().ToList(), Environment.MainOrganization.MetaNetwork);
Assert.IsTrue(_agent.GetAgentIdsForInteractions(InteractionStrategy.Homophily).Any());
Expand Down Expand Up @@ -1358,9 +1358,9 @@ public void TryRecoverBlockerIncompleteKnowledgeTest()
var teammate = AddAgent();
var group = TestCognitiveAgent.CreateInstance(2, Environment);
group.Start();
_ = new ActorOrganization(Environment.MainOrganization.MetaNetwork.ActorOrganization, _agent.AgentId, group.AgentId);
_ = new ActorOrganization(Environment.MainOrganization.MetaNetwork.ActorOrganization, teammate.AgentId, group.AgentId);
_ = new ActorActor(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, teammate.AgentId);
ActorOrganization.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorOrganization, _agent.AgentId, group.AgentId);
ActorOrganization.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorOrganization, teammate.AgentId, group.AgentId);
ActorActor.CreateInstance(Environment.MainOrganization.MetaNetwork.ActorActor, _agent.AgentId, teammate.AgentId);
teammate.KnowledgeModel.AddKnowledge(_knowledge.EntityId, KnowledgeLevel.FullKnowledge, 0, -1);
teammate.KnowledgeModel.InitializeExpertise(0);
_agent.KnowledgeModel.SetKnowledge(_knowledge.EntityId, 0, 1, 0);
Expand Down Expand Up @@ -1581,7 +1581,7 @@ public void TryRecoverBlockerIncompleteBeliefTest1()
{
var knowledge2 = new Knowledge(Environment.MainOrganization.MetaNetwork,
Environment.MainOrganization.Models, "2", 1);
_ = new Belief(Environment.MainOrganization.MetaNetwork, knowledge2, knowledge2.Length,
Belief.CreateInstance(Environment.MainOrganization.MetaNetwork, knowledge2, knowledge2.Length,
MainOrganization.Models.Generator, MainOrganization.Models.BeliefWeightLevel);

MainOrganization.Murphies.IncompleteBelief.On = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void AddBeliefsTest2()
{
_cognitiveArchitecture.KnowledgeAndBeliefs.HasBelief = true;
var actorKnowledge = new ActorKnowledge(_agentId, _knowledge.EntityId, new float[] {0}, 0, -1);
_ = new Belief(Network, _knowledge, _knowledge.Length, RandomGenerator.RandomUniform,
Belief.CreateInstance(Network, _knowledge, _knowledge.Length, RandomGenerator.RandomUniform,
BeliefWeightLevel.RandomWeight);
var expertise = new List<IEntityKnowledge> {actorKnowledge};
_beliefsModel.AddBeliefsFromKnowledge(expertise);
Expand Down
Loading

0 comments on commit 015cc51

Please sign in to comment.