blob: c7b5d29092abcdcd5c9d3f5ad0492a919928ec13 (
plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package org.genivi.commonapi.core.generator
import java.util.HashMap
import java.util.List
import java.util.Stack
import org.franca.core.franca.FType
import javax.inject.Inject
class FTypeCycleDetector {
@Inject
private extension FrancaGeneratorExtensions francaGeneratorExtensions
private val indices = new HashMap<FType, Integer>
private val lowlink = new HashMap<FType, Integer>
private val stack = new Stack<FType>
private var int index
public var String outErrorString
new(FrancaGeneratorExtensions francaGeneratorExtensions) {
this.francaGeneratorExtensions = francaGeneratorExtensions
}
new() {
}
def dispatch hasCycle(FType type) {
indices.clear()
lowlink.clear()
stack.clear()
index = 0
outErrorString = type.name + "->";
return tarjan(type)
}
def dispatch hasCycle(List<FType> types) {
indices.clear()
lowlink.clear()
stack.clear()
index = 0
val typeWithCycle = types.findFirst[type|!indices.containsKey(type) && tarjan(type)]
return typeWithCycle != null
}
// Tarjan's Strongly Connected Components Algorithm
// returns true if a cycle was detected
/**
* Tarjan's Strongly Connected Components Algorithm
*
* @param type
* start searching from type.
* @return <code>true</code> if a dependency cycle was detected.
*/
def private boolean tarjan(FType type) {
indices.put(type, index)
lowlink.put(type, index)
index = index + 1
stack.push(type)
val directlyReferencedTypes = type.directlyReferencedTypes
for (referencedType : directlyReferencedTypes) {
outErrorString = outErrorString + referencedType.name + "->"
if (!indices.containsKey(referencedType)) {
if (tarjan(referencedType))
return true
lowlink.put(
type,
Math::min(lowlink.get(type), lowlink.get(referencedType))
);
} else if (stack.contains(referencedType))
lowlink.put(
type,
Math::min(lowlink.get(type), indices.get(referencedType))
);
}
// if scc root and not on top of stack, then we have a cycle (scc size > 1)
if (lowlink.get(type) == indices.get(type) && !stack.pop().equals(type)) {
outErrorString = outErrorString.subSequence(0, outErrorString.length - 2) as String
return true;
}
return false
}
}
|