-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
257 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#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 | ||
|
||
namespace Symu.Classes.Agents | ||
{ | ||
/// <summary> | ||
/// The Agent interface | ||
/// </summary> | ||
public interface IAgent | ||
{ | ||
//todo => c#8 IAgentId AgentId; | ||
/// <summary> | ||
/// Clone an agent | ||
/// </summary> | ||
/// <returns></returns> | ||
IAgent Clone(); | ||
/// <summary> | ||
/// Set an agent's property value by its name | ||
/// </summary> | ||
/// <param name="propertyName"></param> | ||
/// <param name="value"></param> | ||
void SetProperty(string propertyName, float value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#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; | ||
using Symu.SysDyn.Model; | ||
|
||
namespace Symu.Environment | ||
{ | ||
/// <summary> | ||
/// 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 NodeAgent(string nodeId, IAgentId agentId, string property) | ||
{ | ||
NodeId = nodeId; | ||
AgentId = agentId; | ||
Property = property; | ||
} | ||
public string NodeId { get; } | ||
public IAgentId AgentId { get; } | ||
public string Property { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#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 System.Collections.Generic; | ||
using System.Xml.XPath; | ||
using NCalc2.Grammar; | ||
using Symu.Classes.Agents; | ||
using Symu.Common.Classes; | ||
using Symu.Common.Interfaces; | ||
using Symu.SysDyn; | ||
using Symu.SysDyn.Model; | ||
|
||
#endregion | ||
|
||
namespace Symu.Environment | ||
{ | ||
/// <summary> | ||
/// SysDynModel encapsulate Symu.SysDyn and make the link between Symu.SysDyn.Nodes and Symu.Agents and properties | ||
/// </summary> | ||
public class SysDynModel | ||
{ | ||
private readonly StateMachine _stateMachine; | ||
private readonly List<NodeAgent> _nodeAgentList = new List<NodeAgent>(); | ||
|
||
public SysDynModel(string xmlFile) | ||
{ | ||
_stateMachine = new StateMachine(xmlFile); | ||
} | ||
|
||
public void Process(List<ReactiveAgent> agents) | ||
{ | ||
if (agents == null) | ||
{ | ||
throw new ArgumentNullException(nameof(agents)); | ||
} | ||
|
||
_stateMachine.Process(); | ||
|
||
foreach (var nodeAgent in _nodeAgentList) | ||
{ | ||
var agent = agents.Find(x => x.AgentId.Equals(nodeAgent.AgentId)); | ||
agent.SetProperty(nodeAgent.Property, _stateMachine.GetVariable(nodeAgent.NodeId)); | ||
} | ||
} | ||
|
||
public void AddNodeAgent(string nodeId, IAgentId agentId, string property) | ||
{ | ||
_nodeAgentList.Add(new NodeAgent(nodeId, agentId, property)); | ||
} | ||
|
||
public float GetVariable(string nodeId) | ||
{ | ||
return _stateMachine.GetVariable(nodeId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Symu.Environment; | ||
using SymuTests.Helpers; | ||
|
||
namespace SymuTests.Environment | ||
{ | ||
[TestClass()] | ||
public class SysDynModelTests : BaseTestClass | ||
{ | ||
private const string XmlFile = "../../../Resources/sysdyn.xmile"; | ||
private const string NodeId = "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"); | ||
} | ||
|
||
[TestMethod()] | ||
public void SysDynModelTest() | ||
{ | ||
Assert.AreEqual(10, Environment.SysDynModel.GetVariable(NodeId)); | ||
} | ||
|
||
[TestMethod()] | ||
public void ProcessTest() | ||
{ | ||
Environment.SysDynModel.Process(Environment.WhitePages.AllAgents().ToList()); | ||
Assert.AreEqual(Environment.SysDynModel.GetVariable(NodeId), _agent.Property1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#region Licence | ||
|
||
// Description: SymuBiz - SymuTests | ||
// 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.Classes.Agents; | ||
using Symu.Common.Interfaces; | ||
using Symu.Environment; | ||
using Symu.Repository; | ||
|
||
#endregion | ||
|
||
namespace SymuTests.Helpers | ||
{ | ||
internal sealed class TestSysDynAgent : TestReactiveAgent | ||
{ | ||
public float Property1 { get; set; } | ||
public float Property2 { get; set; } | ||
public TestSysDynAgent(IAgentId id, SymuEnvironment environment) : base(id, environment){} | ||
|
||
public override void SetProperty(string propertyName, float value) | ||
{ | ||
switch (propertyName) | ||
{ | ||
case "Property1": | ||
Property1 = value; | ||
break; | ||
case "Property2": | ||
Property2 = value; | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<xmile version="1.0" xmlns="http://docs.oasis-open.org/xmile/ns/XMILE/v1.0"> | ||
<header> | ||
<name>SysDyn</name> | ||
<vendor>Symu</vendor> | ||
</header> | ||
<sim_specs method="Euler" time_units="Time"> | ||
<start>0</start> | ||
<stop>12</stop> | ||
<dt>0.25</dt> | ||
</sim_specs> | ||
<model> | ||
<variables> | ||
<stock name="Employees"> | ||
<eqn>10</eqn> | ||
<inflow>Attrition</inflow> | ||
<outflow>Recruitment</outflow> | ||
<non_negative /> | ||
</stock> | ||
<flow name="Attrition"> | ||
<eqn>Employees*0.1</eqn> | ||
<non_negative /> | ||
</flow> | ||
<flow name="Recruitment"> | ||
<eqn>Employees*0.1</eqn> | ||
<non_negative /> | ||
</flow> | ||
</variables> | ||
</model> | ||
</xmile> |