rtss-core/model/link.go

75 lines
1.5 KiB
Go

package model
import (
"fmt"
"io"
)
type RR = io.Writer
// 轨道
type Link interface {
TwoPortsPipeElement
IsEquals(pla *PipeLink, plb *PipeLink) bool
}
// 轨道连接点
type LinkNode interface {
Turnout() Turnout
ThreePortsPipeElement
}
var _ Link = (*LinkImpl)(nil)
var _ LinkNode = (*LinkNodeImpl)(nil)
type LinkImpl struct {
*TwoPortsPipeElementImpl
}
type LinkNodeImpl struct {
turnout Turnout
*ThreePortsPipeElementImpl
}
func NewLink(pla *PipeLink, plb *PipeLink) *LinkImpl {
uid := buildLinkUid(pla, plb)
return &LinkImpl{
TwoPortsPipeElementImpl: &TwoPortsPipeElementImpl{
uid: uid,
},
}
}
func NewLinkNode(turnout Turnout) *LinkNodeImpl {
if turnout == nil {
panic("构造LinkNode错误: 道岔不能为空")
}
return &LinkNodeImpl{
turnout: turnout,
ThreePortsPipeElementImpl: &ThreePortsPipeElementImpl{
uid: turnout.Uid(),
},
}
}
func (l *LinkImpl) IsEquals(pla *PipeLink, plb *PipeLink) bool {
return l.uid == buildLinkUid(pla, plb) || l.uid == buildLinkUid(plb, pla)
}
func (n *LinkNodeImpl) Turnout() Turnout {
return n.turnout
}
func buildLinkUid(pla *PipeLink, plb *PipeLink) string {
if pla == nil && plb == nil {
panic("构造link Uid错误: pla和plb不能同时为空")
}
if pla == nil {
return fmt.Sprintf("end->%s(%s)", plb.Pipe.Uid(), plb.Port)
}
if plb == nil {
return fmt.Sprintf("%s(%s)->end", pla.Pipe.Uid(), pla.Port)
}
return fmt.Sprintf("%s(%s)->%s(%s)", pla.Pipe.Uid(), pla.Port, plb.Pipe.Uid(), plb.Port)
}