/
algorithms.php
150 lines (137 loc) · 3.81 KB
/
algorithms.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*$rules = array(
array('d', 'b'),
array('d', 'c'),
array('e', 'a'),
array('e', 'b'),
array('e', 'c'),
array('e', 'd'),
);*/
/*$tags = array(
0 => 'Framework',
1 => 'Angular JS',
2 => 'JavaScript',
3 => 'Single Page Applications',
4 => 'PHP',
5 => 'Behat',
6 => 'Quality Assurance',
7 => 'Testing',
8 => 'Web',
);*/
$tags = array(
0 => 'Framework',
1 => 'Angular JS',
2 => 'JavaScript',
3 => 'Single Page Applications',
4 => 'PHP',
5 => 'Behat',
6 => 'Quality Assurance',
7 => 'Testing',
8 => 'Testing Frameworks',
);
$rules = array(
1 => array(0),
);
function mergeSequences(Array $sequences) {
$merge = array();
$mergedSomething = false;
foreach ($sequences as $sequence) {
$foundExistingMerge = false;
foreach ($merge as $key => $merge_item) {
if (count(array_intersect($merge_item, $sequence))) {
$merge[$key] = array_unique(array_merge($merge_item, $sequence));
$foundExistingMerge = true;
if (!$mergedSomething) $mergedSomething = true;
break;
}
}
if (!$foundExistingMerge)
$merge[] = $sequence;
}
if ($mergedSomething) $merge = mergeSequences($merge);
return $merge;
}
function getSequences() {
global $rules;
return mergeSequences($rules);
}
function printTagTrees() {
foreach (getSequences() as $sequence) {
printTagTree($sequence);
}
}
function printTagTree($sequence) {
global $tags;
$variations = getVariations($sequence);
foreach ($variations as $variation) {
$indentation = '';
foreach ($variation as $id) {
echo $indentation, $tags[$id], PHP_EOL;
$indentation .= ' ';
}
echo PHP_EOL;
}
}
function addTag($tag) {
global $tags;
$tags[] = $tag;
return count($tags);
}
function addRule($from, $to) {
global $tags, $rules;
$fromId = array_search($from, $tags);
$toId = array_search($to, $tags);
$rules[] = array($toId, $fromId);
}
function printRules() {
global $rules;
foreach ($rules as $rule) {
echo implode('', $rule), PHP_EOL;
}
}
function printVariations($sequence) {
foreach (getVariations(str_split($sequence)) as $variation)
echo implode('', $variation), PHP_EOL;
}
function getVariations(Array $sequence, Array $head = array()) {
$variations = array();
$remainder = array_diff($sequence, $head);
if (!count($remainder)) return array($head);
foreach ($remainder as $element) {
$tail = array_diff($remainder, array($element));
$variation = array_merge($head, array($element), $tail);
if (isVariationValid($head, $element, $tail)) {
$newHead = array_merge($head, array($element));
$newVariations = getVariations($sequence, $newHead);
$variations = array_merge($variations, $newVariations);
}
}
return $variations;
}
function isVariationValid(Array $head, $element, Array $tail) {
$rulesBackwards = getRulesBackwards($element);
$rulesForward = getRulesForward($element);
$rulesInHead = array_intersect($head, $rulesBackwards);
$rulesInTail = array_intersect($tail, $rulesForward);
if (count($rulesInHead) || count($rulesInTail))
return false;
return true;
}
function getRulesBackwards($element) {
global $rules;
$rulesElements = array();
foreach ($rules as $rule) {
if ($rule[1] === $element)
$rulesElements[] = $rule[0];
}
return $rulesElements;
}
function getRulesForward($element) {
global $rules;
$rulesElements = array();
foreach ($rules as $rule) {
if ($rule[0] === $element)
$rulesElements[] = $rule[1];
}
return $rulesElements;
}