This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
util.go
60 lines (50 loc) · 1.7 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package core
// Sort will return a sorted list of config.Agent,
// it sorts agents by computing links dependencies between them
//
// use sortOrder param config.SortInputsFirst to get agents which are not waiting events (no sources) firstly (like inputs)
//
// use sortOrder param config.SortOutputsFirst to get agents which are not sources of any other agents firstly (like outputs)
func Sort(agentConflist map[int]*Agent, sortOrder int) []*Agent {
sac := []*Agent{}
// sac = append(sac, agentConflist...)
var agentsDependencyGraph = graph{}
for _, agentConfiguration := range agentConflist {
agentsDependencyGraph[agentConfiguration.ID] = func() []int {
sources := []int{}
for _, port := range agentConfiguration.AgentSources {
sources = append(sources, port.AgentID)
}
return sources
}()
}
order, _ := topSortDFS(agentsDependencyGraph)
if sortOrder == SortInputsFirst {
order = reverseList(order)
}
for _, n := range order {
for _, agentConf := range agentConflist {
if agentConf.ID == n {
sac = append(sac, agentConf)
}
}
}
return sac
}
// WhoWaitForThisAgentName returns agents recipients as portList
func whoWaitForThisAgentID(ID int, agentConfigurations map[int]*Agent) PortList {
var recipentAgents = PortList{}
for _, agentConfiguration := range agentConfigurations {
for _, sourceAgentPort := range agentConfiguration.AgentSources {
if sourceAgentPort.AgentID == ID { // ok on a un agent qui se source sur ID
for _, v := range agentConfiguration.AgentSources {
if ID != v.AgentID {
continue
}
recipentAgents = append(recipentAgents, Port{AgentID: agentConfiguration.ID, PortNumber: v.PortNumber})
}
}
}
}
return recipentAgents
}