# HG changeset patch # User cin # Date 2016-02-18 08:03:47 # Node ID 948c015a9011e00b20c1b7f0d17bb3f6b8b1afd2 # Parent 97fbbf81684416cdc248e7c57fd148402e471d90 sync diff --git a/Implab/Components/RunnableComponent.cs b/Implab/Components/RunnableComponent.cs --- a/Implab/Components/RunnableComponent.cs +++ b/Implab/Components/RunnableComponent.cs @@ -3,51 +3,63 @@ using Implab.Parsing; namespace Implab.Components { public class RunnableComponent : Disposable, IRunnable, IInitializable { - class Automaton : DFAutomaton { - static readonly EDFADefinition _dfa; + + + class Automaton { + enum Operations { + Initialize, + Start, + Stop, + Fail, + Success, + Dispose + } + + static readonly EDFADefinition _def = new EDFADefinition(EnumAlphabet.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(EnumAlphabet.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 { diff --git a/Implab/Parsing/EDFADefinition.cs b/Implab/Parsing/EDFADefinition.cs --- a/Implab/Parsing/EDFADefinition.cs +++ b/Implab/Parsing/EDFADefinition.cs @@ -18,6 +18,10 @@ namespace Implab.Parsing { get { return m_alphabet.Count; } } + public void DefineTransition(int s1, int s2, T input) { + DefineTransition(s1, s2, m_alphabet.Translate(input)); + } + public EDFADefinition Optimize() { var optimized = new EDFADefinition(new EnumAlphabet()); Optimize(optimized, m_alphabet, optimized.Alphabet);