Guest User

Untitled

a guest
Aug 19th, 2019
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { createInterface } = require('readline');
  2. const { createReadStream } = require('fs');
  3.  
  4. async function parse(path) {
  5. let fileName = path.substring(path.lastIndexOf('/') + 1, path.indexOf('.'));
  6. let benchmark = {
  7. matrix: {}
  8. };
  9.  
  10. let objectiveFunction = await readObjectiveFunction(path);
  11. // benchmark[fileName].lastColumn = await readLastColumns(path);
  12. benchmark.matrix = await readConstraints(path, objectiveFunction);
  13.  
  14. return benchmark;
  15. }
  16.  
  17. function readLastColumns(path) {
  18. let values = [];
  19. let value;
  20.  
  21. let lineReader = createInterface({
  22. input: createReadStream(path)
  23. });
  24.  
  25. return new Promise(resolve => {
  26. lineReader.on('line', line => {
  27. if (line.includes('>=')) {
  28. value = line.substring(line.indexOf('>=') + 3, line.indexOf(';'));
  29. values.push(Number(value));
  30. }
  31. });
  32. lineReader.on('close', () => {
  33. resolve(values);
  34. });
  35. });
  36. }
  37.  
  38. function readObjectiveFunction(path) {
  39. let objectiveFunction = [];
  40. let singleLine;
  41.  
  42. let lineReader = createInterface({
  43. input: createReadStream(path)
  44. });
  45. return new Promise(resolve => {
  46. lineReader.on('line', line => {
  47. if (line.includes('min:')) {
  48. singleLine = line.substring(line.indexOf(':') + 3, line.indexOf(';'));
  49. singleLine = singleLine.replace(/\*x[0-9]{1,2}/g, '');
  50. objectiveFunction.push(singleLine);
  51. objectiveFunction = singleLine
  52. .trim()
  53. .split('+')
  54. .map(element => Number(element));
  55. objectiveFunction.push(0);
  56. }
  57. });
  58. lineReader.on('close', () => {
  59. resolve(objectiveFunction);
  60. });
  61. });
  62. }
  63.  
  64. function readConstraints(path, objectiveFunction) {
  65. let matrix = [];
  66. let singleLine;
  67. let lineReader = createInterface({
  68. input: createReadStream(path)
  69. });
  70. return new Promise(resolve => {
  71. lineReader.on('line', line => {
  72. if (line.includes('>=')) {
  73. singleLine = line.substring(line.indexOf('+') + 1, line.indexOf(';'));
  74. singleLine = singleLine.replace('>=', '+');
  75. singleLine = singleLine.replace(/\*x[0-9]{1,2}/g, '');
  76. singleLine = singleLine
  77. .trim()
  78. .split('+')
  79. .map(element => Number(element));
  80. matrix.push(singleLine);
  81. }
  82. });
  83. lineReader.on('close', () => {
  84. matrix.push(objectiveFunction);
  85. resolve(matrix);
  86. });
  87. });
  88. }
  89.  
  90. module.exports = parse;
RAW Paste Data