Some checks failed
CI fuzzy controller / container-test-job (push) Failing after 8s
159 lines
4.9 KiB
JavaScript
159 lines
4.9 KiB
JavaScript
|
|
// Rules parsing
|
|
function getAntecedent(rule) {
|
|
withoutIf = rule.split('if');
|
|
return withoutIf[1].trim().split('then')[0].trim();
|
|
}
|
|
|
|
function getAntecedentComponents(antecedent) {
|
|
return antecedent.split('and').map((i) => i.trim());
|
|
}
|
|
|
|
function getVariable(antecedent) {
|
|
return antecedent.split('is')[0].trim();
|
|
}
|
|
|
|
function getVariableValue(antecedent) {
|
|
return antecedent.split('is')[1].trim();
|
|
}
|
|
|
|
// Rules creation
|
|
|
|
/* exported MessageTypesEnum */
|
|
var MessageTypesEnum = {
|
|
INFO: "info",
|
|
SUCCESS: "success",
|
|
WARNING: "warning",
|
|
DANGER: "danger"
|
|
};
|
|
Object.freeze(MessageTypesEnum);
|
|
|
|
function isEmpty(value) {
|
|
if (typeof value === "function") {
|
|
return false;
|
|
}
|
|
return (value == null || value.length === 0);
|
|
}
|
|
|
|
/* exported showFeedbackMessage */
|
|
function showFeedbackMessage(message, type) {
|
|
alert(type + ' ' + message);
|
|
}
|
|
/* exported errorHandler */
|
|
function errorHandler(response, callBack, errorCallBack) {
|
|
if (!isEmpty(response.error)) {
|
|
// TODO: add l10n
|
|
// showFeedbackMessage(response.error.code + ": " + response.error.message, MessageTypesEnum.DANGER);
|
|
if (!isEmpty(errorCallBack)) {
|
|
errorCallBack(response.data);
|
|
}
|
|
throw response.error.code + ": " + response.error.message +
|
|
" / Details: " + response.error.data;
|
|
}
|
|
if (!isEmpty(callBack)) {
|
|
callBack(response);
|
|
}
|
|
}
|
|
|
|
/* exported getFromRest */
|
|
function getFromRest(url, callBack) {
|
|
$.ajax({
|
|
url: url,
|
|
cache: false,
|
|
success: function (response) {
|
|
errorHandler(response, callBack);
|
|
}
|
|
});
|
|
}
|
|
|
|
/* exported createRule */
|
|
function createRule() {
|
|
var ruleString = "if ";
|
|
var inp = $('.selectpicker.inputVar').children(':selected').map(function() {return $(this).text();}).get();
|
|
var inpVal = $('.selectpicker.inputVal').children(':selected').map(function() {return $(this).text();}).get();
|
|
for (var i = 0; i < inp.length; i++) {
|
|
if (i > 0) {
|
|
ruleString += ' and ';
|
|
}
|
|
ruleString += inp[i] + " is " + inpVal[i];
|
|
}
|
|
$('#ruleContent').val(ruleString);
|
|
}
|
|
|
|
/* exported fillSelect */
|
|
function fillSelect(selectElement, values) {
|
|
$(selectElement).html("");
|
|
$.each(values, function (key, value) {
|
|
$(selectElement).append($("<option />").val(value.id).text(value.name));
|
|
});
|
|
}
|
|
|
|
function fillFuzzyTerms(variablesElement, fuzzyTermsElement) {
|
|
getFromRest("/rule/getFuzzyTerms/"+$(variablesElement).val(), function (fuzzyTerms) {
|
|
var fuzzyTermsData = [];
|
|
$.each(fuzzyTerms, function (key, value) {
|
|
fuzzyTermsData.push({
|
|
id: value.id,
|
|
name: value.description
|
|
});
|
|
});
|
|
fillSelect(fuzzyTermsElement, fuzzyTermsData);
|
|
$(fuzzyTermsElement).selectpicker("refresh");
|
|
$(fuzzyTermsElement).trigger("change");
|
|
});
|
|
}
|
|
|
|
function fillVariables(projectId, variablesElement) {
|
|
getFromRest("/rule/getVariables/"+projectId, function (variables) {
|
|
var variablesData = [];
|
|
$.each(variables, function (key, value) {
|
|
variablesData.push({
|
|
id: value.id,
|
|
name: value.name
|
|
});
|
|
});
|
|
fillSelect(variablesElement, variablesData);
|
|
$(variablesElement).selectpicker("refresh");
|
|
$(variablesElement).trigger("change");
|
|
});
|
|
}
|
|
|
|
function variableValueChanged(variablesElement, fuzzyTermsElement) {
|
|
fillFuzzyTerms(variablesElement, fuzzyTermsElement);
|
|
}
|
|
|
|
function fuzzyTermsValueChanged() {
|
|
createRule();
|
|
}
|
|
|
|
function addVariableSelect(projectId) {
|
|
var variablesElement = $("<select class='selectpicker inputVar m-2' data-live-search='true data-width='70%'></select>");
|
|
fillVariables(projectId, variablesElement);
|
|
return variablesElement;
|
|
}
|
|
|
|
function addFuzzyTermsSelect(variablesElement) {
|
|
fuzzyTermsElement = $("<select class='selectpicker inputVal m-2' data-live-search='true data-width='70%'></select>");
|
|
if ($(variablesElement).val()) {
|
|
fillFuzzyTerms(variablesElement, fuzzyTermsElement);
|
|
}
|
|
return fuzzyTermsElement;
|
|
}
|
|
|
|
function addAntecedent(parentElement, projectId) {
|
|
rowElement = $("<div class='row'></div>");
|
|
if ($(parentElement).find('.row').length) {
|
|
$(rowElement).append("<label class='col col-md-1 m-2'>И</label>");
|
|
} else {
|
|
$(rowElement).append("<label class='col col-md-1 m-2'> </label>");
|
|
}
|
|
var variablesElement = addVariableSelect(projectId);
|
|
var fuzzyTermsElement = addFuzzyTermsSelect(variablesElement)
|
|
$(variablesElement).on( "change", function() {variableValueChanged(variablesElement, fuzzyTermsElement)});
|
|
$(fuzzyTermsElement).on( "change", function() {fuzzyTermsValueChanged()});
|
|
$(rowElement).append(variablesElement);
|
|
$(rowElement).append("<label class='col col-md-1 m-2'>есть</label>");
|
|
$(rowElement).append(fuzzyTermsElement);
|
|
$(parentElement).append(rowElement);
|
|
}
|