-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
200 lines (172 loc) · 4.81 KB
/
gulpfile.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
fs = require('fs'),
readline = require('readline');
gulp.task('default', ['lint'], function() {
var version = require('./package.json').version;
return gulp.src([
'vendor/*.js',
'src/Controls.js',
'src/LayoutEngine.js',
'src/Visualization.js',
'src/Hypergraph.js'
])
.pipe(uglify())
.pipe(concat('abdv-' + version + '.min.js'))
.pipe(gulp.dest('dist'))
});
gulp.task('lint', function() {
return gulp.src([
'src/Controls.js',
'src/LayoutEngine.js',
'src/Visualization.js',
'src/Hypergraph.js'
])
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('convert', function() {
var MAX_ITEMSET_COUNT = 8;
var cannedItemsets = 0;
fs.readdir('./data', function(err, files) {
if (err) throw err;
var graph = [];
// Filters dot files and other stuff from files array
var dataFiles = files.filter(function(file) {
return (fs.lstatSync('./data/' + file).isFile() && file.charAt(0) !== '.')
});
dataFiles.forEach(function(file, i) {
// Create line reader
var reader = readline.createInterface({
input: fs.createReadStream('./data/' + file)
});
console.log('Reading ' + file + ' (' + i + ')');
reader.on('line', function(line) {
var newItemset = readItemset(line, i);
var existingItemset = find(graph, newItemset);
if (!existingItemset) {
// For proper test data, limit the number of itemsets in the graph.
if (graph.length < MAX_ITEMSET_COUNT) {
// If itemset is not already in the graph and the it's not in the first interval file,
// the interval-steps before this one need to be mocked (set to zero).
if (i > 0) {
console.log('New itemset found (which is not in the first interval).');
var frequency = newItemset[newItemset.length-1];
var interval = frequency.intervals[0];
frequency.intervals = [];
for (var j = 0; j < i; j++) {
frequency.intervals.push({
percentage: 0,
label: null
})
}
frequency.intervals.push(interval);
}
graph.push(newItemset);
} else {
cannedItemsets++;
}
} else {
var newFrequency = newItemset[newItemset.length - 1];
var existingFrequency = existingItemset[existingItemset.length - 1];
existingFrequency.intervals.push(newFrequency.intervals[0]);
}
});
if (i === dataFiles.length - 1) {
reader.on('close', function() {
write(graph);
console.log(cannedItemsets + ' itemsets were not converted for the POC data.');
});
}
});
});
});
gulp.task('watch', function() {
gulp.watch('src/*.js', ['default']);
});
/**
* Reads an itemset from an input line.
* @param line string
* @param step interval step
* @returns {Array} Transformed itemset
*/
function readItemset(line, step) {
var itemset = [],
rawAttributes = line.split(' ');
for (var i = 0; i < rawAttributes.length - 1; i++) {
var attr = rawAttributes[i].split(',');
var id = attr[0];
var label = attr[1].replace(/\"/g, '');
itemset.push({
type: 'attribute',
id: id,
label: label
});
}
var rawFrequency = rawAttributes[rawAttributes.length - 1].replace('(', '').replace(')', '');
var splitted = rawFrequency.split(',');
var percentage = parseFloat(splitted[0]).toFixed(2);
var id = parseInt(splitted[1]);
itemset.push({
type: 'frequency',
id: id,
label: id,
intervals: [
{
percentage: parseFloat(percentage),
label: id
}
]
});
return itemset;
};
/**
* Helper function to write graph-array to JSON file.
* @param graph
*/
function write(graph) {
fs.writeFile('data.json', JSON.stringify(graph), function(err) {
if (err) return console.log(err);
console.log('Data converted in data.json');
});
};
/**
* Helper Function to find an existing itemset in the graph.
* @param graph array
* @param newItemset array
* @returns {array|false} Found itemset or false if not found.
*/
function find(graph, newItemset) {
// If graph is empty, it doesn't contain the new itemset
if (graph.length === 0) {
return false;
}
// Create a flat array of ID from new itemset
var idList = newItemset.filter(function(el) {
return el.type === 'attribute';
}).map(function(el) {
return el.id;
});
// loop over all itemsets
graphloop:
for (var i = 0; i < graph.length; i++) {
var itemset = graph[i];
// filter items (no frequency)
var items = itemset.filter(function(item) { return item.type === 'attribute'; });
// If the length is not the same, it's no match.
if (items.length !== idList.length) {
continue graphloop;
}
// loop over all items
for (var j = 0; j < items.length; j++) {
var item = items[j];
if (idList.indexOf(item.id) === -1) {
continue graphloop;
}
return itemset;
}
}
return false;
};