Skip to content

Commit

Permalink
Update SysDynModel
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorisse committed Oct 13, 2020
1 parent 51b6e97 commit f956e91
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
15 changes: 15 additions & 0 deletions SourceCode/Symu/Classes/Agents/ReactiveAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,23 @@ public void Unsubscribe(IAgentId agentId, byte subject)

#endregion

/// <summary>
/// SetProperty is used to set a property value by its name
/// It useful for Symu.SysDyn
/// </summary>
/// <param name="propertyName"></param>
/// <param name="value"></param>
public virtual void SetProperty(string propertyName, float value)
{
}
/// <summary>
/// GetProperty is used to get a property value by its name
/// It useful for Symu.SysDyn
/// </summary>
/// <param name="propertyName"></param>
public virtual float GetProperty(string propertyName)
{
return 0;
}
}
}
3 changes: 3 additions & 0 deletions SourceCode/Symu/Environment/SymuEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ private void SetInteractionSphere(bool initialization)
public void PreStep()
{
var agents = WhitePages.AllAgents().ToList();
// First update variables with the agents' properties' values
SysDynModel.UpdateVariables(agents);
// Then Process
SysDynModel.Process(agents);
agents.ForEach(a => a.PreStep());
}
Expand Down
30 changes: 22 additions & 8 deletions SourceCode/Symu/Environment/SysDynModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Symu.Common.Interfaces;
using Symu.SysDyn;
using Symu.SysDyn.Model;
using Symu.SysDyn.Simulation;

#endregion

Expand All @@ -29,7 +30,7 @@ namespace Symu.Environment
public class SysDynModel
{
private readonly StateMachine _stateMachine;
private readonly List<NodeAgent> _nodeAgentList = new List<NodeAgent>();
private readonly List<SysDynVariableAgent> _variableAgent = new List<SysDynVariableAgent>();

public SysDynModel(string xmlFile)
{
Expand All @@ -45,21 +46,34 @@ public void Process(List<ReactiveAgent> agents)

_stateMachine.Process();

foreach (var nodeAgent in _nodeAgentList)
foreach (var variableAgent in _variableAgent)
{
var agent = agents.Find(x => x.AgentId.Equals(nodeAgent.AgentId));
agent.SetProperty(nodeAgent.Property, _stateMachine.GetVariable(nodeAgent.NodeId));
var agent = agents.Find(x => x.AgentId.Equals(variableAgent.AgentId));
agent.SetProperty(variableAgent.Property, _stateMachine.Variables.GetValue(variableAgent.VariableName));
}
}

public void AddNodeAgent(string nodeId, IAgentId agentId, string property)
public void UpdateVariables(List<ReactiveAgent> agents)
{
_nodeAgentList.Add(new NodeAgent(nodeId, agentId, property));
if (agents == null)
{
throw new ArgumentNullException(nameof(agents));
}
foreach (var variableAgent in _variableAgent)
{
var agent = agents.Find(x => x.AgentId.Equals(variableAgent.AgentId));
_stateMachine.Variables.SetValue(variableAgent.VariableName, agent.GetProperty(variableAgent.Property));
}
}

public void AddVariableAgent(string variableName, IAgentId agentId, string property)
{
_variableAgent.Add(new SysDynVariableAgent(variableName, agentId, property));
}

public float GetVariable(string nodeId)
public float GetVariable(string variableName)
{
return _stateMachine.GetVariable(nodeId);
return _stateMachine.Variables.GetValue(variableName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ namespace Symu.Environment
/// NodeAgent is used with SysDynModel
/// It make the link between a Symu.SysDyn.Node and a Symu.Agent and its property used in SysDyn
/// </summary>
public readonly struct NodeAgent
public readonly struct SysDynVariableAgent
{
public NodeAgent(string nodeId, IAgentId agentId, string property)
public SysDynVariableAgent(string variableName, IAgentId agentId, string property)
{
NodeId = nodeId;
VariableName = variableName;
AgentId = agentId;
Property = property;
}
public string NodeId { get; }
public string VariableName { get; }
public IAgentId AgentId { get; }
public string Property { get; }
}
Expand Down
16 changes: 12 additions & 4 deletions SourceCode/SymuTests/Environment/SysDynModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,36 @@ namespace SymuTests.Environment
public class SysDynModelTests : BaseTestClass
{
private const string XmlFile = "../../../Resources/sysdyn.xmile";
private const string NodeId = "Employees";
private const string VariableName = "Employees";
private TestSysDynAgent _agent;

[TestInitialize]
public void Initialize()
{
_agent = new TestSysDynAgent(Environment.WhitePages.NextAgentId(TestReactiveAgent.ClassId), Environment);
Environment.SysDynModel = new SysDynModel(XmlFile);
Environment.SysDynModel.AddNodeAgent(NodeId, _agent.AgentId, "Property1");
Environment.SysDynModel.AddVariableAgent(VariableName, _agent.AgentId, "Property1");
}

[TestMethod()]
public void SysDynModelTest()
{
Assert.AreEqual(10, Environment.SysDynModel.GetVariable(NodeId));
Assert.AreEqual(10, Environment.SysDynModel.GetVariable(VariableName));
}

[TestMethod()]
public void ProcessTest()
{
Environment.SysDynModel.Process(Environment.WhitePages.AllAgents().ToList());
Assert.AreEqual(Environment.SysDynModel.GetVariable(NodeId), _agent.Property1);
Assert.AreEqual(Environment.SysDynModel.GetVariable(VariableName), _agent.Property1);
}

[TestMethod()]
public void UpdateVariablesTest()
{
_agent.Property1 = 2;
Environment.SysDynModel.UpdateVariables(Environment.WhitePages.AllAgents().ToList());
Assert.AreEqual(Environment.SysDynModel.GetVariable(VariableName), _agent.Property1);
}
}
}
9 changes: 9 additions & 0 deletions SourceCode/SymuTests/Helpers/TestSysDynAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,14 @@ public override void SetProperty(string propertyName, float value)
break;
}
}
public override float GetProperty(string propertyName)
{
return propertyName switch
{
"Property1" => Property1,
"Property2" => Property2,
_ => throw new ArgumentOutOfRangeException(nameof(propertyName))
};
}
}
}

0 comments on commit f956e91

Please sign in to comment.