From 7a13df1b0dd508c7f0bc4254522236729d8dd845 Mon Sep 17 00:00:00 2001 From: soul-walker Date: Mon, 17 Jun 2024 19:45:38 +0800 Subject: [PATCH] =?UTF-8?q?model=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/link.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ model/model.go | 28 ++++++++++------ model/section.go | 31 +++++++++++++++++ model/turnout.go | 39 ++++++++++++++++++++++ 4 files changed, 175 insertions(+), 10 deletions(-) diff --git a/model/link.go b/model/link.go index c0f41d2..84867c5 100644 --- a/model/link.go +++ b/model/link.go @@ -1,5 +1,92 @@ package model +import "io" + +type RR = io.Writer + +// 轨道 type Link interface { RtssModel + PipeElement +} + +// 轨道连接点 +type LinkNode interface { + Turnout() Turnout + PipeElement +} + +type LinkImpl struct { + uid string + paLinkNode *PipeLink + pbLinkNode *PipeLink +} + +type LinkNodeImpl struct { + turnout Turnout + paPipeLink *PipeLink + pbPipeLink *PipeLink + pcPipeLink *PipeLink +} + +func NewLink(nodea LinkNode, nodeb LinkNode) Link { + uid := buildLinkUid(nodea, nodeb) + return &LinkImpl{ + uid: uid, + } +} + +func NewLinkNode(turnout Turnout) LinkNode { + return &LinkNodeImpl{ + turnout: turnout, + } +} + +func (l *LinkImpl) Uid() string { + return l.uid +} + +func (l *LinkImpl) Ports() []PipePort { + return twoPorts +} + +func (l *LinkImpl) GetLinkedElement(port PipePort) *PipeLink { + if port == PipePortA { + return l.paLinkNode + } else if port == PipePortB { + return l.pbLinkNode + } + return nil +} + +func (n *LinkNodeImpl) Turnout() Turnout { + return n.turnout +} + +func (n *LinkNodeImpl) Ports() []PipePort { + return threePorts +} + +func (n *LinkNodeImpl) GetLinkedElement(port PipePort) *PipeLink { + if port == PipePortA { + return n.paPipeLink + } else if port == PipePortB { + return n.pbPipeLink + } else if port == PipePortC { + return n.pcPipeLink + } + return nil +} + +func buildLinkUid(nodea LinkNode, nodeb LinkNode) string { + if nodea == nil && nodeb == nil { + panic("构造link Uid错误: nodea和nodeb不能同时为空") + } + if nodea == nil { + return "->" + nodeb.Turnout().Uid() + } + if nodeb == nil { + return nodea.Turnout().Uid() + "->" + } + return nodea.Turnout().Uid() + "-" + nodeb.Turnout().Uid() } diff --git a/model/model.go b/model/model.go index c002583..44e518b 100644 --- a/model/model.go +++ b/model/model.go @@ -5,27 +5,35 @@ type RtssModel interface { Uid() string } -// 轨道端口 +// 通道端口 type PipePort = string var ( - // PipePortA 轨道端口A + // PipePortA 通道端口A PipePortA PipePort = "A" - // PipePortB 轨道端口B + // PipePortB 通道端口B PipePortB PipePort = "B" - // PipePortC 轨道端口C + // PipePortC 通道端口C PipePortC PipePort = "C" ) +var ( + twoPorts = []PipePort{PipePortA, PipePortB} + threePorts = []PipePort{PipePortA, PipePortB, PipePortC} +) + +// 通道连接关系 type PipeLink struct { - // 轨道 - Pipe Pipe + // 通道元素 + Pipe PipeElement // 端口 Port PipePort } -// 轨道 -type Pipe interface { - // 获取指定端口连接的下一个轨道及端口 - Next(port PipePort) PipeLink +// 通道元素 +type PipeElement interface { + // 获取通道端口 + Ports() []PipePort + // 获取指定端口连接的通道连接关系 + GetLinkedElement(port PipePort) *PipeLink } diff --git a/model/section.go b/model/section.go index bb01cfb..506ec2e 100644 --- a/model/section.go +++ b/model/section.go @@ -1,5 +1,36 @@ package model +// 区段 type Section interface { RtssModel + PipeElement +} + +type SectionImpl struct { + uid string + paPipeLink *PipeLink + pbPipeLink *PipeLink +} + +func NewSection(uid string) Section { + return &SectionImpl{ + uid: uid, + } +} + +func (s *SectionImpl) Uid() string { + return s.uid +} + +func (s *SectionImpl) Ports() []PipePort { + return twoPorts +} + +func (s *SectionImpl) GetLinkedElement(port PipePort) *PipeLink { + if port == PipePortA { + return s.paPipeLink + } else if port == PipePortB { + return s.pbPipeLink + } + return nil } diff --git a/model/turnout.go b/model/turnout.go index 4ffc2bd..c1c239b 100644 --- a/model/turnout.go +++ b/model/turnout.go @@ -1,5 +1,44 @@ package model +import "strings" + +// 道岔 type Turnout interface { RtssModel + PipeElement +} + +type TurnoutImpl struct { + uid string + paPipeLink *PipeLink + pbPipeLink *PipeLink + pcPipeLink *PipeLink +} + +func NewTurnout(uid string) Turnout { + if strings.Trim(uid, " ") == "" { + panic("Turnout uid is empty") + } + return &TurnoutImpl{ + uid: uid, + } +} + +func (t *TurnoutImpl) Uid() string { + return t.uid +} + +func (t *TurnoutImpl) Ports() []PipePort { + return threePorts +} + +func (t *TurnoutImpl) GetLinkedElement(port PipePort) *PipeLink { + if port == PipePortA { + return t.paPipeLink + } else if port == PipePortB { + return t.pbPipeLink + } else if port == PipePortC { + return t.pcPipeLink + } + return nil }