C# 如何在C语言中实现带自动机的状态机#
我正在尝试为使用Automatonymous和RabbitMQ的状态机实现一个简单的示例/演示。不幸的是,我找不到一个可以重建/学习的方法(我找到了一个,但在我看来它一点都不简单)。在我看来,文件也缺乏信息 这是我想到的状态机示例(对不起,它很难看): 请注意,这个例子完全是虚构的,它是否有意义并不重要。这个项目的目的是通过自动化来“取暖” 我想做/想要做的是:C# 如何在C语言中实现带自动机的状态机#,c#,rabbitmq,state-machine,masstransit,automatonymous,C#,Rabbitmq,State Machine,Masstransit,Automatonymous,我正在尝试为使用Automatonymous和RabbitMQ的状态机实现一个简单的示例/演示。不幸的是,我找不到一个可以重建/学习的方法(我找到了一个,但在我看来它一点都不简单)。在我看来,文件也缺乏信息 这是我想到的状态机示例(对不起,它很难看): 请注意,这个例子完全是虚构的,它是否有意义并不重要。这个项目的目的是通过自动化来“取暖” 我想做/想要做的是: 正在运行的四个应用程序: 国家机器本身 发送要解释的请求的“请求者” “验证器”或“解析器”检查所提供的请求是否有效 解释给定请求的
- 正在运行的四个应用程序:
- 国家机器本身
- 发送要解释的请求的“请求者”
- “验证器”或“解析器”检查所提供的请求是否有效
- 解释给定请求的“解释器”
- 例如:
- 请求者发送“x=5”
- 验证程序检查是否包含“=”
- 英特普雷特说“5”
public class InterpreterStateMachine : MassTransitStateMachine<InterpreterInstance>
{
public InterpreterStateMachine()
{
InstanceState(x => x.CurrentState);
Event(() => Requesting, x => x.CorrelateBy(request => request.Request.RequestString, context => context.Message.Request.RequestString)
.SelectId(context => Guid.NewGuid()));
Event(() => Validating, x => x.CorrelateBy(request => request.Request.RequestString, context => context.Message.Request.RequestString));
Event(() => Interpreting, x => x.CorrelateBy(request => request.Request.RequestString, context => context.Message.Request.RequestString));
Initially(
When(Requesting)
.Then(context =>
{
context.Instance.Request = new Request(context.Data.Request.RequestString);
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Request received: {context.Data.Request.RequestString}"))
.Publish(context => new ValidationNeededEvent(context.Instance))
.TransitionTo(Requested)
);
During(Requested,
When(Validating)
.Then(context =>
{
context.Instance.Request.IsValid = context.Data.Request.IsValid;
if (!context.Data.Request.IsValid)
{
this.TransitionToState(context.Instance, Error);
}
else
{
this.TransitionToState(context.Instance, RequestValid);
}
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Request '{context.Data.Request.RequestString}' validated with {context.Instance.Request.IsValid}"))
.Publish(context => new InterpretationNeededEvent(context.Instance))
,
Ignore(Requesting),
Ignore(Interpreting)
);
During(RequestValid,
When(Interpreting)
.Then((context) =>
{
//do something
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Request '{context.Data.Request.RequestString}' interpreted with {context.Data.Answer}"))
.Publish(context => new AnswerReadyEvent(context.Instance))
.TransitionTo(AnswerReady)
.Finalize(),
Ignore(Requesting),
Ignore(Validating)
);
SetCompletedWhenFinalized();
}
public State Requested { get; private set; }
public State RequestValid { get; private set; }
public State AnswerReady { get; private set; }
public State Error { get; private set; }
//Someone is sending a request to interprete
public Event<IRequesting> Requesting { get; private set; }
//Request is validated
public Event<IValidating> Validating { get; private set; }
//Request is interpreted
public Event<IInterpreting> Interpreting { get; private set; }
class ValidationNeededEvent : IValidationNeeded
{
readonly InterpreterInstance _instance;
public ValidationNeededEvent(InterpreterInstance instance)
{
_instance = instance;
}
public Guid RequestId => _instance.CorrelationId;
public Request Request => _instance.Request;
}
class InterpretationNeededEvent : IInterpretationNeeded
{
readonly InterpreterInstance _instance;
public InterpretationNeededEvent(InterpreterInstance instance)
{
_instance = instance;
}
public Guid RequestId => _instance.CorrelationId;
}
class AnswerReadyEvent : IAnswerReady
{
readonly InterpreterInstance _instance;
public AnswerReadyEvent(InterpreterInstance instance)
{
_instance = instance;
}
public Guid RequestId => _instance.CorrelationId;
}
}
public class RequestService : ServiceControl
{
readonly IScheduler scheduler;
IBusControl busControl;
BusHandle busHandle;
InterpreterStateMachine machine;
InMemorySagaRepository<InterpreterInstance> repository;
public RequestService()
{
scheduler = CreateScheduler();
}
public bool Start(HostControl hostControl)
{
Console.WriteLine("Creating bus...");
machine = new InterpreterStateMachine();
repository = new InMemorySagaRepository<InterpreterInstance>();
busControl = Bus.Factory.CreateUsingRabbitMq(x =>
{
IRabbitMqHost host = x.Host(new Uri(/*rabbitMQ server*/), h =>
{
/*credentials*/
});
x.UseInMemoryScheduler();
x.ReceiveEndpoint(host, "interpreting_answer", e =>
{
e.PrefetchCount = 5; //?
e.StateMachineSaga(machine, repository);
});
x.ReceiveEndpoint(host, "2", e =>
{
e.PrefetchCount = 1;
x.UseMessageScheduler(e.InputAddress);
//Scheduling !?
e.Consumer(() => new ScheduleMessageConsumer(scheduler));
e.Consumer(() => new CancelScheduledMessageConsumer(scheduler));
});
});
Console.WriteLine("Starting bus...");
try
{
busHandle = MassTransit.Util.TaskUtil.Await<BusHandle>(() => busControl.StartAsync());
scheduler.JobFactory = new MassTransitJobFactory(busControl);
scheduler.Start();
}
catch (Exception)
{
scheduler.Shutdown();
throw;
}
return true;
}
public bool Stop(HostControl hostControl)
{
Console.WriteLine("Stopping bus...");
scheduler.Standby();
if (busHandle != null) busHandle.Stop();
scheduler.Shutdown();
return true;
}
static IScheduler CreateScheduler()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = MassTransit.Util.TaskUtil.Await<IScheduler>(() => schedulerFactory.GetScheduler()); ;
return scheduler;
}
}
公共类解释器状态机:MassTransitStateMachine
{
公共解释器状态机()
{
InstanceState(x=>x.CurrentState);
事件(()=>requising,x=>x.CorrelateBy(request=>request.request.RequestString,context=>context.Message.request.RequestString)
.SelectId(上下文=>Guid.NewGuid());
事件(()=>Validating,x=>x.CorrelateBy(request=>request.request.RequestString,context=>context.Message.request.RequestString));
事件(()=>解释,x=>x.CorrelateBy(request=>request.request.RequestString,context=>context.Message.request.RequestString));
最初(
何时(请求)
。然后(上下文=>
{
context.Instance.Request=新请求(context.Data.Request.RequestString);
})
.ThenAsync(context=>Console.Out.WriteLineAsync($“收到的请求:{context.Data.Request.RequestString}”))
.Publish(context=>newvalidationneedEvent(context.Instance))
.转换(请求)
);
在(请求的)期间,
何时(验证)
。然后(上下文=>
{
context.Instance.Request.IsValid=context.Data.Request.IsValid;
如果(!context.Data.Request.IsValid)
{
this.transitionState(context.Instance,Error);
}
其他的
{
this.transitionState(context.Instance,RequestValid);
}
})
.ThenAsync(context=>Console.Out.WriteLineAsync($“请求”{context.Data.Request.RequestString}已用{context.Instance.Request.IsValid}验证”))
.Publish(context=>newinterpretationneedevent(context.Instance))
,
忽略(请求),
忽略(口译)
);
在(请求)期间,
何时(口译)
.然后((上下文)=>
{
//做点什么
})
.ThenAsync(context=>Console.Out.WriteLineAsync($“请求”{context.Data.Request.RequestString}用{context.Data.Answer}解释”))
.Publish(context=>newanswerreadyevent(context.Instance))
.转换到(应答准备就绪)
.Finalize(),
忽略(请求),
忽略(验证)
);
SetCompletedWhenFinalized();
}
请求的公共状态{get;private set;}
公共状态请求有效{get;private set;}
公共状态应答就绪{get;private set;}
公共状态错误{get;private set;}
//有人正在向口译员发送请求
请求{get;private set;}的公共事件
//请求已验证
公共事件验证{get;private set;}
//请求被解释
公共事件解释{get;private set;}
类ValidationNeedEvent:IValidationNeeded
{
只读解释器实例;
公共ValidationNeededEvent(解释器实例)
{
_实例=实例;
}
公共Guid RequestId=>\u instance.CorrelationId;
公共请求请求=>\u instance.Request;
}
类解释NeededEvent:需要I解释
{
只读解释器实例;
公共解释NeededEvent(解释实例)
{
_实例=实例;
}
公共Guid RequestId=>\u instance.CorrelationId;
}
班级答疑人事件:IAnswerReady
{
只读解释器实例;
公共应答ReadyEvent(解释器实例)
{
_实例=实例;
}
公共Guid RequestId=>\u instance.CorrelationId;
}
}
然后我有这样的服务:
public class InterpreterStateMachine : MassTransitStateMachine<InterpreterInstance>
{
public InterpreterStateMachine()
{
InstanceState(x => x.CurrentState);
Event(() => Requesting, x => x.CorrelateBy(request => request.Request.RequestString, context => context.Message.Request.RequestString)
.SelectId(context => Guid.NewGuid()));
Event(() => Validating, x => x.CorrelateBy(request => request.Request.RequestString, context => context.Message.Request.RequestString));
Event(() => Interpreting, x => x.CorrelateBy(request => request.Request.RequestString, context => context.Message.Request.RequestString));
Initially(
When(Requesting)
.Then(context =>
{
context.Instance.Request = new Request(context.Data.Request.RequestString);
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Request received: {context.Data.Request.RequestString}"))
.Publish(context => new ValidationNeededEvent(context.Instance))
.TransitionTo(Requested)
);
During(Requested,
When(Validating)
.Then(context =>
{
context.Instance.Request.IsValid = context.Data.Request.IsValid;
if (!context.Data.Request.IsValid)
{
this.TransitionToState(context.Instance, Error);
}
else
{
this.TransitionToState(context.Instance, RequestValid);
}
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Request '{context.Data.Request.RequestString}' validated with {context.Instance.Request.IsValid}"))
.Publish(context => new InterpretationNeededEvent(context.Instance))
,
Ignore(Requesting),
Ignore(Interpreting)
);
During(RequestValid,
When(Interpreting)
.Then((context) =>
{
//do something
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Request '{context.Data.Request.RequestString}' interpreted with {context.Data.Answer}"))
.Publish(context => new AnswerReadyEvent(context.Instance))
.TransitionTo(AnswerReady)
.Finalize(),
Ignore(Requesting),
Ignore(Validating)
);
SetCompletedWhenFinalized();
}
public State Requested { get; private set; }
public State RequestValid { get; private set; }
public State AnswerReady { get; private set; }
public State Error { get; private set; }
//Someone is sending a request to interprete
public Event<IRequesting> Requesting { get; private set; }
//Request is validated
public Event<IValidating> Validating { get; private set; }
//Request is interpreted
public Event<IInterpreting> Interpreting { get; private set; }
class ValidationNeededEvent : IValidationNeeded
{
readonly InterpreterInstance _instance;
public ValidationNeededEvent(InterpreterInstance instance)
{
_instance = instance;
}
public Guid RequestId => _instance.CorrelationId;
public Request Request => _instance.Request;
}
class InterpretationNeededEvent : IInterpretationNeeded
{
readonly InterpreterInstance _instance;
public InterpretationNeededEvent(InterpreterInstance instance)
{
_instance = instance;
}
public Guid RequestId => _instance.CorrelationId;
}
class AnswerReadyEvent : IAnswerReady
{
readonly InterpreterInstance _instance;
public AnswerReadyEvent(InterpreterInstance instance)
{
_instance = instance;
}
public Guid RequestId => _instance.CorrelationId;
}
}
public class RequestService : ServiceControl
{
readonly IScheduler scheduler;
IBusControl busControl;
BusHandle busHandle;
InterpreterStateMachine machine;
InMemorySagaRepository<InterpreterInstance> repository;
public RequestService()
{
scheduler = CreateScheduler();
}
public bool Start(HostControl hostControl)
{
Console.WriteLine("Creating bus...");
machine = new InterpreterStateMachine();
repository = new InMemorySagaRepository<InterpreterInstance>();
busControl = Bus.Factory.CreateUsingRabbitMq(x =>
{
IRabbitMqHost host = x.Host(new Uri(/*rabbitMQ server*/), h =>
{
/*credentials*/
});
x.UseInMemoryScheduler();
x.ReceiveEndpoint(host, "interpreting_answer", e =>
{
e.PrefetchCount = 5; //?
e.StateMachineSaga(machine, repository);
});
x.ReceiveEndpoint(host, "2", e =>
{
e.PrefetchCount = 1;
x.UseMessageScheduler(e.InputAddress);
//Scheduling !?
e.Consumer(() => new ScheduleMessageConsumer(scheduler));
e.Consumer(() => new CancelScheduledMessageConsumer(scheduler));
});
});
Console.WriteLine("Starting bus...");
try
{
busHandle = MassTransit.Util.TaskUtil.Await<BusHandle>(() => busControl.StartAsync());
scheduler.JobFactory = new MassTransitJobFactory(busControl);
scheduler.Start();
}
catch (Exception)
{
scheduler.Shutdown();
throw;
}
return true;
}
public bool Stop(HostControl hostControl)
{
Console.WriteLine("Stopping bus...");
scheduler.Standby();
if (busHandle != null) busHandle.Stop();
scheduler.Shutdown();
return true;
}
static IScheduler CreateScheduler()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = MassTransit.Util.TaskUtil.Await<IScheduler>(() => schedulerFactory.GetScheduler()); ;
return scheduler;
}
}
公共类请求服务:ServiceControl
{
只读IScheduler调度器;
IBusControl总线控制;
布山德尔;
翻译机;
内存存储库;
公共请求服务()
{
调度器=CreateScheduler();
}
公用bool启动(主机控制主机控制)
{
Console.WriteLine(“创建总线…”);
...
InterpreterStateMachine _machine = new InterpreterStateMachine();
InMemorySagaRepository<InterpreterInstance> _repository = new InMemorySagaRepository<InterpreterInstance>();
...
x.ReceiveEndpoint(host, "state_machine", e =>
{
e.PrefetchCount = 1;
//here the state machine is set
e.StateMachineSaga(_machine, _repository);
e.Durable = false;
});
using System;
namespace InterpreterStateMachine.Contracts
{
public interface IValidationNeeded
{
Guid RequestId { get; }
Request Request { get; }
}
}