|
@@ -0,0 +1,68 @@
|
|
|
|
+package ruleEngine
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "sparrow/pkg/actor"
|
|
|
|
+ "time"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type Context interface {
|
|
|
|
+ // 向所有基于success类型的关系节点发消息
|
|
|
|
+ TellSuccess(msg *Message)
|
|
|
|
+ // 基于某个关系发消息
|
|
|
|
+ TellNext(msg *Message, relationType RelationType)
|
|
|
|
+ // 向当前节点发消息,duration 为延迟时间
|
|
|
|
+ TellSelf(msg *Message, duration time.Duration)
|
|
|
|
+ // 发送错误消息消息
|
|
|
|
+ TellError(msg *Message, err error)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// DefaultContext 默认的上下文
|
|
|
|
+type DefaultContext struct {
|
|
|
|
+ nodeCtx *RuleNodeCtx
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func New(nodeCtx *RuleNodeCtx) *DefaultContext {
|
|
|
|
+ return &DefaultContext{nodeCtx: nodeCtx}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (d *DefaultContext) TellSuccess(msg *Message) {
|
|
|
|
+ d.tellNext(msg, []RelationType{Success}, nil)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (d *DefaultContext) TellNext(msg *Message, relationType RelationType) {
|
|
|
|
+ d.tellNext(msg, []RelationType{relationType}, nil)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (d *DefaultContext) tellNext(msg *Message, relationTypes []RelationType, err error) {
|
|
|
|
+ if d.nodeCtx.self.IsDebug {
|
|
|
|
+ // TODO: 输出调试日志
|
|
|
|
+ }
|
|
|
|
+ msg.GetCallBack().onProcessingEnd(d.nodeCtx.self.RuleNodeId)
|
|
|
|
+ d.nodeCtx.chainActor.Tell(
|
|
|
|
+ &actor.RuleNodeToRuleChanTellNextMsg{
|
|
|
|
+ RuleNodeId: d.nodeCtx.self.RuleNodeId,
|
|
|
|
+ RelationTypes: relationTypes,
|
|
|
|
+ Message: msg,
|
|
|
|
+ FailureMessage: err,
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (d *DefaultContext) TellSelf(msg *Message, duration time.Duration) {
|
|
|
|
+ if duration > 0 {
|
|
|
|
+ time.AfterFunc(duration, func() {
|
|
|
|
+ d.nodeCtx.selfActor.Tell(&actor.RuleToSelfMsg{Message: msg})
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (d *DefaultContext) TellError(msg *Message, err error) {
|
|
|
|
+ if d.nodeCtx.self.IsDebug {
|
|
|
|
+ // TODO: 处理调试
|
|
|
|
+ }
|
|
|
|
+ d.nodeCtx.chainActor.Tell(&actor.RuleNodeToRuleChanTellNextMsg{
|
|
|
|
+ RuleNodeId: d.nodeCtx.self.RuleNodeId,
|
|
|
|
+ RelationTypes: []RelationType{Failure},
|
|
|
|
+ Message: msg,
|
|
|
|
+ FailureMessage: err,
|
|
|
|
+ })
|
|
|
|
+}
|