rtss-core/model/link.go

96 lines
1.6 KiB
Go

package model
import "io"
type RR = io.Writer
// 轨道
type Link interface {
RtssModel
PipeElement
}
// 轨道连接点
type LinkNode interface {
Turnout() Turnout
PipeElement
}
var _ Link = (*LinkImpl)(nil)
var _ LinkNode = (*LinkNodeImpl)(nil)
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()
}