model定义

This commit is contained in:
soul-walker 2024-06-17 19:45:38 +08:00
parent 74d0141087
commit 7a13df1b0d
4 changed files with 175 additions and 10 deletions

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}