Mercurial > pub > ImplabNet
changeset 157:948c015a9011 v2
sync
author | cin |
---|---|
date | Thu, 18 Feb 2016 11:03:47 +0300 |
parents | 97fbbf816844 |
children | 130781364799 |
files | Implab/Components/RunnableComponent.cs Implab/Parsing/EDFADefinition.cs |
diffstat | 2 files changed, 51 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab/Components/RunnableComponent.cs Mon Feb 15 04:22:15 2016 +0300 +++ b/Implab/Components/RunnableComponent.cs Thu Feb 18 11:03:47 2016 +0300 @@ -3,51 +3,63 @@ namespace Implab.Components { public class RunnableComponent : Disposable, IRunnable, IInitializable { - class Automaton : DFAutomaton<ExecutionState> { - static readonly EDFADefinition<ExecutionState> _dfa; + + + class Automaton { + enum Operations { + Initialize, + Start, + Stop, + Fail, + Success, + Dispose + } + + static readonly EDFADefinition<ExecutionState> _def = new EDFADefinition<ExecutionState>(EnumAlphabet<ExecutionState>.FullAlphabet); + static readonly DFAStateDescriptior[] _states; static Automaton() { + var created = _def.AddState(); // initial state + var initializing = _def.AddState(); + var ready = _def.AddState(); + var starting = _def.AddState(); + var running = _def.AddState(); + var stopping = _def.AddState(); + var error = _def.AddState(); + var disposing = _def.AddState(); + var disposed = _def.AddState(new int[] { 0 }); - var token = Token - .New(ExecutionState.Uninitialized).Optional() // we can skip uninitialized state - .Cat( - Token.New(ExecutionState.Ready) // uninitialized -> initial - .Cat( - Token.New(ExecutionState.Starting) // initial -> starting - .Cat( - Token.New(ExecutionState.Running) // running -> {stopping -> stopped | failed } - .Cat( - Token.New(ExecutionState.Stopping) // running -> stopping - .Cat( - Token.New(ExecutionState.Stopped) // stopping -> stopped - .Or(Token.New(ExecutionState.Failed)) // stopping -> failed - ) - .Or(Token.New(ExecutionState.Failed)) // running -> failed - ) - .Or(Token.New(ExecutionState.Failed)) // starting -> failed - ).EClosure() - ) - .Or(Token.New(ExecutionState.Failed)) // uninitialized->failed - .Cat(Token.New(ExecutionState.Disposed).Tag(0)) // ... -> disposed - ); - - var builder = new DFABuilder(); - token.Accept(builder); + _def.DefineTransition(created,initializing,(int)Operations.Initialize); + + _def.DefineTransition(initializing,ready,(int)Operations.Success); + _def.DefineTransition(initializing,error,(int)Operations.Fail); + + _def.DefineTransition(ready, starting, (int)Operations.Start); + _def.DefineTransition(ready, disposing, (int)Operations.Dispose); + - var _dfa = new EDFADefinition<ExecutionState>(EnumAlphabet<ExecutionState>.FullAlphabet); - builder.BuildDFA(_dfa); // don't optimize dfa to avoid remapping of the alphabet + _def.DefineTransition(starting, running, (int)Operations.Success); + _def.DefineTransition(starting, error, (int)Operations.Fail); + + _def.DefineTransition(running, stopping, (int)Operations.Stop); + _def.DefineTransition(running, error, (int)Operations.Fail); + _def.DefineTransition(stopping, ready, (int)Operations.Success); + _def.DefineTransition(stopping, error, (int)Operations.Fail); + + _def.DefineTransition(disposing, disposed, (int)Operations.Success); + + _states = _def.States; } - public Automaton() : base(_dfa.States, INITIAL_STATE, ExecutionState.Reserved) { + int m_state; + + public Automaton() { + m_state = DFADefinitionBase.INITIAL_STATE; } - public void MoveTo(ExecutionState state) { + void Move(Operations op) { - if (!CanMove((int)state)) - throw new InvalidOperationException(String.Format("Illegal state transition from {0} to {1}", Current, state)); - Move((int)state); - m_context.info = state; } public ExecutionState Current {
--- a/Implab/Parsing/EDFADefinition.cs Mon Feb 15 04:22:15 2016 +0300 +++ b/Implab/Parsing/EDFADefinition.cs Thu Feb 18 11:03:47 2016 +0300 @@ -18,6 +18,10 @@ get { return m_alphabet.Count; } } + public void DefineTransition(int s1, int s2, T input) { + DefineTransition(s1, s2, m_alphabet.Translate(input)); + } + public EDFADefinition<T> Optimize() { var optimized = new EDFADefinition<T>(new EnumAlphabet<T>()); Optimize(optimized, m_alphabet, optimized.Alphabet);