單一飛達分析,僅有一個 PID=-1 (根節點)。目標:找出每個設備的 ID 及其父 ID (上游設備)。
ID PID 1 -1
關係表中 Equipment=1 的 ConnectionShapes 含 -1, 2。排除上游 -1,得 2。
ID PID 1 -1 2 1
Equipment=2 關聯 1, 3, 8, 7, 10。排除上游 1,得 3, 8, 7, 10。
ID PID 1 -1 2 1 3 2 8 2 7 2 10 2
Equipment=3 關聯 2, 4, 11。排除上游 2,得 4, 11。
ID PID 1 -1 2 1 3 2 8 2 7 2 10 2 4 3 11 3
// 偽代碼
function BuildTree(rootId):
tree.Add(rootId, -1)
Queue q = new Queue()
q.Enqueue(rootId)
while q not empty:
current = q.Dequeue()
children = GetDownstreamDevices(current) // 查關係表
foreach child in children:
if child != GetUpstreamDevice(current): // 排除父節點
tree.Add(child, current)
q.Enqueue(child)
BelongTo 区分