| @@ -0,0 +1,8 | |||||
|  | 1 | using System; | |||
|  | 2 | ||||
|  | 3 | namespace Implab.Components { | |||
|  | 4 | public interface IFactory<out T> { | |||
|  | 5 | T Create(); | |||
|  | 6 | } | |||
|  | 7 | } | |||
|  | 8 | ||||
| @@ -0,0 +1,141 | |||||
|  | 1 | using System; | |||
|  | 2 | using Implab.Parsing; | |||
|  | 3 | ||||
|  | 4 | namespace Implab.Components { | |||
|  | 5 | public class RunnableComponent : Disposable, IRunnable, IInitializable { | |||
|  | 6 | class Automaton : DFAutomaton<ExecutionState> { | |||
|  | 7 | static readonly EDFADefinition<ExecutionState> _dfa; | |||
|  | 8 | ||||
|  | 9 | static Automaton() { | |||
|  | 10 | ||||
|  | 11 | var token = Token | |||
|  | 12 | .New(ExecutionState.Uninitialized).Optional() // we can skip uninitialized state | |||
|  | 13 | .Cat( | |||
|  | 14 | Token.New(ExecutionState.Ready) // uninitialized -> initial | |||
|  | 15 | .Cat( | |||
|  | 16 | Token.New(ExecutionState.Starting) // initial -> starting | |||
|  | 17 | .Cat( | |||
|  | 18 | Token.New(ExecutionState.Running) // running -> {stopping -> stopped | failed } | |||
|  | 19 | .Cat( | |||
|  | 20 | Token.New(ExecutionState.Stopping) // running -> stopping | |||
|  | 21 | .Cat( | |||
|  | 22 | Token.New(ExecutionState.Stopped) // stopping -> stopped | |||
|  | 23 | .Or(Token.New(ExecutionState.Failed)) // stopping -> failed | |||
|  | 24 | ) | |||
|  | 25 | .Or(Token.New(ExecutionState.Failed)) // running -> failed | |||
|  | 26 | ) | |||
|  | 27 | .Or(Token.New(ExecutionState.Failed)) // starting -> failed | |||
|  | 28 | ).EClosure() | |||
|  | 29 | ) | |||
|  | 30 | .Or(Token.New(ExecutionState.Failed)) // uninitialized->failed | |||
|  | 31 | .Cat(Token.New(ExecutionState.Disposed).Tag(0)) // ... -> disposed | |||
|  | 32 | ); | |||
|  | 33 | ||||
|  | 34 | var builder = new DFABuilder(); | |||
|  | 35 | token.Accept(builder); | |||
|  | 36 | ||||
|  | 37 | var _dfa = new EDFADefinition<ExecutionState>(EnumAlphabet<ExecutionState>.FullAlphabet); | |||
|  | 38 | builder.BuildDFA(_dfa); // don't optimize dfa to avoid remapping of the alphabet | |||
|  | 39 | ||||
|  | 40 | } | |||
|  | 41 | ||||
|  | 42 | public Automaton() : base(_dfa.States, INITIAL_STATE, ExecutionState.Reserved) { | |||
|  | 43 | } | |||
|  | 44 | ||||
|  | 45 | public void MoveTo(ExecutionState state) { | |||
|  | 46 | ||||
|  | 47 | if (!CanMove((int)state)) | |||
|  | 48 | throw new InvalidOperationException(String.Format("Illegal state transition from {0} to {1}", Current, state)); | |||
|  | 49 | Move((int)state); | |||
|  | 50 | m_context.info = state; | |||
|  | 51 | } | |||
|  | 52 | ||||
|  | 53 | public ExecutionState Current { | |||
|  | 54 | get { | |||
|  | 55 | return (ExecutionState)m_context.info; | |||
|  | 56 | } | |||
|  | 57 | } | |||
|  | 58 | } | |||
|  | 59 | ||||
|  | 60 | readonly Automaton m_automaton = new Automaton(); | |||
|  | 61 | IPromise m_pending; | |||
|  | 62 | Exception m_lastError; | |||
|  | 63 | ||||
|  | 64 | protected RunnableComponent(bool initialized) { | |||
|  | 65 | if (initialized) | |||
|  | 66 | m_automaton.MoveTo(ExecutionState.Ready); | |||
|  | 67 | else | |||
|  | 68 | m_automaton.MoveTo(ExecutionState.Uninitialized); | |||
|  | 69 | } | |||
|  | 70 | ||||
|  | 71 | #region IInitializable implementation | |||
|  | 72 | ||||
|  | 73 | public void Init() { | |||
|  | 74 | ||||
|  | 75 | } | |||
|  | 76 | ||||
|  | 77 | #endregion | |||
|  | 78 | ||||
|  | 79 | #region IRunnable implementation | |||
|  | 80 | ||||
|  | 81 | public IPromise Start() { | |||
|  | 82 | return Safe.InvokePromise(() => { | |||
|  | 83 | Promise promise; | |||
|  | 84 | lock (m_automaton) { | |||
|  | 85 | if (m_automaton.Current == ExecutionState.Starting) | |||
|  | 86 | return m_pending; | |||
|  | 87 | m_automaton.MoveTo(ExecutionState.Starting); | |||
|  | 88 | m_pending = promise = new Promise(); | |||
|  | 89 | } | |||
|  | 90 | ||||
|  | 91 | var start = Safe.InvokePromise(OnStart); | |||
|  | 92 | promise.On(null, null, start.Cancel); | |||
|  | 93 | start.On(promise.Resolve, promise.Reject, promise.CancelOperation); | |||
|  | 94 | ||||
|  | 95 | return promise.Then(() => { | |||
|  | 96 | lock(m_automaton) { | |||
|  | 97 | m_automaton.MoveTo(ExecutionState.Running); | |||
|  | 98 | m_pending = null; | |||
|  | 99 | } | |||
|  | 100 | ||||
|  | 101 | Run(); | |||
|  | 102 | }, err => { | |||
|  | 103 | if (BeginTransition(RUNNING_REQUIRE)) { | |||
|  | 104 | m_lastError = err; | |||
|  | 105 | CompleteTransition(FAILED_STATE); | |||
|  | 106 | throw new PromiseTransientException(err); | |||
|  | 107 | } | |||
|  | 108 | throw new OperationCanceledException(); | |||
|  | 109 | }, reason => { | |||
|  | 110 | throw new OperationCanceledException("The operation was cancelled", reason); | |||
|  | 111 | }); | |||
|  | 112 | }); | |||
|  | 113 | } | |||
|  | 114 | ||||
|  | 115 | protected virtual IPromise OnStart() { | |||
|  | 116 | return Promise.SUCCESS; | |||
|  | 117 | } | |||
|  | 118 | ||||
|  | 119 | protected virtual void Run() { | |||
|  | 120 | } | |||
|  | 121 | ||||
|  | 122 | public IPromise Stop() { | |||
|  | 123 | throw new NotImplementedException(); | |||
|  | 124 | } | |||
|  | 125 | ||||
|  | 126 | public ExecutionState State { | |||
|  | 127 | get { | |||
|  | 128 | throw new NotImplementedException(); | |||
|  | 129 | } | |||
|  | 130 | } | |||
|  | 131 | ||||
|  | 132 | public Exception LastError { | |||
|  | 133 | get { | |||
|  | 134 | throw new NotImplementedException(); | |||
|  | 135 | } | |||
|  | 136 | } | |||
|  | 137 | ||||
|  | 138 | #endregion | |||
|  | 139 | } | |||
|  | 140 | } | |||
|  | 141 | ||||
| @@ -12,27 +12,12 namespace Implab.Fx { | |||||
| 12 | m_target = target; |  | 12 | m_target = target; | |
| 13 | } |  | 13 | } | |
| 14 |  | 14 | |||
| 15 | protected override void Signal |  | 15 | protected override void SignalHandler(HandlerDescriptor handler, int signal) { | |
| 16 | if (m_target.InvokeRequired) |  | 16 | if (m_target.InvokeRequired) | |
| 17 | m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor>(base.Signal |  | 17 | m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor, int>(base.SignalHandler), handler, signal); | |
| 18 | else |  | |||
| 19 | base.SignalSuccess(handler); |  | |||
| 20 | } |  | |||
| 21 |  | ||||
| 22 | protected override void SignalCancelled(Promise<T>.HandlerDescriptor handler, Exception reason) { |  | |||
| 23 | if (m_target.InvokeRequired) |  | |||
| 24 | m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor,Exception>(base.SignalCancelled), handler, reason); |  | |||
| 25 | else |  | 18 | else | |
| 26 | base.Signal |  | 19 | base.SignalHandler(handler, signal); | |
| 27 | } |  | 20 | } | |
| 28 |  | ||||
| 29 | protected override void SignalError(Promise<T>.HandlerDescriptor handler, Exception error) { |  | |||
| 30 | if (m_target.InvokeRequired) |  | |||
| 31 | m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor,Exception>(base.SignalError), handler, error); |  | |||
| 32 | else |  | |||
| 33 | base.SignalError(handler, error); |  | |||
| 34 | } |  | |||
| 35 |  | ||||
| 36 | } |  | 21 | } | |
| 37 | } |  | 22 | } | |
| 38 |  | 23 | |||
| @@ -8,9 +8,9 namespace Implab { | |||||
| 8 |  | 8 | |||
| 9 | const int UNRESOLVED_SATE = 0; |  | 9 | const int UNRESOLVED_SATE = 0; | |
| 10 | const int TRANSITIONAL_STATE = 1; |  | 10 | const int TRANSITIONAL_STATE = 1; | |
| 11 | const int SUCCEEDED_STATE = 2; |  | 11 | protected const int SUCCEEDED_STATE = 2; | |
| 12 | const int REJECTED_STATE = 3; |  | 12 | protected const int REJECTED_STATE = 3; | |
| 13 | const int CANCELLED_STATE = 4; |  | 13 | protected const int CANCELLED_STATE = 4; | |
| 14 |  | 14 | |||
| 15 | const int CANCEL_NOT_REQUESTED = 0; |  | 15 | const int CANCEL_NOT_REQUESTED = 0; | |
| 16 | const int CANCEL_REQUESTING = 1; |  | 16 | const int CANCEL_REQUESTING = 1; | |
| @@ -22,7 +22,8 namespace Implab { | |||||
| 22 | Exception m_error; |  | 22 | Exception m_error; | |
| 23 | int m_handlersCount; |  | 23 | int m_handlersCount; | |
| 24 |  | 24 | |||
| 25 | readonly THandler[] m_handlers = new THandler[RESERVED_HANDLERS_COUNT]; |  | 25 | //readonly THandler[] m_handlers = new THandler[RESERVED_HANDLERS_COUNT]; | |
|  | 26 | THandler[] m_handlers; | |||
| 26 | MTQueue<THandler> m_extraHandlers; |  | 27 | MTQueue<THandler> m_extraHandlers; | |
| 27 | int m_handlerPointer = -1; |  | 28 | int m_handlerPointer = -1; | |
| 28 | int m_handlersCommited; |  | 29 | int m_handlersCommited; | |
| @@ -60,7 +61,7 namespace Implab { | |||||
| 60 |  | 61 | |||
| 61 | protected void EndSetResult() { |  | 62 | protected void EndSetResult() { | |
| 62 | CompleteTransit(SUCCEEDED_STATE); |  | 63 | CompleteTransit(SUCCEEDED_STATE); | |
| 63 |  |  | 64 | Signal(); | |
| 64 | } |  | 65 | } | |
| 65 |  | 66 | |||
| 66 |  | 67 | |||
| @@ -78,14 +79,13 namespace Implab { | |||||
| 78 | protected void SetError(Exception error) { |  | 79 | protected void SetError(Exception error) { | |
| 79 | if (BeginTransit()) { |  | 80 | if (BeginTransit()) { | |
| 80 | if (error is OperationCanceledException) { |  | 81 | if (error is OperationCanceledException) { | |
|  | 82 | m_error = error.InnerException; | |||
| 81 | CompleteTransit(CANCELLED_STATE); |  | 83 | CompleteTransit(CANCELLED_STATE); | |
| 82 | m_error = error.InnerException; |  | |||
| 83 | OnCancelled(); |  | |||
| 84 | } else { |  | 84 | } else { | |
| 85 | m_error = error is PromiseTransientException ? error.InnerException : error; |  | 85 | m_error = error is PromiseTransientException ? error.InnerException : error; | |
| 86 | CompleteTransit(REJECTED_STATE); |  | 86 | CompleteTransit(REJECTED_STATE); | |
| 87 | OnError(); |  | |||
| 88 | } |  | 87 | } | |
|  | 88 | Signal(); | |||
| 89 | } else { |  | 89 | } else { | |
| 90 | WaitTransition(); |  | 90 | WaitTransition(); | |
| 91 | if (m_state == SUCCEEDED_STATE) |  | 91 | if (m_state == SUCCEEDED_STATE) | |
| @@ -101,22 +101,18 namespace Implab { | |||||
| 101 | if (BeginTransit()) { |  | 101 | if (BeginTransit()) { | |
| 102 | m_error = reason; |  | 102 | m_error = reason; | |
| 103 | CompleteTransit(CANCELLED_STATE); |  | 103 | CompleteTransit(CANCELLED_STATE); | |
| 104 |  |  | 104 | Signal(); | |
| 105 | } |  | 105 | } | |
| 106 | } |  | 106 | } | |
| 107 |  | 107 | |||
| 108 | protected abstract void Signal |  | 108 | protected abstract void SignalHandler(THandler handler, int signal); | |
| 109 |  | ||||
| 110 | protected abstract void SignalError(THandler handler, Exception error); |  | |||
| 111 |  | 109 | |||
| 112 | protected abstract void SignalCancelled(THandler handler, Exception reason); |  | 110 | void Signal() { | |
| 113 |  | ||||
| 114 | void OnSuccess() { |  | |||
| 115 | var hp = m_handlerPointer; |  | 111 | var hp = m_handlerPointer; | |
| 116 | var slot = hp +1 ; |  | 112 | var slot = hp +1 ; | |
| 117 | while (slot < m_handlersCommited) { |  | 113 | while (slot < m_handlersCommited) { | |
| 118 | if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) { |  | 114 | if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) { | |
| 119 | Signal |  | 115 | SignalHandler(m_handlers[slot], m_state); | |
| 120 | } |  | 116 | } | |
| 121 | hp = m_handlerPointer; |  | 117 | hp = m_handlerPointer; | |
| 122 | slot = hp +1 ; |  | 118 | slot = hp +1 ; | |
| @@ -126,43 +122,7 namespace Implab { | |||||
| 126 | if (m_extraHandlers != null) { |  | 122 | if (m_extraHandlers != null) { | |
| 127 | THandler handler; |  | 123 | THandler handler; | |
| 128 | while (m_extraHandlers.TryDequeue(out handler)) |  | 124 | while (m_extraHandlers.TryDequeue(out handler)) | |
| 129 | Signal |  | 125 | SignalHandler(handler, m_state); | |
| 130 | } |  | |||
| 131 | } |  | |||
| 132 |  | ||||
| 133 | void OnError() { |  | |||
| 134 | var hp = m_handlerPointer; |  | |||
| 135 | var slot = hp +1 ; |  | |||
| 136 | while (slot < m_handlersCommited) { |  | |||
| 137 | if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) { |  | |||
| 138 | SignalError(m_handlers[slot],m_error); |  | |||
| 139 | } |  | |||
| 140 | hp = m_handlerPointer; |  | |||
| 141 | slot = hp +1 ; |  | |||
| 142 | } |  | |||
| 143 |  | ||||
| 144 | if (m_extraHandlers != null) { |  | |||
| 145 | THandler handler; |  | |||
| 146 | while (m_extraHandlers.TryDequeue(out handler)) |  | |||
| 147 | SignalError(handler, m_error); |  | |||
| 148 | } |  | |||
| 149 | } |  | |||
| 150 |  | ||||
| 151 | void OnCancelled() { |  | |||
| 152 | var hp = m_handlerPointer; |  | |||
| 153 | var slot = hp +1 ; |  | |||
| 154 | while (slot < m_handlersCommited) { |  | |||
| 155 | if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) { |  | |||
| 156 | SignalCancelled(m_handlers[slot], m_error); |  | |||
| 157 | } |  | |||
| 158 | hp = m_handlerPointer; |  | |||
| 159 | slot = hp +1 ; |  | |||
| 160 | } |  | |||
| 161 |  | ||||
| 162 | if (m_extraHandlers != null) { |  | |||
| 163 | THandler handler; |  | |||
| 164 | while (m_extraHandlers.TryDequeue(out handler)) |  | |||
| 165 | SignalCancelled(handler, m_error); |  | |||
| 166 | } |  | 126 | } | |
| 167 | } |  | 127 | } | |
| 168 |  | 128 | |||
| @@ -194,12 +154,15 namespace Implab { | |||||
| 194 |  | 154 | |||
| 195 | if (m_state > 1) { |  | 155 | if (m_state > 1) { | |
| 196 | // the promise is in the resolved state, just invoke the handler |  | 156 | // the promise is in the resolved state, just invoke the handler | |
| 197 |  |  | 157 | SignalHandler(handler, m_state); | |
| 198 | } else { |  | 158 | } else { | |
| 199 | var slot = Interlocked.Increment(ref m_handlersCount) - 1; |  | 159 | var slot = Interlocked.Increment(ref m_handlersCount) - 1; | |
| 200 |  | 160 | |||
| 201 | if (slot < RESERVED_HANDLERS_COUNT) { |  | 161 | if (slot < RESERVED_HANDLERS_COUNT) { | |
| 202 |  | 162 | |||
|  | 163 | if (slot == 0) | |||
|  | 164 | Interlocked.CompareExchange(ref m_handlers, new THandler[RESERVED_HANDLERS_COUNT], null); | |||
|  | 165 | ||||
| 203 | m_handlers[slot] = handler; |  | 166 | m_handlers[slot] = handler; | |
| 204 |  | 167 | |||
| 205 | while (slot != Interlocked.CompareExchange(ref m_handlersCommited, slot + 1, slot)) { |  | 168 | while (slot != Interlocked.CompareExchange(ref m_handlersCommited, slot + 1, slot)) { | |
| @@ -212,7 +175,7 namespace Implab { | |||||
| 212 | if (slot < m_handlersCommited) { |  | 175 | if (slot < m_handlersCommited) { | |
| 213 | if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) != hp) |  | 176 | if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) != hp) | |
| 214 | continue; |  | 177 | continue; | |
| 215 |  |  | 178 | SignalHandler(m_handlers[slot], m_state); | |
| 216 | } |  | 179 | } | |
| 217 | break; |  | 180 | break; | |
| 218 | } while(true); |  | 181 | } while(true); | |
| @@ -233,27 +196,11 namespace Implab { | |||||
| 233 | // therefore we need to fetch a handler from the queue and execute it |  | 196 | // therefore we need to fetch a handler from the queue and execute it | |
| 234 | // note that fetched handler may be not the one that we have added |  | 197 | // note that fetched handler may be not the one that we have added | |
| 235 | // even we can fetch no handlers at all :) |  | 198 | // even we can fetch no handlers at all :) | |
| 236 |  |  | 199 | SignalHandler(handler, m_state); | |
| 237 | } |  | 200 | } | |
| 238 | } |  | 201 | } | |
| 239 | } |  | 202 | } | |
| 240 |  | 203 | |||
| 241 | protected void InvokeHandler(THandler handler) { |  | |||
| 242 | switch (m_state) { |  | |||
| 243 | case SUCCEEDED_STATE: |  | |||
| 244 | SignalSuccess(handler); |  | |||
| 245 | break; |  | |||
| 246 | case CANCELLED_STATE: |  | |||
| 247 | SignalCancelled(handler, m_error); |  | |||
| 248 | break; |  | |||
| 249 | case REJECTED_STATE: |  | |||
| 250 | SignalError(handler, m_error); |  | |||
| 251 | break; |  | |||
| 252 | default: |  | |||
| 253 | throw new Exception(String.Format("Invalid promise state {0}", m_state)); |  | |||
| 254 | } |  | |||
| 255 | } |  | |||
| 256 |  | ||||
| 257 | #endregion |  | 204 | #endregion | |
| 258 |  | 205 | |||
| 259 | #region IPromise implementation |  | 206 | #region IPromise implementation | |
| @@ -71,16 +71,20 namespace Implab { | |||||
| 71 |  | 71 | |||
| 72 | #region implemented abstract members of AbstractPromise |  | 72 | #region implemented abstract members of AbstractPromise | |
| 73 |  | 73 | |||
| 74 | protected override void Signal |  | 74 | protected override void SignalHandler(HandlerDescriptor handler, int signal) { | |
| 75 | handler.SignalSuccess(); |  | 75 | switch (signal) { | |
| 76 | } |  | 76 | case SUCCEEDED_STATE: | |
| 77 |  | 77 | handler.SignalSuccess(); | ||
| 78 | protected override void SignalError(HandlerDescriptor handler, Exception error) { |  | 78 | break; | |
| 79 | handler.SignalError(error); |  | 79 | case REJECTED_STATE: | |
| 80 | } |  | 80 | handler.SignalError(Error); | |
| 81 |  | 81 | break; | ||
| 82 | protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) { |  | 82 | case CANCELLED_STATE: | |
| 83 | handler.SignalCancel( |  | 83 | handler.SignalCancel(CancellationReason); | |
|  | 84 | break; | |||
|  | 85 | default: | |||
|  | 86 | throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); | |||
|  | 87 | } | |||
| 84 | } |  | 88 | } | |
| 85 |  | 89 | |||
| 86 | protected override Signal GetResolveSignal() { |  | 90 | protected override Signal GetResolveSignal() { | |
| @@ -175,16 +175,20 namespace Implab { | |||||
| 175 | return signal; |  | 175 | return signal; | |
| 176 | } |  | 176 | } | |
| 177 |  | 177 | |||
| 178 | protected override void Signal |  | 178 | protected override void SignalHandler(HandlerDescriptor handler, int signal) { | |
| 179 | handler.SignalSuccess(m_result); |  | 179 | switch (signal) { | |
| 180 | } |  | 180 | case SUCCEEDED_STATE: | |
| 181 |  | 181 | handler.SignalSuccess(m_result); | ||
| 182 | protected override void SignalError(HandlerDescriptor handler, Exception error) { |  | 182 | break; | |
| 183 | handler.SignalError(error); |  | 183 | case REJECTED_STATE: | |
| 184 | } |  | 184 | handler.SignalError(Error); | |
| 185 |  | 185 | break; | ||
| 186 | protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) { |  | 186 | case CANCELLED_STATE: | |
| 187 | handler.SignalCancel( |  | 187 | handler.SignalCancel(CancellationReason); | |
|  | 188 | break; | |||
|  | 189 | default: | |||
|  | 190 | throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); | |||
|  | 191 | } | |||
| 188 | } |  | 192 | } | |
| 189 |  | 193 | |||
| 190 | #endregion |  | 194 | #endregion | |
| @@ -1,8 +1,9 | |||||
| 1 | namespace Implab.Components { |  | 1 | namespace Implab.Components { | |
| 2 |  | 2 | |||
| 3 | public enum ExecutionState { |  | 3 | public enum ExecutionState { | |
|  | 4 | Reserved = 0, | |||
| 4 | Uninitialized, |  | 5 | Uninitialized, | |
| 5 |  |  | 6 | Ready, | |
| 6 | Starting, |  | 7 | Starting, | |
| 7 | Running, |  | 8 | Running, | |
| 8 | Stopping, |  | 9 | Stopping, | |
| @@ -182,6 +182,8 | |||||
| 182 | <Compile Include="Components\App.cs" /> |  | 182 | <Compile Include="Components\App.cs" /> | |
| 183 | <Compile Include="Components\IRunnable.cs" /> |  | 183 | <Compile Include="Components\IRunnable.cs" /> | |
| 184 | <Compile Include="Components\ExecutionState.cs" /> |  | 184 | <Compile Include="Components\ExecutionState.cs" /> | |
|  | 185 | <Compile Include="Components\RunnableComponent.cs" /> | |||
|  | 186 | <Compile Include="Components\IFactory.cs" /> | |||
| 185 | </ItemGroup> |  | 187 | </ItemGroup> | |
| 186 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |  | 188 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
| 187 | <ItemGroup /> |  | 189 | <ItemGroup /> | |
| @@ -1,13 +1,9 | |||||
| 1 | using Implab.Parsing; |  | 1 | using Implab.Parsing; | |
| 2 | using System; |  | |||
| 3 | using System.Collections.Generic; |  | |||
| 4 | using System.Linq; |  | 2 | using System.Linq; | |
| 5 | using System.Text; |  | |||
| 6 | using System.Threading.Tasks; |  | |||
| 7 |  | 3 | |||
| 8 | namespace Implab.JSON { |  | 4 | namespace Implab.JSON { | |
| 9 |  |  | 5 | class JSONGrammar : Grammar<JSONGrammar> { | |
| 10 | public enum TokenType |  | 6 | public enum TokenType { | |
| 11 | None, |  | 7 | None, | |
| 12 | BeginObject, |  | 8 | BeginObject, | |
| 13 | EndObject, |  | 9 | EndObject, | |
| @@ -1,12 +1,7 | |||||
| 1 | using Implab; |  | 1 | using Implab.Parsing; | |
| 2 | using Implab.Parsing; |  | |||
| 3 | using System; |  | 2 | using System; | |
| 4 | using System.Collections.Generic; |  | |||
| 5 | using System.Diagnostics; |  | 3 | using System.Diagnostics; | |
| 6 | using System.IO; |  | 4 | using System.IO; | |
| 7 | using System.Linq; |  | |||
| 8 | using System.Text; |  | |||
| 9 | using System.Threading.Tasks; |  | |||
| 10 |  | 5 | |||
| 11 | namespace Implab.JSON { |  | 6 | namespace Implab.JSON { | |
| 12 | /// <summary> |  | 7 | /// <summary> | |
| @@ -192,11 +187,10 namespace Implab.JSON { | |||||
| 192 | if (m_memberContext == MemberContext.MemberName) { |  | 187 | if (m_memberContext == MemberContext.MemberName) { | |
| 193 | m_context.info.memberName = (string)tokenValue; |  | 188 | m_context.info.memberName = (string)tokenValue; | |
| 194 | break; |  | 189 | break; | |
| 195 | } else { |  | |||
| 196 | m_elementType = JSONElementType.Value; |  | |||
| 197 | m_elementValue = tokenValue; |  | |||
| 198 | return true; |  | |||
| 199 | } |  | 190 | } | |
|  | 191 | m_elementType = JSONElementType.Value; | |||
|  | 192 | m_elementValue = tokenValue; | |||
|  | 193 | return true; | |||
| 200 | case JsonTokenType.Number: |  | 194 | case JsonTokenType.Number: | |
| 201 | m_elementType = JSONElementType.Value; |  | 195 | m_elementType = JSONElementType.Value; | |
| 202 | m_elementValue = tokenValue; |  | 196 | m_elementValue = tokenValue; | |
| @@ -11,7 +11,7 namespace Implab.Parsing { | |||||
| 11 | public const int UNCLASSIFIED = 0; |  | 11 | public const int UNCLASSIFIED = 0; | |
| 12 |  | 12 | |||
| 13 | int m_nextId = 1; |  | 13 | int m_nextId = 1; | |
| 14 | int[] m_map; |  | 14 | readonly int[] m_map; | |
| 15 |  | 15 | |||
| 16 | public int Count { |  | 16 | public int Count { | |
| 17 | get { return m_nextId; } |  | 17 | get { return m_nextId; } | |
| @@ -1,9 +1,4 | |||||
| 1 | using |  | 1 | using System; | |
| 2 | using System; |  | |||
| 3 | using System.Collections.Generic; |  | |||
| 4 | using System.Linq; |  | |||
| 5 | using System.Text; |  | |||
| 6 | using System.Threading.Tasks; |  | |||
| 7 |  | 2 | |||
| 8 | namespace Implab.Parsing { |  | 3 | namespace Implab.Parsing { | |
| 9 | public class AltToken: BinaryToken { |  | 4 | public class AltToken: BinaryToken { | |
| @@ -7,7 +7,7 using System.Threading.Tasks; | |||||
| 7 |  | 7 | |||
| 8 | namespace Implab.Parsing { |  | 8 | namespace Implab.Parsing { | |
| 9 | public class CDFADefinition : DFADefinitionBase { |  | 9 | public class CDFADefinition : DFADefinitionBase { | |
| 10 | Alphabet m_alphabet; |  | 10 | readonly Alphabet m_alphabet; | |
| 11 |  | 11 | |||
| 12 | public Alphabet Alphabet { |  | 12 | public Alphabet Alphabet { | |
| 13 | get { return m_alphabet; } |  | 13 | get { return m_alphabet; } | |
| @@ -1,9 +1,4 | |||||
| 1 | using |  | 1 | using System; | |
| 2 | using System; |  | |||
| 3 | using System.Collections.Generic; |  | |||
| 4 | using System.Linq; |  | |||
| 5 | using System.Text; |  | |||
| 6 | using System.Threading.Tasks; |  | |||
| 7 |  | 2 | |||
| 8 | namespace Implab.Parsing { |  | 3 | namespace Implab.Parsing { | |
| 9 | public class CatToken : BinaryToken { |  | 4 | public class CatToken : BinaryToken { | |
| @@ -166,7 +166,7 namespace Implab.Parsing { | |||||
| 166 |  | 166 | |||
| 167 | int[] GetStateTags(HashSet<int> state) { |  | 167 | int[] GetStateTags(HashSet<int> state) { | |
| 168 | Debug.Assert(state != null); |  | 168 | Debug.Assert(state != null); | |
| 169 | return state.Where( |  | 169 | return state.Where(m_ends.ContainsKey).Select(pos => m_ends[pos]).ToArray(); | |
| 170 | } |  | 170 | } | |
| 171 |  | 171 | |||
| 172 | int DefineState(IDFADefinition automa, HashSet<int> state) { |  | 172 | int DefineState(IDFADefinition automa, HashSet<int> state) { | |
| @@ -15,7 +15,7 namespace Implab.Parsing { | |||||
| 15 |  | 15 | |||
| 16 | DFAStateDescriptior[] m_statesArray; |  | 16 | DFAStateDescriptior[] m_statesArray; | |
| 17 |  | 17 | |||
| 18 | p |  | 18 | protected DFADefinitionBase() { | |
| 19 | m_states = new List<DFAStateDescriptior>(); |  | 19 | m_states = new List<DFAStateDescriptior>(); | |
| 20 |  | 20 | |||
| 21 | m_states.Add(new DFAStateDescriptior()); |  | 21 | m_states.Add(new DFAStateDescriptior()); | |
| @@ -47,7 +47,7 namespace Implab.Parsing { | |||||
| 47 |  | 47 | |||
| 48 | public int AddState(int[] tag) { |  | 48 | public int AddState(int[] tag) { | |
| 49 | var index = m_states.Count; |  | 49 | var index = m_states.Count; | |
| 50 | bool final = tag |  | 50 | bool final = tag != null && tag.Length != 0; | |
| 51 | m_states.Add(new DFAStateDescriptior { |  | 51 | m_states.Add(new DFAStateDescriptior { | |
| 52 | final = final, |  | 52 | final = final, | |
| 53 | transitions = new int[AlphabetSize], |  | 53 | transitions = new int[AlphabetSize], | |
| @@ -139,7 +139,7 namespace Implab.Parsing { | |||||
| 139 |  | 139 | |||
| 140 | // строим карты соотвествия оптимальных состояний с оригинальными |  | 140 | // строим карты соотвествия оптимальных состояний с оригинальными | |
| 141 |  | 141 | |||
| 142 | var initialState = optimalStates. |  | 142 | var initialState = optimalStates.Single(x => x.Contains(INITIAL_STATE)); | |
| 143 |  | 143 | |||
| 144 | // карта получения оптимального состояния по соотвествующему ему простому состоянию |  | 144 | // карта получения оптимального состояния по соотвествующему ему простому состоянию | |
| 145 | int[] reveseOptimalMap = new int[m_states.Count]; |  | 145 | int[] reveseOptimalMap = new int[m_states.Count]; | |
| @@ -184,10 +184,8 namespace Implab.Parsing { | |||||
| 184 | foreach (var term in A) { |  | 184 | foreach (var term in A) { | |
| 185 | // ищем все переходы класса по символу term |  | 185 | // ищем все переходы класса по символу term | |
| 186 | var s2 = reveseOptimalMap[ |  | 186 | var s2 = reveseOptimalMap[ | |
| 187 | optimalMap[s].Select(x => m_states[x].transitions[term]) // все элементарн |  | 187 | optimalMap[s].Select(x => m_states[x].transitions[term]).FirstOrDefault(x => x != 0) // первое допустимое элементарное состояние, если есть | |
| 188 |  |  | 188 | ]; | |
| 189 | .FirstOrDefault() // первое допустимое элементарное состояние, если есть |  | |||
| 190 | ]; |  | |||
| 191 |  | 189 | |||
| 192 | HashSet<int> A2; |  | 190 | HashSet<int> A2; | |
| 193 | if (!classes.TryGetValue(s2, out A2)) { |  | 191 | if (!classes.TryGetValue(s2, out A2)) { | |
| @@ -37,7 +37,7 namespace Implab.Parsing { | |||||
| 37 | Debug.Assert(states != null); |  | 37 | Debug.Assert(states != null); | |
| 38 | Debug.Assert(current >= 0 && current < states.Length); |  | 38 | Debug.Assert(current >= 0 && current < states.Length); | |
| 39 | m_contextStack.Push(m_context); |  | 39 | m_contextStack.Push(m_context); | |
| 40 | m_context. |  | 40 | m_context.states = states; | |
| 41 | m_context.current = current; |  | 41 | m_context.current = current; | |
| 42 | m_context.info = info; |  | 42 | m_context.info = info; | |
| 43 | } |  | 43 | } | |
| @@ -52,5 +52,10 namespace Implab.Parsing { | |||||
| 52 | Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length); |  | 52 | Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length); | |
| 53 | m_context.current = m_context.states[m_context.current].transitions[input]; |  | 53 | m_context.current = m_context.states[m_context.current].transitions[input]; | |
| 54 | } |  | 54 | } | |
|  | 55 | ||||
|  | 56 | protected bool CanMove(int input) { | |||
|  | 57 | Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length); | |||
|  | 58 | return m_context.states[m_context.current].transitions[input] != UNREACHEBLE_STATE; | |||
|  | 59 | } | |||
| 55 | } |  | 60 | } | |
| 56 | } |  | 61 | } | |
| @@ -1,20 +1,15 | |||||
| 1 | using Implab; |  | 1 | using Implab; | |
| 2 | using System; |  | 2 | using System; | |
| 3 | using System.Collections.Generic; |  | |||
| 4 | using System.Linq; |  | |||
| 5 | using System.Text; |  | |||
| 6 | using System.Threading.Tasks; |  | |||
| 7 |  | 3 | |||
| 8 | namespace Implab.Parsing { |  | 4 | namespace Implab.Parsing { | |
| 9 | public class EDFADefinition<T> : DFADefinitionBase where T : struct, IConvertible { |  | 5 | public class EDFADefinition<T> : DFADefinitionBase where T : struct, IConvertible { | |
| 10 | EnumAlphabet<T> m_alphabet; |  | 6 | readonly EnumAlphabet<T> m_alphabet; | |
| 11 |  | 7 | |||
| 12 | public EnumAlphabet<T> Alphabet { |  | 8 | public EnumAlphabet<T> Alphabet { | |
| 13 | get { return m_alphabet; } |  | 9 | get { return m_alphabet; } | |
| 14 | } |  | 10 | } | |
| 15 |  | 11 | |||
| 16 | public EDFADefinition(EnumAlphabet<T> alphabet) |  | 12 | public EDFADefinition(EnumAlphabet<T> alphabet) { | |
| 17 | : base() { |  | |||
| 18 | Safe.ArgumentNotNull(alphabet, "alphabet"); |  | 13 | Safe.ArgumentNotNull(alphabet, "alphabet"); | |
| 19 | m_alphabet = alphabet; |  | 14 | m_alphabet = alphabet; | |
| 20 | } |  | 15 | } | |
| @@ -1,11 +1,9 | |||||
| 1 | using |  | 1 | using System; | |
| 2 | using System; |  | |||
| 3 | using System.Collections.Generic; |  | 2 | using System.Collections.Generic; | |
| 4 | using System.Diagnostics; |  | 3 | using System.Diagnostics; | |
| 5 | using System.Globalization; |  | 4 | using System.Globalization; | |
| 6 | using System.Linq; |  | 5 | using System.Linq; | |
| 7 | using System.Text; |  | 6 | using System.Diagnostics.CodeAnalysis; | |
| 8 | using System.Threading.Tasks; |  | |||
| 9 |  | 7 | |||
| 10 | namespace Implab.Parsing { |  | 8 | namespace Implab.Parsing { | |
| 11 | /// <summary> |  | 9 | /// <summary> | |
| @@ -13,10 +11,11 namespace Implab.Parsing { | |||||
| 13 | /// </summary> |  | 11 | /// </summary> | |
| 14 | /// <typeparam name="T">Тип перечислений</typeparam> |  | 12 | /// <typeparam name="T">Тип перечислений</typeparam> | |
| 15 | public class EnumAlphabet<T> : AlphabetBase<T> where T : struct, IConvertible { |  | 13 | public class EnumAlphabet<T> : AlphabetBase<T> where T : struct, IConvertible { | |
|  | 14 | [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | |||
| 16 | static readonly T[] _symbols; |  | 15 | static readonly T[] _symbols; | |
| 17 | static readonly EnumAlphabet<T> _fullAlphabet; |  | 16 | static readonly EnumAlphabet<T> _fullAlphabet; | |
| 18 |  | 17 | |||
| 19 | [S |  | 18 | [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")] | |
| 20 | static EnumAlphabet() { |  | 19 | static EnumAlphabet() { | |
| 21 | if (!typeof(T).IsEnum) |  | 20 | if (!typeof(T).IsEnum) | |
| 22 | throw new InvalidOperationException("Invalid generic parameter, enumeration is required"); |  | 21 | throw new InvalidOperationException("Invalid generic parameter, enumeration is required"); | |
| @@ -10,16 +10,8 namespace Implab { | |||||
| 10 | m_context = context; |  | 10 | m_context = context; | |
| 11 | } |  | 11 | } | |
| 12 |  | 12 | |||
| 13 | protected override void Signal |  | 13 | protected override void SignalHandler(HandlerDescriptor handler, int signal) { | |
| 14 | m_context.Post(x => base.Signal |  | 14 | m_context.Post(x => base.SignalHandler(handler, signal), null); | |
| 15 | } |  | |||
| 16 |  | ||||
| 17 | protected override void SignalError(Promise<T>.HandlerDescriptor handler, Exception error) { |  | |||
| 18 | m_context.Post(x => base.SignalError(handler, error), null); |  | |||
| 19 | } |  | |||
| 20 |  | ||||
| 21 | protected override void SignalCancelled(Promise<T>.HandlerDescriptor handler, Exception reason) { |  | |||
| 22 | m_context.Post(x => base.SignalCancelled(handler, reason), null); |  | |||
| 23 | } |  | 15 | } | |
| 24 | } |  | 16 | } | |
| 25 | } |  | 17 | } | |
| @@ -1,12 +1,5 | |||||
| 1 | using System; |  | 1 | using System; | |
| 2 | using Implab.Diagnostics; |  | |||
| 3 | using Implab.Parallels; |  | |||
| 4 | using Implab; |  | 2 | using Implab; | |
| 5 | using System.Collections.Generic; |  | |||
| 6 | using System.Collections.Concurrent; |  | |||
| 7 | using System.Threading; |  | |||
| 8 | using Implab.JSON; |  | |||
| 9 | using System.IO; |  | |||
| 10 | using System.Threading.Tasks; |  | 3 | using System.Threading.Tasks; | |
| 11 |  | 4 | |||
| 12 | namespace MonoPlay { |  | 5 | namespace MonoPlay { | |
| @@ -27,7 +20,10 namespace MonoPlay { | |||||
| 27 | } |  | 20 | } | |
| 28 |  | 21 | |||
| 29 | static IPromise<int> DoItem(int x) { |  | 22 | static IPromise<int> DoItem(int x) { | |
| 30 | return Promise<int>.FromResult(x + 1); |  | 23 | //return Promise<int>.FromResult(x + 1); | |
|  | 24 | var p = new Promise<int>(); | |||
|  | 25 | p.Resolve(x+1); | |||
|  | 26 | return p; | |||
| 31 | } |  | 27 | } | |
| 32 |  | 28 | |||
| 33 | static async Task<int> DoWork() { |  | 29 | static async Task<int> DoWork() { | |
        
        General Comments 0
    
    
  
  
                      You need to be logged in to leave comments.
                      Login now
                    
                