Name
global.ProcessUtils
Description
ProcessUtils for AFP uses
Script
var ProcessUtils = Class.create();
ProcessUtils.prototype = {
initialize: function() {
this.LOGIC_WILDCARD = "(.*)";
},
initMatrix: function(rows, cols, defaultValue) {
var arr = [];
// Creates all lines:
for (var i = 0; i < rows; i++) {
// Creates an empty line
arr.push([]);
// Adds cols to the empty line:
arr[i].push(new Array(cols));
for (var j = 0; j < cols; j++) {
// Initializes:
arr[i][j] = defaultValue;
}
}
return arr;
},
levenshteinDistance: function(s, t) {
var n = s.length;
var m = t.length;
var d = this.initMatrix(n + 1, m + 1, 0);
if (n == 0) return m;
if (m == 0) return n;
var i;
var j;
for (i = 0; i <= m; i++) {
d[i] = [i];
}
// increment each column in the first row
for (j = 0; j <= n; j++) {
d[0][j] = j;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
// Set cost to 0 when words match
var cost = s[i - 1] == (t[j - 1]) ? 0 : 1;
// Minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i][j] = this.minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
}
}
return d;
},
levenshteinDistanceAlign: function(s, t) {
var n = s.length;
var m = t.length;
if (n == 0) return this.LOGIC_WILDCARD;
if (m == 0) return this.LOGIC_WILDCARD;
var d = this.levenshteinDistance(s, t);
var list = [];
var i = n;
var j = m;
// Backtrace using the path with minimum values
while (i > 0 && j > 0) {
var minimum = this.minimum(d[i - 1][j], d[i][j - 1], d[i - 1][j - 1]);
if (minimum == d[i - 1][j - 1]) {
if (s[i - 1] == (t[j - 1])) {
list.push(s[i - 1]);
} else {
this.addWildcart(list);
}
i--;
j--;
} else if (minimum == d[i][j - 1]) {
j--;
this.addWildcart(list);
} else {
i--;
this.addWildcart(list);
}
}
if (i > 0 || j > 0) {
this.addWildcart(list);
}
var res = '';
var prefix = "";
for (i = list.length - 1; i >= 0; i--) {
res += prefix;
res += list[i];
prefix = " ";
}
return res.replace(/\s+/g, '');
},
minimum: function(a, b, c) {
var mi = a;
if (b < mi) mi = b;
if (c < mi) mi = c;
return mi;
},
addWildcart: function(list) {
if (list.length > 0 && list[list.length - 1] == this.LOGIC_WILDCARD) {
return;
}
list.push(this.LOGIC_WILDCARD);
},
tokenize: function(input) {
var DELIM = "\\.:/[]\\";
input = input.replace(/\W+/g, " "); // Replace all non-word characters with spaces
var tokens = input.split(new RegExp("[" + DELIM + "]+")); // Split the input string using the delimiter characters
return tokens.join(" "); // Join the tokens with spaces and return the resulting string
},
prepareProcessRegex: function(regexSuggestionBase) {
if (!regexSuggestionBase) {
return "";
}
var maxRegexSize = 55;
if (regexSuggestionBase.length > maxRegexSize) {
regexSuggestionBase = regexSuggestionBase.substring(0, maxRegexSize) + ".*";
}
var regexSuggestion = regexSuggestionBase.replace(/\s+/g, ".*");
if (!regexSuggestion.startsWith("(.*)")) {
regexSuggestion = ".*" + regexSuggestion;
}
if (!regexSuggestion.endsWith("(.*)")) {
regexSuggestion = regexSuggestion + ".*";
}
regexSuggestion = regexSuggestion.replace(/:/g, ".*");
regexSuggestion = regexSuggestion.replace(/\//g, ".*");
// replace all non word marks we add to .*
regexSuggestion = regexSuggestion.replace(/_+/g, ".*");
regexSuggestion = regexSuggestion.replace(/#+/g, ".*");
// replace '(' and ')' with empty string
regexSuggestion = regexSuggestion.replace(/\(/g, "");
regexSuggestion = regexSuggestion.replace(/\)/g, "");
regexSuggestion = regexSuggestion.replace(/___/g, ".*");
regexSuggestion = regexSuggestion.replace(/###/g, ".*");
regexSuggestion = regexSuggestion.replace(/\s+/g, ".*");
regexSuggestion = regexSuggestion.replace(/\?/g, "");
regexSuggestion = regexSuggestion.replace(/{/g, "");
regexSuggestion = regexSuggestion.replace(/}/g, "");
// change .*(.*) or (.*).* to .*
regexSuggestion = regexSuggestion.replace(/\.\*\(\.\*\)/g, ".*");
regexSuggestion = regexSuggestion.replace(/\(\.\*\)\.\*/g, ".*");
// unify .*
regexSuggestion = regexSuggestion.replace(/[\\.\\*]+/g, ".*");
return regexSuggestion;
},
compareLength : function(a,b) {
return b.length - a.length;
},
createRegex: function(processes) {
processes.sort(this.compareLength);
var regexBase = '';
for (var i in processes) {
var process = processes[i];
process = this.prepareProcessRegex(process);
if (regexBase.length != 0) {
regexBase = this.levenshteinDistanceAlign(regexBase, process);
} else {
regexBase = process;
}
}
return this.prepareProcessRegex(regexBase);
},
removePath: function(command) {
if (command.trim().startsWith('//')) {
return command;
}
if (command.indexOf(':') != -1 && command.indexOf(':\\') == -1) {
var result = '';
var parts = command.split(':');
var isFirst = true;
for (var i in parts) {
var item = parts[i];
if (isFirst) {
isFirst = false;
} else {
result += ':';
}
result += this.removePath(item);
}
command = result;
}
var prePath = '';
if (command.indexOf('=') != -1) {
//handle case of java -k path=c:/path/kuku/my.jar --> java -k path=my.jar, make sure we remove only the path
var commandParts = command.split("=");
if (commandParts.length == 2) { // handle just 1 assignment case
prePath = commandParts[0];
command = commandParts[1];
}
}
command = command.replace(/\//g, '\\');
var lastIndex = command.lastIndexOf("\\");
if (command.length > lastIndex) {
command = command.substring(lastIndex + 1);
}
if (prePath) {
command = prePath + "=" + command;
}
return command;
},
similarity: function(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) {
return 1.0;
}
return (longerLength - this.editDistance(longer, shorter)) / parseFloat(longerLength);
},
editDistance: function(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue),
costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
},
type: 'ProcessUtils'
};
Sys ID
2cb7426aebf9a9101a443e82b8522836