-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysDynModel.cs
119 lines (105 loc) · 3.73 KB
/
SysDynModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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.Engine;
using Symu.SysDyn;
using Symu.SysDyn.Model;
using Symu.SysDyn.Simulation;
#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<SysDynVariableAgent> _variableAgent = new List<SysDynVariableAgent>();
public SysDynModel()
{
_stateMachine = new StateMachine {Optimized = true};
}
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 variableAgent in _variableAgent)
{
var agent = agents.Find(x => x.AgentId.Equals(variableAgent.AgentId));
agent.SetProperty(variableAgent.Property, _stateMachine.Variables.GetValue(variableAgent.VariableName));
}
}
public void UpdateVariables(List<ReactiveAgent> agents)
{
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));
}
/// <summary>
/// Get the value of a variable by its name
/// </summary>
/// <param name="variableName"></param>
/// <returns></returns>
public float GetVariable(string variableName)
{
return _stateMachine.Variables.GetValue(variableName);
}
/// <summary>
/// Set the simulation
/// </summary>
/// <param name="pauseInterval"></param>
/// <param name="fidelity"></param>
/// <param name="timeUnits"></param>
public void SetSimulation(Fidelity fidelity, ushort pauseInterval, TimeStepType timeUnits)
{
switch (fidelity)
{
case Fidelity.Low:
_stateMachine.Simulation.DeltaTime = 0.5F;
break;
case Fidelity.Medium:
_stateMachine.Simulation.DeltaTime = 0.25F;
break;
case Fidelity.High:
_stateMachine.Simulation.DeltaTime = 0.125F;
break;
default:
throw new ArgumentOutOfRangeException(nameof(fidelity), fidelity, null);
}
_stateMachine.Simulation.PauseInterval = pauseInterval;
_stateMachine.Simulation.TimeUnits = timeUnits;
}
public void Initialize()
{
_stateMachine.Initialize();
}
}
}