-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
86 lines (74 loc) · 2.04 KB
/
index.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
const
EspressoLogicMinimizer = require('bindings')('EspressoLogicMinimizer'),
crypto = require('crypto'),
debug = require('debug')('espresso'),
fs = require('fs'),
tmp = require('tmp');
/**
* Applies the Espresso Heuristic Logic Minimizer algorithm to the provided
* data in PLA format
*
* @param {Array} data
* @returns {Array}
*/
function minimize(data) {
if (!Array.isArray(data)) {
throw new Error('EspressoLogicMinimizer: expected an array, got a ' + typeof data);
}
const badContent = data.filter((element) => typeof element != 'string');
if (badContent.length > 0) {
throw new Error('EspressoLogicMinimizer: incorrect data content. Only strings are supported: ' + badContent);
}
return EspressoLogicMinimizer.minimize_from_data(data);
}
/**
* @class Espresso
*/
class Espresso {
/**
* @throws
* @param {number} input - number of input variables
* @param {number} output - number of output variables
*/
constructor(inputSize, outputSize) {
this.result = null;
this.tmpfile = tmp.fileSync();
debug('TMP FILE CREATED: %s', this.tmpfile);
fs.writeSync(this.tmpfile.fd, '.i ' + inputSize + '\n');
fs.writeSync(this.tmpfile.fd, '.o ' + outputSize + '\n');
}
/**
* Pushes a truth table line into the minimizer
* @param {array.<number>} input
* @param {array.<number>} output
*/
push(input, output) {
if (this.result) {
return this.result;
}
fs.writeSync(this.tmpfile.fd,
input.reduce((p, c) => p + (c ? '1' : '0'), '')
+ ' '
+ output.reduce((p, c) => p + (c ? '1' : '0'), '')
+ '\n'
);
}
/**
* Performs the minimization
*/
minimize() {
if (this.result) {
return this.result;
}
fs.closeSync(this.tmpfile.fd);
debug('Providing file to espresso: ', this.tmpfile);
debug('EXISTS: ', fs.existsSync(this.tmpfile.name));
this.result = EspressoLogicMinimizer.minimize_from_path(this.tmpfile.name);
this.tmpfile.removeCallback();
return this.result;
}
}
module.exports = {
minimize,
Espresso
};