diff --git a/.gitignore b/.gitignore index 4ce6fdd..f03b9e4 100644 --- a/.gitignore +++ b/.gitignore @@ -337,4 +337,8 @@ ASALocalRun/ .localhistory/ # BeatPulse healthcheck temp database -healthchecksdb \ No newline at end of file +healthchecksdb + +CodeMap1.dgml + +SimpleStateMachine.png \ No newline at end of file diff --git a/CodeMap1.dgml b/CodeMap1.dgml deleted file mode 100644 index a17b13b..0000000 --- a/CodeMap1.dgml +++ /dev/null @@ -1,463 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Examples/Examples.csproj b/Examples/Examples.csproj index 07cd9f3..afd8aea 100644 --- a/Examples/Examples.csproj +++ b/Examples/Examples.csproj @@ -6,10 +6,10 @@ - - - - + + + + diff --git a/README.md b/README.md index ba74655..47d57e2 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,32 @@ -[![NuGet Pre Release](https://img.shields.io/nuget/vpre/SimpleStateMachineLibrary.svg)](https://www.nuget.org/packages/SimpleStateMachineLibrary) -[![](https://img.shields.io/github/stars/SimpleStateMachine/SimpleStateMachineLibrary)](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) -[![](https://img.shields.io/github/license/SimpleStateMachine/SimpleStateMachineLibrary)](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) -# SimpleStateMachineLibrary + +[![NuGet Pre Release](https://img.shields.io/nuget/vpre/SimpleStateMachineLibrary.svg)](https://www.nuget.org/packages/SimpleStateMachineLibrary) [![](https://img.shields.io/github/stars/SimpleStateMachine/SimpleStateMachineLibrary)](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) [![NuGet Downloads](https://img.shields.io/nuget/dt/SimpleStateMachineLibrary)](https://www.nuget.org/packages/SimpleStateMachineLibrary) [![](https://img.shields.io/github/license/SimpleStateMachine/SimpleStateMachineLibrary)](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) [![](https://img.shields.io/github/languages/code-size/SimpleStateMachine/SimpleStateMachineLibrary)](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) + [![]( https://img.shields.io/github/last-commit/SimpleStateMachine/SimpleStateMachineLibrary)](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) [![](https://img.shields.io/badge/chat-slack-blueviolet.svg)](https://join.slack.com/t/simplestatemachine/shared_invite/zt-fnfhvvsx-fTejcpPn~PPb2ojdG_MQBg) [![](https://img.shields.io/badge/chat-telegram-blue.svg)](https://t.me/joinchat/HMLJFkv9do6aDV188rhd0w) + [![Build Status](https://dev.azure.com/GMIKE/SimpleStateMachineLibrary/_apis/build/status/SimpleStateMachine.SimpleStateMachineLibrary?branchName=master)](https://dev.azure.com/GMIKE/SimpleStateMachineLibrary/_build/latest?definitionId=2&branchName=master) + # SimpleStateMachineLibrary A C# library for realization simple state-machine on .Net -# Why SimpleStateMachine? - Create state machine in **two steps** : -1. Create scheme in [node editor](https://github.com/SimpleStateMachine/SimpleStateMachineNodeEditor) ♦️ -2. Load scheme in your project using [library](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary)📑 - - Just describe your app logic and run the state machine🚀 - -## Give a Star! :star: + + ## Give a Star! :star: If you like or are using this project please give it a star. Thanks! -## Сontent -1. [Features](#Features) -2. [Examples](#Examples) -4. [Documentation](#Documentation) -4. [License](#License) -## Features + # Why SimpleStateMachine? +Create state machine in **three** steps : + +**1.** Create scheme in [node editor🔗](https://github.com/SimpleStateMachine/SimpleStateMachineNodeEditor) and load it in your project using [this library📚](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary) +```C# +StateMachine stateMachine = new StateMachine("scheme.xml"); +``` +**2.** Describe your app logic on events⚡ + ```C# +stateMachine.GetState("State1").OnExit(Action1); +stateMachine.GetState("State2").OnEntry(Action2); +stateMachine.GetTransition("Transition1").OnInvoke(Action3); +stateMachine.OnChangeState(Action4); +``` +**3.** Run the state machine🚘 + ```C# +stateMachine.Start(); +``` +## Features💡 State machine properties: * Start state @@ -34,93 +42,23 @@ Useful extensions for work: * Export/Import to/from XML * Logging - -## Examples: - -### Structure ### -```C# - StateMachine stateMachine = new StateMachine(); - - //Add states - State state1 = stateMachine.AddState("State1"); - State state2 = stateMachine.AddState("State2"); - State state3 = stateMachine.AddState("State3"); - State state4 = stateMachine.AddState("State4"); - - //Add transitions three ways: - - //Standart way - Transition transition1 = stateMachine.AddTransition("Transition1", state1, state2); - - //From state - Transition transition2 = state2.AddTransitionFromThis("Transition2", state3); - - //To state - Transition transition3 = state4.AddTransitionToThis("Transition3", state3); - - //Add action on entry or/and exit - state1.OnExit(Action1); - state2.OnEntry(Action2); - state3.OnExit(Action3); - state4.OnExit(Action4); - - //Set start state - state1.SetAsStartState(); - - //Start work - stateMachine.Start(); -``` -### Actions Syntax ### -##### Action on entry/exit ##### -```C# - void ActionOnEtnry(State state, Dictionary parameters) - { - //you need invoke transition in entry or exit action, differently work state machine will be end - state.StateMachine.InvokeTransition("Transition1"); - } - +## Getting Started📂 +Install from Nuget: +```sh + Install-Package SimpleStateMachineLibrary ``` -##### Action on change state ##### -```C# - void ActionOnChangeState(State stateFrom, State stateTo) - { +## Documentation📄 +Documentation here: https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki - } -``` -##### Action on transition invoke ##### -```C# - void ActionOnTransitionInvoke(Transition transition, Dictionary parameters) - { + ## FAQ❔ + If you think you have found a bug, create a github [issue](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/issues). + + But if you just have questions about how to use: + +- [Slack channel](https://join.slack.com/t/simplestatemachine/shared_invite/zt-fnfhvvsx-fTejcpPn~PPb2ojdG_MQBg) +- [Telegram channel](https://t.me/joinchat/HMLJFkv9do6aDV188rhd0w) - } -``` -## Documentation -* StateMachine - * [Create](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/StateMachine#Create) - * [Import](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/StateMachine#Import) - * [Export](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/StateMachine#Export) - * [Logging](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/StateMachine#Logging) -* State - * [Create](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/State#Create) - * [Get](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/State#Get) - * [Exists](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/State#Exists) - * [Delete](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/State#Delete) - * [Entry](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/State#Entry) - * [Exit](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/State#Exit) -* Transition - * [Create](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Transition#Create) - * [Get](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Transition#Get) - * [Exists](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Transition#Exists) - * [Delete](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Transition#Delete) - * [Invoke](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Transition#Invoke) - * [Parameters](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Transition#Parameters) -* Data - * [Create](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Data#Create) - * [Get](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Data#Get) - * [Exists](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Data#Exists) - * [Delete](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Data#Delete) - * [Change](https://github.com/SimpleStateMachine/SimpleStateMachineLibrary/wiki/Data#Change) -## License +## License📑 Copyright (c) SimpleStateMachine diff --git a/SimpleStateMachineLibrary/Data/Data.cs b/SimpleStateMachineLibrary/Data/Data.cs index b9f55a1..bbd8077 100644 --- a/SimpleStateMachineLibrary/Data/Data.cs +++ b/SimpleStateMachineLibrary/Data/Data.cs @@ -19,18 +19,18 @@ public object Value private Action _onChange; - internal Data(StateMachine stateMachine, string nameData, object valueData, Action actionOnChange) : base(stateMachine, nameData) + internal Data(StateMachine stateMachine, string nameData, object valueData, Action actionOnChange, bool withLog) : base(stateMachine, nameData) { Value = valueData; - stateMachine?._logger?.LogDebug("Create data \"{NameData}\" ", nameData); + //stateMachine?._logger.LogDebug("Create data \"{NameData}\" ", nameData); + + stateMachine._AddData(this, out _, true, withLog); if (actionOnChange != null) { OnChange(actionOnChange); - } - - stateMachine.AddData(this, out bool result, true); + } } public Data Delete() @@ -48,7 +48,7 @@ public Data OnChange(Action actionOnChange) actionOnChange = Check.Object(actionOnChange, this.StateMachine?._logger); _onChange += actionOnChange; - this.StateMachine._logger?.LogDebugAndInformation("Method \"{NameMethod}\" subscribe on change data \"{NameData}\"", actionOnChange.Method.Name, this.Name); + this.StateMachine._logger.LogDebug("Method \"{NameMethod}\" subscribe on change data \"{NameData}\"", actionOnChange.Method.Name, this.Name); return this; } } diff --git a/SimpleStateMachineLibrary/Data/DataWorkWithXML.cs b/SimpleStateMachineLibrary/Data/DataWorkWithXML.cs index 54d90b5..7423213 100644 --- a/SimpleStateMachineLibrary/Data/DataWorkWithXML.cs +++ b/SimpleStateMachineLibrary/Data/DataWorkWithXML.cs @@ -7,22 +7,25 @@ namespace SimpleStateMachineLibrary { public partial class Data { - internal static XElement ToXElement(Data data) + internal static XElement _ToXElement(Data data, bool withLog) { Check.NamedObject(data, data?.StateMachine?._logger); XElement element = new XElement("Data"); element.Add(new XAttribute("Name", data.Name)); element.Add(new XAttribute("Value", data.Value.ToString())); - data.StateMachine._logger?.LogDebug("Data \"{NameData}\" to XElement", data.Name); + + if(withLog) + data.StateMachine._logger.LogDebug("Data \"{NameData}\" to XElement", data.Name); + return element; } - internal XElement ToXElement() + internal XElement _ToXElement(bool withLog) { - return Data.ToXElement(this); + return Data._ToXElement(this, withLog); } - internal static Data FromXElement(StateMachine stateMachine, XElement data) + internal static Data _FromXElement(StateMachine stateMachine, XElement data, bool withLog) { stateMachine = Check.Object(stateMachine, stateMachine?._logger); data = Check.Object(data, stateMachine?._logger); @@ -30,8 +33,12 @@ internal static Data FromXElement(StateMachine stateMachine, XElement data) string Name = data.Attribute("Name")?.Value; string Value = data.Attribute("Value")?.Value; - stateMachine?._logger?.LogDebug("Initialization data \"{NameData}\" from XElement", Name); - return stateMachine.AddData(Name, Value); + Data dataObj = stateMachine._AddData(Name, Value, null, result: out bool result, exception:true, withLog: false); + + if((result)&&(withLog)) + stateMachine?._logger.LogDebug("Initialization data \"{NameData}\" from XElement", Name); + + return dataObj; } } diff --git a/SimpleStateMachineLibrary/SimpleStateMachineLibrary.csproj b/SimpleStateMachineLibrary/SimpleStateMachineLibrary.csproj index 0701970..927afb1 100644 --- a/SimpleStateMachineLibrary/SimpleStateMachineLibrary.csproj +++ b/SimpleStateMachineLibrary/SimpleStateMachineLibrary.csproj @@ -9,18 +9,18 @@ MIT statemachine state-machine finite-state-machine https://github.com/SimpleStateMachine/SimpleStateMachineLibrary - 2.1.0.0 + 2.1.1.0 Library for SimpleStateMachine SimpleStateMachine.png - 2.1.0.0 - 2.1.0.0 - Add methods for check on exists -Update package: Microsoft.Extensions.Logging.Abstractions -Some fix + 2.1.1.0 + 2.1.1.0 + add PreviousState and CurrentTransition +Some fix for logging +Microsoft.Extensions.Logging.Abstractions 3.1.5 - + diff --git a/SimpleStateMachineLibrary/StateMachines/StateMachine.cs b/SimpleStateMachineLibrary/StateMachines/StateMachine.cs index 00f4f22..e6a4ec6 100644 --- a/SimpleStateMachineLibrary/StateMachines/StateMachine.cs +++ b/SimpleStateMachineLibrary/StateMachines/StateMachine.cs @@ -19,10 +19,10 @@ public partial class StateMachine private Dictionary _data = new Dictionary(); - internal string _currentState; + public string CurrentStateName { get; private set; } - internal string _previousState; - internal string _currentTransition { get; private set; } + public string PreviousStateName{ get; private set; } + public string CurrentTransitionName { get; private set; } internal string _nextTransition; internal string _startState { get; private set; } @@ -33,10 +33,9 @@ public partial class StateMachine internal Action _onChangeState; - public State CurrentState { get { return GetState(_currentState); } } - public Transition CurrentTransition { get { return GetTransition(_currentTransition); } } - - + public State CurrentState { get { return _GetState(CurrentStateName, out _, true, false); } } + public Transition CurrentTransition { get { return _GetTransition(CurrentTransitionName, out _, true, false); } } + public State PreviousState { get { return _GetState(PreviousStateName, out _, true, false); } } @@ -58,46 +57,47 @@ public StateMachine(ILogger logger=null) public StateMachine(XDocument xDocument, ILogger logger = null) : this(logger) { - FromXDocument(this, xDocument); + _FromXDocument(this, xDocument, true); } public StateMachine(string xDocumentPath, ILogger logger = null): this(logger) { - FromXDocument(this, xDocumentPath); + _FromXDocument(this, xDocumentPath, true); } public StateMachine OnChangeState(Action actionOnChangeState) { _onChangeState += actionOnChangeState; - _logger?.LogDebug("Method \"{NameMethod}\" subscribe on change state State Machine", actionOnChangeState.Method.Name); + _logger.LogDebug("Method \"{NameMethod}\" subscribe on change state for State Machine", actionOnChangeState.Method.Name); return this; } public State SetStartState(State state) { - _startState = _StateExists(state.Name, out _, true); + _startState = _StateExists(state.Name, out _, true, false); - _logger?.LogDebug("State \"{NameState}\" set as start", state.Name); + _logger.LogDebug("State \"{NameState}\" set as start", state.Name); return state; } public State SetStartState(string stateName) { - State state = GetState(stateName); + State state = _GetState(stateName, out bool result, true, false); _startState = state.Name; - _logger?.LogDebug("State \"{NameState}\" set as start", stateName); + if(result) + _logger.LogDebug("State \"{NameState}\" set as start", stateName); return state; } public InvokeParameters InvokeTransition(string nameTransition, Dictionary parameters=null) { - _nextTransition = TransitionExists(nameTransition, out _); + _nextTransition = _TransitionExists(nameTransition, out _,true, false); - CheckBeforeInvoke(this._logger); + _CheckBeforeInvoke(this._logger, true); InvokeParameters invokeParameters = new InvokeParameters(this); if(parameters!=null) @@ -105,19 +105,21 @@ public InvokeParameters InvokeTransition(string nameTransition, Dictionary parameters = null) @@ -126,7 +128,7 @@ public InvokeParameters InvokeTransition(Transition transition, Dictionary obj = new List(); string message; - if (string.IsNullOrEmpty(_previousState)) + + if (string.IsNullOrEmpty(PreviousStateName)) { - message = "State \"{StateNew}\" was set"; + obj.Add(CurrentStateName); + message = "State \"{StateNew}\" was set"; } else { + obj.Add(PreviousStateName); + obj.Add(CurrentStateName); message = "State \"{StateOld}\" change on \"{StateNew}\""; - previousState = GetState(_previousState); - } + previousState = _GetState(PreviousStateName, out _, true, false); + } + _onChangeState?.Invoke(previousState, currentState); - _logger?.LogDebug(message, obj); - currentState.Exit(_currentParameters); + _logger.LogDebug(message, obj.ToArray()); + currentState._Exit(_currentParameters, true); return this; } - private void CheckStartState() + internal void _CheckStartState() { string message; if (string.IsNullOrEmpty(_startState)) { message = "Start state not set"; var exception = new NullReferenceException(message: message); - _logger?.LogError(exception, message); + _logger.LogError(exception, message); throw exception; } - _startState = _StateExists(_startState, out _, true); - _currentState = _startState; + _startState = _StateExists(_startState, out _, true, false); + CurrentStateName = _startState; } public void Start(Dictionary startParameters = null) { - CheckStartState(); + _CheckStartState(); - _logger?.LogDebugAndInformation("Start work state machine"); + _logger.LogInformation("Start work state machine"); _currentParameters = startParameters; - ChangeState(); + _ChangeState(); while (_nextTransition != null) { - InvokeTransition(); + _InvokeTransition(); - ChangeState(); + _ChangeState(); } - _logger?.LogDebugAndInformation("End work state machine"); + _logger.LogInformation("End work state machine"); } - + } } diff --git a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithData.cs b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithData.cs index a5144f0..a619578 100644 --- a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithData.cs +++ b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithData.cs @@ -8,58 +8,43 @@ namespace SimpleStateMachineLibrary public partial class StateMachine { - internal Data _GetData(string nameData, out bool result, bool exception) + internal Data _GetData(string nameData, out bool result, bool exception, bool withLog) { var data_ = Check.GetElement(_data, nameData, this._logger, out result, exception); - if (exception) - _logger?.LogDebug("Get data \"{NameData}\"", nameData); - else - _logger?.LogDebug("Try get data \"{NameData}\"", nameData); + if (withLog) + { + if (exception) + _logger.LogDebug("Get data \"{NameData}\"", nameData); + else + _logger.LogDebug("Try get data \"{NameData}\"", nameData); + } return data_; } - //private Data _GetData(Data data, out bool result, bool exception) - //{ - // var data_ = Check.GetElement(_data, data, this._logger, out result, exception); - - // if (exception) - // _logger?.LogDebug("Get data \"{NameData}\"", data.Name); - // else - // _logger?.LogDebug("Try get data \"{NameData}\"", data.Name); - - // return data_; - //} - internal string _DataExists(string nameData, out bool result, bool exeption) + internal string _DataExists(string nameData, out bool result, bool exeption, bool withLog) { return Check.Contains(_data, nameData, this._logger, out result, exeption); } public bool DataExists(string nameData) { - nameData = _DataExists(nameData, out bool result, false); + nameData = _DataExists(nameData, out bool result, false, true); return result; } public Data GetData(string nameData) { - return _GetData(nameData, out bool result, true); + return _GetData(nameData, out bool result, true, true); } public Data TryGetData(string nameData, out bool result) { - return _GetData(nameData, out result, false); + return _GetData(nameData, out result, false, true); } - //public Data TryGetData(Data data, out bool result) - //{ - // return _GetData(data, out result, false); - //} - - - - internal Data _AddData(string nameData, object valueData, Action actionOnChange, out bool result, bool exception) + internal Data _AddData(string nameData, object valueData, Action actionOnChange, out bool result, bool exception, bool withLog) { //throw that element already contains result = Check.NotContains(_data, nameData, this._logger, exception); @@ -67,10 +52,10 @@ internal Data _AddData(string nameData, object valueData, Action a if (!result) return null; - return new Data(this, nameData, valueData, actionOnChange); + return new Data(this, nameData, valueData, actionOnChange, withLog); } - internal Data AddData(Data data, out bool result, bool exception) + internal Data _AddData(Data data, out bool result, bool exception, bool withLog) { //throw that element already contains result = Check.NotContains(_data, data, this._logger, exception); @@ -79,53 +64,61 @@ internal Data AddData(Data data, out bool result, bool exception) return null; _data.Add(data.Name, data); - - if (exception) - _logger?.LogDebug("Add data \"{NameData}\"", data.Name); - else - _logger?.LogDebug("Try add data \"{NameData}\"", data.Name); + if (withLog) + { + if (exception) + _logger.LogDebug("Add data \"{NameData}\"", data.Name); + else + _logger.LogDebug("Try add data \"{NameData}\"", data.Name); + } return data; } - internal Data AddData(XElement xElement) + internal Data _AddData(XElement xElement, bool withLog) { - return SimpleStateMachineLibrary.Data.FromXElement(this, Check.Object(xElement, this._logger)); + return Data._FromXElement(this, Check.Object(xElement, this._logger), withLog); } public Data AddData(string nameData, object valueData = default(object), Action actionOnChange = null) { - return _AddData(nameData, valueData, actionOnChange, out bool result, true); + return _AddData(nameData, valueData, actionOnChange, out bool result, true, true); } public Data TryAddData(out bool result, string nameData, object valueData = default(object), Action actionOnChange = null) { - return _AddData(nameData, valueData, actionOnChange, out result, false); + return _AddData(nameData, valueData, actionOnChange, out result, false, true); } - private Data _DeleteData(Data data, out bool result, bool exception) + private Data _DeleteData(Data data, out bool result, bool exception, bool withLog) { var data_ = Check.Remove(_data, data, this._logger, out result, exception); - if (exception) - _logger?.LogDebug("Delete data \"{NameData}\"", data.Name); - else - _logger?.LogDebug("Try delete data \"{NameData}\"", data.Name); + if (withLog) + { + if (exception) + _logger.LogDebug("Delete data \"{NameData}\"", data.Name); + else + _logger.LogDebug("Try delete data \"{NameData}\"", data.Name); + } return data_; } - private Data _DeleteData(string dataName, out bool result, bool exception) + private Data _DeleteData(string dataName, out bool result, bool exception, bool withLog) { var data_ = Check.Remove(_data, dataName, this._logger, out result, exception); - if (exception) - _logger?.LogDebug("Delete data \"{NameData}\"", dataName); - else - _logger?.LogDebug("Try delete data \"{NameData}\"", dataName); + if (withLog) + { + if (exception) + _logger.LogDebug("Delete data \"{NameData}\"", dataName); + else + _logger.LogDebug("Try delete data \"{NameData}\"", dataName); + } return data_; @@ -134,22 +127,22 @@ private Data _DeleteData(string dataName, out bool result, bool exception) public Data DeleteData(string nameData) { - return _DeleteData(nameData, out bool result, true); + return _DeleteData(nameData, out bool result, true, true); } public Data DeleteData(Data data) { - return _DeleteData(data, out bool result, true); + return _DeleteData(data, out bool result, true, true); } public Data TryDeleteData(string nameData, out bool result) { - return _DeleteData(nameData, out result, false); + return _DeleteData(nameData, out result, false, true); } public Data TryDeleteData(Data data, out bool result) { - return _DeleteData(data, out result, false); + return _DeleteData(data, out result, false, true); } } } diff --git a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithStates.cs b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithStates.cs index e59ada5..68f32ef 100644 --- a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithStates.cs +++ b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithStates.cs @@ -4,64 +4,48 @@ using System.Collections.Generic; using System.Xml.Linq; - namespace SimpleStateMachineLibrary { public partial class StateMachine { - internal State _GetState(string nameState, out bool result, bool exception) - { - var _state = Check.GetElement(_states, nameState, this._logger, out result, exception); - - if (exception) - _logger?.LogDebug("Get state \"{NameState}\"", nameState); - else - _logger?.LogDebug("Try get state \"{NameState}\"", nameState); - - return _state; - } - - //private State _GetState(State state, out bool result, bool exception) - //{ - // var _state = Check.GetElement(_states, state, this._logger, out result, exception); - - // if (exception) - // _logger?.LogDebug("Get state \"{NameState}\"", state.Name); - // else - // _logger?.LogDebug("Try get state \"{NameState}\"", state.Name); - - // return _state; - //} - - internal string _StateExists(string nameState, out bool result, bool exeption) + internal string _StateExists(string nameState, out bool result, bool exeption, bool withLog) { return Check.Contains(_states, nameState, this._logger, out result, exeption); } + public bool StateExists(string nameState) { - nameState = _StateExists(nameState, out bool result, false); + nameState = _StateExists(nameState, out bool result, false, true); return result; } + internal State _GetState(string nameState, out bool result, bool exception, bool withLog) + { + var _state = Check.GetElement(_states, nameState, this._logger, out result, exception); + + if (withLog) + { + if (exception) + _logger.LogDebug("Get state \"{NameState}\"", nameState); + else + _logger.LogDebug("Try get state \"{NameState}\"", nameState); + } + + return _state; + } + public State GetState(string nameState) { - return _GetState(nameState, out bool result, true); + return _GetState(nameState, out bool result, true, true); } public State TryGetState(string nameState, out bool result) { - return _GetState(nameState, out result, false); + return _GetState(nameState, out result, false, true); } - //public State TryGetState(State state, out bool result) - //{ - // return _GetState(state, out result, false); - //} - - - - internal State _AddState(string nameState, Action> actionOnEntry, Action> actionOnExit, out bool result, bool exception) + internal State _AddState(string nameState, Action> actionOnEntry, Action> actionOnExit, out bool result, bool exception, bool withLog) { //throw that element already contains result = Check.NotContains(_states, nameState, this._logger, exception); @@ -70,10 +54,10 @@ internal State _AddState(string nameState, Action> actionOnEntry = null, Action> actionOnExit = null) { - return _AddState(nameState, actionOnEntry, actionOnExit, out bool result, true); + return _AddState(nameState, actionOnEntry, actionOnExit, out bool result, true, true); } public State TryAddState(out bool result, string nameState, Action> actionOnEntry = null, Action> actionOnExit = null) { - return _AddState(nameState, actionOnEntry, actionOnExit, out result, false); + return _AddState(nameState, actionOnEntry, actionOnExit, out result, false, true); } - private State _DeleteState(State state, out bool result, bool exception) + internal State _DeleteState(State state, out bool result, bool exception, bool withLog) { var _state = Check.Remove(_states, state, this._logger, out result, exception); - - if (exception) - _logger?.LogDebug("Delete state \"{NameState}\"", state.Name); - else - _logger?.LogDebug("Try delete state \"{NameState}\"", state.Name); - + if (withLog) + { + if (exception) + _logger.LogDebug("Delete state \"{NameState}\"", state.Name); + else + _logger.LogDebug("Try delete state \"{NameState}\"", state.Name); + } return _state; } - private State _DeleteState(string stateName, out bool result, bool exception) + internal State _DeleteState(string stateName, out bool result, bool exception, bool withLog) { var _state = Check.Remove(_states, stateName, this._logger, out result, exception); - if (exception) - _logger?.LogDebug("Delete state \"{NameState}\"", stateName); - else - _logger?.LogDebug("Try delete state \"{NameState}\"", stateName); + if (withLog) + { + if (exception) + _logger.LogDebug("Delete state \"{NameState}\"", stateName); + else + _logger.LogDebug("Try delete state \"{NameState}\"", stateName); + } return _state; } @@ -139,22 +130,22 @@ private State _DeleteState(string stateName, out bool result, bool exception) public State DeleteState(State state) { - return _DeleteState(state, out bool result, true); + return _DeleteState(state, out bool result, true, true); } public State DeleteState(string stateName) { - return _DeleteState(GetState(stateName), out bool result, true); + return _DeleteState(_GetState(stateName, out bool result, true, false), out result, true, true); } public State TryDeleteState(State state, out bool result) { - return _DeleteState(state, out result, false); + return _DeleteState(state, out result, false, true); } public State TryDeleteState(string stateName, out bool result) { - return _DeleteState(stateName, out result, false); + return _DeleteState(stateName, out result, false, true); } } } diff --git a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitions.cs b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitions.cs index 743df9b..ab4a081 100644 --- a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitions.cs +++ b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitions.cs @@ -9,65 +9,44 @@ namespace SimpleStateMachineLibrary { public partial class StateMachine { - //internal Transition _GetTransition(Transition transition, out bool result, bool exception) - //{ - // var _transition = Check.GetElement(_transitions, transition, this._logger, out result, exception); - - // if(exception) - // _logger?.LogDebug("Get transition \"{NameTransition}\"", transition.Name); - // else - // _logger?.LogDebug("Try get transition \"{NameTransition}\"", transition.Name); + internal string _TransitionExists(string nameTransition, out bool result, bool exeption, bool withLog) + { + return Check.Contains(_transitions, nameTransition, this._logger, out result, exeption); + } - // return _transition; - //} + public bool TransitionExists(string nameTransition) + { + nameTransition = _TransitionExists(nameTransition,out bool result, false, true); + return result; + } - internal Transition _GetTransition(string nameTransition, out bool result, bool exception) + internal Transition _GetTransition(string nameTransition, out bool result, bool exception, bool withLog) { var _transition = Check.GetElement(_transitions, nameTransition, this._logger, out result, exception); - if (exception) - _logger?.LogDebug("Get transition \"{NameTransition}\"", nameTransition); - else - _logger?.LogDebug("Try get transition \"{NameTransition}\"", nameTransition); + if (withLog) + { + if (exception) + _logger.LogDebug("Get transition \"{NameTransition}\"", nameTransition); + else + _logger.LogDebug("Try get transition \"{NameTransition}\"", nameTransition); + } return _transition; } - internal string _TransitionExists(string nameTransition, out bool result, bool exeption) - { - return Check.Contains(_transitions, nameTransition, this._logger, out result, exeption); - } - internal string TransitionExists(string nameTransition, out bool result) - { - return _TransitionExists(nameTransition, out result, true); - } - public bool TransitionExists(string nameTransition) - { - nameTransition = _TransitionExists(nameTransition,out bool result, false); - return result; - } - public Transition GetTransition(string nameTransition) { - return _GetTransition(nameTransition, out bool result, true); + return _GetTransition(nameTransition, out bool result, true, true); } public Transition TryGetTransition(string nameTransition, out bool result) { - return _GetTransition(nameTransition, out result, false); + return _GetTransition(nameTransition, out result, false, true); } - //public Transition TryGetTransition(Transition transition, out bool result) - //{ - // return _GetTransition(transition, out result, false); - //} - - - - - - internal Transition _AddTransition(string nameTransition, string stateFrom, string stateTo, Action> actionOnInvoke, out bool result, bool exception) + internal Transition _AddTransition(string nameTransition, string stateFrom, string stateTo, Action> actionOnInvoke, out bool result, bool exception, bool withLog) { //throw that element already contains result = Check.NotContains(_transitions, nameTransition, this._logger, exception); @@ -75,10 +54,10 @@ internal Transition _AddTransition(string nameTransition, string stateFrom, stri if (!result) return null; - return new Transition(this, nameTransition, stateFrom, stateTo, actionOnInvoke); + return new Transition(this, nameTransition, stateFrom, stateTo, actionOnInvoke, withLog); } - internal Transition AddTransition(Transition transition, out bool result, bool exception) + internal Transition _AddTransition(Transition transition, out bool result, bool exception, bool withLog) { //throw that element already contains result = Check.NotContains(_transitions, transition, this._logger, exception); @@ -87,84 +66,92 @@ internal Transition AddTransition(Transition transition, out bool result, bool e return null; _transitions.Add(transition.Name, transition); - - if (exception) - _logger?.LogDebug("Add transition \"{NameTransition}\"", transition.Name); - else - _logger?.LogDebug("Try add transition \"{NameTransition}\"", transition.Name); + if (withLog) + { + if (exception) + _logger.LogDebug("Add transition \"{NameTransition}\" from state \"{NameStateFrom}\" to state \"{NameStateTo}\"", transition.Name, transition.StateFrom, transition.StateTo); + else + _logger.LogDebug("Try add transition \"{NameTransition}\" from state \"{NameStateFrom}\" to state \"{NameStateTo}\"", transition.Name, transition.StateFrom, transition.StateTo); + } return transition; } - internal Transition AddTransition(XElement xElement) + internal Transition _AddTransition(XElement xElement, bool withLog) { - return SimpleStateMachineLibrary.Transition.FromXElement(this, Check.Object(xElement, this._logger)); + return Transition._FromXElement(this, Check.Object(xElement, this._logger), true); } public Transition AddTransition(string nameTransition, State stateFrom, State stateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, stateFrom?.Name, stateTo?.Name, actionOnInvoke, out bool result, true); + return _AddTransition(nameTransition, stateFrom?.Name, stateTo?.Name, actionOnInvoke, out bool result, true, true); } public Transition AddTransition(string nameTransition, State stateFrom, string nameStateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, stateFrom?.Name, nameStateTo, actionOnInvoke, out bool result, true); + return _AddTransition(nameTransition, stateFrom?.Name, nameStateTo, actionOnInvoke, out bool result, true, true); } public Transition AddTransition(string nameTransition, string nameStateFrom, State stateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, nameStateFrom, stateTo?.Name, actionOnInvoke, out bool result, true); + return _AddTransition(nameTransition, nameStateFrom, stateTo?.Name, actionOnInvoke, out bool result, true, true); } public Transition AddTransition(string nameTransition, string nameStateFrom, string nameStateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, nameStateFrom, nameStateTo, actionOnInvoke, out bool result, true); + return _AddTransition(nameTransition, nameStateFrom, nameStateTo, actionOnInvoke, out bool result, true, true); } public Transition TryAddTransition(out bool result, string nameTransition, State stateFrom, State stateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, stateFrom?.Name, stateTo?.Name, actionOnInvoke, out result, false); + return _AddTransition(nameTransition, stateFrom?.Name, stateTo?.Name, actionOnInvoke, out result, false, true); } public Transition TryAddTransition(out bool result, string nameTransition, State stateFrom, string nameStateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, stateFrom?.Name, nameStateTo, actionOnInvoke, out result, false); + return _AddTransition(nameTransition, stateFrom?.Name, nameStateTo, actionOnInvoke, out result, false, true); } public Transition TryAddTransition(out bool result, string nameTransition, string nameStateFrom, State stateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, nameStateFrom, stateTo?.Name, actionOnInvoke, out result, false); + return _AddTransition(nameTransition, nameStateFrom, stateTo?.Name, actionOnInvoke, out result, false, true); } public Transition TryAddTransition(out bool result, string nameTransition, string nameStateFrom, string nameStateTo, Action> actionOnInvoke = null) { - return _AddTransition(nameTransition, nameStateFrom, nameStateTo, actionOnInvoke, out result, false); + return _AddTransition(nameTransition, nameStateFrom, nameStateTo, actionOnInvoke, out result, false, true); } - internal Transition _DeleteTransition(Transition transition, out bool result, bool exception) + internal Transition _DeleteTransition(Transition transition, out bool result, bool exception, bool withLog) { var _transition = Check.Remove(_transitions, transition, this._logger,out result, exception); - if (exception) - _logger?.LogDebug("Delete transition \"{NameTransition}\"", transition.Name); - else - _logger?.LogDebug("Try delete transition \"{NameTransition}\"", transition.Name); + if (withLog) + { + if (exception) + _logger.LogDebug("Delete transition \"{NameTransition}\"", transition.Name); + else + _logger.LogDebug("Try delete transition \"{NameTransition}\"", transition.Name); + } return _transition; } - internal Transition _DeleteTransition(string transitionName, out bool result, bool exception) + internal Transition _DeleteTransition(string transitionName, out bool result, bool exception, bool withLog) { var _transition = Check.Remove(_transitions, transitionName, this._logger, out result, exception); - if (exception) - _logger?.LogDebug("Delete transition \"{NameTransition}\"", transitionName); - else - _logger?.LogDebug("Try delete transition \"{NameTransition}\"", transitionName); + if (withLog) + { + if (exception) + _logger.LogDebug("Delete transition \"{NameTransition}\"", transitionName); + else + _logger.LogDebug("Try delete transition \"{NameTransition}\"", transitionName); + } return _transition; } @@ -172,22 +159,22 @@ internal Transition _DeleteTransition(string transitionName, out bool result, bo public Transition DeleteTransition(Transition transition) { - return _DeleteTransition(transition, out bool result, true); + return _DeleteTransition(transition, out bool result, true, true); } public Transition DeleteTransition(string transitionName) { - return _DeleteTransition(transitionName, out bool result, true); + return _DeleteTransition(transitionName, out bool result, true, true); } public Transition TryDeleteTransition(Transition transition, out bool result) { - return _DeleteTransition(transition, out result, false); + return _DeleteTransition(transition, out result, false, true); } public Transition TryDeleteTransition(string transitionName, out bool result) { - return _DeleteTransition(transitionName, out result, false); + return _DeleteTransition(transitionName, out result, false, true); } } } diff --git a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitionsForState.cs b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitionsForState.cs index 8bd681e..b255e65 100644 --- a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitionsForState.cs +++ b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithTransitionsForState.cs @@ -8,83 +8,90 @@ namespace SimpleStateMachineLibrary { public partial class StateMachine { - - private Dictionary GetTransitionsFromState(string stateName, out bool result, bool exceptions) + internal Dictionary _GetTransitionsFromState(string stateName, out bool result, bool exceptions, bool withLog) { result = Check.Contains(_states, stateName, this._logger, exceptions); var transitionsFromState = result ? Check.GetValuesWhere(_transitions, (Transition x) => x.StateFrom == stateName, this._logger, out result, exceptions): new Dictionary(); + + if(withLog) + _logger.LogDebug("Get transitions from state \"{NameState}\" ", stateName); - _logger?.LogDebug("Get transitions from state \"{NameState}\" ", stateName); return transitionsFromState; } - private Dictionary GetTransitionsFromState(State state, out bool result, bool exceptions) + internal Dictionary _GetTransitionsFromState(State state, out bool result, bool exceptions, bool withLog) { result = Check.Contains(_states, state, this._logger, exceptions); var transitionsFromState = result ? Check.GetValuesWhere(_transitions, (Transition x) => x.StateFrom == state.Name, this._logger, out result, exceptions) : new Dictionary(); + + if(withLog) + _logger.LogDebug("Get transitions from state \"{NameState}\" ", state.Name); - _logger?.LogDebug("Get transitions from state \"{NameState}\" ", state.Name); return transitionsFromState; } public Dictionary GetTransitionsFromState(string stateName) { - return GetTransitionsFromState(stateName, out bool result, true); + return _GetTransitionsFromState(stateName, out bool result, true, true); } public Dictionary GetTransitionsFromState(State state) { - return GetTransitionsFromState(state, out bool result, true); + return _GetTransitionsFromState(state, out bool result, true, true); } public Dictionary TryGetTransitionsFromState(string stateName, out bool result) { - return GetTransitionsFromState(stateName, out result, false); + return _GetTransitionsFromState(stateName, out result, false, true); } public Dictionary TryGetTransitionsFromState(State state, out bool result) { - return GetTransitionsFromState(state, out result, false); + return _GetTransitionsFromState(state, out result, false, true); } - private Dictionary GetTransitionsToState(string stateName, out bool result, bool exceptions) + internal Dictionary _GetTransitionsToState(string stateName, out bool result, bool exceptions, bool withLog) { result = Check.Contains(_states, stateName, this._logger, exceptions); var transitionsToState = result ? Check.GetValuesWhere(_transitions, (Transition x) => x.StateTo == stateName, this._logger, out result, exceptions): new Dictionary(); - _logger?.LogDebug("Get transitions to state \"{NameState}\" ", stateName); + if(withLog) + _logger.LogDebug("Get transitions to state \"{NameState}\" ", stateName); + return transitionsToState; } - private Dictionary GetTransitionsToState(State state, out bool result, bool exceptions) + internal Dictionary _GetTransitionsToState(State state, out bool result, bool exceptions, bool withLog) { result = Check.Contains(_states, state, this._logger, exceptions); var transitionsToState = result ? Check.GetValuesWhere(_transitions, (Transition x) => x.StateTo == state.Name, this._logger, out result, exceptions) : new Dictionary(); - _logger?.LogDebug("Get transitions to state \"{NameState}\" ", state.Name); + if(withLog) + _logger.LogDebug("Get transitions to state \"{NameState}\" ", state.Name); + return transitionsToState; } public Dictionary GetTransitionsToState(string stateName) { - return GetTransitionsToState(stateName, out bool result, true); + return _GetTransitionsToState(stateName, out bool result, true, true); } public Dictionary GetTransitionsToState(State state) { - return GetTransitionsToState(state, out bool result, true); + return _GetTransitionsToState(state, out bool result, true, true); } public Dictionary TryGetTransitionsToState(string stateName, out bool result) { - return GetTransitionsToState(stateName, out result, false); + return _GetTransitionsToState(stateName, out result, false, true); } public Dictionary TryGetTransitionsToState(State state, out bool result) { - return GetTransitionsToState(state, out result, false); + return _GetTransitionsToState(state, out result, false, true); } } diff --git a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithXML.cs b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithXML.cs index 36c29b8..6bd7cb9 100644 --- a/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithXML.cs +++ b/SimpleStateMachineLibrary/StateMachines/StateMachineWorkWithXML.cs @@ -9,18 +9,19 @@ namespace SimpleStateMachineLibrary public partial class StateMachine { - private static XDocument ToXDocument(StateMachine stateMachine, string nameFile) - { + internal static XDocument _ToXDocument(StateMachine stateMachine, string nameFile, bool withLog) + { Check.Object(stateMachine, stateMachine?._logger); Check.Name(nameFile, stateMachine?._logger); XDocument xDocument = new XDocument(); XElement stateMachineXElement = new XElement("StateMachine"); xDocument.Add(stateMachineXElement); + stateMachine?._logger.LogDebug("StateMachine to XDocument"); XElement states = new XElement("States"); stateMachineXElement.Add(states); foreach(var state in stateMachine._states) { - states.Add(state.Value.ToXElement()); + states.Add(state.Value.ToXElement(withLog)); } if (stateMachine?._startState != null) @@ -35,7 +36,7 @@ private static XDocument ToXDocument(StateMachine stateMachine, string nameFile) foreach (var transition in stateMachine._transitions) { - transitions.Add(transition.Value.ToXElement()); + transitions.Add(transition.Value._ToXElement(withLog)); } XElement datas = new XElement("DATA"); @@ -43,56 +44,56 @@ private static XDocument ToXDocument(StateMachine stateMachine, string nameFile) foreach (var data in stateMachine._data) { - datas.Add(data.Value.ToXElement()); + datas.Add(data.Value._ToXElement(withLog)); } xDocument.Save(nameFile); - stateMachine?._logger?.LogDebug("StateMachine to XDocument"); + return xDocument; } public XDocument ToXDocument(string nameFile) { - return StateMachine.ToXDocument(this, nameFile); + return StateMachine._ToXDocument(this, nameFile, true); } - private static StateMachine FromXDocument(StateMachine stateMachine, XDocument xDocument) + internal static StateMachine _FromXDocument(StateMachine stateMachine, XDocument xDocument, bool withLog) { XElement stateMachineXElement = Check.Object(xDocument, stateMachine?._logger).Element("StateMachine"); stateMachineXElement = Check.Object(stateMachineXElement, stateMachine?._logger); var States = stateMachineXElement.Element("States")?.Elements()?.ToList(); - States?.ForEach(x => stateMachine.AddState(x)); + States?.ForEach(x => stateMachine._AddState(x, true)); var startState = stateMachineXElement.Element("StartState"); string nameStartState = startState?.Attribute("Name").Value; if (!string.IsNullOrEmpty(nameStartState)) stateMachine.SetStartState(nameStartState); var Transitions = stateMachineXElement.Element("Transitions")?.Elements()?.ToList(); - Transitions?.ForEach(x => stateMachine.AddTransition(x)); + Transitions?.ForEach(x => stateMachine._AddTransition(x, true)); var Datas = stateMachineXElement.Element("DATA")?.Elements()?.ToList(); - Datas?.ForEach(x => stateMachine.AddData(x)); - stateMachine?._logger?.LogDebug("StateMachine from XDocument"); + Datas?.ForEach(x => stateMachine._AddData(x, true)); + stateMachine?._logger.LogDebug("StateMachine from XDocument"); return stateMachine; } - private static StateMachine FromXDocument(StateMachine stateMachine, string xDocumentPath) + internal static StateMachine _FromXDocument(StateMachine stateMachine, string xDocumentPath, bool withLog) { xDocumentPath = Check.Name(xDocumentPath, stateMachine?._logger); XDocument xDocument = XDocument.Load(xDocumentPath); - return FromXDocument(stateMachine, xDocument); + return _FromXDocument(stateMachine, xDocument, withLog); } public static StateMachine FromXDocument(XDocument xDocument, ILogger logger = null) { StateMachine stateMachine = new StateMachine(logger); - return FromXDocument(stateMachine, xDocument); + return _FromXDocument(stateMachine, xDocument, true); } public static StateMachine FromXDocument(string xmlFilePath, ILogger logger = null) { StateMachine stateMachine = new StateMachine(logger); - return FromXDocument(stateMachine, xmlFilePath); + return _FromXDocument(stateMachine, xmlFilePath, true); } } diff --git a/SimpleStateMachineLibrary/States/State.cs b/SimpleStateMachineLibrary/States/State.cs index 6598cfa..d5915c1 100644 --- a/SimpleStateMachineLibrary/States/State.cs +++ b/SimpleStateMachineLibrary/States/State.cs @@ -11,9 +11,11 @@ public partial class State : NamedObject private Action> _onExit; - internal State(StateMachine stateMachine, string nameState, Action> actionOnEntry, Action> actionOnExit) : base(stateMachine, nameState) + internal State(StateMachine stateMachine, string nameState, Action> actionOnEntry, Action> actionOnExit, bool withLog) : base(stateMachine, nameState) { - stateMachine?._logger?.LogDebug("Create state \"{NameState}\" ", nameState); + //stateMachine?._logger.LogDebug("Create state \"{NameState}\" ", nameState); + + StateMachine._AddState(this, out bool result, true, withLog); if (actionOnEntry != null) { @@ -24,18 +26,9 @@ internal State(StateMachine stateMachine, string nameState, Action> actionOnEntry) actionOnEntry = Check.Object(actionOnEntry, this.StateMachine?._logger); _onEntry += actionOnEntry; - this.StateMachine._logger?.LogDebug("Method \"{NameMethod}\" subscribe on entry for state \"{NameState}\"", actionOnEntry.Method.Name, this.Name); + this.StateMachine._logger.LogDebug("Method \"{NameMethod}\" subscribe on entry for state \"{NameState}\"", actionOnEntry.Method.Name, this.Name); return this; } @@ -67,20 +60,24 @@ public State OnExit(Action> actionOnExit) actionOnExit = Check.Object(actionOnExit, this.StateMachine?._logger); _onExit += actionOnExit; - this.StateMachine._logger?.LogDebug("Method \"{NameMethod}\" subscribe on exit for state \"{NameState}\"", actionOnExit.Method.Name, this.Name); + this.StateMachine._logger.LogDebug("Method \"{NameMethod}\" subscribe on exit for state \"{NameState}\"", actionOnExit.Method.Name, this.Name); return this; } - internal void Entry(Dictionary parameters) + internal void _Entry(Dictionary parameters, bool withLog) { _onEntry?.Invoke (this, parameters); - this.StateMachine._logger?.LogDebugAndInformation("Entry to state \"{NameState}\"", this.Name); + + if(withLog) + this.StateMachine._logger.LogDebug("Entry to state \"{NameState}\"", this.Name); } - internal void Exit(Dictionary parameters) + internal void _Exit(Dictionary parameters, bool withLog) { _onExit?.Invoke(this, parameters); - this.StateMachine._logger?.LogDebugAndInformation("Exit from state \"{NameState}\"", this.Name); + + if (withLog) + this.StateMachine._logger.LogDebug("Exit from state \"{NameState}\"", this.Name); } } } diff --git a/SimpleStateMachineLibrary/States/StateWorkWithTransitionsToThisState.cs b/SimpleStateMachineLibrary/States/StateWorkWithTransitionsToThisState.cs index 6ccd0f1..3711b43 100644 --- a/SimpleStateMachineLibrary/States/StateWorkWithTransitionsToThisState.cs +++ b/SimpleStateMachineLibrary/States/StateWorkWithTransitionsToThisState.cs @@ -6,7 +6,6 @@ namespace SimpleStateMachineLibrary { public partial class State { - public Dictionary GetTransitionsToThis() { return this.StateMachine.GetTransitionsToState(this); @@ -36,6 +35,5 @@ public Transition TryAddTransitionToThis(out bool result, string nameTransition, { return this.StateMachine.TryAddTransition(out result, nameTransition, nameStateFrom, this, actionOnInvoke); } - } } diff --git a/SimpleStateMachineLibrary/States/StateWorkWithXML.cs b/SimpleStateMachineLibrary/States/StateWorkWithXML.cs index bd37f56..2ec051b 100644 --- a/SimpleStateMachineLibrary/States/StateWorkWithXML.cs +++ b/SimpleStateMachineLibrary/States/StateWorkWithXML.cs @@ -7,27 +7,34 @@ namespace SimpleStateMachineLibrary { public partial class State { - internal static XElement ToXElement(State state) + internal static XElement ToXElement(State state, bool withLog) { Check.NamedObject(state, state?.StateMachine?._logger); XElement element = new XElement("State"); element.Add(new XAttribute("Name", state.Name)); - state.StateMachine._logger?.LogDebug("State \"{NameState}\" to XElement", state.Name); + if(withLog) + state.StateMachine._logger.LogDebug("State \"{NameState}\" to XElement", state.Name); + return element; } - internal XElement ToXElement() + internal XElement ToXElement(bool withLog) { - return State.ToXElement(this); + return State.ToXElement(this, withLog); } - internal static State FromXElement(StateMachine stateMachine, XElement state) + internal static State FromXElement(StateMachine stateMachine, XElement state, bool withLog) { string Name = state.Attribute("Name")?.Value; - stateMachine?._logger?.LogDebug("Initialization state \"{NameState}\" from XElement", Name); - return stateMachine.AddState(Name); + + State stateObj = stateMachine._AddState(Name, null, null, out bool result, true, false); + + if ((result) && (withLog)) + stateMachine?._logger.LogDebug("Initialization state \"{NameState}\" from XElement", Name); + + return stateObj; } diff --git a/SimpleStateMachineLibrary/Transitions/Transition.cs b/SimpleStateMachineLibrary/Transitions/Transition.cs index d44c724..8171f0a 100644 --- a/SimpleStateMachineLibrary/Transitions/Transition.cs +++ b/SimpleStateMachineLibrary/Transitions/Transition.cs @@ -13,20 +13,20 @@ public partial class Transition : NamedObject private Action> _onInvoke; - internal Transition(StateMachine stateMachine, string nameTransition, string stateFrom, string stateTo, Action> actionOnInvoke) : base(stateMachine, nameTransition) + internal Transition(StateMachine stateMachine, string nameTransition, string stateFrom, string stateTo, Action> actionOnInvoke, bool withLog) : base(stateMachine, nameTransition) { - StateFrom = stateMachine._StateExists(stateFrom, out _, true); - StateTo = stateMachine._StateExists(stateTo, out _, true); + StateFrom = stateMachine._StateExists(stateFrom, out _, true, false); + StateTo = stateMachine._StateExists(stateTo, out _, true, false); - stateMachine?._logger?.LogDebug("Create transition \"{NameTransition}\" from state \"{NameStateFrom}\" to state \"{NameStateTo}\"", nameTransition, stateFrom, stateTo); + //stateMachine?._logger.LogDebug("Create transition \"{NameTransition}\" from state \"{NameStateFrom}\" to state \"{NameStateTo}\"", nameTransition, stateFrom, stateTo); + + stateMachine._AddTransition(this, out _, true, withLog); if (actionOnInvoke != null) { OnInvoke(actionOnInvoke); - } - - stateMachine.AddTransition(this, out _, true); + } } public Transition Delete() @@ -45,7 +45,7 @@ public Transition OnInvoke(Action> action _onInvoke += actionOnInvoke; - this.StateMachine._logger?.LogDebug("Method \"{NameMethod}\" subscribe on invore for transition \"{NameTransition}\"", actionOnInvoke.Method.Name, this.Name); + this.StateMachine._logger.LogDebug("Method \"{NameMethod}\" subscribe on invoke for transition \"{NameTransition}\"", actionOnInvoke.Method.Name, this.Name); return this; } @@ -55,10 +55,10 @@ public InvokeParameters Invoke(Dictionary parameters) return StateMachine.InvokeTransition(this, parameters); } - internal Transition Invoking(Dictionary parameters) + internal Transition _Invoking(Dictionary parameters) { _onInvoke?.Invoke (this, parameters); - this.StateMachine._logger?.LogDebugAndInformation("Invoke transition \"{NameTransition}\"", this.Name); + this.StateMachine._logger.LogDebug("Invoke transition \"{NameTransition}\"", this.Name); return this; } diff --git a/SimpleStateMachineLibrary/Transitions/TransitionWorkWithXML.cs b/SimpleStateMachineLibrary/Transitions/TransitionWorkWithXML.cs index c483d09..287f387 100644 --- a/SimpleStateMachineLibrary/Transitions/TransitionWorkWithXML.cs +++ b/SimpleStateMachineLibrary/Transitions/TransitionWorkWithXML.cs @@ -7,24 +7,26 @@ namespace SimpleStateMachineLibrary { public partial class Transition { - internal static XElement ToXElement(Transition transition) + internal static XElement _ToXElement(Transition transition, bool withLog) { Check.NamedObject(transition, transition?.StateMachine?._logger); XElement element = new XElement("Transition"); element.Add(new XAttribute("Name", transition.Name)); element.Add(new XAttribute("From", transition.StateFrom)); element.Add(new XAttribute("To", transition.StateTo)); + + if(withLog) + transition.StateMachine._logger.LogDebug("Transition \"{NameTransition}\" to XElement", transition.Name); - transition.StateMachine._logger?.LogDebug("Transition \"{NameTransition}\" to XElement", transition.Name); return element; } - internal XElement ToXElement() + internal XElement _ToXElement(bool withLog) { - return Transition.ToXElement(this); + return Transition._ToXElement(this, withLog); } - internal static Transition FromXElement(StateMachine stateMachine, XElement transition) + internal static Transition _FromXElement(StateMachine stateMachine, XElement transition, bool withLog) { stateMachine = Check.Object(stateMachine, stateMachine?._logger); transition = Check.Object(transition, stateMachine?._logger); @@ -33,8 +35,11 @@ internal static Transition FromXElement(StateMachine stateMachine, XElement tran string From = transition.Attribute("From")?.Value; string To = transition.Attribute("To")?.Value; - stateMachine?._logger?.LogDebug("Initialization transition \"{NameTransition}\" from XElement", Name); - return stateMachine.AddTransition(Name, From, To); + Transition transitionObj = stateMachine._AddTransition(Name, From, To, null, out bool result, true, false); + if((result)&&(withLog)) + stateMachine?._logger.LogDebug("Initialization transition \"{NameTransition}\" from XElement", Name); + + return transitionObj; } } } diff --git a/SimpleStateMachineLibrary/logo.jpg b/SimpleStateMachineLibrary/logo.jpg deleted file mode 100644 index bb8ec32..0000000 Binary files a/SimpleStateMachineLibrary/logo.jpg and /dev/null differ diff --git a/Tests/StateMachineTests.cs b/Tests/StateMachineTests.cs index 38867e8..9ead5e4 100644 --- a/Tests/StateMachineTests.cs +++ b/Tests/StateMachineTests.cs @@ -62,7 +62,6 @@ public void StateMachineFromCode() Assert.IsTrue(stateMachine.TransitionExists("Transition1")); state1.SetAsStartState(); - state1.OnExit(Method1); state2.OnExit(Method2); state3.OnExit(Method3); state4.OnExit(Method4); @@ -86,8 +85,6 @@ public void StateMachineFromXML() var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole().AddDebug().SetMinimumLevel(LogLevel.Debug); }); var logger = loggerFactory.CreateLogger(); - - StateMachine stateMachine = StateMachine.FromXDocument("text.xml", logger); stateMachine.GetState("State1").OnExit(Method1); diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index a324721..9f2b0a0 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -7,12 +7,12 @@ - - - - - - + + + + + + diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 0000000..8c739b4 --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,34 @@ +jobs: +- job: Linux + pool: + vmImage: 'ubuntu-18.04' + variables: + buildConfiguration: 'Release' + steps: + - script: cd $(Build.SourcesDirectory) && dotnet build + displayName: 'Linux Build and Tests' + - task: PublishTestResults@2 + inputs: + testRunner: VSTest + testResultsFiles: '**/*.trx' + +- job: Windows + pool: + vmImage: 'windows-2019' + variables: + buildConfiguration: 'Release' + steps: + - task: DotNetCoreInstaller@0 + inputs: + version: '3.1.302' + - script: cd $(Build.SourcesDirectory) && dotnet build + displayName: 'Windows Full Build and Tests' + - task: PublishTestResults@2 + inputs: + testRunner: VSTest + testResultsFiles: '**/*.trx' + - task: PublishCodeCoverageResults@1 + inputs: + summaryFileLocation: $(Build.SourcesDirectory)\artifacts\coverage.cobertura.xml + reportDirectory: $(Build.SourcesDirectory)\artifacts + codecoverageTool: cobertura