Commit f2fdfd25 authored by Iker Hurtado's avatar Iker Hurtado
Browse files

Make independent search by elements and by formulas. Add partial (pending of...

Make independent search by elements and by formulas. Add partial (pending of complete definition) support for the NOT operator and one level of parentheses

Little tested
parent d24ee075
Pipeline #92498 skipped with stage
......@@ -80,7 +80,7 @@ class MaterialNameBox{
enableInput() {
this.materialNameField.element.disabled = false;
this.button.disabled = true;
this.button.disabled = false;
}
}
......
......@@ -178,6 +178,7 @@ class NewSearchMod {
this.elementTable.setClickListener(elementArray => {
this.searchBox.addElements(elementArray);
this.addMatNameButton.disabled = true; // Not always necessary but it simplifies the code
this.addFormulaButton.disabled = true;
});
this.elementTable.setDeselectListener(e => this.searchBox.removeElementORFormulaInSearchQuery(e));
......@@ -187,8 +188,9 @@ class NewSearchMod {
if (formula.trim() !== ''){
this.searchBox.addTag(formula, 'F');
//this.formulaBox.disable(true);
this.addElementButton.disabled = true;
this.addMatNameButton.disabled = true;
this.materialNameBox.disableInput();
// this.materialNameBox.disableInput(); It doesn't seem needed
}
});
......@@ -199,8 +201,7 @@ class NewSearchMod {
this.searchBox.addTag(name, 'MN');// this.addTag(name, 'MN');
this.addElementButton.disabled = true;
this.addFormulaButton.disabled = true;
//TODO: are the following two lines needed, when the related buttons are already diasbled??
this.formulaBox.disableInput();
// this.formulaBox.disableInput(); It doesn't seem needed
this.materialNameBox.disableInput();
}
});
......@@ -871,7 +872,7 @@ class FormulaBox{
enableInput() {
this.formulaTextField.disabled = false;
this.formulaButton.disabled = true;
this.formulaButton.disabled = false;
}
/*
......
......@@ -84,6 +84,8 @@ class SearchBox{
getOptimadeQuery(allowOtherElements){
console.log('getOptimadeQuery this.searchQuery', this.searchQuery)
if (this.searchQuery.length === 0 ||
!isQueryWellFormed(this.searchQuery)) return '';
......@@ -91,6 +93,37 @@ class SearchBox{
return `material_name="${this.searchQuery[0]}"`;
}else{ // elements and formulas
let searchQueryMod = [];
let parOptimadeQueries = [];
let openIndex = -1;
let parCounter = 0;
this.searchQuery.forEach( (item, i) => {
if ( this.searchQuery[i] === '(' ){
openIndex = i;
}
else if ( this.searchQuery[i] === ')' ){
searchQueryMod.push('('+parCounter);
const parSearchQuery = this.searchQuery.slice(openIndex+1, i);
//console.log('parSearchQuery: ',i, parSearchQuery, getOptimadeFromSearchQuery(allowOtherElements, parSearchQuery));
parOptimadeQueries.push(getOptimadeFromSearchQuery(allowOtherElements, parSearchQuery))
openIndex = -1;
parCounter++;
}else if (openIndex < 0) searchQueryMod.push(item);
});
// console.log('FFFFFFFFF searchQueryMod: ', searchQueryMod, parOptimadeQueries, getOptimadeFromSearchQuery(allowOtherElements, searchQueryMod, parOptimadeQueries));
//********* Not valid expressions
// return getOptimadeFromSearchQuery(allowOtherElements, this.searchQuery);
return getOptimadeFromSearchQuery(allowOtherElements, searchQueryMod, parOptimadeQueries);
}
function getOptimadeFromSearchQuery(allowOtherElements, searchQuery, optimadeSubqueries){
let optimadeQuery = '';
// Get the subqueries: parts of the query separated by ORs
......@@ -98,20 +131,18 @@ class SearchBox{
let currentSubquery = [];
subqueries.push(currentSubquery);
this.searchQuery.forEach( (item, i) => {
searchQuery.forEach( (item, i) => {
if (this.searchQuery[i] === 'OR'){
if (searchQuery[i] === 'OR'){
currentSubquery = []; // new subquery
subqueries.push(currentSubquery);
}else
currentSubquery.push(item);
});
console.log("getOptimadeQuery subqueries: ", subqueries);
console.log("getOptimadeQuery subqueries: ", subqueries)
// Subqueries are either a formula or several elements separated by ANDs
// Subqueries are either elements or formulas separated by ANDs
subqueries.forEach( (subquery, i) => {
if (i > 0) optimadeQuery += ' OR '
......@@ -119,7 +150,10 @@ class SearchBox{
if (allowOtherElements){ // Inclusive search
subquery.forEach( (item, i) => { // For every item in the subquery
if ( isElement(item) ){
if (item.charAt(0) === '('){
// console.log('OPPPPP', optimadeSubqueries[+item.charAt(1)])
optimadeQuery += `(${optimadeSubqueries[+item.charAt(1)]})`
}else if ( isElement(item) ){
optimadeQuery += `elements HAS "${item}"`
}else if ( item === 'AND' || item === 'NOT'){
......@@ -133,7 +167,8 @@ class SearchBox{
}else{ // Exclusive search. For now only one formula or elements subqueries supported. Operator NOT doesn't make sense here
if (subquery.length === 1 && !isElement(subquery[0])){ // Only one formula
// if (subquery.length === 1 && !isElement(subquery[0])){ // Only one formula
if (!isElement(subquery[0])){ // formula(s)
optimadeQuery += `formula="${subquery[0]}"`;
}else{ // Several items, they all should be elements
......@@ -141,33 +176,27 @@ class SearchBox{
}
}
});
console.log('optimadeQuery: ', optimadeQuery)
});
return optimadeQuery;
}
function isElement(item){
return util.ELEMENTS.includes(item)
}
function getOptimadeExclusiveANDSubquery(subquery){ // this subquery is supposed to be form only for elements and ANDs separating
let optSubquery = 'elements HAS ONLY ';
subquery.forEach( (item) => {
if (isElement(item)){//item !== 'AND'){ // check if element isntead ?
optSubquery += `"${item}",`;
}
function isElement(item){
return util.ELEMENTS.includes(item)
}
});
optSubquery = optSubquery.substring(0, optSubquery.length-1);
return optSubquery;
}
}
function getOptimadeExclusiveANDSubquery(subquery){ // this subquery is supposed to be form only for elements and ANDs separating
let optSubquery = 'elements HAS ONLY ';
subquery.forEach( (item) => {
if (isElement(item)){//item !== 'AND'){ // check if element isntead ?
optSubquery += `"${item}",`;
}
});
optSubquery = optSubquery.substring(0, optSubquery.length-1);
return optSubquery;
}
function isQueryWellFormed(searchQuery){
......@@ -178,6 +207,7 @@ class SearchBox{
// they are in right position
(openingParIndex >= 0 && openingParIndex < closingParIndex+1))
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment